/*

	Overview:   TabNav is a JS object that controls one set of tabs
			    and their respective content blocks

	Usage: 		An instance of this object needs to be instantiated for each
				block of tabs on the page. The markup should be structured
				with the controller anchor and corresponding content div to
				be in the same relative position in it's respective parent.
				Ex) Second anchor tag controls the second content div tag.

				NOTE:
				* The content tabs must have a class name of tab-content
				* The control anchors must be the only anchors in the parent
				* The position of the anchor must be the same as the content
				* The H5 should have a classname called "alt-heading"

	Arguments:	Takes two string literal arguments. (controls, contents)
				controlsID: is the id of the parent block of the tab controls
				contentsID: is the id of the parent block of the content blocks

	Methods:	Object is self-initializing so the constructor is built in as init..

				init: 		Content Blocks and alternate headings are hidden.
							Displays the first content block in the source.

				hideAltHeadings: Hides alt headings(h5.alt-heading)

				toggleControls: Switches active link/tab
								Calls the toggleContent method

				toggleContent: Switches active content block.



	Sample Use: var myTabs = new TabNav("myControlsID", "myContentsID");

*/

function TabNav(controlsID, contentsID){

	// Properties
	// ==========================
	var t = this;
	t.controls = new Array();
	t.contents = new Array();
	t.active = 0;

	// Self-Initialization
	// ==========================
	if((document.getElementById) && (document.getElementsByTagName)){

		// Tab Controls
		t.controlsParent = document.getElementById(controlsID);
		if(t.controlsParent){
			t.controls = t.controlsParent.getElementsByTagName('a');

			// Look for Deep Links
			/*t.hash = window.location.hash;
			if(t.hash != ''){
				t.hash = t.hash.slice(1,t.hash.length);
				t.active = t.hash-1;
				t.controls[t.hash-1].parentNode.className = 'active';
			}*/
			t.deeplink = window.location.href;
			t.param = t.deeplink.lastIndexOf('?');
			if(t.param>=0){
				t.display = t.deeplink.slice(t.deeplink.lastIndexOf('tab=')+4,t.deeplink.length);
				t.active = t.display-1;
				t.controls[t.display-1].parentNode.className = 'active';
				xiti_page_name = xiti_page_names[t.active];
			}else{
				t.controls[0].parentNode.className = 'active'; // SET FIRST LI AS ACTIVE
				xiti_page_name = xiti_page_names[0];
			}
			for(var i=0, control; control=t.controls[i]; i++){
				control.pos = i;
				control.onclick = function(){
					// XiTi tagging function call
					xiti_page_name = xiti_page_names[this.pos];
					xt_med("C", xiti_level2, xiti_chapter_name+"::"+xiti_page_name, "N");
					t.activate(this.pos);
					this.blur();
					return false;
				}
				if(control.parentNode.nodeName == 'li'){control = control.parentNode;}
				if(control.className == 'active'){t.active = i;}
			}
		}else{return false;}

		// Content Blocks
		t.contentsParent = document.getElementById(contentsID);
		if(t.contentsParent){
			t.divs = t.contentsParent.getElementsByTagName('div');
			for(var j=0, div; div=t.divs[j]; j++){
				if(div.className == 'tab-content'){
					t.contents[(t.contents.length)] = div;
				}
			}
		}else{return false}


	}else{return false}

	// Methods
	// ==========================

	t.toggleControls = function(){
		for(var i=0, control; control=t.controls[i]; i++){
			if(control.parentNode.nodeName.toLowerCase() == 'li'){control = control.parentNode;}
			control.className = 'inactive';
		}
		if(t.controls[t.active].parentNode.nodeName.toLowerCase() == 'li'){
			t.controls[t.active].parentNode.className = 'active';
		}else{
			t.controls[t.active].className = 'active';
		}
	}

	t.toggleBlocks = function(){
		for(var c=0, content; content=t.contents[c]; c++){
			content.className = 'tab-content-hidden';
			contentImage = content.getElementsByTagName('img')[0];
			if (contentImage) {
				contentImage.style.display = 'block';
			}
		}
		activeBlock = t.contents[t.active];
		activeBlock.className = 'tab-content';
		var activeDivs = activeBlock.getElementsByTagName('div');
		for(var k=0, adiv; adiv=activeDivs[k]; k++){
			if(adiv.className == 'gallery'){
				adiv.style.display = 'block';
			}
		}
	}

	t.showAllBlocks = function(){
		for(var c=0, content; content=t.contents[c]; c++){content.className = 'tab-content';}
	}

	t.hideAltHeadings = function(){
		altHeadings = t.contentsParent.getElementsByTagName('h3');

		if(altHeadings){
			for(var h=0, altHeading; altHeading=altHeadings[h]; h++){
				if(altHeading.className == 'tab-alt-heading'){altHeading.className = 'tab-alt-heading-hidden';}
			}
		}
	}

	t.activate = function(pos){
		t.active = pos;
		t.toggleControls();
		t.toggleBlocks();
	}

	t.init = function(){
		t.toggleBlocks();
		//t.hideAltHeadings();
	}


	// Initialize
	var constructor = t.init();

}

// Onload Init
onloadHandlers[onloadHandlers.length] = 'Tabs = new TabNav("tab-controls", "tabs")';



