
/*
*	Tabset Dohicky:
*	More content to come...
*
*/

//New constructor
function TabMenu(mainId, wrapperClass, selectedCss){
	if(!mainId) mainId = '';
	if(!wrapperClass) wrapperClass = 'tabmenu';
	if(!selectedCss) selectedCss = 'selected';
	
	this.mainId = mainId;				//DOM-ID for the UL tag 
	this.wrapperClass = wrapperClass;	//CSS Classname for DIV wrapper
	this.itemId = new Array();			//Array of DOM-IDs for each Tab
	this.label = new Array();			//Array of Labels for each Tab
	this.obj = new Array();				//Array of DOM-IDs for each content DIV associated with the Tab
	this.itemClass = new Array();		//Array of CSS Classnames for each Tab
	this.onClick = new Array();			//Array of onclick() attribs for each Tab
	this.selectedCss = selectedCss;		//CSS Classname used to mark Tabs as selected when clicked
	this.debug = false;
}
TabMenu.prototype.initTabs   = initTabs;  //Class method()
TabMenu.prototype.addTabItem = __addTabItem; //Class method()
TabMenu.prototype.writeMenu  = __writeMenu;	//Class method()
TabMenu.prototype.clickTab   = __clickTab; //Class method()
	
	
	
//   initTabs('content', '', 'prod_tabs', 'selected', /(songs)|(dances)/gi);	
function initTabs( id, mainId, cssClass, cssSelected, regex_btnTxt_remove ){
	
	if( !id ){ alert("Please define an element ID for use in initTabs().\n\nExample::   initTabs( 'myDomId' );"); return false; }
	
	if(!regex_btnTxt_remove) regex_btnTxt_remove = '';
	//regex_btnTxt_remove = /(songs)|(dances)/gi;
	var top = selectedItem = butId = namedAnchors = tabPaneContent = '';
	var tabset = blocks = new Array();
	regex_hr_pattern = /(<hr(?:.*?)>)|(<p><hr(?:.*?)><\/p>)/i; 
	
	tabset_name = 'tabmenu' + tab_rand();
	eval('window.'+tabset_name+' = new TabMenu("'+tabset_name+'", cssClass, cssSelected)');
	container = MM_findObj( id );
	
	//Split by HR tags
	blocks = container.innerHTML.split( regex_hr_pattern ); //split has a bug in IE.  Fix does not work in FF
	
	for(x=0,first=(-1);x<blocks.length;x++){
		if( blocks[x].length ){ 
			//Isolate the named anchors and section titles 
			//a  = blocks[x].match( /(<a(?:.*?)name=(.*?)\b[^>]*><\/a>)|(<a(?:.*?)name="(.*?)"(?:.*?)><\/a>)/i );
			a  = blocks[x].match( /(?:<[a-z1-9]>)?(?:.*)?<a(?:.*?)(?:(?:name="(.*?)"(?:.*?))|(?:name=(.*?)\b[^>]*))><\/a>(?:.*)?(?:<\/[a-z1-9]>)?/i );
			blocks[x] = blocks[x].replace(/(<a(?:.*?)><\/a>)|(<p(?:.*?)>(?:.*)?<a(?:.*?)>(?:.*)?<\/a><\/p>)|(<h3(?:.*?)>(?:.*)?<a(?:.*?)><\/a>(?:.*)?<\/h3>)/i, '');
			blocks[x] = blocks[x].replace(/<[a-z1-9]>(?:&nbsp;)*?<\/[a-z1-9]>/i, '');
			
			h3 = blocks[x].match( /<h3(?:.*?)>(.*?)(?!<a(?:.*?)><\/a>)<\/h3>/i ); 
			//alert( blocks[x] );
			//alert( a );
			
			if( a != null ){
				if(h3 != null){ 
					if(regex_btnTxt_remove!='') h3[1] = h3[1].replace(regex_btnTxt_remove, '');
				}else{ 
					h3 = new Array(); h3[1] = a[1]; //'Undefined Tab';
				}
				
				//Remove the named anchors
				tabPaneContent = blocks[x].replace(/(<a(?:.*?)><\/a>)|(<p(?:.*?)>(?:.*)?<a(?:.*?)>(?:.*)?<\/a><\/p>)|(<h3(?:.*?)>(?:.*)?<a(?:.*?)><\/a>(?:.*)?<\/h3>)/i, '')
				
				//Add the tabs and tab-content
				eval('newTabId = '+tabset_name+'.addTabItem(h3[1], "'+tabset_name+'_'+x+'_"+ a[1])');
				tabset.push( '<div id="'+ tabset_name +'_'+x+'_'+ a[1] +'" style="visibility:hidden;display:none;">' + tabPaneContent.replace(/<[a-z1-9]>(?:&nbsp;)*?<\/[a-z1-9]>/i, '') + "</div>" );
											
				
				namedAnchors += '<a name="'+ a[1] +'"></a>';
				//Find selected
				//if( window.location.search.substring(1).length > 0 ){
					//if a named anchor is available in the QS then try and use for selected...
				//}else{
					//Get id of first item
					if(first<0) first = x;
					if(first==x) selectedItem = newTabId; 
				//}
				
			}else{
				//Add the remaining content and eliminate the HR tags
				top = top + blocks[x].replace( regex_hr_pattern , '');
			}
		}			
	}
	
	//Ouput the page content
	eval('container.innerHTML = top + '+tabset_name+'.writeMenu("", false)');
	container.innerHTML += tabset.join(" \n ");
	//alert( namedAnchors );
	
	//Select the first item
	eval(tabset_name+'.clickTab( MM_findObj(selectedItem) )');

}


/*===========================================*/


//Adds a new tab item to the tabset
function __addTabItem(label, obj, itemClass, onClick){
	if(!onClick) onClick = '';
	if(!itemClass) itemClass = '';
	itemId = this.mainId + '.' + this.itemId.length;
	
	this.itemId.push( itemId );
	this.label.push( label );
	this.obj.push( obj );
	this.itemClass.push( itemClass );
	this.onClick.push( onClick );
	
	return itemId;
}


//Outputs the tab menu
//function writeMenu(echo, tabSet){
function __writeMenu(objName, echo, anchors, test){
	if(test!=true) test = false;
	if(test) alert("start");
	
	//If tabset is not defined and 'this' is then set to 'this
	if(objName==undefined || objName=='') objName = this.mainId;
	if(echo!=false) echo = true;
	if(anchors!=true) anchors = false;
	
	var content = wrapper = id = class_str = href_str = '';
	
	if(this.itemId.length > 0){
		//Build menu html...
		
		content += '<div class="'+ this.wrapperClass +'">';
		content += '  <ul id="'+ this.mainId +'"> ' + "\n";
		//content += '  <ul id="'+ this.mainId +'_'+ tab_rand() +'"> ' + "\n";
		//content += '  <ul> ' + "\n";
		for(i=0; i<this.itemId.length; i++) {
			
			class_str = (this.itemClass[i].length>0)? ' class="'+ this.itemClass[i] +'"'	: '';				
			href_str  = (anchors)?				 	  ' href="#'+ this.obj[i] +'"'			: ' href="#"';
			
			content += '    <li'+ class_str +'><a id="'+ this.itemId[i] +'" '+ href_str +' '+ class_str +' onclick="'+ this.onClick[i] +';'+ objName +'.clickTab(this);"><span>'+ this.label[i] +'</span></a></li> ' + "\n";
		}
		content += "  </ul>\n";
		content += "</div>\n";
		
		//Output
		if( echo ){
			document.writeln(content);
		}else{
			return content
		}
	}else{
		//No items to show
		//Return Error here...
	}
}


//Marks a tab and tabpane as selected, while unselecting the others
function __clickTab(e, tabSet){
	if( e != '' ){
		//If tabset is not defined and 'this' is then set to 'this
		if(tabSet==undefined) tabSet = this;
		//if( tabSet.itemId.length==0 && this.itemId.length>0 ) tabSet = this;
		
		//alert(tabSet.valueOf);
		for(i=0; i<tabSet.itemId.length; i++) {
			//alert(tabSet.obj[i]);
			itm = MM_findObj(tabSet.itemId[i]);
			obj = tab_el(tabSet.obj[i]);
			//alert(itm.id +" = "+ e.id);
			if(obj != null){
				if(itm.id == e.id){ 
					itm.className = tabSet.selectedCss;
					if(itm.parentNode != null) itm.parentNode.className = tabSet.selectedCss;
					obj.style.visibility = "visible";
					obj.style.display    = "block";
				}else{ 
					itm.className = "";
					if(itm.parentNode != null) itm.parentNode.className = "";
					obj.style.visibility = "hidden";
					obj.style.display    = "none";
				}
			}
		}
	}
}

//Convenience function to return the element from 'id'
function tab_el(o){	return document.getElementById(o); }
function tab_rand(){ return Math.floor(Math.random()*11); }

