// JavaScript Document
 $(document).ready(function(){
   
   	var leftpos;
	var timer;
	var href;
	var finalWidth;
	var margin;
	
	if($("body").outerHeight() < 592)
	{
		$("#Main").css("margin-top",0);	
	}

	
	var name = $("#Slider").attr("name");
	
	switch (name)
	{
		case "SayingIDo":
			init($("#SayingIDo"))
		break;
		case "Home":
			init($("#Home"))
		break;
		case "Gallery":
			init($("#Gallery"))
		break;
		case "Chapel":
			init($("#Chapel"))
		break;
		case "Chauffeur":
			init($("#Chauffeur"))
		break;
		case "Contact":
			init($("#Contact"))
		break;
		case "Menus":
			init($("#Menus"))
		break;
		case "Photography":
			init($("#Photography"))
		break;
	}
		  
   $("#Home").click(function(){
		slideMenuControl($("#Home"),"functions.htm");	
	});
   
   $("#Menus").click(function(){
		slideMenuControl($("#Menus"),"menus.htm");	
	});
	
    $("#Gallery").click(function(){
		slideMenuControl($("#Gallery"),"gallery.htm");	
	});
	
	$("#Chapel").click(function(){
		slideMenuControl($("#Chapel"),"http://www.functionsatfederation.com.au/chapel.htm");	
	});
	
	$("#Chauffeur").click(function(){
		slideMenuControl($("#Chauffeur"),"chauffeur.htm");		
	});
	
	$("#Contact").click(function(){
		slideMenuControl($("#Contact"),"contact.htm");		
	});
	
	$("#SayingIDo").click(function(){
		slideMenuControl($("#SayingIDo"),"sayingido.htm");		
	});
	
	$("#Photography").click(function(){
		slideMenuControl($("#Photography"),"chapelphotography.htm");		
	});

	
	function slideMenuControl(control,inhref)
	{
	    var originalsliderpos =  $("#Slider").offset().left - $("#WhiteScroll").offset().left;
	    var originalsliderwidth = $("#Slider").outerWidth();
		
		href = inhref;		
		finalWidth = control.outerWidth();
		leftpos = control.offset().left - $("#WhiteScroll").offset().left - originalsliderpos;	
		rightPoint = Math.abs(leftpos) +  finalWidth;
		
		if(leftpos >= 0)
			animateRight();
		else
			timer = setInterval(animateLeft,1);			
	
	}		

	function animateLeft()
	{	
		var	time = rightPoint/250 * 750;		
		margin = $("#Slider").css("margin-left");
		
		if(margin == "auto") //IE fix
			margin = 0;
			
		$("#Slider").css("margin-left",parseInt(margin) - 4);
		$("#Slider").width($("#Slider").width()+4);	
		
		//slider.style.left = parseInt(slider.style.left) - 2 + "px";
		if(parseInt(margin) <= leftpos + 2 )		
		{
			$("#Slider").css("margin-left",leftpos);		
			$("#Slider").animate( { width:finalWidth}, time,"linear", function(){window.location.href=href;} );
			clearTimeout(timer);			
		}	
	}	


	function init(control)
	{			
		$("#Slider").width(control.outerWidth());		
 		$("#Slider").css("left",control.offset().left - $("#WhiteScroll").offset().left);
	}
	

	function animateRight()
	{
			var	time = rightPoint/250 * 750;			
			$("#Slider").animate( { width:rightPoint}, time );
			$("#Slider").animate( { width:finalWidth,marginLeft:leftpos}, time ,"linear", function(){window.location.href=href;} );
	}	
	
	
  });
