jQuery.noConflict();

jQuery(document).ready(function(){

	
	jQuery('#mainImageContainer').hide();
	jQuery('#loader').show();
	
	var dim;
	var numberOfPictures = parseInt(jQuery('#numberOfPictures').html());
	var activePicture;
	var activePictureP;
	var activePictureM;
	var activePictureString;
	var activePicturePString;
	var activePictureMString;
	var imageScrolled;
	var activeSub
	
	//--------------------------------------------------
	// Begin of functions
	//--------------------------------------------------
	
	function changeActivePicture(newActivePicture) {
		
		activePicture = parseInt(newActivePicture);
		activePictureP = (activePicture + 1);
		activePictureM = activePicture - 1;
		
		if (activePicture < 10) {
			activePictureString = "0"+activePicture;
		} else {
			activePictureString = activePicture;
		}
		if (activePictureP < 10) {
			activePicturePString = "0"+activePictureP;
		} else {
			activePicturePString = activePictureP;
		}
		if (activePictureM < 10) {
			activePictureMString = "0"+activePictureM;
		} else {
			activePictureMString = activePictureM;
		}
		
		jQuery('#mainImageContainer'+activePictureString).css('z-index', -1);
		jQuery('#mainImageContainer'+activePicturePString).css('z-index', -2);
		jQuery('#mainImageContainer'+activePictureMString).css('z-index', -3);
		
		jQuery('.activePicture').removeClass('activePicture');
		jQuery('.onePicture:nth-child('+activePicture+')').addClass('activePicture');
	
	}
	
	var resizeTimer = null;
	
	function triggerTimer() {
		if (resizeTimer) clearTimeout(resizeTimer); 
		resizeTimer = setTimeout(adjustAllPictures, 800); 
	}
	
	function adjustActivePicture() {
		jQuery('.bigImageContainer').css('height', jQuery(window).height());
		jQuery('.bigImageContainer').css('width', jQuery(window).width());
		if (jQuery(window).width() / jQuery(window).height() < 3/2) {
			jQuery('#mainImage').removeAttr('width');
			jQuery('#mainImage').attr('height', jQuery(window).height());
		} else {
			jQuery('#mainImage').removeAttr('height');
			jQuery('#mainImage').attr('width', jQuery(window).width());
		}
		triggerTimer();
	}
	
	function adjustAllPictures() {
		if (jQuery(window).width() / jQuery(window).height() < 3/2) {
			for (var i = 1; i <= numberOfPictures; i++) {
				if (i < 10) {
					iString = "0"+i;
				} else {
					iString = i;
				}
				jQuery('#image'+iString).removeAttr('width');
				jQuery('#image'+iString).attr('height', jQuery(window).height());
			}
		} else {
			for (var i = 1; i <= numberOfPictures; i++) {
				if (i < 10) {
					iString = "0"+i;
				} else {
					iString = i;
				}
				jQuery('#image'+iString).removeAttr('height');
				jQuery('#image'+iString).attr('width', jQuery(window).width());
			}
		}
	}
	
	
	//--------------------------------------------------
	// End of functions
	//--------------------------------------------------
	
	
	
	
	changeActivePicture(parseInt(jQuery('#activePicture').html()));
	
	
	
	
	
	
	if (screen.width < 600) {
		dim = "600_400";
	} else if (screen.width >= 600) {
		dim = "1500_1000";
		if (screen.width > 1500) {
			dim = "2400_1600";
		}
		if (screen.width > 2400) {
			dim = "3000_2000";
		}
		if (screen.width > 3000) {
			dim = "3720_2480";
		}
	}
	
	jQuery('#mainImage').attr('src', '');
	jQuery('#mainImage').attr('src', 'content/images/'+activePictureString+'/'+dim+'.jpg');
	
	jQuery('.bigImageContainer').css('height', jQuery(window).height());
	jQuery('.bigImageContainer').css('width', jQuery(window).width());
	adjustActivePicture();
	adjustAllPictures();
	
	jQuery('#mainImage').load(function() {
		jQuery('#loader').hide();
		jQuery('#mainImageContainer').show();
	});
	
	
	
	jQuery(window).resize(function() {
		adjustActivePicture();
	});
	
	function reverseScroll() {
		jQuery('#mainImageContainer'+activePictureString).animate({
			left: 0
		}, 200);
		jQuery('#mainImageContainer'+activePicturePString).animate({
			left: jQuery(window).width()
		}, 200);
		jQuery('#mainImageContainer'+activePictureMString).animate({
			left: -jQuery(window).width()
		}, 200);
	}
/*
	
	jQuery('body').mousedown(function(e) {
		var mouseRelative = e.pageX;
		
		jQuery('body').mousemove(function(ev) {
			jQuery('#mainImageContainer'+activePictureString).css('left', ev.pageX - mouseRelative);
			jQuery('#mainImageContainer'+activePicturePString).css('left', ev.pageX - mouseRelative + jQuery(window).width());
			jQuery('#mainImageContainer'+activePictureMString).css('left', ev.pageX - mouseRelative - jQuery(window).width());
			imageScrolled = ev.pageX - mouseRelative;
		});
	}).mouseup(function() {
		jQuery('body').unbind('mousemove');
		if (imageScrolled > jQuery(window).width() / 3) {
			if (activePictureM > 0) {
				jQuery('#mainImageContainer'+activePictureString).animate({
					left: jQuery(window).width()
				}, 200, function() {
					jQuery('#mainImageContainer'+activePictureString).css('z-index', -100);
				});
				jQuery('#mainImageContainer'+activePictureMString).animate({
					left: 0
				}, 200, function() {
					jQuery('#mainImageContainer'+activePictureMString).css('z-index', -1);
					changeActivePicture(activePictureM);
				});
			} else {
				reverseScroll();
			}
		} else if (imageScrolled < - jQuery(window).width() / 3) {
			if (activePictureP <= numberOfPictures) {
				jQuery('#mainImageContainer'+activePictureString).animate({
					left: -jQuery(window).width()
				}, 200, function() {
					jQuery('#mainImageContainer'+activePictureString).css('z-index', -100);
				});
				jQuery('#mainImageContainer'+activePicturePString).animate({
					left: 0
				}, 200, function() {
					jQuery('#mainImageContainer'+activePicturePString).css('z-index', -1);
					changeActivePicture(activePictureP);
				});
			} else {
				reverseScroll();
			}
		} else {
			reverseScroll();
		}
	});
	
*/
	


	jQuery('.navigationLink').click(function(event) {
		var linkID = parseInt(jQuery(event.target).attr('id'));
		if (linkID != activePicture) {
			jQuery('#mainImage').fadeOut('slow', function() {
				jQuery('#loader').show();
				changeActivePicture(linkID);
				jQuery('#pictureDescriptionText').html(jQuery('#pd'+activePictureString).html());
				jQuery('#mainImage').attr('src', '');
				jQuery('#mainImage').attr('src', 'content/images/'+activePictureString+'/'+dim+'.jpg');
				jQuery('#mainImage').load(function() {
					jQuery('#loader').hide();
					jQuery('#mainImage').fadeIn();
					jQuery('#contentContainer').fadeIn('slow');
				});
			});
		} else {
			jQuery('#contentContainer').fadeIn('slow');
		}
		activeSub = 1;
		jQuery.get('pages/content.php?ajaxID='+linkID, function(data) {
			jQuery('#content').html(data);
			jQuery('.showSub').unbind('click', showSubHandler);
			jQuery('.showSub').bind('click', showSubHandler);
		});
		jQuery('.showSub').bind('click', showSubHandler);
		return false;
	});
	
	jQuery('#closeLink').click(function() {
		jQuery('#contentContainer').fadeOut('slow');
		return false;
	});
	
	activeSub = 1;
	var newSub;
	
	var showSubHandler = function() {
		newSub = parseInt(jQuery(this).attr('id'));
		if (activeSub != newSub) {
			jQuery('#sub'+activeSub).slideUp('slow');
			jQuery('#sub'+newSub).slideDown('slow');
			activeSub = newSub;
		}
		return false;
	}
	
	jQuery('.showSub').bind('click', showSubHandler);
	
	
	

   
});
