var total_slides;
var selected_slide= 1;
var selected_infotype= 1;
var info_selected= false;
var target= '#s' + selected_slide;
var qu= false;

var timer;

var loadedPages= new Array();
var activeMenu= "";

var imagesToPreload = ["assets/img/menu-items.jpg", "assets/img/bike.jpg", "assets/img/bike2.jpg", "assets/img/dame.jpg"];

set_dimensions();
var preload= new ImagePreloader(imagesToPreload, onPreload);

jQuery(function($)
{
	set_imageSize();
	
	total_slides= $("#GalleryHolder li").length;
	$("#Loading").fadeOut("fast");
	$("#Menu, #GalleryMenu").fadeTo("normal", 1);
	$(".designed_by").fadeIn();
	
	// User has been sent directly to a page, handle resetting of defaults
	if ( pageRequested() )
	{
		target= location.hash;
		page= target.replace("#", "");
		
		selected_infotype= page;
		activeMenu= page;
		
		$("." + page).addClass(page + "-active");
		
		if (jQuery.inArray(selected_infotype, loadedPages) == -1)
		{
			$("#" + selected_infotype + " .infopage .inner .content").html('<img src="assets/img/loading.gif" alt="Loading..." />');
		}
	}
	
	// Scroll to the selected slide/page
	$("#Grid").scrollTo( target, { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: true,
				onAfter: function()
				{ 
					if ( pageRequested() )
					{	
						$.get(page + ".html", function(data)
						{
							$(target + " .infopage .inner .content").html(data);
							$(target).addClass("autoflow");
							
							loadedPages[loadedPages.length]= page;
							
							if ( scrollBarDetected() )
							{
								$("#" + selected_infotype + " .infopage").addClass("push");
							}
							
							$(".enlarge").lightBox(
								{
									overlayBgColor: '#EEE',
									imageBtnClose: 'assets/img/close.png',
									imageLoading: 'assets/img/loading.gif'
								}
							);
							
							if (selected_infotype == "contact")
							{
								$("input#send").click( function()
								{
									var formdata= $("form[name=contact]").serialize();
		
									jQuery.post( "send_mail.php", formdata,
										function(data, stauts)
										{
											$(".form_message").show().html(data);
											$("form[name='contact']")[0].reset();
										}	
									); 
		
									return false;
								});
							}
						});
					}else
					{
						$("#Intro").fadeIn("normal");
					}
			 	}
	});
	
	$(window).resize( function() 
	{
		set_dimensions();
		set_imageSize();
		
		if ( scrollBarDetected() )
		{
			$(currentPage() + " .infopage").addClass("push");
		}
		
		if ( !info_selected)
		{
			clearTimeout(timer);
			timer= setTimeout("$('#Grid').scrollTo( currentSlide(), { axis: 'yx', duration: 200, queue: false } )", 200);
		}
		
		if ( info_selected )
		{
			clearTimeout(timer);
			timer= setTimeout("$('#Grid').scrollTo( currentPage(), { axis: 'yx', duration: 200, queue: false } )", 200);
		}
	});
	
	$("#TitleBar, #Nav #back-to-gallery, a#arrow-next, a#arrow-prev").click( function()
	{
		info_selected= false;
		
		if ( activeMenu != "" )
			$("." + activeMenu).removeClass(activeMenu + "-active");
	});
	
	$("a#arrow-next").click( function()
	{
		selected_slide++;
			
		if ( selected_slide > total_slides )
		{
			selected_slide= 1;
		}
		
		$("#Intro").fadeOut();
		$('#Grid').scrollTo( currentSlide(), { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: true } );
		
		return false;
	});
	
	$("a#arrow-prev").click( function()
	{
		selected_slide--;
			
		if ( selected_slide <= 0 )
		{
			selected_slide= total_slides;
		}
		
		$("#Intro").fadeOut();
		$('#Grid').scrollTo( currentSlide(), { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: true } );
		
		return false;
	});
	
	$("#Nav #back-to-gallery").click( function()
	{
		$('#Grid').scrollTo( "#s" + selected_slide, { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: true } );
	});
	
	$("#TitleBar").click( function()
	{
		$('#Grid').scrollTo( "#s1", { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: true, onAfter: function() { } } );
		$("#Intro").fadeIn();
	});
	
	$("a.info_link").live( "click", function()
	{
			$("#Intro").fadeOut();
			$(".autoflow").removeClass("autoflow");
			
			if ( activeMenu != "" )
				$("." + activeMenu).removeClass(activeMenu + "-active");
			
			$(this).addClass(this.rel + "-active");
			activeMenu= this.rel;
			
			var qu_it= true;
			
			if ( info_selected )
				qu_it= false;
				
			info_selected= true;
			selected_infotype= this.rel.slice(this.rel.length-1);
			selected_infotype= this.rel;
			
			if (jQuery.inArray(selected_infotype, loadedPages) == -1)
			{
				$("#" + selected_infotype + " .infopage .inner .content").html('<img src="assets/img/loading.gif" alt="Loading..." />');
			}
			
			$('#Grid').scrollTo( "#" + this.rel, { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: true,
						onAfter: function()
						{
							location.hash= selected_infotype;
									
							// Page has not been loaded, GET it
							if (jQuery.inArray(selected_infotype, loadedPages) == -1)
							{
								$.get(selected_infotype + ".html", function(data)
								{
									$("#" + selected_infotype + " .infopage .inner .content").html(data);
									$("#" + selected_infotype).addClass("autoflow");
									loadedPages[loadedPages.length]= selected_infotype;
											
									// Scrollbars fuck with our layout
									if ( scrollBarDetected() )
									{
										$("#" + selected_infotype + " .infopage").addClass("push");
									}
											
									// Activate lightbox for newly loaded page.
									$(".enlarge").lightBox(
									{
										overlayBgColor: '#EEE',
										imageBtnClose: 'assets/img/close.png',
										imageLoading: 'assets/img/loading.gif'
									});
									
									if (selected_infotype == "contact")
									{
										$("input#send").click( function()
										{
											var formdata= $("form[name=contact]").serialize();
		
											jQuery.post( "send_mail.php", formdata,
												function(data, stauts)
												{
													$(".form_message").show().html(data);
													$("form[name='contact']")[0].reset();
												}	
											); 
		
											return false;
										});
									}
								});
							}
							// Page has already been loaded, "cached" in HTML
							else
							{
								$("#" + selected_infotype).addClass("autoflow");
							}
						}
			});
			
			return false;
		}
	);
});

function pageRequested()
{
	var loc= location.hash;
	return ( loc != "" && loc != '#' && loc != 'undefined' ) ? true : false;
}

function scrollBarDetected()
{
  var docHeight = $(document).height();
  var scroll    = $(currentPage() + " .infopage .inner").height();
  
  return ( scroll > docHeight ) ? true : false;
}

function currentSlide()
{
	return "#s" + selected_slide;
}

function currentPage()
{
	return "#" + selected_infotype;
}

function set_dimensions()
{
	var w= $(window).width();
	var h= $(window).height();

	$("#Grid").width(w).height(h);
	$("#Grid .container").height(h);
	$("#Grid .container_inner").width(w).height(h);
	$("#GalleryHolder li").width(w).height(h);
}

// Based on jQuery Supersized plugin
function set_imageSize()
{
	var options= {
			startwidth: 1024,
			startheight: 768,
			minsize: .25,
			slideshow: 0,
			slideinterval: 2000
	};
				
	 $("#GalleryHolder img").each(function()
	 {
	 	//Define image ratio & minimum dimensions
		var minwidth = options.minsize*(options.startwidth);
		var minheight = options.minsize*(options.startheight);
		var ratio = options.startheight/options.startwidth;
			
		//Gather browser and current image size
		var imagewidth = $(this).width();
		var imageheight = $(this).height();
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
			
		//Check for minimum dimensions
		if ((browserheight < minheight) && (browserwidth < minwidth))
		{
			$(this).height(minheight);
			$(this).width(minwidth);
		}
		else
		{	
			//When browser is taller	
			if (browserheight > browserwidth)
			{
				imageheight = browserheight;
				imagewidth = browserheight/ratio;
				$(this).height(browserheight);
				$(this).width(imagewidth);
				    
				if (browserwidth > imagewidth)
				{
				    imagewidth = browserwidth;
				    imageheight = browserwidth * ratio;
				    $(this).width(browserwidth);
				    $(this).height(imageheight);
				}
			}
			
			//When browser is wider
			if (browserwidth >= browserheight)
			{
				imagewidth = browserwidth;
				imageheight = browserwidth * ratio;
				$(this).width(browserwidth);
				$(this).height(imageheight);
				    
				if (browserheight > imageheight)
				{
				    imageheight = browserheight;
				    imagewidth = browserheight/ratio;
				    $(this).height(browserheight);
				    $(this).width(imagewidth);
				}
			}
		}
			
		return true;
	});
}

function onPreload(preloadedImages, totalImages)
{
	if ( totalImages != preloadedImages.length )
   	{
   		alert("The gallery was not loaded properly!");
		return;
   	}

	preloadedImages.splice(preloadedImages.indexOf('assets/img/menu-items.jpg'), 1);
		
	for( i= 0; i < preloadedImages.length; i++ )
	{
  		$("#GalleryHolder li#s" + Number(i+2)).append('<img src="' + imagesToPreload[i+1] + '" alt="" />');
  	}
  		
	set_imageSize();
}
