/**
 * Generates css controlled tab view switching 
 * 
 * @author James Baker, Tom Bermel (24/09/08)
 */

if (!console) var console = {};
if (!console.log) console.log = {};

// set up Argos callout namespace
if (!argos) var argos = {};
if (!argos.tabviewer) argos.tabviewer = {};

argos.tabviewer.tabswitchview = {	

	init : function() {
		// shorten namespace
		var tabviewer = argos.tabviewer.tabswitchview;
		var tabs = $('ul.tabrow li');
		var tabbedareas = $('div.view');
		var uls = $('.herorow .viewitem ul');
		
		for (var i = 0; i < uls.length; i++) {
			newHeight = $(uls[i]).parent().parent().height() - 26;
			$(uls[i]).height(newHeight);
		}
		
		for (var i = 0; i < tabs.length; i++) {
			if (tabs.length > 1) {
				$('div#heroproducts').removeClass("singletabbedarea");
				$('div#heroproducts').addClass("tabbedarea");
				$('div.pntabs').show();
				var selectedTab = tabs[1];
				var selectedTabbedArea = tabbedareas[1];
				var firstTab = tabbedareas[0];
				$(firstTab).hide();
				$(selectedTab).addClass("selected");
				$(tabs[0]).removeClass("selected");
				$(selectedTabbedArea).show();
			}
			else if (tabs.length == 1) {
				$('#heroproducts .tabrow .tab').show();
				var selectedTab = tabs[0];
				var selectedTabbedArea = tabbedareas[0];
				$(selectedTab).addClass("selected");
				$(selectedTabbedArea).show();
			}
		
		}
		
			
		
		// display all tabs in default state
		$(".tabbedarea .tab").show();
	
		$(".tabbedarea .tab a").click(
			function(){
				
		//locate/establish specific ID value of the parent tab and corresponding view
				
				var activeParent = $(this).parent();
				
				var activeView = $(this).attr("href");
				//deselect tabs and hide all panels
				$(activeParent).parent().siblings().removeClass("selected");
				//$(".tabbedarea .view").hide();
				
				$(activeView).siblings(".view").hide();
				
				//select tab and show selected panel
				$(activeParent).parent().addClass("selected");
				$(activeView).show();
				for (var i = 0; i < uls.length; i++) {
					newHeight = $(uls[i]).parent().parent().height() - 26;
					$(uls[i]).height(newHeight);
				};
				return false;
		   }	
		);
		$(".pntabs a").click(
			function(){
				
		//locate/establish specific ID value of the parent tab and corresponding view
				var activeView = $(this).attr("href");
				var viewIndex = activeView.substring(13,14);			
				var activeParent = tabs[viewIndex - 1];
	
				//deselect tabs and hide all panels
				$(activeParent).siblings().removeClass("selected");
				//$(".tabbedarea .view").hide();
				
				$(activeView).siblings(".view").hide();
				
				//select tab and show selected panel
				$(activeParent).addClass("selected");
				$(activeView).show();
				for (var i = 0; i < uls.length; i++) {
					newHeight = $(uls[i]).parent().parent().height() - 26;
					$(uls[i]).height(newHeight);
				};
				window.location.hash=activeView;
				window.scrollBy(0,-35);
				
				return false;
		   }	
		);		
		
	}
	
}