var qu= false;
var selected_slide= 1;
var selected_infotype= 1;
var info_selected= false;

var total_slides;
var timer;
var preload= null;

var aImg = ["http://partofyourlife.com/assets/img/CF7W6185-2.jpg", "http://partofyourlife.com/assets/img/CF7W6178-1.jpg", "http://partofyourlife.com/assets/img/CF7W6115.jpg", "http://partofyourlife.com/assets/img/CF7W6152-2.jpg"];

set_dimensions();

jQuery(function($)
{
	set_imagesize();
	
	total_slides= $("#GalleryHolder li").length;
	
	$('#Grid').scrollTo( '#s' + selected_slide, { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: true,
				onAfter: function()
				{ 
					$(".designed_by").fadeIn();
					$("#TitleBar #loading").hide();
					
					if ($.browser.msie) {
						$("#TitleBar #subtitle, #Arrows").css("visibility", "visible");
					}else
					{
						$("#TitleBar #subtitle, #Arrows").fadeIn();
					}
						
					$("#NavHolder").animate({ width: "200px" }, 500, 'easeOutCubic', function()
					{
						$("#NavContainer").hover(
							function()
							{
								$("#NavHolder").stop();
								$("#arrow_next").stop();
								$("#arrow_next").animate({ left: '105px' }, 500, 'easeOutCubic');
								$("#NavHolder").animate({ width: '200px' }, 500, 'easeOutCubic');
				
								if ( !$.browser.msie )
									$("#Arrows").fadeTo("normal", 1);
							},
							function()
							{
								$("#arrow_next").animate({ left: '6px' }, 500, 'easeOutCubic');
								$("#NavHolder").animate({ width: '0px' }, 500, 'easeOutCubic');
					
								if ( !$.browser.msie )
									$("#Arrows").fadeTo("normal", 0.5);
							}
						);
					});
			 	}
	});
	
	preload= new ImagePreloader(aImg, onPreload);

	if (!$.browser.msie)
	{
		$("#Arrows a, #NavHolder a").hover(
			function()
			{
				$(this).fadeTo("normal", 0.5);
			},
			function()
			{
				$(this).fadeTo("slow", 1);
			}
		);
	}

	$(window).resize( function() 
	{
		set_dimensions();
		set_imagesize();
		
		if ( !info_selected) {
			clearTimeout(timer);
			timer= setTimeout("$('#Grid').scrollTo( '#s' + selected_slide, { axis: 'yx', duration: 200, queue: false } )", 200);
		}
		
		if ( info_selected ) {
			clearTimeout(timer);
			timer= setTimeout("$('#Grid').scrollTo( cur_infotype(), { axis: 'yx', duration: 200, queue: false } )", 200);
		}
	});
	
	$("#Nav #gallery, a#arrow_next, a#arrow_prev").click( function()
	{
		if ( info_selected )
			qu= true;
		else
			qu= false;
			
		info_selected= false;
	});
	
	$("a#arrow_next").click( function()
	{
		selected_slide++;
			
		if ( selected_slide > total_slides )
		{
			selected_slide= 1;
		}
		
		$('#Grid').scrollTo( "#s" + selected_slide, { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: qu } );
		
		return false;
	});
	
	$("a#arrow_prev").click( function()
	{
		selected_slide--;
			
		if ( selected_slide <= 0 )
		{
			selected_slide= total_slides;
		}
			
		$('#Grid').scrollTo( "#s" + selected_slide, { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: qu } );
		
		return false;
	});
	
	$("#Nav #gallery").click( function()
	{
		//selected_slide= 1;
		$('#Grid').scrollTo( "#s" + selected_slide, { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: qu } );
	});
	
	$("a.info_link").click( function()
		{
			$(".container_inner.info.autoflow").removeClass("autoflow");
			
			var qu_it= true;
			
			if ( info_selected )
				qu_it= false;
				
			info_selected= true;
			selected_infotype= this.rel.slice(this.rel.length-1);
			$('#Grid').scrollTo( "#" + this.rel, { axis: 'yx', duration: 1300, easing: 'easeOutCubic', queue: qu_it,onAfter: function() { $("#info_" + selected_infotype).addClass("autoflow"); } } );
		}
	);
	
	$("form[name='contact']").submit( function() {
		var formdata= $(this).serialize();
		
		jQuery.post( "send_mail.php", formdata,
			function(data, stauts) {
				$(".form_message").show().html(data);
				$("form[name='contact']")[0].reset();
			}	
		); 
		
		return false;
	});
});

function cur_archetype()
{
	return "#type" + selected_archetype + "_" + archetype_img;
}

function cur_infotype()
{
	return "#info_" + 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= 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;
				    $(this).height(browserheight);
				    imagewidth = browserheight/ratio;
				    $(this).width(imagewidth);
				    
				    if (browserwidth > imagewidth){
				    	imagewidth = browserwidth;
				    	$(this).width(browserwidth);
				    	imageheight = browserwidth * ratio;
				    	$(this).height(imageheight);
				    }
				
				}
				
				//When browser is wider
				if (browserwidth >= browserheight)
				{
				    imagewidth = browserwidth;
				    $(this).width(browserwidth);
				    imageheight = browserwidth * ratio;
				    $(this).height(imageheight);
				    
				    if (browserheight > imageheight)
				    {
				    	imageheight = browserheight;
				    	$(this).height(browserheight);
				    	imagewidth = browserheight/ratio;
				    	$(this).width(imagewidth);
				    }
				}
			}
			//return false;
		});
	};

	function onPreload(aImages, nImages)
	{
		var oDiv = document.getElementById("theDiv");

		if ( nImages != aImg.length )
   		{
			//oDiv.innerHTML = "Images did not load properly";
			return;
   		}

		for( i= 0; i < aImg.length; i++ )
		{
  			$("#GalleryHolder li#s" + Number(i+2)).append('<img src="' + aImg[i] + '" />');
  		}
  
		set_imagesize();
	}