$(document).ready(function(){
	
	var current = 1;
	var maxwidth = $(window).width();
	var panels = $("#panels").children().length;

	$("#quick-buttons li").each(function(counter) { $(this).attr("id","to-"+(counter+1)); });
	$("#ticker ul li").each(function(counter) { $(this).attr("id","news-"+(counter+1)).fadeOut(0,function(){ $("#ticker li#news-1").fadeIn(0); }); });
	$("#ticker ul:first li:first-child").addClass("on");	
	$("#ticker ul li p").css("display","none");
	$("#ticker-more").css("display","none");
	
	
	$("#panels, #panels-o").css("width",(maxwidth*panels)+"px");	
	$(".slide").css("width",maxwidth+"px");	
	
	$(window).resize(function() {
		maxwidth = $(window).width();
		var newcoords = (maxwidth*current)-maxwidth;
		$("#panels, #panels-o").css("width",(maxwidth*panels)+"px");
		$("#panels, #panels-o").css("left","-"+newcoords+"px");	
		$(".slide").css("width",maxwidth+"px");		
	});
	 
		$("#quick-buttons li").click(function(){ 		
			var goingto = $(this).attr("id").replace(/to-/,'');	
			$("#quick-buttons li").removeClass("on");
			$(this).addClass("on");
			
			if(current == goingto) {
				//do nothing
			} else {
				var coords = (maxwidth*goingto)-maxwidth;			
					$("#panels-o").animate({ "opacity" : "0" },"fast", function(){
						$("#panels-o").animate({ "left" : "-"+coords+"px"},"slow", function() {
								$("#panels-o").animate({ "opacity" : "1.0" },"fast");
							}					
						);
					});	
					$("#panels").delay(300).animate({ "left" : "-"+coords+"px" },"slow");		
					current = goingto;	
			}									
		});		
		
		// Autoslider
		function autoslide(){
		
			var goingto = 0;
			if(current == "1") { goingto = 2; }
			if(current == "2") { goingto = 3; }
			if(current == "3") { goingto = 1; }	
				
			$("#quick-buttons li").removeClass("on");
			$("#quick-buttons li#to-"+goingto).addClass("on");
			
				var coords = (maxwidth*goingto)-maxwidth;			
					$("#panels-o").animate({ "opacity" : "0" },"fast", function(){
						$("#panels-o").animate({ "left" : "-"+coords+"px"},"slow", function() {
								$("#panels-o").animate({ "opacity" : "1.0" },"fast");
							}					
						);
					});	
					$("#panels").delay(300).animate({ "left" : "-"+coords+"px" },"slow");		
					current = goingto;				
		}
		
		var autoslider = setInterval( autoslide, 10000);
		$("#quick-buttons ul li").click(function(){ clearInterval(autoslider); });
			  
  });
