$(document).bind('ready.bins', function() {

	/**
	 * Berichten
	 */
	$('div.berichten div.bericht').hover(function() {
		$(this).css({
			cursor:				'pointer'
		}).addClass('light');
	}, function() {
		$(this).css({
			cursor:				'normal'
		}).removeClass('light');
	}).click(function() {
		var a		= $(this).find('p > a:first');
		var href	= $(a).attr('href');
		var target	= $(a).attr('target');
		if (target!=undefined && target=='_blank') {
			window.open(href);
		} else {
			window.location = href;
		}
	});

	/**
	 * Customer search form
	 */
	if ($('.customer_search_form').length>=1) {

		if (IE6) { // Fix for IE6, so it handles overflow:hidden correct
			$('div.maps .bincontent').width($('div.maps .bincontent').width());
		}

		// Animation menu
		$('.customer_search_form').data('status', 'show');

		// Grabber
		$('.customer_search_form .grab')
		.css('cursor', 'pointer')
		.click(function() {
			var div			= $(this).parents('.customer_search_form');
			//var iFrameID 	= $(this).parents('.customer_search_form').siblings('iframe').attr('id');

			if (div.data('status')=='show') {
				$(this).css('background-image', "url('img/arrow_red_left.gif')");
				div.animate({
					right:	-(parseInt(div.width())-15)
				}, {
					duration:	500,
					easring:	'swing',
					complete:	function() {
						$(this)
						.data('status', 'hide')
						.find('h2').hide(0);
					}
				});
			} else {
				$(this).css('background-image', "url('img/arrow_red_right.gif')");
				div
				.find('h2').show(0).end()
				.animate({
					right:		0
				}, {
					duration:	500,
					easring:	'swing',
					complete:	function() {
						$(this).data('status', 'show');
					}
				});
			}
			/*
			// Resize map to fit on screen with overlay menu
			var containerWidth	= parseInt(div.parents('.bincontent').width());
			var divWidth		= parseInt(div.width());
			var divRight		= parseInt(div.css('right'));
			$('#'+iFrameID).width(containerWidth - (divWidth + divRight));

			// Call map internal updateSize function
			document.getElementById(iFrameID).contentWindow.updateMapSize();
			//*/
		});

		function filterCustomers() {

			var param = $('#customer-sub-categories, #customer-main-categories').serialize();

			if (arguments[0]) {
				var hash	= getHashObject(arguments[0]);
				param		= param + '&hash='+hash.filter;
			}

			xmlHttpApp('customer', param, function(data) {
				$('div.leden > .bincontent').html(data.data); // Append to container
				$('.customerRow').trigger('init'); // Trigger initialization of sliding, etc.
				if (data.setHash==true) {
					setHash(data.hash, 'filter');
				} else {
					clearHash();
				}

				if (data.querystring) {
					var qs	= data.querystring;
					// Set form values
					for (fieldName in qs) {
						var type = typeof(qs[fieldName]);
						var dude = qs[fieldName];

						switch (type) {
							case 'string':
								$('*[name="'+fieldName+'"]').val(qs[fieldName]);
							break;
							case 'object':
								for (i in dude) {
									$('input[name^="'+fieldName+'"][value="'+dude[i]+'"]').attr('checked', 'checked');
									if (fieldName=='intOptionID') {
										$('input[name^="'+fieldName+'"][value="'+dude[i]+'"]').trigger('check');
										$('#subc_'+dude[i]).find('input:checkbox').removeAttr('checked');
									}
								}
							break;
						}
					}
				}
			});
		}

		// Animate onclick checkbox
		$('ul.categories input:checkbox')
		.bind({
			'check':	function() {
				var val	= $(this).val();
				if ($(this).attr('checked')) {
					$('#subc_'+val)
					.css('display', 'inline')
					.find('input:checkbox')
						.removeAttr('disabled');
				} else {
					$('#subc_'+val)
					.css('display', 'none')
					.find('input:checkbox')
						.attr('disabled', 'disabled');
				}
			},
			click:		function() {

				// Old submit, this must be used for non-js stuff
				//window.location = $(this).siblings('span.hidden').text();

				$(this).trigger('check');
				filterCustomers();
			}
		})
		.trigger('check');

		// Animate tags
		$('.customer_subcategories li')
		.find('label').click(function(e) {
			e.stopImmediatePropagation();
		})
		.end()
		.find('input:checkbox').bind({
			click:	function(e) {
				e.stopImmediatePropagation();
				$(this).trigger('status');
			},
			'check':	function(e) {
				$(this).attr('checked', 'checked');
				$(this).trigger('status');
			},
			'uncheck':	function(e) {
				$(this).removeAttr('checked');
				$(this).trigger('status');
			},
			'status':	function(e) {
				filterCustomers();
			}
		});

		// If hash set, check files and filter customers
		if (window.location.hash) {
			filterCustomers(window.location.hash);
		}
	}

	/**
	 * Customer List overview bin
	 */
	$('.customerRow')
	.live('init', function() {
		$(this)
		.data('status', 'normal')
		.css({
			cursor: 'pointer'
		});
		if ($(this).find('img[src$="dummy.gif"]').length==1) {
			var finalHeight = parseInt($(this).find('div.header').height());

			$(this)
			.data('status', 'small')
			.data('oldHeight', 116/*$(this).height()*/)
			.data('newHeight', finalHeight)
			.height(finalHeight)
			.css('overflow', 'hidden');
		}
	})
	.live('mouseenter', function(e) {

		$(this).data('mouseStatus', 'over');

		// Get the previous row and the current row
		var prevRow	= $(e.currentTarget).parent().data('previousCustomer');
		var currRow	= $(e.currentTarget);

		// Fold out current target, if its not already
		if (currRow.data('status')=='small') {
			currRow
			.stop(true, true)
			.queue(function() {
				setTimeout(function() {
					// If the mouse is gone, we simply stop
					if (currRow.data('mouseStatus')=='out') {
						// stop
						currRow.clearQueue();
					}
					if (typeof(prevRow)!="undefined" && parseInt(prevRow.height())!=parseInt(prevRow.data('oldHeight'))) {
						prevRow = undefined;
					}
					currRow.dequeue();
				}, 310)
			})
			.animate({
				height:		currRow.data('oldHeight')
			}, {
				duration : 	200,
				easing:		'swing',
				step:		function(now) {
					//console.log(prevRow);
					if (typeof(prevRow)!="undefined" && $(prevRow).attr('id')!=$(currRow).attr('id')) {
						//console.log('wumbo---');
						prevRow
						.css({
							height	: prevRow.data('oldHeight') - (now - prevRow.data('newHeight'))
						});
					}
				},
				complete:		function() {
					$(e.currentTarget).parent().data('previousCustomer', currRow);
					if (typeof(prevRow)!="undefined") {
						prevRow.data('position', 'hidden');
					}
					currRow.data('position', 'show');
				}
			});
		} else {
			// we are at a not foldable row, check if any other row is open, that should be folded
			if (typeof(prevRow)!="undefined" && $(prevRow).attr('id')!=$(currRow).attr('id')) {
				prevRow
				.queue(function() {
					setTimeout(function() {
						// If the mouse is gone, we simply stop
						if (currRow.data('mouseStatus')=='out') {
							// stop
							prevRow.clearQueue();
						}
						prevRow.dequeue();
					}, 310)
				})
				.animate({
					height:		prevRow.data('newHeight')
				}, {
					duration : 	200,
					easing:		'swing',
					complete:	function() {
						$(e.currentTarget).parent().removeData('previousCustomer');
					}
				});
			}
		}
	})
	.live('mouseleave', function(e) {
		$(this).data('mouseStatus', 'out');
	})
	.live('click', function() {
		document.location	= $(this).find('.header a').attr('href');
	})
	.trigger('init');

	$('.customerRow p.ellipsis').each(function() {
		var maxHeight	= $(this).outerHeight(true);
		while (this.scrollHeight>maxHeight) {
			this.innerHTML	= this.innerHTML.split('...').join('');
			this.innerHTML	= this.innerHTML.substr(0, this.innerHTML.length-10)+'...';
		}
		// Now find the last space ' ' and split on that
		this.innerHTML	= this.innerHTML.substr(0, this.innerHTML.lastIndexOf(' '))+'...';
	});
	//$('.customerRow:first').parent()
	$('.bincontent:has(.customerRow)')
	.bind('mouseleave', function(e) {

		// Fix in IE, spacing between rows is like mouseleave for IE
		if ($(this).data('previousCustomer')!=undefined) {
			var oldRow	=	 $(this).data('previousCustomer');
			oldRow
			.queue(function() {
				setTimeout(function() {
					oldRow.dequeue();
				}, 310)
			})
			.animate({
				height:		oldRow.data('newHeight')
			}, {
				duration : 	200,
				easing:		'swing',
				complete:	function() {
					$(e.currentTarget).removeData('previousCustomer');
				}
			});
		}
	});
	// Fix for IE with early mouseout trigger, so we replace margin with border
	if (IE6) {
		$('.bincontent:has(.customerRow)')
		.css({
			margin:			0,
			borderBottom:	'4px solid #FFFFFF'
		});
	}


	/**
	 * Customer detail page
	 */
	$('.customer_detail .thumb')
	.css('cursor', 'pointer')
	.click(function() {
		var id 	= $(this).attr('id');
		id		= id.substr(id.indexOf('_')+1);
		$(this).siblings('.main, .iframe').removeClass('show').filter('#main_'+id).addClass('show');
		if (id==6) {
			if (typeof(dimLoaded)!='undefined' && dimLoaded==true) {
				var iframe	= document.getElementById('main_6').contentWindow;
				iframe.centerMap();
			} else {
				dimLoaded		= false;
				interval = setInterval(function() {
					var iframe	= document.getElementById('main_6').contentWindow;
					var dimens	= iframe.map.getSize();
					if (dimens.height>0 || dimens.width>0) {
						iframe.centerMap();
						clearInterval(interval);
						dimLoaded		= true;
					}
				}, 200);
			}
		}
	}).filter(':first').click();

	/**
	 * Slideshow bin
	 */
	$('div.slideshow')
	.each(function() {
		// Make caption absolute, since cycle-script makes images absolute
		$(this)
		.find('div.caption').css({
			display	: 'block'
		})
		.find('span').css({
			display		: 'block',
			float		: 'left'
		});

		// Skip when only 1 image
		if ($(this).find('img[class^="img"]').length==1) {

			// Hide arrows
			$(this).find('div.caption > img').css({
				display		: 'none'
			});

			// Show caption for current image
			$(this).find('img[class^="img"]').each(function() {
				var caption	= $(this).parents('div.bincontent').find('div.caption > span');
				var txt		= $(this).attr('alt');
				$(caption)
				.css({
					display	:	($(this).attr('alt')!='') ? 'block' : 'none'
				})
				.html(txt);
			});

			return;
		}

		var iprev		= $(this).find('img.prev');
		var inext		= $(this).find('img.next');

		 // Add animation for images
		$(this).find('div.slides').cycle({
			fx:     'scrollHorz',
			timeout: 10000,
			speed:  700,
			next:   $(inext),
			prev:   $(iprev),
			before:	function(cImg, nImg) {
				var caption	= $(this).parents('div.bincontent').find('div.caption > span');
				var txt		= $(nImg).attr('alt');
				$(caption)
				.css({
					display	:	($(nImg).attr('alt')!='') ? 'block' : 'none'
				})
				.html(txt);
			}
		});
	});

	/**
	 * Uitgelicht bins
	 */
	$('div.uitgelicht').each(function() {

		// fix image height
		var uitH = 0;
		$(this).find('div.bincontent > a').each(function() {
			uitH += parseInt($(this).outerHeight());
		});
		$(this).find('div.imgcontainer').height(uitH);
		$(this).find('div.imgcontainer img').height(uitH);

		// add mouseover event
		$(this).find('div.bincontent > a').mouseover(function() {

			var parent		= $(this).parent();
			var id			= parseInt($(parent).children('a').index(this)) + 1;

			// Change highlight
			$(parent).children('a').removeClass('light');
			$(this).addClass('light');
			$(parent).children('a:last').addClass('last');

			// Change image
			$(parent).find('div.imgcontainer > img').hide(0);
			$(parent).find('div.imgcontainer > img.img'+id).show(0);
		});
	});

	/**
	 * Half bins
	 */
	$('div.halfLeft').each(function() {
		var left			= $(this).find('div.bincontent');
		var right			= $(this).next('div.halfRight:first').find('div.bincontent');

		var leftHeight 			= parseInt($(left).height());
		var rightHeight 		= parseInt($(right).height());
		var leftOuterHeight 	= parseInt($(left).outerHeight());
		var rightOuterHeight 	= parseInt($(right).outerHeight());

		if (leftHeight>rightHeight) {
			$(right).height(rightHeight+(leftOuterHeight-rightOuterHeight));
		} else {
			$(left).height(leftHeight+(rightOuterHeight-leftOuterHeight));
		}
	});
});