var newstimer;

function nextHeader() {

	var current = $("#rotateheaders div").index($("div.selected")[0]);

	if ($("#rotateheaders div.selected").length==0) {
		$("#rotateheaders div").hide();
	} else {
		$("#rotateheaders div.selected").fadeOut("slow", function(){
			$(this).removeClass("selected");
		});
	}


	if (current<0) {
		current = Math.floor(Math.random()*($("#rotateheaders div").length));
	}
	
	if (current==$("#rotateheaders div").length-1)
		current = 0;
	else
		current += 1;
		
	$("#rotateheaders div").eq(current).addClass("selected").fadeIn("slow");
	
	setTimeout("nextHeader()", 9000);

}

function nextNews() {
	index = $("#highlights div.content").index( $('div.selected')[0])+1;
	total = $("#highlights div.content").length;
	
	
	
	if (index==total) {
		$("#highlights div.selected").fadeOut("fast", function() {
			$(this).removeClass("selected");
		});
		
		$("#highlights div.content").eq(0).addClass("selected").fadeIn("slow");
		
		$("#highlights span.current").html("1");
	}
	else {
		$("#highlights div.selected").fadeOut("fast", function() {
			$(this).removeClass("selected");
		});
		
		$("#highlights div.content").eq(index).addClass("selected").fadeIn("slow");
		
		$("#highlights span.current").html(index+1);

	}
		
	newstimer = setTimeout("nextNews()", 8000);	
}


function nextQuote() {
	
	setTimeout("nextQuote()", 12000);
	
	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");
	
	
}


$(document).ready(function() {

	nextHeader();

	$("#highlights span.total").html($("#highlights div.content").length);
	
	$("#highlights a.previous").click(function() {
		var index = $("#highlights div.content").index( $("div.selected")[0]);
		var length = $("#highlights div.content").length;

		$("#highlights div.selected").fadeOut("fast", function() {
			$(this).removeClass("selected");
		});
		
		if (index==0) {
			$("#highlights div.content").eq(length-1).addClass("selected").fadeIn("slow");
			$("#highlights span.current").html(length);
		}
		else {
			$("#highlights div.content").eq(index-1).addClass("selected").fadeIn("slow");
			$("#highlights span.current").html(index);
		}
		
		
		clearTimeout(newstimer);
		newstimer = setTimeout("nextNews()", 8000);	
		
		return false;
	});
	
	$("#highlights a.next").click(function() {
		var index = $("#highlights div.content").index( $('div.selected')[0]);
		var length = $("#highlights div.content").length;
		
		$("#highlights div.selected").fadeOut("fast", function() {
			$(this).removeClass("selected");
		});
		
		if (index==length-1) {
			$("#highlights div.content").eq(0).addClass("selected").fadeIn("slow");
			$("#highlights span.current").html("1");
		}
		else {
			$("#highlights div.content").eq(index+1).addClass("selected").fadeIn("slow");
			$("#highlights span.current").html(index+2);
		}
	
		clearTimeout(newstimer);
		newstimer = setTimeout("nextNews()", 8000);	
		
		return false;
	});

	newstimer = setTimeout("nextNews()", 8000);	

	
	$("#casestudies span.total").html($("#casestudies div.rotate").length);
	
	$("#casestudies a.previous").click(function() {
		var index = $("#casestudies div.rotate").index( $("div.rotate.selected")[0]);
		var length = $("#casestudies div.rotate").length;

		$("#casestudies div.selected").fadeOut("fast", function() {
			$(this).removeClass("selected");
		});
		
		if (index==0) {
			$("#casestudies div.rotate").eq(length-1).addClass("selected").fadeIn("slow");
			$("#casestudies span.current").html(length);
		}
		else {
			$("#casestudies div.rotate").eq(index-1).addClass("selected").fadeIn("slow");
			$("#casestudies span.current").html(index);
		}
		
		
		return false;
	});
	
	$("#casestudies a.next").click(function() {
		var index = $("#casestudies div.rotate").index( $("div.rotate.selected")[0]);
		var length = $("#casestudies div.rotate").length;

		
		$("#casestudies div.selected").fadeOut("fast", function() {
			$(this).removeClass("selected");
		});
		
		if (index==length-1) {
			$("#casestudies div.rotate").eq(0).addClass("selected").fadeIn("slow");
			$("#casestudies span.current").html("1");
		}
		else {
			$("#casestudies div.rotate").eq(index+1).addClass("selected").fadeIn("slow");
			$("#casestudies span.current").html(index+2);
		}
	

		
		return false;
	});	
	
	/* 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");
		});
		
		$("#caselist").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
		
		$("#homecontent div").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
		
		
    }
	
	/* Page tools */
	
	$("#email a").click(function(){
		location.href='mailto:?SUBJECT=Link to Website of Sierra Asia Partners - (http://www.sierraasia.com)&BODY=Below is a link to a page from the Sierra Asia Partners web site that contains information I think you%27ll find interesting:%0D%0A%0D%0ATo view the page, visit '+escape(location.href)+'%0D%0A%0D%0A--------------------------------------------------------%0D%0AABOUT SIERRA ASIA PARTNERS. -  http://www.sierraasia.com/%0D%0ASierra Asia Partners is an advisory and financial services company based in China and the United States and focused on China. At Sierra Asia, we use our extensive experience operating in Asia to help you navigate the complexities of the China market and reliably establish, structure, and maintain successful operations there..';
		return false;
	});
	
	$("#bookmark a").click(function(){
	
		var opera =" First click OK and then hit CTRL+T to bookmark this page.";
	
	/*	if($.browser.opera) {
			alert(opera);
			return false;
		}

			
		return false; */
		

		if ( navigator.appName != 'Microsoft Internet Explorer' ) {
			window.sidebar.addPanel("Sierra Asia Partners - "+page,location.href,"");}
		else {
			window.external.AddFavorite(location.href,"Sierra Asia Partners - "+page); 
		}

		
		return false;
	});
	
	$("#print a").click(function(){
		print();
		return false;
	});
	

	/* Prepare external links */
	
	$("a").each(function(i){
		if($(this).attr("rel")=="external")
			$(this).attr("target","_blank");

	});
	
	/* Text Size tools */
	
	if($.cookie("ac_textsize")==null)
		$("#maincontent").attr("class","small");
	else
		$("#maincontent").attr("class",$.cookie("ac_textsize"));

	$("#pagetools #increase").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;
	});
	
	
	$("#pagetools #decrease").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;
	});
	
	
	/* Linkareas  */
	
	$(".linkarea").hover(function(){
		$(this).css({ cursor: 'pointer' });
	},function(){
	});
	
	
	$(".linkarea").click(function(){
		$(this).find("a").eq(0).click();
	});
	
	$(".linkarea a").click(function(){

		if ($(this).attr("rel")=="external") {
			var newWindow = window.open($(this).attr("href"), '_blank');
			newWindow.focus();
				
			return false;
		}
		else {
			document.location = $(this).attr("href");
			
			return false;
		}
	});		
	
	$(".linkarea.thickbox").unbind("click");
	
	$(".linkarea.thickbox").click(function(event){
		$(this).find("a:eq(0)").trigger("click");
		return false;
	});
	
	/* Forms */
	
	$("input.email").click(function(){
		if (this.value=="Enter Email...") this.value="";
	});
	
	$("#emailalerts").submit(function(){
	
		if ($("#emailalerts input.email").attr("value") == '') { 
			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 = $("#emailalerts 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;
	});
	
	/* Tables */
	
	
	$("table.stripe").each(function(i){
		$(this).find("tr:even").addClass('even');
	});
	
	/* Property Finder */
	
	$("input").click(function(){ 
		if (this.value == "Search Our Site" || this.value == "Email Address")
			this.value = "";
	});
	
	/* Rotating quotes */
	
	nextQuote();
});