$(document).ready(function () {

	/**
	 * Deze file zorgt voor de foto "album" effect op de voorpagina.
 	 **/

 	var Browser = {
		Version: function() {
			var version = 999;
			if (navigator.appVersion.indexOf("MSIE") != -1)
				version = parseFloat(navigator.appVersion.split("MSIE")[1]);
			return version;
		}
	}

 	var autoRotateTimer;

	var autoRotate = function() {
		var activeElement = $('div.photo-buttons li.active');
		var nextEelement = (activeElement.next().size() == 1 ? activeElement.next() : $('div.photo-buttons li:first'));
		nextEelement.find('a').click();
	}

	var autoRotatorInit = function() {
		clearInterval(autoRotateTimer);
		autoRotateTimer = setInterval(autoRotate, 10000);
	}

	var shuffle = function(o){ //v1.0
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
	};

	var fetchPhotos = function (photoId, actionId) {

		//$('#debug').append('fetchPhotos <br/>');
		$('div.photo-text').hide();

		var photoButtonsDiv = $('div.photo-buttons');

		if (Browser.Version() < 9) {
			$('div.photo-text[photo=' + photoId + ']').dequeue().show();
		} else {
			$('div.photo-text[photo=' + photoId + ']').dequeue().css('opacity', 1).fadeIn('slow',function () {

			});
		}
	}

	var zIndex = 500;

	$('div.photo-buttons a').live('click', function (ev) {

		autoRotatorInit();
		var photoButtonsDiv = $('div.photo-buttons');
		var activePhotoElm = $('div.action-photos img[rel=top]')
		var activePhotoId = activePhotoElm.attr('photo');

		// Geklikte foto
		var clickPhotoId = $(this).attr('photo');
		var clickPhotoNr = $(this).attr('photoNr');
		var actionId = $(this).attr('actionId');

		if(!isIos())
		{
			// Flash tonen bij niet Ios
			showFlashItem(clickPhotoNr);
		}

		photoButtonsDiv.attr('rel','active');

		// Eerst alle actieve li inactief maken
		$('div.photo-buttons ul li').removeAttr('class');

		var liElm = $(this).parent();

		liElm.attr('class','active');
		var totalPhotos = $('div.action-photos img').length;

		// Slider photos ophalen
		fetchPhotos(clickPhotoId, actionId);

		// Actieve top foto hiden
		activePhotoElm.dequeue();
		activePhotoElm.fadeOut(500,function () {
			$(this).css({'z-index':zIndex--,'position':'absolute'}).fadeIn(1200).removeAttr('rel');
		});

		// Foto op de top zetten
		$('div.action-photos img[photo=' + clickPhotoId + ']').css('z-index',800).attr('rel','top');

		autoRotatorInit();

		// Standaard actie blokkeren
		ev.preventDefault();
	});

	var updateContainerHeight = function () {
		// Get the height of the largest ".photo-text"
		var maxHeightText = 0;
		$('div.front .photo-text').each(function () {
			var height = $(this).height();
			if (height > maxHeightText) {
				maxHeightText = height;
			}
		});
		var minHeight = maxHeightText + 125; // add offset(105) + spacing for ".photo-buttons" (20px)
		if ($('div.front > .text').height() < minHeight) {
			$('div.front > .text').height(minHeight); // Make it fit
		}
	}
	updateContainerHeight();
	$(window).load(updateContainerHeight); // check again when the font is loaded

	/* De auto rotate timer */


	$('div.front > .text').hover(function() {
		clearInterval(autoRotateTimer);
	}, autoRotatorInit);



	autoRotatorInit();
	fetchPhotos(1, $('div.photo-buttons ul li:first-child a').attr('actionid'));

	$('div.photo-buttons ul li:first-child').addClass('active');

});
