// IMPORTANT!!
// Compile this code useing Google Closure Comiler prior to release
// HERE: http://closure-compiler.appspot.com/home

/* Related files
----------------
/_/images/sprites/tabUI.png
/_/js/adhoc/tabs.css
/_/css/hacks/ie6.css
*/

// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name tabs.js
// ==/ClosureCompiler==

var currentSelectedTab="";
$(function () {
var tabContainers = $('div.tabs>div, div.tabbedUI>div');
if (($("dt").size()>0)&&($("div.tabs>div, div.tabbedUI>div").size()>0)) {pageHasTabsAndConcertinas=true;}
//start accessibility tweaks
	$("div.tabs, div.tabbedUI").each(function(){$(this).before("<a href=\"#\" class=\"removeTabStyles\">Tip for screen reader users - this next batch of links may be tricky to navigate through because of its visual appearance. To remove this styling and behaviour and change it back to a simple list of links, simply activate THIS link.</a>")});
	//end accessibility tweaks

var tabs = $('div.tabs li, div.tabbedUI li');
	tabContainers.hide();
	$("#page div.tabs>div, #page div.tabbedUI>div").css("background","#fff");
	$('div.tabs>div:first, div.tabbedUI>div:first').show();
	$('div.tabs>ul, div.tabbedUI>ul').addClass("tabbedListLI");
	$('div.tabs:not(div.useTabAnchor), div.tabbedUI:not(div.useTabAnchor)').find('li:first').addClass('selected');
	currentSelectedTab = $("li.selected a:first").attr("href");
	/* Add in extra markup required to make whole tab clickable, not just the a element*/
	$("div.tabs>ul>li>a, div.tabbedUI>ul>li>a").wrapInner("<span class=\"tabText\"></span>");
	$("div.tabs>ul>li>a, div.tabbedUI>ul>li>a").each(function(i){
		$(this).attr("id","tabLink"+(i+1));
	});
	$("div.tabs:not(div.useTabAnchor), div.tabbedUI:not(div.useTabAnchor)").find("ul.tabbedListLI>li>a")
		.click(function () {
					$(tabContainers).hide().filter(this.hash).show();
					$(tabs).removeClass('selected');
					$(this).parent().addClass('selected');
					currentSelectedTab = this.hash;
					// call analytics function to pageTag tabs
					//alert(currentSelectedTab)
					//alert(typeof pageTagTabUi)
					if(typeof sendWebAnalytics == typeof Function){
						//alert("typeof")
						//pageTagUniqueId(currentSelectedTab);
						sendWebAnalytics({pageName:s.pageName+"_"+currentSelectedTab});
					}
					return false;
	});
	$("div.tabs>ul>li, div.tabbedUI>ul>li").css("padding","0");
	$("div.tabs>ul>li>a, div.tabbedUI>ul>li>a").css("padding","0 0 0 8px");
	/* Insert dummy links to give us a way of stopping IE adding the annoying dotted line*/
	$("div.tabs>ul, div.tabbedUI>ul").css("position","relative");
	$("div.tabs>ul>li>a, div.tabbedUI>ul>li>a").each(function(){
		$(this).after("<a href=\"#\" class=\"dummy\" tabindex=\"-1\"></a>");
	});
	$("div.tabs>ul>li>a:not(a.dummy), div.tabbedUI>ul>li>a:not(a.dummy)")
		.click(function(){$(this).next().focus();$("div.tabs>ul>li, div.tabbedUI>ul>li").removeClass("rcvdTabFocus");})
		.focus(function(){$(this).parent().addClass("rcvdTabFocus");})
		.blur(function(){$(this).parent().removeClass("rcvdTabFocus");});
	//handle links to specific tabs
	var URLhasFragId = location.href.indexOf("#")!=-1;
	if (URLhasFragId){
		if (!concertinaOpened) {
			// only run this if we don't have concertinas in play (otherwise scrollTo destroys the focus() on the active concertina item)
			window.scroll(0,0);
			var fragId=parseInt(location.href.split("#")[1].split("tab")[1]);
			$('a#tabLink' + fragId).click();
		}
	}
	
	// check for links on the page that switch tabs on same page
	// first find all tab ids in use:
	$("div.tabs>div").each(function(){
		// now find all links on the page that refer to this tab
		$("#mainContent a[href*=#" + $(this).attr("id") + "]").filter(":not(ul.tabbedListLI>li>a)").each(function(){
			//check to see if href is simply href="#tab1" rather than href="/section/file.htm#tab2"
			var hashLinkOnly = ($(this).attr("href").indexOf("#")==0);
			if (($(this).attr("href").indexOf(__CMS_CurrentUrl)!=-1)||(hashLinkOnly)) {
				$(this).addClass("retriggerTabSwitch");
			}
		});
	});
	// finally, add event to links that need to retrigger the tab switch behaviour
	$("a.retriggerTabSwitch").click(function(){
					$(tabContainers).hide().filter(this.hash).show();
					$(tabs).removeClass('selected');
					$(tabs).find("a[href=" + this.hash + "]").parent().addClass('selected');
					return false;
	});
	
	if ($("div.tabs .tabNestedSwitchedContent, div.tabbedUI .tabNestedSwitchedContent").size()>0)
	{
		var $nestedContentLinkList="";
		var $nestedContentPanelId = 1;
		//look for nested tab content that needs to be switched
		$("div.tabs>div, div.tabbedUI>div").each(function(){
			if ($(this).find(".tabNestedSwitchedContent").size()>0){
			 //grab the heading for each sub item and use to build up a UL
				$nestedContentLinkList="";
				$(this).find(".tabNestedSwitchedContent").each(function(){
					$nestedContentLinkList+="<li><a href=\"#nestedContentPanel_" + $nestedContentPanelId + "\">" + $(this).find(">h2").text() + "</a></li>\n";
					$(this).attr("id","nestedContentPanel_" + $nestedContentPanelId);
					$nestedContentPanelId++;
				});
				$nestedContentLinkList = "<ul class=\"tabNestedSwitchedContentLinks\">\n" + $nestedContentLinkList + "</ul>\n";
				$(this).prepend($nestedContentLinkList);
				//alert($nestedContentLinkList);
				//hide them all except first one
				$(this).find(".tabNestedSwitchedContent").hide();
			 $(this).find(".tabNestedSwitchedContent:first").show();
			}
		});
		var $n_thisHash ="";
		$(".tabNestedSwitchedContentLinks a").click(function(){
			$n_thisHash = "#" + $(this).attr("href").split("#")[1];
			$(this).parent().parent().parent().find(".tabNestedSwitchedContent").hide();
			$(this).parent().parent().find("a").removeClass("active");
			$(this).addClass("active");
			//alert($(this).attr("href"));
			$($n_thisHash).fadeIn(300);
			return false;
		});
	}
	

	//start accessibility tweaks
	$("a.removeTabStyles").each(function(){
		$(this).click(function(){
			$(this).next().find("div").show();
			$(this).next().find("a.dummy").remove();
			$(this).next().find(">ul>li>a").unbind("click");
			$(this).next().find(">div").each(function(){
				//generate new jump to links
				var $thisId = $(this).attr("id");
				$(this).find("ul>li:first").prepend("<a href=\"#jump" + $thisId + "\" name=\"jump" + $thisId + "\" id=\"jump" + $thisId + "\"></a>");
			});
			$(this).next().find(">ul>li>a").each(function(){
				$(this).attr("href","#jump" + $(this).attr("href").replace("#",""));
				$(this).click(function(){
//					alert($(this).attr("href"));
				});
			});
			//$(this).remove();
			return false;
		});
	});
	//end accessibility tweaks
});
