$(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 overview
	$('.customerRow').hover(function() {
		$(this).css({
			cursor:		'pointer',
			background:	'#f3f3ed'
		})
		.find('.header').addClass('header_over');
	}, function() {
		$(this).css({
			cursor:		'normal',
			background:	''
		})
		.find('.header').removeClass('header_over');
	}).click(function() {
		document.location	= $(this).find('.header a').attr('href');
	});
	$('.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(' '))+'...';
	});

	// Customer Detail
	$('.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 code
	$('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));
		}
	});
});