// This function is used to define if the browser supports the needed
// features
function hasSupport() {

	if (typeof hasSupport.support != "undefined")
		return hasSupport.support;
	
	var ie55 = /msie 5\.[56789]/i.test( navigator.userAgent );
	
	hasSupport.support = ( typeof document.implementation != "undefined" &&
			document.implementation.hasFeature( "html", "1.0" ) || ie55 )
			
	// IE55 has a serious DOM1 bug... Patch it!
	if ( ie55 ) {
		document._getElementsByTagName = document.getElementsByTagName;
		document.getElementsByTagName = function ( sTagName ) {
			if ( sTagName == "*" )
				return document.all;
			else
				return document._getElementsByTagName( sTagName );
		};
	}

	return hasSupport.support;
}

///////////////////////////////////////////////////////////////////////////////////
// The constructor for tab2 panes
//
// el : HTMLElement		The html element used to represent the tab2 pane
// bUseCookie : Boolean	Optional. Default is true. Used to determine whether to us
//						persistance using cookies or not
//
function WebFXtab2Pane( el, bUseCookie ) {
	if ( !hasSupport() || el == null ) return;
	
	this.element = el;
	this.element.tab2Pane = this;
	this.pages = [];
	this.selectedIndex = null;
	this.useCookie = bUseCookie != null ? bUseCookie : false;
	
	// add class name tag to class name
	this.element.className = this.classNameTag + " " + this.element.className;
	
	// add tab2 row
	this.tab2Row = document.createElement( "div" );
	this.tab2Row.className = "tab2-row";
	el.insertBefore( this.tab2Row, el.firstChild );

	var tab2Index = 0;
	if ( this.useCookie ) {
		tab2Index = Number( WebFXtab2Pane.getCookie( "webfxtab2_" + this.element.id ) );
		if ( isNaN( tab2Index ) )
			tab2Index = 0;
	}
	this.selectedIndex = tab2Index;
	
	// 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 == "tab2-page") {
			this.addtab2Page( cs[i] );
		}
	}
}

WebFXtab2Pane.prototype.classNameTag = "dynamic-tab2-pane-control";

WebFXtab2Pane.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 )
			WebFXtab2Pane.setCookie( "webfxtab2_" + this.element.id, n );	// session cookie
	}
};
	
WebFXtab2Pane.prototype.getSelectedIndex = function () {
	return this.selectedIndex;
};
	
WebFXtab2Pane.prototype.addtab2Page = function ( oElement ) {
	if ( !hasSupport() ) return;
	
	if ( oElement.tab2Page == this )	// already added
		return oElement.tab2Page;

	var n = this.pages.length;
	var tp = this.pages[n] = new WebFXtab2Page( oElement, this, n );
	tp.tab2Pane = this;
	
	// move the tab2 out of the box
	this.tab2Row.appendChild( tp.tab2 );
			
	if ( n == this.selectedIndex )
		tp.show();
	else
		tp.hide();
		
	return tp;
};
	
WebFXtab2Pane.prototype.dispose = function () {
	this.element.tab2Pane = null;
	this.element = null;		
	this.tab2Row = null;
	
	for (var i = 0; i < this.pages.length; i++) {
		this.pages[i].dispose();
		this.pages[i] = null;
	}
	this.pages = null;
};



// Cookie handling
WebFXtab2Pane.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=/";
};

WebFXtab2Pane.getCookie = function (sName) {
	var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
	var res = re.exec( document.cookie );
	return res != null ? res[3] : null;
};

WebFXtab2Pane.removeCookie = function ( name ) {
	setCookie( name, "", -1 );
};








///////////////////////////////////////////////////////////////////////////////////
// The constructor for tab2 pages. This one should not be used.
// Use WebFXtab2Page.addtab2Page instead
//
// el : HTMLElement			The html element used to represent the tab2 pane
// tab2Pane : WebFXtab2Pane	The parent tab2 pane
// nindex :	Number			The index of the page in the parent pane page array
//
function WebFXtab2Page( el, tab2Pane, nIndex ) {
	if ( !hasSupport() || el == null ) return;
	
	this.element = el;
	this.element.tab2Page = this;
	this.index = nIndex;
	
	var cs = el.childNodes;
	for (var i = 0; i < cs.length; i++) {
		if (cs[i].nodeType == 1 && cs[i].className == "tab2") {
			this.tab2 = cs[i];
			break;
		}
	}
	
	// insert a tag around content to support keyboard navigation
	
	
	var a = document.createElement( "A" );
	this.aElement = a;
	a.href = "#";
	a.onclick = function () { return false; };
	while ( this.tab2.hasChildNodes() )
		a.appendChild( this.tab2.firstChild );
	this.tab2.appendChild( a );

	
	// hook up events, using DOM0
	var oThis = this;
	this.tab2.onclick = function () { oThis.select(); };
	this.tab2.onmouseover = function () { WebFXtab2Page.tab2Over( oThis ); };
	this.tab2.onmouseout = function () { WebFXtab2Page.tab2Out( oThis ); };
}

WebFXtab2Page.prototype.show = function () {
	var el = this.tab2;
	var s = el.className + " selected";
	s = s.replace(/ +/g, " ");
	el.className = s;
	
	this.element.style.display = "block";
};

WebFXtab2Page.prototype.hide = function () {
	var el = this.tab2;
	var s = el.className;
	s = s.replace(/ selected/g, "");
	el.className = s;

	this.element.style.display = "none";
};
	
WebFXtab2Page.prototype.select = function () {
	this.tab2Pane.setSelectedIndex( this.index );
};
	
WebFXtab2Page.prototype.dispose = function () {
	this.aElement.onclick = null;
	this.aElement = null;
	this.element.tab2Page = null;
	this.tab2.onclick = null;
	this.tab2.onmouseover = null;
	this.tab2.onmouseout = null;
	this.tab2 = null;
	this.tab2Pane = null;
	this.element = null;
};

WebFXtab2Page.tab2Over = function ( tab2page ) {
	var el = tab2page.tab2;
	var s = el.className + " hover";
	s = s.replace(/ +/g, " ");
	el.className = s;
};

WebFXtab2Page.tab2Out = function ( tab2page ) {
	var el = tab2page.tab2;
	var s = el.className;
	s = s.replace(/ hover/g, "");
	el.className = s;
};


// This function initializes all uninitialized tab2 panes and tab2 pages
function setupAlltab2s() {
	if ( !hasSupport() ) return;

	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var tab2PaneRe = /tab2\-pane/;
	var tab2PageRe = /tab2\-page/;
	var cn, el;
	var parenttab2Pane;
	
	for ( var i = 0; i < l; i++ ) {
		el = all[i]
		cn = el.className;

		// no className
		if ( cn == "" ) continue;
		
		// uninitiated tab2 pane
		if ( tab2PaneRe.test( cn ) && !el.tab2Pane )
			new WebFXtab2Pane( el );
	
		// unitiated tab2 page wit a valid tab2 pane parent
		else if ( tab2PageRe.test( cn ) && !el.tab2Page &&
					tab2PaneRe.test( el.parentNode.className ) ) {
			el.parentNode.tab2Pane.addtab2Page( el );			
		}
	}
}

function disposeAlltab2s() {
	if ( !hasSupport() ) return;
	
	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var tab2PaneRe = /tab2\-pane/;
	var cn, el;
	var tab2Panes = [];
	
	for ( var i = 0; i < l; i++ ) {
		el = all[i]
		cn = el.className;

		// no className
		if ( cn == "" ) continue;
		
		// tab2 pane
		if ( tab2PaneRe.test( cn ) && el.tab2Pane )
			tab2Panes[tab2Panes.length] = el.tab2Pane;
	}
	
	for (var i = tab2Panes.length - 1; i >= 0; i--) {
		tab2Panes[i].dispose();
		tab2Panes[i] = null;
	}
}


// initialization hook up

// DOM2
if ( typeof window.addEventListener != "undefined" )
	window.addEventListener( "load", setupAlltab2s, false );

// IE 
else if ( typeof window.attachEvent != "undefined" ) {
	window.attachEvent( "onload", setupAlltab2s );
	window.attachEvent( "onunload", disposeAlltab2s );
}

else {
	if ( window.onload != null ) {
		var oldOnload = window.onload;
		window.onload = function ( e ) {
			oldOnload( e );
			setupAlltab2s();
		};
	}
	else 
		window.onload = setupAlltab2s;
}