$("document").ready(function()
{
	//replace all select menus
	$("select").each(function()
	{
		$(this).selectmenu({width:200,maxHeight:300,style:'dropdown'});
	});
	
	//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');});
		});
	});
	
	//Carousel
	var currenttimer;
	var autofadetime=10000;
	var autofadetimelonger=30000;
	var carouseltimerfunc=function()
	{
		var prev=$(".product:visible");
		switch(prev.attr('id'))
		{
			case "carouselcomm":
				next=$("#carouselpers");
				break;
			case "carouselpers":
				next=$("#carouselhome");
				break;
			case "carouselhome":
				next=$("#carouselbusi");
				break;
			case "carouselbusi":
				next=$("#carouselclai");
				break;
			case "carouselclai":
				next=$("#carouselcomm");
				break;
		}
		rotatecarousel(prev,next,autofadetime);
	};
	
	$("#carouselnav a").click(function()
	{
		var href=$(this).attr('href');
		var dest=href.substring(href.length-4,href.length);
		var previous=$(".product:visible");
		if(previous.length==1)
		{
			if(previous.attr('id')!="carousel"+dest)
			{
				var next=$("#carousel"+dest);
				rotatecarousel(previous,next,autofadetimelonger);
			}
		}
		return false;
	});
	
	currenttimer=setTimeout(carouseltimerfunc,autofadetime);
	
	function rotatecarousel(prev,next,delay)
	{
		clearTimeout(currenttimer);
		var screenwidth=$("html").outerWidth(true);
		next.css({'left':screenwidth,'display':'block'});
		prev.animate({'left':(0-screenwidth)},1000,function(){prev.css({'display':'none'});});
		next.animate({'left':0},1000);
		$("#carouselnav a").removeClass('highlight');
		$("#"+next.attr('id')+"button").addClass('highlight');
		currenttimer=setTimeout(carouseltimerfunc,delay);
	}
	
	//smooth scrolling contents
	$(".contents a,.btt a").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"));
		}
	});
});
	
