var script_name = getScriptName();
var currentAtId = "default"; //Global variable for current tab
var saveObj = ""; //Global variable for current main nav object

$(document).ready(function(){
	$(".twitter-share-button").click(function (ev) {
		var href = $(this).attr('href');
		ev.preventDefault();
		window.open(href, 'ShareToolbox', 'height=300,width=500,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
	});
		
// 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();
					}
				});
			});
});
