
var count = 0;
var testimonials = new Array();
var custTest = new Array(6);
var nimages = 5;
var images = new Array();
var img1 = 0;
var img2 = 1;
var img3 = 2;
var img4 = 3;
var img5 = 4;
var originalsize = new Array(17);
var t5;
var inc = 0;

$(window).load(function(){

	$(".cust-test").hide();
	$(".test-text").hide();
	
	height = $("#home-content").outerHeight(false);
	$(".home-content-bg").attr("height",height+10);

	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		ieversion=new Number(RegExp.$1);
		if(ieversion<=7.0){
			$('ul','.about-us-list').css("marginTop","50px");
			$('ul','.products-list').css("marginTop","45px");
			$('li','.about-us-list').css("height","33px");
			$('li','.products-list').css("height","33px");
		}
	}
	
	custTest[0] = 2;
	custTest[1] = 2;
	custTest[2] = 2;
	custTest[3] = 2;
	custTest[4] = 2;
	custTest[5] = 2;
	custTest[6] = 2;
	custTest[7] = 2;
	custTest[8] = 2;
	
	testimonials[0] = '“Consumer United saved me $543 on a six-month policy for identical coverage terms with the old carrier. Thanks!”<br><span>--Jennifer B., Asbury Park, NJ</span>';
	testimonials[1] = '"I just saved $350 on my auto insurance and $35 on my renters insurance. That'+"'"+'s a REAL economic stimulus refund check.”<br><span>--Geoff G., Methuen, MA</span>';
	testimonials[2] = '"[Consumer United] saved me over six hundred dollars per year on my vehicle and homeowner'+"'"+'s insurance, and I can'+"'"+'t tell you how happy that makes me."<br><span>--Susie O., Plattsburgh, NY</span>';
	testimonials[3] = '"CU made it easy to switch my insurance, save money and they were a pleasure to work with."<br><span>--Court C., Boston, MA</span>';

	for(i=1;i<18;i++){
		originalsize[i-1] = new Array(5);
		originalsize[i-1][0] = parseInt($("#cust-"+i).css("width"));
		originalsize[i-1][1] = parseInt($("#cust-"+i).css("height"));
		originalsize[i-1][2] = parseInt($("#cust-"+i).css("top"));
		originalsize[i-1][3] = parseInt($("#cust-"+i).css("left"));
		originalsize[i-1][4] = parseInt($("#cust-"+i).css("z-index"));
		$("#cust-"+i+"-img").attr("width",originalsize[i-1][0]);
		$("#cust-"+i+"-img").attr("height",originalsize[i-1][1]);
	}
	
	$("#testimonial-block").html(testimonials[count]);
	t1 = setTimeout("nextTestimonial()",5000);
	
	width = parseInt($("#slider").css("width"));
	
	if(width<=1800){
		shift = (1800-width)/2
		$("#people").css("left","-"+shift+"px");
	}
	else{
		shift = (width-1800)/2
		$("#people").css("left",shift+"px");
	}
	
	$(".cust-test").css("z-index", 2000);
	
	$(".slider-person").hover(function(){
		
		id = $(this).attr("id");
		index = parseInt(id.substr(5))-1;
		if($(this).is(":animated")){
			$(this).stop(true, false);
			$("#"+id+"-img").stop(true, false);
		}
		
		newwidth = Math.round(1.6*originalsize[index][0]);
		newheight = Math.round(1.6*originalsize[index][1]);
		newleft = originalsize[index][3]-Math.round((newwidth-originalsize[index][0])/2);
		if(originalsize[index][2]=="auto"){
			newtop = 0-Math.round((newheight-originalsize[index][1])/2);
		}
		else{
			newtop = originalsize[index][2]-Math.round((newheight-originalsize[index][1])/2);
		}
		
		
		$(this).css("z-index",1000+index);
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
			ieversion=new Number(RegExp.$1);
			if(ieversion<8.0){
				$("#slider").css("z-index",100);
			}
		}
		
		$(this).animate({ 
			width: newwidth, 
			height: newheight,
			left: newleft,
			top: newtop
		}, 250);
		
		t4 = setTimeout("displayCaption(id)",500);
		
	}, function(){
		
		id = $(this).attr("id");
		if($(this).is(":animated")){
			$(this).stop(true, true);
			$("#"+id+"-img").stop(true, true);
		}
		index = parseInt(id.substr(5))-1;
		clearTimeout(t4);
		
		newwidth = originalsize[index][0];
		newheight = originalsize[index][1];
		newtop = originalsize[index][2];
		newleft = originalsize[index][3];
		
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
			ieversion=new Number(RegExp.$1);
			if(ieversion<8.0){
				$("#slider").css("z-index",8);
			}
		}
		$(this).animate({ 
			width: newwidth, 
			height: newheight,
			left: newleft,
			top: newtop
		}, 250, function(){
			$(this).css("z-index",originalsize[index][4]);
		});
		
		clearTimeout(t5);
		$("#"+id+"-test").stop();
		$("#"+id+"-test").hide();
		$("."+id+"-save").stop();
		$("."+id+"-save").css("height","0px");
		$("."+id+"-save").css("width","0px");
		for(i=0;i<custTest[index-3];i++){
			$("#"+id+"-text-"+i).hide();
		}
	});
});

function displayCaption(id){
	$("#"+id+"-test").css("height","10px");
	$("#"+id+"-test").css("width","1px");
	$("#"+id+"-test").css("top","26px");
	$("#"+id+"-test").css("z-index",500);
	$("#"+id+"-test").show(0,function(){
		$("#"+id+"-test").animate({
			width: 414,
			height: 10,
			top: 26
		}, 250, function(){
			$("#"+id+"-test").animate({
				width: 414,
				height: 142,
				top: -40
			}, 250, function(){
				$("."+id+"-save").css("z-index",550);
				$("."+id+"-save").animate({
					width: 271,
					height: 68
				}, 250);
				inc = 0;
				if(t5!=null){
					clearTimeout(t5);
				}
				testimonialText(id);
			});
		});
	});
}

function testimonialText(id){
	
	index = parseInt(id.substr(5))-4;
	
	if(inc==custTest[index]){
		$('#'+id+'-text-'+(inc-1)).fadeOut('fast',function(){
			inc = 0;
			testimonialText(id);
		});
	}
	else if(inc>0){
		inc++;
		$('#'+id+'-text-'+(inc-2)).fadeOut('fast',function(){
			$('#'+id+'-text-'+(inc-1)).fadeIn('fast',function(){
				t5 = setTimeout("testimonialText(id)",5000);
			});
		});
	}
	else{
		inc++;
		$('#'+id+'-text-'+(inc-1)).fadeIn('fast',function(){
			t5 = setTimeout("testimonialText(id)",5000);
		});
	}
}

$(window).resize(function(){
	
	width = parseInt($("#slider").css("width"));
	
	if(width<=1800){
		shift = (1800-width)/2
		$("#people").css("left","-"+shift+"px");
	}
	else{
		shift = (width-1800)/2
		$("#people").css("left",shift+"px");
	}
	
});

function nextTestimonial(){
	
	count++;
	if(count>3){
		count = 0;
	}
	
	$("#testimonial-block").fadeOut('slow', function(){
		$("#testimonial-block").html(testimonials[count]);
		$("#testimonial-block").fadeIn('slow', function(){
			t2 = setTimeout("nextTestimonial()",5000);
		});
	});
}
