// requires jQuery

var slidePanelTimer;

function startInterval(obj){
	slidePanelTimer = setInterval(
		function(){
			obj.slidePanels("right");
		},
		7000);
}

function stopInterval(){
	clearInterval(slidePanelTimer);
}
	
$(document).ready(function () {

	var slidePanelsWidth;
	
	startInterval($("#homeMainPanels"));
	
	$(".slideContainer").each(function(i){
		$(this).mouseover(function(){ stopInterval() });
		$(this).mouseout(function(){ startInterval($(this)) });
	});
	
	$(".slideContainer .slidePanels").each(function(i){
		slidePanelsWidth = 0;
		$(this).find(".slidePanel").each(function(i){
			slidePanelsWidth += $(this).width();
		});
		$(this).width(slidePanelsWidth+"px");
	});
	
	$.fn.slidePanels = function(dir){
		var panel = $(this).find(".slidePanels");
		var curLeftPos = parseInt(panel.position().left);
		var newLeftPos = "";
		var animate = false;
		var duration = (panel.attr("duration"))?parseInt(panel.attr("duration")):300;
		var easing = (panel.attr("easing"))?panel.attr("easing"):"linear";
		var callBack = (panel.attr("callBack"))?function(){eval(panel.attr("callBack"));}:null;
		
		if(dir=="left"){
			newLeftPos = curLeftPos + $(this).width();
			if(newLeftPos <= 0){ animate=true; }
		}else if(dir=="right"){
			newLeftPos = curLeftPos - $(this).width();
			if((newLeftPos+panel.width()) > 0){ animate=true; }
			else{
				//newLeftPos = 0;
				//animate=true;
			}
		}else{
			newLeftPos = "-"+($(this).width()*(dir-1));
			animate=true;
		}
		
		if(animate && !panel.is(":animated")){
			panel.animate({left: newLeftPos+"px"},duration,easing,callBack);
		}
		
		return $(this);
	};
});
