$("li").hover(
  function () {
    $(this).addClass("hover");
  },
  function () {
    $(this).removeClass("hover");
  }
);

/* =========================================
* 	Global
* ======================================= */
	
	if( location.href.indexOf("epstage") !== -1 )
	{
		var root_path = "/preview.upstatealliance";
	}
	else if( location.href.indexOf("preview") !== -1 )
	{
		var root_path = "";
	}
	else
	{
		var root_path = "";
	}
	
	// hide left side tabs initially
	function hideTabs()
	{
		$('div#tab1').hide();
		$('div#tab2').hide();
		$('div#tab3').hide();
		$('div#tab4').hide();
		
		$('li#btn_tab1').removeClass('current');
		$('li#btn_tab2').removeClass('current');
		$('li#btn_tab3').removeClass('current');
		$('li#btn_tab4').removeClass('current');
	}
	
	// get current location
	var currentUrl = window.location;
	
	//alert(root_path);
	var hierarchy = currentUrl.pathname.replace('index/','').replace(root_path, '');

/* ===========
* 	onload
* ===========*/
$(document).ready(function()
{
/* =========================================
* 	Track external links automatically
* ======================================= */
	
	$('html a:not(.popupwindow)').filter(function()
	{
		// link that was clicked
		var theHref = this;
		
		if (theHref.hostname && theHref.hostname !== location.hostname)
		{
			$(theHref).not(".noAutoIcon").addClass("offSite");
			$(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event)
			{
				var code=event.charCode || event.keyCode;
				if (!code || (code && code == 13))
				{
					// build link
					var fixedLink = this.href;
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/virtual/outbound' + hierarchy + '/' + fixedLink;

					// track
					pageTracker._trackPageview(fixedLink);
				};
			});
		};
	});
	
/* =========================================
* 	Tracking for pdf files
* ======================================= */	
	
	$("a[href*=.pdf]").click(function()
	{
		var link = $(this).attr("href");
		var filename = link.substr(link.lastIndexOf("/")).replace('/','');
		var tracklink = "/virtual/pdf" + hierarchy + filename;
		
		// track
		pageTracker._trackPageview(tracklink);
	});

/* =========================================
* 	Tracking for csv files
* ======================================= */	
	
	$("a[href*=.csv]").click(function()
	{
		var link = $(this).attr("href");
		var filename = link.substr(link.lastIndexOf("/")).replace('/','');
		var tracklink = "/virtual/csv" + hierarchy + filename;
		
		// track
		pageTracker._trackPageview(tracklink);
	});
	
/* =========================================
* 	Tracking for doc files
* ======================================= */	
	
	$("a[href*=.doc]").click(function()
	{
		var link = $(this).attr("href");
		var filename = link.substr(link.lastIndexOf("/")).replace('/','');
		var tracklink = "/virtual/doc" + hierarchy + filename;
		
		// track
		pageTracker._trackPageview(tracklink);
	});
	
/* =========================================
* 	Tracking for docx files
* ======================================= */	
	
	$("a[href*=.docx]").click(function()
	{
		var link = $(this).attr("href");
		var filename = link.substr(link.lastIndexOf("/")).replace('/','');
		var tracklink = "/virtual/docx" + hierarchy + filename;
		
		// track
		pageTracker._trackPageview(tracklink);
	});
	
/* =========================================
* 	Tracking for xls files
* ======================================= */	
	
	$("a[href*=.xls]").click(function()
	{
		var link = $(this).attr("href");
		var filename = link.substr(link.lastIndexOf("/")).replace('/','');
		var tracklink = "/virtual/xls" + hierarchy + filename;
		
		// track
		pageTracker._trackPageview(tracklink);
	});

/* =========================================
* 	Input field focus
* ======================================= */

	$("input.field").focus(function(){
		if(this.value == this.autotext){
			this.value = "";
		}
	}).blur(function(){
		if(this.value == ""){
			this.value = this.autotext;
		}
	}).each(function() {
		this.autotext = this.value;
	});

/* =========================================
* 	Tabs
* ======================================= */

	hideTabs();
	
	$('div#tab1').show();
	$('li#btn_tab1').addClass('current');
	
	$('li#btn_tab1').click(function()
	{
		hideTabs();
		$('div#tab1').show();
		$('li#btn_tab1').addClass('current');
		$(this).blur();
		return false;
	});
	
	$('li#btn_tab2').click(function()
	{
		hideTabs();
		$('div#tab2').show();
		$('li#btn_tab2').addClass('current');
		$(this).blur();
		return false;
	});

	$('li#btn_tab3').click(function()
	{
		hideTabs();
		$('div#tab3').show();
		$('li#btn_tab3').addClass('current');
		$(this).blur();
		return false;
	});
	
	$('li#btn_tab4').click(function()
	{
		hideTabs();
		$('div#tab4').show();
		$('li#btn_tab4').addClass('current');
		$(this).blur();
		return false;
	});
	

/* =========================================
* 	Country selector
* ======================================= */

	// hide by default
	$("#countries").hide();
	
	// if click on page, hide the menu
	$("body").click(function()
	{
		$("#countries").hide();
	});
	
	// toggle click
	$("#country").click(function()
	{
		$("#countries").toggle("slow");
	});

/* =========================================
* 	Homepage Boxes
* ======================================= */

	// remove current / hide all boxes
	function resetall()
	{
		$("div.left_home").addClass("sifr_hide");
		$("#boxes > li").removeClass("current");
	}
	
	// init
	function init()
	{
		resetall();
		
		// show box 1
		$("#home_left_column_advmat").removeClass("sifr_hide");
		$("#advmat").addClass("current");
		$.cookie("current_state","advmat");
	}
	
	function change(box)
	{
		// reset after fadeOut to eliminate jitter
		resetall();
		
		// add current
		$("#"+box).addClass("current");
		
		// fade out image
		$("#top-image").fadeOut("normal", function()
		{
			// change image
			$("#top-image").attr("src", root_path+"/images/header_"+box+".jpg");
			
			// fade in image
			$("#top-image").fadeIn("normal");
		});
		
		// fade-in
		$("#home_left_column_"+box).removeClass("sifr_hide");
		
		// set cookie with current state
		$.cookie("current_state", box);

	}
	
	// run init
	init();
	
	// operations
	$("#boxes > li").click(function ()
	{ 
		// this_rel
		var this_rel = $(this).attr("rel");
		
		// check and make sure we aren't already on this box
		if($("#"+this_rel).hasClass("current") == false)
		{
			// change
			change(this_rel);
		}
	});
	
	// object of items
	var items = {"advmat": 1, "biosciences": 2, "automotive": 3, "energy": 4, "aerospace": 5};
	
	// next button
	$("#arrow_right").click(function ()
	{ 
		
		// what is our current state
		var current_state 		= $.cookie("current_state");
		var current_number 		= items[current_state];
		var show_next_number	= 0;
		var new_state			= "";
		
		// if we are number 4, set to 1
		if(current_number == 5)
		{
			show_next_number = 1;
		}
		else
		{
			show_next_number = current_number + 1;
		}
		
		// go through array, and find the value with the new number
		$.each(items, function(key, val){
			if(val == show_next_number)
			{
				new_state = key;
			}
		});
		
		// cycle
		change(new_state);
	});
	
	// previous button
	$("#arrow_left").click(function ()
	{
		// what is our current state
		var current_state 		= $.cookie("current_state");
		var current_number 		= items[current_state];
		var show_prev_number	= 0;
		var new_state			= "";
		
		// if we are number 4, set to 1
		if(current_number == 1)
		{
			show_prev_number = 5;
		}
		else
		{
			show_prev_number = current_number - 1;
		}
		
		// go through array, and find the value with the new number
		$.each(items, function(key, val){
			if(val == show_prev_number)
			{
				new_state = key;
			}
		});
		
		// cycle
		change(new_state);
	});
	
/* =========================================
* 	WordPress comment form validation
* ======================================= */

	$("#commentform").validate({
		messages: {
			author: "*",
			email: "*",
			comment: "*"
		}
	});

	

});
