/* -----------------------------------------------------------------
// Tabbed Items Classes
//
// Class for TNT Post tabbed items
// Dependency: 
// Used by: page object
// Version: 1.2 (20071112)
//
// Copyright 2007, TNT Post
//
// CVL 2007-11-07: Vertical tabs added
// AFR 2007-07-25: Function TabbedSection has been altered
----------------------------------------------------------------- */

/* -----------------------------------------------------------------
// TabbedItems Class
----------------------------------------------------------------- */
TabbedItems = function() {
	/* Horizontal tabs */
	this.sections = new Array; 
	var sections = getElementsByClassName("tabbed-section", document);
	if (sections.length > 0) {
		for (var i = 0; i < sections.length; i++) {
			this.sections[this.sections.length] = new TabbedSection(sections[i],"ts" + i);
		};
	};
	/* Vertical tabs */
	this.vSections = new Array;
	var vSections = getElementsByClassName("v-tab-nav", document);
	if (vSections.length > 0) {
		for (var i = 0; i < vSections.length; i++) {
			this.vSections[this.vSections.length] = new TabbedSectionVertical(vSections[i],"tvs" + i);
		};
	};
	tickle();
};
TabbedItems.prototype.destroy = function() {
	/* Horizontal tabs cleanup */
	for (var i = 0; i < this.sections.length; i++) {
		for (var j = 0; j < this.sections[i].tabs.childNodes; j++) {
			var u = this.sections[i].tabs.childNodes[j];
			u.number = u.onclick = null;
		};
	};
	/* Vertical tabs cleanup */
	for (var i = 0; i < this.vSections.length; i++) {
		for (var j = 0; j < this.vSections[i].lis; j++) {
			this.vSections[i].lis[j].onmouseover = this.vSections[i].lis[j].onmouseout = null;
		};
	};
};

/* -----------------------------------------------------------------
// TabbedSection Class
----------------------------------------------------------------- */
TabbedSection = function(obj, id) {
	// gather children and construct the menu
	this.items = getElementsByClassName("tabbed-item", obj);
	this.headers = getElementsByClassName("tabbed-header", obj);
	this.changeImages = getElementsByClassName("changeableImage", obj);
	// create tabbed menu
	this.tabs = document.createElement("ul");
	this.tabs.className = "tabs";
	
	for (var i = 0; i < this.headers.length; i++) {
		var tab = document.createElement("li");
		var self = this;
		tab.number = i;
		if(this.headers[i].title) var label = this.headers[i].title;
		else var label = this.headers[i].innerHTML;
		var strimage = '';
		for (j = 0; j < this.changeImages.length; j++){
		    
			if(this.changeImages[j].getAttribute('tabindex') == i){
			    strimage = this.changeImages[j].value; 
			} 
		}		
		tab.innerHTML = "<a href=\"#\" onclick=\"ChangeImage('" + strimage + "');\">" + label + "</a>";
		tab.onclick = function() { 
			self.resetAll(this.number);
			return false;
		}
		this.tabs.appendChild(tab);
	}
	this.tabs.childNodes[0].className = "first";
	obj.insertBefore(this.tabs,this.items[0]);
	// set defaults
	this.resetAll(0);
}
TabbedSection.prototype.resetAll = function(exception) {
	for (var i = 0; i < this.items.length; i++) {
		if (i != exception) {
			removeClass(this.items[i], "current");
			removeClass(this.tabs.childNodes[i], "current");
		} else {
			addClass(this.items[i], "current");
			addClass(this.tabs.childNodes[i], "current");
			if (document.getElementById("changeimage")) {
			     document.getElementById("changeimage").style.display = "";
			     if (this.items[i].className.indexOf("tab-wide") > 0) {
			          document.getElementById("changeimage").style.display = "none";
			    }
			}
		}
	}
	tickle();
}

/* -----------------------------------------------------------------
// TabbedSectionVertical Class
----------------------------------------------------------------- */
TabbedSectionVertical = function(obj, id) {
	this.locationUri = location.href; // URI to highlight
	// gather children
	this.lis = obj.getElementsByTagName("li");
	if (this.lis.length > 0) {
		for (var i=0; i < this.lis.length; i++) {
			// hook up behaviors
			this.lis[i].onmouseover = function(){
				addClass(this, "hover");
			};
			this.lis[i].onmouseout = function(){
				removeClass(this, "hover");
			};
			// highlight current item corresponding to URI
			if ( this.locationUri.match(this.lis[i].getElementsByTagName("a")[0].href) ) {
				addClass(this.lis[i], "current");
			};
		};
	};
};
