// iPhone/iPod/iPad detection
if (navigator && navigator.platform && navigator.platform.match(/(iPad|iPod|iPhone)/g)) {
	document.write('<link type="text/css" rel="stylesheet" href="style/cbd3-mob.css">');
}
if (navigator && navigator.platform && navigator.platform.match(/(iPad)/g)) {
	document.write('<link type="text/css" rel="stylesheet" href="style/cbd3-ipad.css">');
}
// Detect Android
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
  document.write('<link type="text/css" rel="stylesheet" href="style/cbd3-android.css">');
}
$(function() {
	// find all gallery images on this page
	// init list of images
	// create duplicates if needed
	// move into position
	// fade in

	var iImageWidth = 952;
	
	var jImages = $('#images img');
	var iOriginalLength = jImages.length;
	if (jImages.length > 1  && jImages.length < 4 ) {
		$('#images').append(jImages.clone());
		jImages = $('#images img');
	}
	var iAdjustment = iImageWidth / 2;
	var iPreparePrev = jImages.length - 1;
	var iPrev = 0;
	var iCurrent = 1;
	var iNext = 2;
	var iPrepareNext = 3;
	
	function InitPositions() {
		var iCounter = -1;
		jImages.hide().each(function() {
			$(this).css({ marginLeft: iCounter++ * iImageWidth - iAdjustment });
		}).show();
		iCurrentDescr =  iCurrent % iOriginalLength;
		$('#descr'+ iCurrentDescr).show();
		
		// move to desired frame
		iDesired = parseInt(location.search.slice(6));
		if (isNaN(iDesired)) {
			iDesired == 1;
		}
		
		while (iCurrent < iDesired) {
			ForceNext();
		}
		while (iCurrent > iDesired) {
			ForcePrev();
		}
	}
	
	function iCheckIndex(iIndex) {
		return (iIndex < 0) ? jImages.length - 1 : (iIndex >= jImages.length) ? 0 : iIndex;
	}
	
	function NextImage() {
		if (!$('#images img:animated').length) {
			// prepare the next graphic
			$(jImages[iPrepareNext]).css({ marginLeft: 2 * iImageWidth - iAdjustment }).show();
			
			// animate graphics into new positions
			$('#descr'+ iCurrentDescr).hide();
			$(jImages[iPrev]).animate({ marginLeft: '-='+iImageWidth },  function() { $(this).hide(); $('#descr'+ iCurrentDescr).show() });
			$(jImages[iCurrent]).animate({ marginLeft: '-='+iImageWidth });
			$(jImages[iNext]).animate({ marginLeft: '-='+iImageWidth });
			$(jImages[iPrepareNext]).animate({ marginLeft: '-='+iImageWidth });
			
			iPreparePrev = iCheckIndex(iPreparePrev + 1);
			iPrev = iCheckIndex(iPrev + 1);
			iCurrent = iCheckIndex(iCurrent + 1);
			iCurrentDescr =  iCurrent % iOriginalLength;
			iNext = iCheckIndex(iNext + 1);
			iPrepareNext = iCheckIndex(iPrepareNext + 1);
		}
	}
	
	function ForceNext() {
		// prepare the next graphic
		$(jImages[iPrepareNext]).css({ marginLeft: 2 * iImageWidth - iAdjustment }).show();
		
		// move graphics into new positions
		$('#descr'+ iCurrentDescr).hide();
		$(jImages[iPrev]).animate({ marginLeft: '-='+iImageWidth }, 0).hide();
		$(jImages[iCurrent]).animate({ marginLeft: '-='+iImageWidth }, 0);
		$(jImages[iNext]).animate({ marginLeft: '-='+iImageWidth }, 0);
		$(jImages[iPrepareNext]).animate({ marginLeft: '-='+iImageWidth }, 0);
		
		iPreparePrev = iCheckIndex(iPreparePrev + 1);
		iPrev = iCheckIndex(iPrev + 1);
		iCurrent = iCheckIndex(iCurrent + 1);
		iCurrentDescr =  iCurrent % iOriginalLength;
		iNext = iCheckIndex(iNext + 1);
		iPrepareNext = iCheckIndex(iPrepareNext + 1);
		$('#descr'+ iCurrentDescr).show();
	}	
	
	function PrevImage() {
		if (!$('#images img:animated').length) {
			// prepare the previous graphic
			$(jImages[iPreparePrev]).css({ marginLeft: -2 * iImageWidth - iAdjustment }).show();
			
			// animate graphics into new positions
			$('#descr'+ iCurrentDescr).hide();
			$(jImages[iPreparePrev]).animate({ marginLeft: '+='+iImageWidth });
			$(jImages[iPrev]).animate({ marginLeft: '+='+iImageWidth });
			$(jImages[iCurrent]).animate({ marginLeft: '+='+iImageWidth });
			$(jImages[iNext]).animate({ marginLeft: '+='+iImageWidth }, function() { $(this).hide(); $('#descr'+ iCurrentDescr).show(); });
			
			iPreparePrev = iCheckIndex(iPreparePrev - 1);
			iPrev = iCheckIndex(iPrev - 1);
			iCurrent = iCheckIndex(iCurrent - 1);
			iCurrentDescr =  iCurrent % iOriginalLength;
			iNext = iCheckIndex(iNext - 1);
			iPrepareNext = iCheckIndex(iPrepareNext - 1);
		}
	}

	function ForcePrev() {
		// prepare the previous graphic
		$(jImages[iPreparePrev]).css({ marginLeft: -2 * iImageWidth - iAdjustment }).show();
		// move graphics into new positions
		$('#descr'+ iCurrentDescr).hide();
		$(jImages[iPreparePrev]).animate({ marginLeft: '+='+iImageWidth }, 0);
		$(jImages[iPrev]).animate({ marginLeft: '+='+iImageWidth }, 0);
		$(jImages[iCurrent]).animate({ marginLeft: '+='+iImageWidth }, 0);
		$(jImages[iNext]).animate({ marginLeft: '+='+iImageWidth }, 0).hide();
		iPreparePrev = iCheckIndex(iPreparePrev - 1);
		iPrev = iCheckIndex(iPrev - 1);
		iCurrent = iCheckIndex(iCurrent - 1);
		iCurrentDescr =  iCurrent % iOriginalLength;
		iNext = iCheckIndex(iNext - 1);
		iPrepareNext = iCheckIndex(iPrepareNext - 1);
		$('#descr'+ iCurrentDescr).show();
	}


	InitPositions();
	
	$('#control-next, #control-label').click(function() {
		NextImage();
	});
	
	$('#control-prev').click(function() {
		PrevImage();
	});

	$('#descr-arrow').toggle( 
		function() { $('#description p').hide(); $(this).addClass('closed'); }, 
		function() { $('#description p').show(); $(this).removeClass('closed'); }
	);

	// dropdowns
	$('#mainnav li').hover(
		function(){
			$(this).addClass('hover')
		},
		function(){
			$(this).removeClass('hover')
		}
	);
	// history images
	$('.history-img-small img').click(function() { $('.history-img-large img').attr('src', this.src.replace('small', 'large')) });

});

