var newstimer;

function nextNews() {
	index = $("p.newsitems a").index( $('a.selected')[0])+1;
	
	if (index==3) {
		$("p.newsitems a").removeClass("selected");
		$("p.newsitems a").eq(0).addClass("selected");
		$("#newsevents div").hide();
		$("#news_1").fadeIn("fast");
	}
	else {
		$("p.newsitems a").removeClass("selected");
		$("p.newsitems a").eq(index).addClass("selected");
		$("#newsevents div").hide();
		$("#news_"+(index+1)).fadeIn("fast");
	}
		
	newstimer = setTimeout("nextNews()", 8000);	
}

function scrollLogos() {

	$("#logos li:eq(0)").animate({marginLeft: '-150px'}, {duration: 3500, easing: "linear", complete: function(){

		$("#logos li:eq(0)").css({marginLeft: 0});
		$("#logos li:eq(0)").appendTo("#logos ul");
		
		scrollLogos();
	}});
}

function fadeLogos() {


	if($("#logos li.selected").length==0) {
		$("#logos li:eq(0)").addClass("selected").show();
		setTimeout("fadeLogos()", 9000);
		return false;
	}
		
	totalImages = $("#logos li").length-1;
	currentImage = $("#logos li").index( $("li.selected")[0]);
	

	if (currentImage!=totalImages) {
		$("#logos li").eq(currentImage+1).fadeIn("slow").addClass("selected");
		$("#logos li").eq(currentImage).fadeOut("slow").removeClass("selected");
	}
	else {
		$("#logos li").eq(0).fadeIn("slow").addClass("selected");
		$("#logos li").eq(totalImages).fadeOut("slow").removeClass("selected");
	}
	
	
	setTimeout("fadeLogos()", 9000);


}



function nextHeader() {
	
		
	setTimeout("nextHeader()", 6000);
	
	$("h1 span").eq(1).fadeIn("slow");
	$("h1 span:first").appendTo("h1");
	$("h1 span:last").fadeOut("slow");
}

function nextQuote() {
	
	setTimeout("nextQuote()", 10000);
	
	var index = $("#quoterotate blockquote").index($('blockquote.active')[0]);
	var total = $("#quoterotate blockquote").length;
	
	var rnd = Math.floor(Math.random()*total);
	
	while (rnd==index)
		rnd = Math.floor(Math.random()*total);
		
	$("#quoterotate blockquote.active").fadeOut("slow",function(){
		$(this).removeClass("active");
	});
		
	$("#quoterotate blockquote").eq(rnd).hide();	
	$("#quoterotate blockquote").eq(rnd).addClass("active");
	$("#quoterotate blockquote").eq(rnd).fadeIn("slow");
	
	
/*	if (index==total-1) {
		$("#quoterotate blockquote.active").fadeOut("slow",function(){
			$(this).removeClass("active");
		});
		
		$("#quoterotate blockquote").eq(0).hide();
		$("#quoterotate blockquote").eq(0).addClass("active");
		$("#quoterotate blockquote").eq(0).fadeIn("slow");
		
	}
	else {
		$("#quoterotate blockquote.active").fadeOut("slow",function(){
			$(this).removeClass("active");
		});
		
	$("#quoterotate blockquote").eq(index+1).hide();	
		$("#quoterotate blockquote").eq(index+1).addClass("active");
		$("#quoterotate blockquote").eq(index+1).fadeIn("slow");
	} */
}

$(document).ready(function() {

	/* IE6 fix for dropdowns  */

	if (document.all) {
		 $("#globalnav li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
				
       $("#mainnav li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
		
		$("#pagetools").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
		
		$("div.dropdown").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
    }
	
	/* Page tools */
	
	$("#email").click(function(){
		location.href='mailto:?SUBJECT=Link%20to%20Website%20of%20Andersen%20%26%20Company%20LLP%20(http://www.aandco.com/)&BODY=Below is a link to the website of Andersen %26 Company, LLP.%0D%0A%0D%0ATo view the page, visit: '+escape(location.href)+'%0D%0A%0D%0A-------------------------------------------%0D%0AAndersen %26 Company, LLP, is a leading provider of business consulting and accounting services in the Bay Area. Clients rely on us for our far-ranging understanding of their businesses and the industries in which they operate.';
		return false;
	});
	$("#bookmark").click(function(){
	
		var general = "First click OK and then hit CTRL+D to bookmark this page.";
		var opera =" First click OK and then hit CTRL+T to bookmark this page.";
	
		if($.browser.opera)
			alert(opera);
		else
			alert(general);
			
		return false;
	});
	$("#print").click(function(){
		print();
		return false;
	});

	/* Text Size tools */
	
	if($.cookie("ac_textsize")==null)
		$("#maincontent").attr("class","small");
	else
		$("#maincontent").attr("class",$.cookie("ac_textsize"));

	$("#textsize a.larger").click(function(){
		if ($("#maincontent").attr("class")=="small")
			$("#maincontent").attr("class","medium");
		else
			$("#maincontent").attr("class","large");
			
		$.cookie('ac_textsize', $("#maincontent").attr("class"), { path: '/', expires: 60 });
					
		return false;
	});
	$("#textsize a.smaller").click(function(){
		if ($("#maincontent").attr("class")=="large")
			$("#maincontent").attr("class","medium");
		else
			$("#maincontent").attr("class","small");
			
		$.cookie('ac_textsize', $("#maincontent").attr("class"), { path: '/', expires: 60 });
			
		return false;
	});
	
	
	
	/* Prepare external links */
	
	$("a").each(function(i){
		if($(this).attr("rel")=="external")
			$(this).attr("target","_blank");
	});
	
	/* Linkareas  */
	
	
	$(".linkarea").hover(function(){
		$(this).css({ cursor: 'pointer' });
	},function(){
	});
	
	$(".linkarea a").click(function(){

		if ($(this).attr("rel")=="external") {
			var newWindow = window.open($(this).attr("href"), '_blank');
			newWindow.focus();
				
			return false;
		}
	});	

	$(".linkarea").click(function(){		
		if ($(this).find("a").attr("rel")=="external" || $(this).find("a").attr("rel")=="video") {
			$(this).find("a").eq(0).trigger("click");
		}
		else 
			document.location = $(this).find("a").eq(0).attr("href");

	});
	
	/* Homepage Logos scroller */
	

	setTimeout("fadeLogos()", 500);
	
	
	/* Homepage News */

	$("p.newsitems a").click(function(){
	
		clearTimeout(newstimer);
		
		$("p.newsitems a").removeClass("selected");
		
		$(this).addClass("selected");
		
		$("#newsevents div").hide();
		
		$("#"+$(this).attr("rel")).fadeIn("fast");
		
		newstimer = setTimeout("nextNews()", 8000);
		
		return false;
	});
	
	newstimer = setTimeout("nextNews()", 8000);	
	
	
	
	/* Rotating banners */
	
	setTimeout("nextHeader()", 6000);
	
	/* Rotating quotes */
	
	nextQuote();
	
	
	/* Contact Us maps */
	
	$("#gmapSantaRosaLink").click(function(){
		GUnload(); 
		$('div.map').hide(); 
		$('#santarosa').show(); 
		loadSantaRosa(); 
		return false;
	});	
	
	$("#gmapSanFranciscoLink").click(function(){
		GUnload(); 
		$('div.map').hide(); 
		$('#sanfrancisco').show(); 
		loadSanFrancisco(); 
		return false;
	});	
	
	$("#gmapPetalumaLink").click(function(){
		GUnload(); 
		$('div.map').hide(); 
		$('#petaluma').show(); 
		loadPetaluma(); 
		return false;
	});	
	
	$("div.map p.close").click(function(){
		GUnload();
		$(this).parent("div.map").hide();
	});	
	
	/* Forms */
	
	$("input.email").click(function(){
		if (this.value=="Enter email address") this.value="";
	});
	
	$("#emailalertsform").submit(function(){
	
		if ($("#emailalertsform input.email").attr("value") == '' || $("#emailalertsform input.email").attr("value") == undefined) { 
			alert('Please enter you email address'); 
			return false;
		} 		
		
		if (window.RegExp)
		{
			var tempStr = "a";
			var tempReg = new RegExp(tempStr);
			if (tempReg.test(tempStr)) supported = 1;
		}
		
		var str = $("#emailalertsform input.email").attr("value");

		if (!supported) 
			return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,5}|[0-9]{1,3})(\\]?)$");
			
		var valid = (!r1.test(str) && r2.test(str));
			
		if (valid == false)
			alert("'"+str+"'" + " is not a valid email address");
			
		return valid;
	});
	
	
});