$(document).ready(function(){
	var nextlocked = false;
	var prevlocked = false;
	
	setdayornight();
	
	$(function() {
		$("#dateofvisit").datepicker();
	});

	
	
	$("hr").each(
		function()
		{
			$(this).replaceWith('<p class="clearunderline"></p>');
		} 
	    );

	$('#datepicker').datepicker({
                    dateFormat: 'dd-mm-yy',
                    onSelect: function(dateText, inst) {
                     var date_array=dateText.split("-");
                 window.location = "http://www.at-bristol.org.uk/index.php?id=145&jour=" + date_array[0] + "&mois=" + date_array[1] + "&annee=" + date_array[2];
					}
     });
	
	$(".closed").click(function(e)
	{
			e.preventDefault();
	});
	
    $(".next").click(function(e){
		e.preventDefault();
		if(!nextlocked)
		{		
			var leftval = parseInt($('#slider').css('left'));
			var width = 75;
			var nomonths = 10;
			var maxvalue = (width * nomonths) * -1;
			if(leftval > maxvalue)
			{
				nextlocked = true;
				$("#slider").animate({left:"-=75px"},{
        duration: 'fast', // how fast we are animating
        easing: 'linear', // the type of easing
        complete: function() 
		{ // the callback
          nextlocked = false;
		  var leftval = parseInt($('#slider').css('left'));
		  if(leftval < 0)
		  {
			  $(".previous").removeClass("disablenav");
		      $(".next").removeClass("disablenav");
		  }
		  if(leftval <= maxvalue)
		  {
		      $(".next").addClass("disablenav");
		   }
		   if(leftval == 0)
		   {
				$(".previous").addClass("disablenav");
		   }
		}					 
		});
			}
		}
    });
	
	 $(".previous").click(function(e){
	 	e.preventDefault();
		if(!prevlocked)
		{
			var leftval = parseInt($('#slider').css('left'));
				var width = 75;
			var nomonths = 10;
			var maxvalue = (width * nomonths) * -1;
			if(leftval < 0)
			{
				prevlocked = true;
				$("#slider").animate({left:"+=75px"},{
					duration: 'fast', // how fast we are animating
					easing: 'linear', // the type of easing
					complete: function() 
					{ // the callback
					   prevlocked = false;
					   var leftval = parseInt($('#slider').css('left'));
					  if(leftval < 0)
					  {
						  $(".previous").removeClass("disablenav");
						  $(".next").removeClass("disablenav");
					  }
					  if(leftval <= maxvalue)
					  {
						  $(".next").addClass("disablenav");
					   }
					   if(leftval == 0)
					   {
							$(".previous").addClass("disablenav");
					   }
					}
        		});
			}
		}
	});
	
	function moveToDate()
	{
		var width = 75;
		var nomonths = 10;
		var maxvalue = (width * nomonths) * -1;
        if(typeof(thedistance) == 'undefined' || !thedistance)
		{
			thedistance = 0;
		}
		distanceval = parseInt(thedistance);
		if(distanceval < 0)
		{
		    $(".previous").removeClass("disablenav");
			$(".next").removeClass("disablenav");
		}
		if(distanceval <= maxvalue)
		{
			$(".next").addClass("disablenav");
			thedistance = "-750";	
			//disable nextarrow	
		}
		if(distanceval >= 0)
		{
		    //disable nextarrow	
			$(".previous").addClass("disablenav");
		}
		 $("#slider").animate({left: thedistance},{duration:10});
	}
	
	moveToDate();
}); 








function toggleTime(timevar)
{
	var timevar = timevar.replace(/\n/g, "\\n" );
	if(timevar == "day")
	{
		$("body").removeClass("night").removeClass("education-night").addClass("day");
		$.cookie("dayornight", "day", { expires: 0.04 });

	}
	if(timevar == "night")
	{
		$("body").removeClass("day").removeClass("education-day").addClass("night");
		$.cookie("dayornight", "night", { expires: 0.04 });
	}
	if(timevar == "education-day")
	{
		$("body").removeClass("night").removeClass("education-night").addClass("education-day");
		$.cookie("dayornight", "day", { expires: 0.04 });

	}
	if(timevar == "education-night")
	{
		$("body").removeClass("day").removeClass("education-day").addClass("education-night").addClass("night");
		$.cookie("dayornight", "night", { expires: 0.04 });
	}

}

function setdayornight()
{
	if($.cookie("dayornight") == "night")
	{
		$("body").removeClass("day").addClass("night");	
			
	}
}

function dayornight()
{
	if(!$.cookie("dayornight"))
	{
		$.cookie("dayornight", "day", { expires: 0.04 });
	}
	var cookiesetting =  $.cookie("dayornight");
	var cookiesetting = cookiesetting.replace(/\n/g, "\\n" );
	return cookiesetting;
}

function changeTemplate(changeto)
{
	if(changeto == "education")
	{
		if(!$.cookie("dayornight"))
		{
			$("body").removeClass("day").removeClass("education-day").addClass("education-day");
		}
		else
		{
			if($.cookie("dayornight") == "day")
			{
				$("body").removeClass("day").removeClass("education-day").addClass("education-day");
			}
			else if($.cookie("dayornight") == "night")
			{
				$("body").removeClass("night").removeClass("education-night").addClass("education-night");
			}
		}
	}
	if(changeto == "normal")
	{
		if(!$.cookie("dayornight"))
		{
			$("body").removeClass("education-day").removeClass("day").addClass("day");
		}
		else
		{
			if($.cookie("dayornight") == "day")
			{
				$("body").removeClass("education-day").removeClass("day").addClass("day");
			}
			else if($.cookie("dayornight") == "night")
			{
				$("body").removeClass("education-night").removeClass("night").addClass("night");
			}
		}
	}
}