var docDivs;
var tabLinks;
var subTabLinks;
var subTabContainers;
var docA;
var currentContainer;
var hideCont_i;
var tabInnerHTML;

function toTop() {
	self.scrollTo(0, 0)
}

String.prototype.getAnchor = function() {
	return /#([a-z][\w.:-]*)$/i.exec(this)[1];
}

function changeTabHref(){

	docA.each(function(item){
					
	if(item.href){
			if(item.href.match('#tab_') == '#tab_'){
				hrefString = item.href;
				hrefString = hrefString.replace('#tab_','#tab_tab_');
				item.href = hrefString;
				item.onclick = function() {showlayer(this);}
			}
			if(item.href.match('#ddetail_subtab') == '#ddetail_subtab'){
				hrefString = item.href;
				hrefString = hrefString.replace('#ddetail_subtab','#tab_ddetail_subtab');
				item.href = hrefString;
				item.onclick = function() {showlayer(this);}
			}
		}			   
					   
	})
}

function upDOM(startHere, finishHere){
	parentPath = startHere;
	j = 0;
	while(parentTag != finishHere || j >= 100){
		parentPath = parentPath.parentNode;
		var parentTag = parentPath.tagName.toLowerCase();
		j++;
	}
	return parentPath;
}

function upDomById(startHere, finishHere){
	parentPath = startHere;
	j = 0;
	while(parentId != finishHere || j >= 100){
		parentPath = parentPath.parentNode;
		if(parentPath.id){
			var parentId = parentPath.id;
			parentId = parentId.match(finishHere);
		}
		j++;
	}
	return parentPath;
}

function hideThis(thisObj){
	if(thisObj.style){
		thisObj.style.display = 'none';
	}
}

function showThis(thisObj){
	if(thisObj.style){
		thisObj.style.display = 'block';
	}
	if($('container_wrapper')){
		if($('container_wrapper').style.visibility == 'hidden'){
			$('container_wrapper').style.visibility = 'visible';
		}
	}
}

function clearStyles(theseObj){
	for (var i = 0; i < theseObj.length; i++) {
		if(theseObj[i].parentNode.tagName.toLowerCase() == 'td'){
			theseObj[i].parentNode.className = '';
		}else{
			theseObj[i].className = '';
		}
	}
}

function setSelected(thisObj){
	if(thisObj.parentNode.tagName.toLowerCase() == 'td'){
		thisObj.parentNode.className = 'selected';
	}else{
		thisObj.className = 'selected';
	}
}

function getTab(v){
	return v.split('#tab_')[1];
}

function hideSubTabs(){
	if(subTabContainers.length>=1){
		for(var i = 0; i < subTabContainers.length; i++){
			hideThis(subTabContainers[i]);
		}
	}
}

function hideContentLayers(){
	for(var i = 0; i < docDivs.length; i++){
		if(docDivs[i].id && docDivs[i].id.match('tab_') == 'tab_'){
			hideThis(docDivs[i]);
		}
	}
}

function openTabContent(thisTabContent){
	thisTabContent_id = thisTabContent;
	thisTabContent = $(thisTabContent);
	if(!thisTabContent) return;
	clearStyles(subTabLinks);
	hideContentLayers();
	//check href target of level 1 anchors, same as thisTabContent? clear level 1 styles, setSelected & hide subTabs
	
	tabLinks.each(function(item){						   
		linkTarget = $(getTab(item.href));
		if(item.href.match('#tab_') == '#tab_' && linkTarget == thisTabContent){
			clearStyles(tabLinks);			
			setSelected(item);
			hideSubTabs();
		}			   						   
	})
	
	if($('ddetail_tabs').hasClassName('homePageTabs') && currentContainer != thisTabContent_id){
		
		heightChange = new fx.Style(thisTabContent_id, 'margin-top', {duration:500});

		heightChange.set(282);
		heightChange.custom(282, 0);
		showThis($(thisTabContent_id));
		if($(currentContainer)){
			showThis($(currentContainer));
			$(currentContainer).parentNode.style.zIndex = 20;
			$(thisTabContent_id).parentNode.style.zIndex = 21;
		}
		
		if (!navigator.appVersion.match("MSIE")){
			opacChange = new Fx.Style(thisTabContent_id, 'opacity', {duration: 500});
			opacChange.set(0);
			opacChange.custom(0.0, 1.0);
		}
		
		currentContainer = thisTabContent_id;
	}else{
		
		//check href target of level 2 anchors, same as thisTabContent? set selected and show its container. also, set selected to associated level 1 anchor 
		showThis(thisTabContent);
		subTabLinks.each(function(item){							  
			if(item.href.match('#tab_') == '#tab_' && $(getTab(item.href)) == thisTabContent){
				//reference to the container object - which WILL CONTAIN 'ddetail_subtab'+i
				subTabContainer = upDomById(item, 'ddetail_subtab');
				showThis(subTabContainer);
				setSelected(item);			
				tabLinks.each(function(item){								   
					jLinkTarget = $(getTab(item.href));
					if(jLinkTarget == subTabContainer){
						setSelected(item);
					}				   								   
				})
			}
		
		})
		
		//check if thisTabContent is a subTab, if so, set selected to the first anchor and open its target href
		if(thisTabContent.parentNode.id == 'ddetail_subtabs_wrapper'){
			subLinks = thisTabContent.getElementsByTagName('a');		
			showThis($(getTab(subLinks[0].href)));
			setSelected(subLinks[0]);
		}
	}
	if(!$('ddetail_tabs').hasClassName('noTabScroll')){
		toTop();
	}
}

function deeplink(){
	if(!document.location.hash){
		if($$('.defaultTabContent')[0]){
			tabContent = $$('.defaultTabContent')[0].parentNode.id;
		}else{
			tabContent = $(getTab(tabLinks[0].href));
		}	
	}else{
		if(document.location.hash.match('subtab') == 'subtab'){	
			newHash = document.location.hash;
			
			if(newHash.match('#tab_tab_') == '#tab_tab_'){
				newHash = newHash.replace('#tab_tab_','');
			}else if(newHash.match('#tab_') == '#tab_'){
				newHash = newHash.replace('#tab_','');
			}
			
			tabContent = $(newHash);
		}else{
			if(document.location.hash.match('#tab_tab_') != '#tab_tab_'){
				tabContent = $(getTab("#tab_"+document.location.hash.getAnchor()));
				document.location = "#tab_"+document.location.hash.getAnchor();
			}else{
				tabContent = $(getTab(document.location.hash));	
			}
		}	
	}
	openTabContent(tabContent);
}

function showlayer(link){
	if(link){
			//for hbx tracking
			tabInnerHTML = link.innerHTML.stripTags();
			tabInnerHTMLclean = _hbxStrip(tabInnerHTML);
			var _content = _mlc;
			var _rpnClean = _hbxStrip(_rpn);
			_hbPageView(_rpnClean + "+" + "tab_" + tabInnerHTMLclean,_content);
		openTabContent(getTab(link.href));
		window.location.replace(link.href);
	}
}

function initTabs(){
	//if theres tabs, run it
	
	
	if($('container_wrapper')){
		$('container_wrapper').style.visibility = "hidden";
	}
	
	if($('ddetail_tabs')){
		//ARRAYS
		
		subTabLinks = $A($('ddetail_subtabs_wrapper').getElementsByTagName('a'));
		if(!subTabLinks[0]){
			hideThis($('ddetail_subtabs_wrapper'));
		}
		
		docA = $A(document.getElementsByTagName('a'));
		changeTabHref();
	
		docDivs = $A(document.getElementsByTagName('div'));		
		tabLinks = $A($('ddetail_tabs').getElementsByTagName('a'));	
		
		subTabContainers = $A($('ddetail_subtabs_wrapper').childNodes);
		
		//PERFORM FUNCTIONS
		
		deeplink();
	}
}

Event.onDOMReady(function() {
	if($('ddetail_tabs')){
		initTabs();
	}
});