/* Released: 2010-03-18 12:16:50 */

/**
 * @author stec
 */


function TabNavigation() {
	var me = this;
	var virgin = true;	//too see if tabs are clicked for the first time
	
	try {
		var gTabClickTracker = new TabClickTracker();
		var tracker = new WebTrends();
	} catch (err) {}
	
	me.gotoTabFromUrlHash();

	/* attach click event on tab links */
	$gJQ("#tab-navigation li a").click(function(e){
		var href = $gJQ(this).attr("href");
		me.gotoTab(href);
	});

	/* prefix local links to tabs with "tab:<anchor>" */
	$gJQ("#tab-navigation li a").each(function(i, el){
		var hash = $gJQ(el).attr("href");
		var href = hash.replace("#", "#tab:");
		$gJQ(el).attr("href", href);
	});
	
	/* prefix local links to anchor within tabs with "tab:<tabname>:<anchor>" */
	$gJQ("#tab-wrapper a[href^=#]:not([href=#])").not("[href^=#tab:]").each(function(){
		var anchorId = $gJQ(this).attr("href").replace("#","");
		var tabContentId = $gJQ(this).parents(".tab-content").attr("id");
		var newAnchorId = "tab:" + tabContentId + ":" + anchorId;
		$gJQ(this).attr("href", "#" + newAnchorId);	//replace href
		$gJQ("#"+anchorId).attr("id", newAnchorId); //replace id
	});

	// scroll to top, prevent default
	$gJQ(".back-to-top-link").each(function(){
		$gJQ(this).click(function(e){
			e.preventDefault();
			window.scrollTo(0,0);
		});
	});
	
    /* enable history */
	$gJQ(window).bind( 'hashchange', function(e) {
		try {
			me.gotoTab(window.location.hash);
			
			//analytics on tab clicks
			try {
				//don't track the hashchange trigger on the first time
				if (virgin) return;
				gTabClickTracker.trackTabClick(me._getHash(window.location.hash));
				tracker.dcsVar();
				tracker.dcsMeta();
				tracker.dcsMultiTrack('WT.Tab_ID',me._getTabTitle());
			} catch (e) {}

		} catch(e) {
			me.gotoFirst();
		} finally {
			virgin = false;
		}
	});
	$gJQ(window).trigger( 'hashchange' );

	
}

TabNavigation.prototype = {
	_cache : {},
	
	gotoTab : function(hash) {
		if (document.location.href.match(/#$/gi)) {
			return;	//do nothing when empty # is present in url
		}
		var elTab = $gJQ(" > #" + this._getHash(hash), this._getTabContentContainers().parent());

		if (!elTab || elTab.length == 0) {
			return;	//do nothing when hash doesn't match with an ID of container
		}

		$gJQ(this._getTabContentContainers()).addClass('hide');
		$gJQ(elTab).removeClass('hide');
		this._highlightCurrentTab("#" + this._getHash(hash));
	},
	
	gotoTabFromUrlHash : function() {
		try {
			this.gotoTab(this._getHash());
		} catch(e) {}
	},
	
	gotoNext : function() {
		//$gJQ(this._getCurrentTab()).next().find("a").trigger("click");
		var href = $gJQ(this._getCurrentTab()).next().children("a").attr('href');
		if (href) document.location = href;
	},
	
	gotoPrevious : function() {
		//$gJQ(this._getCurrentTab()).prev().find("a").trigger("click");
		var href = $gJQ(this._getCurrentTab()).prev().children("a").attr('href');
		if (href) document.location = href;
	},
	
	gotoFirst : function() {
		var hash = $gJQ("#tab-navigation li:first a").attr("href");
		this.gotoTab(hash);
	},
	
	_getHash : function(hash) {
		var hash = hash || window.location.hash;
		
		//clean up;
		hash = hash.replace(/#(\w+:)?/, "").split(":")[0];
		return hash;
	},
	
	_getTabTitle : function(){
		return $gJQ("#tab-navigation strong span").text();
	},

	_getCurrentTab : function() {
		return $gJQ("#tab-navigation strong").parent().eq(0);
	},
	
	_highlightCurrentTab : function(hash) {
		var hash = this._getHash(hash).replace(/#(\w+:)?/, "");

		//unwrap strong
		$gJQ("#tab-navigation>li>strong>a").each(function(){
			$gJQ(this).parent().replaceWith(this);
		});
		
		$gJQ("#tab-navigation>li>a[href$="+hash+"]").wrap("<strong>");
	},
	
	_getTabContentContainers : function() {
		if (this._cache.tabContentContainers) {
			return this._cache.tabContentContainers;
		}
		
		var me = this;
		var els = [];
		$gJQ("#tab-navigation>li a").each(function(){
			var hash = $gJQ(this).attr('href');
			els.push("#" + me._getHash(hash));
		});
		return this._cache.tabContentContainers = $gJQ(els.join(','));
	}
};
