$("document").ready(function()
{
	//FAQ accordion
	$("#faqs h4 a").each(function()
	{
		$(this).click(function()
		{
			if($(this).parent().next('div').is(':visible'))
			{
				$(this).removeClass('highlight').find('span').text(" [+] ").end().parent().next('div').slideUp('fast');
			}
			else
			{
				$(this).addClass('highlight').find('span').text(" [-] ").end().parent().next('div').slideDown('fast');
			}
			return false;
		}).parent().next('div').hide();
	});
	
	//main menu
	var startpoint=10;
	$("#headernav").attr("id","jqheadernav");
	$("#jqheadernav>li:not(#homelink)")
	.each(function(index)
	{
		var distance=(index*110)+202;
		if($(this).hasClass("active"))
		{
			startpoint=distance;
			$(this).parent().css({backgroundPosition:''+startpoint+'px -153px'});
		}
		$(this)
		.hover(function()
		{
			var d=distance;
			$(this).parent().stop(true,false).animate({backgroundPosition:''+d+'px -153px'});
			$(this).find("ul").css({'display':'block','zIndex':'6'}).stop().fadeTo('fast',1);
		},
		function()
		{
			var z=index;
			$(this).parent().stop(true,false).animate({backgroundPosition:''+startpoint+'px -153px'});
			$(this).find("ul").css({'zIndex':z}).stop().fadeTo('fast',0,function(){$(this).css('display','none');});
		});
	});
	
	//smooth scrolling contents
	$(".contents a:not(.noscroll),.btt a:not(.noscroll)").click(function()
	{
		var scrollto=0;
		var link=$(this).attr('href').substring(1);
		if(link!="top")
		{
			scrollto=$("a[name="+link+"]").offset().top;
		}
		$('html, body').animate({scrollTop: scrollto},500,function(){location.hash=link;});
		return false;
	});
	
	//Callback form
	$('.callmeback').click(function()
	{
		$('body')
		.append('<div id="overlay"></div>')
		.append($('<div class="callbackform"></div>').load('/includes/callbackform.php',{'productId':product}));
		return false;
	});
	$('#callbackday').live('change',function()
	{
		if($(this).find("option:selected").text()=="Today")
		{
			var today=new Date();
			if(today.getDay()==6)
			{
				$('#callbacktime').html(gettimes(""+today.getHours()+":"+today.getMinutes(),"12:15"));
			}
			else
			{
				$('#callbacktime').html(gettimes(""+today.getHours()+":"+today.getMinutes(),"18:15"));
			}
		}
		else if($(this).find("option:selected").text()=="Tomorrow")
		{
			var today=new Date();
			if(today.getDay()==5)
			{
				$('#callbacktime').html(gettimes("9:00","12:15"));
			}
			else
			{
				$('#callbacktime').html(gettimes("9:00","18:15"));
			}
		}
		else if($(this).find("option:selected").text()=="Saturday")
		{
			$('#callbacktime').html(gettimes("9:00","12:15"));
		}
		else
		{
			$('#callbacktime').html(gettimes("9:00","18:15"));
		}
	});
	//Contact form
	$('#emailcontact').click(function()
	{
		$('body')
		.append('<div id="overlay"></div>')
		.append($('<div class="callbackform"></div>').load('/includes/contactform.php',{'dummy':'dummy'}));
		return false;
	});
	$('#callbackclose, #contactclose').live('click',function()
	{
		$('#overlay').remove();
		$('.callbackform').remove();
		return false;
	});
	$('#claimclose').live('click',function()
	{
		$('#overlay').remove();
		$('#claimsform').remove();
		return false;
	});
});

function gettimes(start, end)
{
	var t=start.split(":");
	var output="";
	if(t[1]%15!=0)
	{
		t[1]=(15-(t[1]%15))+parseInt(t[1]);
		if(t[1]==60)
		{
			t[1]="00";
			t[0]++;
		}
	}
	while((t[0]+":"+t[1])!=end)
	{
		output+="<option>"+t[0]+":"+t[1]+"</option>";
		t[1]=parseInt(t[1])+15;
		if(t[1]==60)
		{
			t[1]="00";
			t[0]++;
		}
	}
	output+="<option>"+t[0]+":"+t[1]+"</option>";
	return output;
}
