var totalAds = 0; 		//Total number of banner ads on page
var adWidth = 255; 		//width of banner ad image
var finalPosition = 0;	//will be the final position: top; value for #carousel
var currentSlide = 1; 	//current slide visible
var play = true; 		//is slider playing true|false
var intervalID; 		//setInterval ID
if(ie7) adWidth = 258;

$(document).ready(function(){
	/*
	var gaPageArray=[
	"business",
	"do-it-yourself",
	"online_communities"];
	*/
	var gaPageArray=[
	"/hompage-tab/business/",
	"/hompage-tab/do-it-yourself/",
	"/hompage-tab/online_communities/"];	
	
	var tabNumberArray=[
	"firsttab",
	"secondtab",
	"thirdtab"];
	
	$("#tab-items li a").each(function (i){
		$(this).bind("click", function(){
			var t = $(this);
			var parent = t.parent();
			var tab = $("#" + parent.attr("id") + "-content");
			
			$(".tab-wrapper").removeClass("selected");
			tab.addClass("selected");
			
			//Set height of container
			$("#tabs").css("height",$("#tab-items").outerHeight() + tab.outerHeight());
			
			tab.fadeIn(0, function(){		
				//Adds cleartype back into IE's text. This is a bug in IE/Windows when applying opacity to text.
				if(this.style.removeAttribute) this.style.removeAttribute("filter"); //http://blog.bmn.name/2008/03/jquery-fadeinfadeout-ie-cleartype-glitch/
			});
				
			$("#tab-items li").removeClass("selected");
			parent.addClass("selected");
			
			//pageTracker._trackEvent("links", "/", gaPageArray[i], 0);
			pageTracker._trackPageview(gaPageArray[i]);			
			
			createCookie("hometab",tabNumberArray[i],365);
				
			return false;
		});
	});	
	
	
	$("#projects-nav li").each(function (i){
		$(this).bind("mouseover", function(){		
			$("#projects-content li").css("display", "none");		
			$("#projects-content li").eq(i).fadeIn(200, function(){		
				if(this.style.removeAttribute) this.style.removeAttribute("filter");
			});	
		});
	});
	
	
	//Position background image
	$("#industry-items a").each(function (i){
		$(this).bind("mouseover", function(){			
			var position = "0px " + -((i+1)*111) + "px";
			//alert(position)
			$("#industry-image").css("backgroundPosition",position);
		});
		
		$(this).bind("mouseout", function(){			
			$("#industry-image").css("backgroundPosition","0px 0px");
		});
		
	});
	
	
	//Set height of tab container
	$("#tabs").css("height",$("#tab-items").outerHeight() + $(".tab-wrapper.selected").outerHeight());
	
	
	//Shows first projects item by default
	$("#projects-content ul li:first-child").css("display","block");
	
	
	//Set the description for the first banner
	var title = $("#carousel li:nth-child("+currentSlide+") img").attr("alt");	
	$("#ad-title").text(title);
	
	
	//Open In New Window
	$("a[rel*='window']").click(function(){
		window.open($(this).attr("href"));
		return false;
	});
	
	
	//Set totalads variable to total number of LIs. Calculate and set final position variable
	totalAds = $("#carousel li").length;
	finalPosition = (totalAds-1) * adWidth;
	
	
	//Move previous
	$("#prev_btn").click(function(){
		adSlide("previous");
		adSlideToggle(true);
		return false;
	});
	
	
	//Pause toggle
	$("#pause_btn").click(function(){
		adSlideToggle(play);
		return false;
	});
	
	
	//Move next
	$("#next_btn").click(function(){
		adSlide("next");
		adSlideToggle(true);
		return false;
	});
	
	
	//Start playing slider via setInterval
	initializeSlide();
});

function initializeSlide()
{
	intervalID = window.setInterval(function(){
		adSlide("next");
	}, 5500); //5500 is number of milliseconds
}

function adSlide(direction)
{
	var currentPosition = parseInt($("#carousel").css("top"));

	if(direction == "next")
	{
		//Is position of slider equal to the last item in the slider
		if(Math.abs(currentPosition) >= finalPosition)
		{
			positionto = 0;//.css("top",0);
			currentSlide = 1;
		} else {
			positionto = currentPosition-adWidth;//.css("top",currentPosition-adWidth);
			currentSlide++;
		}	
	} 
	
	if(direction == "previous")
	{
		//Is position of the slider equal to the first item in the slider
		if(Math.abs(currentPosition) == 0)
		{
			positionto = -(totalAds-1) * adWidth;//.css("top",-(totalAds-1) * adWidth);
			currentSlide = totalAds;
		} else {
			positionto = currentPosition+adWidth;//.css("top",currentPosition+adWidth);
			currentSlide--;
		}
	}
	
	//$("#carousel").animate({"top": positionto}, 400);
	$("#carousel").css("top", positionto);
	
	title = $("#carousel li:nth-child("+currentSlide+") img").attr("alt");
	
	$("#ad-title").text(title);	
}

function adSlideToggle(playslider)
{	
	if(playslider == true)
	{
		//Slider is currently auto playing. This stops it.
		$("#pause_btn img").attr("src","/images/next-btn.jpg");
		window.clearInterval(intervalID);
		play = false;
	} else {
		//Slder is currently paused. This plays it
		$("#pause_btn img").attr("src","/images/pause-btn.jpg");
		initializeSlide();
		play = true;
	}
}

//http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
