var script_name = getScriptName();
var currentAtId = "default"; //Global variable for current tab
var saveObj = ""; //Global variable for current main nav object

function createNavigation(ulid) {
	var images = document.getElementById(ulid).getElementsByTagName("img");

	//Determine the current selected tab
	for (var i=0; i < images.length; i++) {
		if (images[i].src.indexOf('_at') > 1) {currentAtId = images[i].id;} 
		else {
			images[i].onclick = function() {
				stopTimer();
				if (saveObj.id != this.id) {
					resetMenu(); //Make sure menu is at default state
					saveObj = this; //Save the new object
					displaySubNav(this.id,currentAtId,this,1); //Display the sub navigation

					var headersubnav = document.getElementById("headersubnav");
					if (headersubnav.className.indexOf("headersubnav_hover") === -1) {
						updateClassName(headersubnav,"headersubnav_hover","");
					}

					return false;
				} else {
					return true;
				}
			};
			if (currentAtId != this.id) {
				images[i].onmouseout = function() {startTimer();};
			}
		}
		// Create the sub navigation events
		if (images[i].id != 'default') 
			createSubNav(images[i].id);	
	}
}

function createSubNav(id) {
	var subnav = document.getElementById('headersubnav_'+id).getElementsByTagName("li");

	for(var i=0; i < subnav.length; i++) {
		subnav[i].onmouseover = function() {
			stopTimer();
			if (id == currentAtId && this.className.indexOf('at') == -1) {updateClassName(this,"hover","");}
		};
		subnav[i].onmouseout = function() {
			if (id == currentAtId) {updateClassName(this,"","hover");}
			else {startTimer();} 
		};
	}
}

function displaySubNav(x,y,img,state) {
	var d = document.getElementById('headersubnav_default');
	x = document.getElementById('headersubnav_'+x);
	y = document.getElementById('headersubnav_'+y);

	if (state == 1) {img.src = img.src.replace('_off','_on');}
	else {img.src=img.src.replace('_on','_off');}

	if (x != y) {
		d.style.display="none"; //Hide the default
		y.style.display="none"; //Hide the last item
		x.style.display="block"; //Show the current item
	}
}

function startTimer() {
	stopTimer(); //Make sure it is stopped first
	delayhide = setTimeout("resetMenu()",1000);
}

function resetMenu() {
	if (typeof saveObj == "object") {
		displaySubNav(currentAtId,saveObj.id,saveObj,0);
		saveObj = '';
		stopTimer();
		updateClassName(document.getElementById("headersubnav"),"","headersubnav_hover");
	}
}

function stopTimer() {
	if (typeof delayhide != "undefined") {
		clearTimeout(delayhide);
	};
}

$(document).ready(function(){
	createNavigation("mainnav");
		
// Creates the tabs for double news rotators and double onestories
	var hasNewsRotators = $("#press_info1").find(".newsrotator").length && $("#press_info2").find(".newsrotator").length;
	var hasNewsLists = $("#press_info1").find(".onestory").length && $("#press_info2").find(".onestory").length;
		
	if (hasNewsRotators || hasNewsLists) {
		$("#press_leftcol")
			.each(function(){
				$(this)
					.addClass("js")
					.find("#press_info1")
						.before("<div class=\"tabbar\"></div>")
						.end()
					.find("#press_info1,#press_info2")
						.wrapAll("<div class=\"tabcontent\"></div>")
						.each(function(i){
							var ids = "," + $(this).attr("id") + "," + $(this).find("[id]").map(function(){
									return $(this).attr("id");
								}).get().join(",") + ",";
								
							var IsActive = i === 0;
							if (location.hash.length > 0) {
								IsActive = ids.indexOf("," + location.hash.substr(1) + ",") > -1;
							}
							var cssclass = "active";
							
							if (!IsActive) {
								$(this).hide();
								cssclass = "";
							}
							
							var head = $(this).find("h3").text();
							head = head.replace(/ /g, "&nbsp;");
							var url = "#" + $(this).attr("id") || "#tab_" + i;

							var html = "<span class=\"" + cssclass + "\"><a href=\"" + url + "\">" + head + "</a></span>";
							
							$(this).parent().prev(".tabbar").append(html);
						})
						.end()
					.find(".tabbar")
						.children("span")
							.click(function(ev){
								ev.preventDefault();
								
								var tabs = $(this).parent().siblings(".tabcontent").children("#press_info1,#press_info2");
								var active = $(this).children("a").attr("href").split("#")[1];
								var links = $(this).parent().find("a");
								location.hash = active;

								tabs
									.not("#" + active)
										.hide()
										.end()
									.filter("#" + active)
										.show();
										
								links
									.each(function(){
										var href = $(this).attr("href").split("#")[1];
										
										if (active === href) {
											$(this).parents("span").addClass("active");
										} else {
											$(this).parents("span").removeClass("active");
										}
									});
			
							});
			});
	}
	
// Video library Information Widget
	$(".videolibrary_informationtype")
		.find(".body")
			.each(function(){
				var thisid = $(this).parents(".videolibrary_informationtype").attr("id");
				var tableid = "videolibrary_informationtype_table_" + thisid.split("_")[1];
				var $table = $("<table id='" + tableid + "' class='featured' cellpadding='0' cellspacing='0'><tr></tr></table>").prependTo(this);
				
				$(this).find("div.featured").each(function(n){
					$(this).children(".thumb").appendTo("#" + tableid + " tr").wrap("<td></td>").mouseover(function(ev){
						ev.preventDefault();
						var index = $(this).parent().prevAll().length;
						$(this)
							.parent()
							.addClass("active")
								.siblings()
								.removeClass("active")
								.end()
							.parents("table")
								.siblings("div.featured")
									.hide()
									.eq(index)
									.show();
					});
					
					if (n === 0){
						$("#" + tableid + " tr td").eq(0).addClass("active");
					} else {
						$(this).hide();
					}
				});
			});
});