/***********************************************************************************
							TAB PAGE
************************************************************************************
	Created: 		Stefan Brandt
	Date: 		22.3.2005

	Modified:		Stefan Brandt
	Date:		15.6.2005
	Note:		Fixed the multi-row bug
				Fixed the gap between tab pages when user changes the window size or tab
************************************************************************************
***********************************************************************************/

/***********************************************************************************
							Browser support
***********************************************************************************/
function browserCheck() {
	if (typeof browserCheck.support != "undefined") return browserCheck.support;
	var ie55 = /msie 5\.[56789]/i.test( navigator.userAgent );
	browserCheck.support = ( typeof document.implementation != "undefined" &&
			document.implementation.hasFeature( "html", "1.0" ) || ie55 )
			
	//  Fix the IE55 bug
	if ( ie55 ) {
		document._getElementsByTagName = document.getElementsByTagName;
		document.getElementsByTagName = function(tagNames) {
			if (tagNames == "*")
				return document.all;
			else
				return document._getElementsByTagName( sTagName );
		};
	}
	return browserCheck.support;
}

/***********************************************************************************
 tabPane arguments

	el : HTML Element		The HTML element is used to represent the tab pane
	bUseCookie : Boolean	Optional. Default is true.
***********************************************************************************/
function tabPane( el, bUseCookie ) {
	if ( !browserCheck() || el == null ) return;
	this.element = el;
	this.element.tabPane = this;
	this.pages = [];
	this.selectedIndex = null;
	this.useCookie = bUseCookie != null ? bUseCookie : true;
	// add class name
	this.element.className = this.classNameTag + " " + this.element.className;
	
	// add tab row
	this.tabRow = document.createElement( "div" );
	this.tabRow.className = "tab-row";
	el.insertBefore( this.tabRow, el.firstChild );

	var tabIndex = 0;
	if ( this.useCookie ) {
		tabIndex = Number( tabPane.getCookie( "cradeon_" + this.element.id ) );
		if ( isNaN( tabIndex ) )
			tabIndex = 0;
	}
	this.selectedIndex = tabIndex;
	
	// loop through child nodes and add them
	var cs = el.childNodes;
	var n;
	for (var i = 0; i < cs.length; i++) {
		if (cs[i].nodeType == 1 && cs[i].className == "tabPage") {
			this.addtabPage( cs[i] );
		}
	}
}

tabPane.prototype.classNameTag = "dynamic-tabPane-control";

tabPane.prototype.setSelectedIndex = function ( n ) {
	if (this.selectedIndex != n) {
		if (this.selectedIndex != null && this.pages[ this.selectedIndex ] != null )
			this.pages[ this.selectedIndex ].hide();
		this.selectedIndex = n;
		this.pages[ this.selectedIndex ].show();
		if ( this.useCookie )
			tabPane.setCookie( "cradeon_" + this.element.id, n );	// session cookie
	}
};
	
tabPane.prototype.getSelectedIndex = function () {
	return this.selectedIndex;
};
	
tabPane.prototype.addtabPage = function ( oElement ) {
	if ( !browserCheck() ) return;
	
	// tab page already added; return the tab page
	if ( oElement.tabPage == this )	
		return oElement.tabPage;

	var n = this.pages.length;
	var tp = this.pages[n] = new tabPage( oElement, this, n );
	tp.tabPane = this;

	// move the tab out of the box
	this.tabRow.appendChild( tp.tab );
	
	if ( n == this.selectedIndex )
		tp.show();
	else
		tp.hide();	
	return tp;
};
	
tabPane.prototype.dispose = function () {
	this.element.tabPane = null;
	this.element = null;		
	this.tabRow = null;
	
	for (var i = 0; i < this.pages.length; i++) {
		this.pages[i].dispose();
		this.pages[i] = null;
	}
	this.pages = null;
};

// create cookie
tabPane.setCookie = function ( sName, sValue, nDays ) {
	var expires = "";
	if ( nDays ) {
		var d = new Date();
		d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
		expires = "; expires=" + d.toGMTString();
	}
	document.cookie = sName + "=" + sValue + expires + "; path=/";
};
// get cookie
tabPane.getCookie = function (sName) {
	var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
	var res = re.exec( document.cookie );
	return res != null ? res[3] : null;
};
// remove cookie
tabPane.removeCookie = function ( name ) {
	setCookie( name, "", -1 );
};

/***********************************************************************************
 tabPage arguments

 el : HTML Element			The HTML element used to represent the tab pane
 tabPane : tabPane			The parent tab pane
 nindex :	Number			The index of the page in the parent pane page array
***********************************************************************************/
function tabPage( el, tabPane, nIndex ) {
	if ( !browserCheck() || el == null ) return;
	this.element = el;
	this.element.tabPage = this;
	this.index = nIndex;
	
	var cs = el.childNodes;
	for (var i = 0; i < cs.length; i++) {
		if (cs[i].nodeType == 1 && cs[i].className == "tab") {
			this.tab = cs[i];
			break;
		}
	}
	// keyboard navigation support
	var a = document.createElement( "A" );
	this.aElement = a;
	a.href = "#";
	a.onclick = function () { return false; };
	while ( this.tab.hasChildNodes() )
		a.appendChild( this.tab.firstChild );
	this.tab.appendChild( a );

	// hook up events
	var oThis = this;
	this.tab.onclick = function () { oThis.select();};
	this.tab.onmouseover = function () { tabPage.tabOver( oThis ); };
	this.tab.onmouseout = function () { tabPage.tabOut( oThis ); };	
}

tabPage.prototype.show = function () {
	var el = this.tab;
	var s = el.className + " selected";
	s = s.replace(/ +/g, " ");
	el.className = s;
	this.element.style.display = "block";
};

tabPage.prototype.hide = function () {
	var el = this.tab;
	var s = el.className;
	s = s.replace(/ selected/g, "");
	el.className = s;
	this.element.style.display = "none";
};
	
tabPage.prototype.select = function () {
	this.tabPane.setSelectedIndex( this.index );
};

tabPage.prototype.dispose = function () {
	this.aElement.onclick = null;
	this.aElement = null;
	this.element.tabPage = null;
	this.tab.onclick = null;
	this.tab.onmouseover = null;
	this.tab.onmouseout = null;
	this.tab = null;
	this.tabPane = null;
	this.element = null;
};

// got no idea what this code is supposed to do
// so i made it useless / KR
tabPage.tabOver = function ( tabPage ) 
{	/*
	var el = tabPage.tab;
	var s = el.className + " hover";
	s = s.replace(/ +/g, " ");
	el.className = s;
	*/
};

tabPage.tabOut = function ( tabPage ) 
{	/*
	var el = tabPage.tab;
	var s = el.className;
	s = s.replace(/ hover/g, "");
	el.className = s;
	*/
};


// initialize tab pages
function createtabPages() {
	if ( !browserCheck() ) return;
	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var tabPaneRe = /tabPane/;
	var tabPageRe = /tabPage/;
	var cn, el;
	var parenttabPane;
	
	for ( var i = 0; i < l; i++ ) {
		el = all[i]
		cn = el.className;
		
		// class name missing
		if ( cn == "" ) continue;
		
		// Tab Pane not initialized
		if ( tabPaneRe.test( cn ) && !el.tabPane )
			new tabPane( el );
		// Tab Pane initialized but the tab page is not initialized 
		else if ( tabPageRe.test( cn ) && !el.tabPage && tabPaneRe.test( el.parentNode.className ) ) {
			el.parentNode.tabPane.addtabPage( el );			
		}
	}
}

// dispose Tab Pages
function disposetabPages() {
	if ( !browserCheck() ) return;
	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var tabPaneRe = /tabPane/;
	var cn, el;
	var tabPanes = [];
	
	for ( var i = 0; i < l; i++ ) {
		el = all[i]
		cn = el.className;
		// no className
		if ( cn == "" ) continue;
		// tab pane
		if ( tabPaneRe.test( cn ) && el.tabPane )
			tabPanes[tabPanes.length] = el.tabPane;
	}
	for (var i = tabPanes.length - 1; i >= 0; i--) {
		tabPanes[i].dispose();
		tabPanes[i] = null;
	}
}

// initialization hook up
// DOM2
if ( typeof window.addEventListener != "undefined" )
	window.addEventListener( "load", createtabPages, false );
// IE 
else if ( typeof window.attachEvent != "undefined" ) {
	window.attachEvent( "onload", createtabPages );
	window.attachEvent( "onunload", disposetabPages );
}
else {
	if ( window.onload != null ) {
		var oldOnload = window.onload;
		window.onload = function ( e ) {
			oldOnload( e );
			createtabPages();
		};
	}
	else 
		window.onload = createtabPages;
}
