var max_wait=60000;
var wait_interval=5000;
//init with first picture
i=1;
jQuery(function($){
	$("ul.navigation>li").hover(function(){ $(this).addClass("hover"); }, function(){ $(this).removeClass("hover"); });
	//$(".fancybox").colorbox({width:"770px",height:"880px", href:"/legal-guide/preview.php"});
	$(".fancybox").colorbox({width:"770px",onComplete:function(){ initPreview(); }});
	$(".purchase_guide").colorbox({width:"700px",height:"400px", inline:true, href:"#terms_box"});

	if($.browser.msie){$("#banner li .desc").css("bottom","2px");}
	$("#banner li:first-child").children(".desc").fadeTo(0,"0").fadeTo(3000,"0.85");
	if($("#banner").children().length>1){setInterval("nextPic()",6000)};

	
	//legal guide timeout checker
	dowait();



	//animate the homepage
	if($("ul.scroll").length){animHome();}

});	

var curtime=0;
var dowait=function(){
	var waiting=$("#waiting");
	if(waiting.length){
		if(curtime>=(max_wait/wait_interval)){
			waiting.removeClass("is_waiting");
		}else{
			var c_tx=waiting.attr("rel");
			waiting.parent().load("http://calventurelaw.com/legal-guide/success.php",'tx='+c_tx,function(){
				if(curtime<(max_wait/wait_interval)){
					curtime++;
					setTimeout("dowait()",wait_interval);
				}
			});
		}

	}
}


function initPreview(){
	//preview guide
	$("#preview_submit").click(function(e){
		e.preventDefault();
		$.post("/legal-guide/preview.php",{
			first_name:$("#first_name").val(),
			last_name:$("#last_name").val(),
			phone:$("#phone").val(),
			email:$("#email").val()
		},function(data){
			$("#preview_container").html(data);
			$.fn.colorbox.resize();
			initPreview();
		});
	});

}

function nextPic(){
	$("#banner li:nth-child("+i+")").fadeOut("slow");
	i++;
	if(i>$("#banner").children().length){i=1;}
	$("#banner li:nth-child("+i+")").fadeIn("slow").children(".desc").fadeTo(0,"0").fadeTo(3000,"0.85");
}

function purchaseGuide(){
	if($("#accept_terms:checked").length){
		location.href="/legal-guide/process.php";
	}else{
		$("#accept_terms").parent().addClass("required");
	}
}

var homeAnimId=-1;

function animHome(){
	homeAnimId=setInterval("setHome('anim')",5000);
}

function setHome(pos){
	var $scroll=$("ul.scroll");
	var cPos=parseInt($scroll.css("left").replace("px","")-50)/-500;
	//make sure that the animation is over
	if(cPos==parseInt(cPos)){
		
		if(pos=="anim"){
			//what to do when animating?
			pos="+";
		}else{
			//clear out the out scroller if the user clicks on an indicator
			if(homeAnimId!=-1){
				clearTimeout(homeAnimId);
				homeAnimId=-1;
			}
		}
		//allow for incrementing too
		if(pos=="+"){
			pos=cPos+1;
		}else if(pos=="-"){
			pos=cPos-1;
		};
		//don't go past the end
		if(pos>$scroll.children().length-1){pos=0;}
		if(pos<0){pos=$scroll.children().length-1;}
		//move the scroller
		$scroll.animate({left:((-500*pos)+50)+"px"},'slow');
		//set the correct indicator
		$("ul.indicators").find(".active").removeClass("active");
		$($("ul.indicators").children()[pos]).children("a").addClass("active");

	}

}