	/*----------------------------------------------------------------------------
	*/
		function getElmById(idString)	{ return document.getElementById(idString);}
	/*----------------------------------------------------------------------------*/
	/*----------------------------------------------------------------------------
	*/
		function createElm(tagString)	{ return document.createElement(tagString);}
	/*----------------------------------------------------------------------------*/

	/*--------------------- Constants Start --------------------------------------*/
	var tabsEditMode = false;			// true = prevents tab clicks from following links.
	var isVertOpen	= false;
	var selectedTab	= 0;
	var nextTabId	=2;
	var tabsEditMode	= false;		// false, when the tab was beign dragged rather than clickedk
	var barHorizontal = getElmById('horizontalDivId');
	var barVertical		= getElmById('verticalDivId');
	var newTabWidth		= 1; //38
	var tabMaxLength	= 12;
	/*--------------------- Constants End --------------------------------------*/

/*========================  Double Click Function =====================================*/

 var dcTime=450;    // doubleclick time
 var dcDelay=100;   // no clicks after doubleclick
 var dcAt=0;        // time of doubleclick
 var savEvent=null; // save Event for handling doClick().
 var savEvtTime=0;  // save time of click event.
 var savTO=null;    // handle of click setTimeOut
 var dblclickElmId = -1;
 
 function hadDoubleClick() {
   var d = new Date();
   var now = d.getTime();
   if ((now - dcAt) < dcDelay) {
     return true;
   }
   return false;
 }
 
 function handleTabClick( elmId , whichEvt) {

   which = whichEvt.type;
   dblclickElmId = elmId; 

   switch (which) {
     case "click": 
       // If we've just had a doubleclick then ignore it
       if (hadDoubleClick()) return false;
         
       // Otherwise set timer to act.  It may be preempted by a doubleclick.
       savEvent = which;
       d = new Date();
       savEvtTime = d.getTime();
       savTO = setTimeout("doClick(savEvent)", dcTime);
       break;
     case "dblclick":
       doDoubleClick(which);
       break;
     default:
   }
 }
 
 function doClick(which) {
   // preempt if DC occurred after original click.
   if (savEvtTime - dcAt <= 0) {
     return false;
   }
	handleSingleClick(dblclickElmId);
 }
 
 function doDoubleClick(which) {
   var d = new Date();
   dcAt = d.getTime();
   if (savTO != null) {
     clearTimeout( savTO );          // Clear pending Click  
     savTO = null;
   }
	editTabText(dblclickElmId);
 }

/*========================  Double Click Function END ==================================*/
	Droppables.add('transferImg' , {onDrop:function(elt){ transDivAction(elt); }} );

	/*----------------------------------------------------------------------------
	*/
	function handleSingleClick( i ) { 	
		if ( tabsEditMode ) 	{
			tabsEditMode=false ; 
		} else {
			tabAnchorElm = getElmById('tabHrefSpan_' + i );
			document.location=tabAnchorElm.firstChild.nodeValue;
		}
	}

	/*----------------------------------------------------------------------------
	*/
	function popElm( whichBar ) {

		retObj = { label:'', href:''};

		if ( whichBar == barHorizontal ) {
			retObj.label	=  barHorizontal.lastChild.childNodes.item(1).firstChild.nodeValue; 
			retObj.href		=  barHorizontal.lastChild.childNodes.item(2).firstChild.nodeValue;
			barHorizontal.removeChild(whichBar.lastChild);
		} else {
			retObj.label 	= barVertical.firstChild.childNodes.item(1).firstChild.nodeValue;
			retObj.href 	=  barVertical.firstChild.childNodes.item(2).firstChild.nodeValue;
			barVertical.removeChild(whichBar.firstChild);
		}
		return retObj;
	}


	/*----------------------------------------------------------------------------
	*/
	function getTabText( currTabId ) {

		if ( isNaN(currTabId))	{
			theElm = currTabId.childNodes.item(1);
		}	else 	{
			if  (  parseInt(currTabId) <= 0  ) 
				currTabId = selectedTab;
			theElm = getElmById('tabTextSpan_' + currTabId );
		}

		return (theElm.firstChild.nodeValue);
	}

	/*----------------------------------------------------------------------------
	*/
	function setTabText( currTabId , nodeText ) {
		if ( isNaN(currTabId) )	{
			theElm = currTabId.childNodes.item(1);
		}	else 	{
			currTabId = ( parseInt(currTabId) > 0 ) ? currTabId : selectedTab;
			theElm = getElmById('tabTextSpan_' + currTabId );
		}

		theElm.firstChild.nodeValue = nodeText;
	}

	/*----------------------------------------------------------------------------
	*/
	function updateTabValue()	{
		
		enteredText = getElmById('tabInputTextElm');
		//alert('update tab'+enteredText.value);
		
		enteredText.style.color=( enteredText.value.length > tabMaxLength) ? 'green' : 'black';
	}
	
	/*----------------------------------------------------------------------------
	*/
	function mkInputForm( defaultText, horiz )	{	
		divElm  = createElm('div');
		divElm.id = 'tabDivTextElm';
		formElm = createElm('form');
		//formElm.onsubmit = 'return false'; 
		
		if (horiz) { formElm.id='tabFormTextElm'; } else {
			
		formElm.id='tabFormVertTextElm';
		}
		
		formElm.onsubmit = new Function ("evt" , "completeTabTextChange();");
		inputElm = createElm('input');
		inputElm.id='tabInputTextElm';
		inputElm.value = defaultText;
		//inputElm.onchange = new Function ("evt" , "completeTabTextChange()");
		//inputElm.onkeyup= new Function ("evt" , "updateTabValue()");
		inputElm.onblur= new Function ("evt" , "completeTabTextChange()");
		
		formElm.appendChild( inputElm ) ;
		divElm.appendChild( formElm );
		return ( divElm ); 
	}
	/*----------------------------------------------------------------------------
	*/
	function editTabText( tabIdxToChange) {
	//	alert('editidx:'+tabIdxToChange);
		tabsEditMode = true;
		selectedTab = tabIdxToChange;
		tabElm = getElmById('tab_element_'+(tabIdxToChange));
		
		if((tabElm.className=='ll')||(tabElm.className=='llhl')){
			horiz = true;
		//	alert('horiz');
		} else {
			horiz = false;
		//	alert('vert'+tabElm.className);
		}
		
		curTextVal = getTabText( tabIdxToChange );
		//tabSpanElm =  tabElm.childNodes.item(1);
		tabSpanElm =  tabElm;
		
		setTabText( tabIdxToChange , '' );
		
		tabSpanElm.appendChild( mkInputForm( curTextVal, horiz) ) ;

		inputElm = getElmById('tabInputTextElm');
		inputElm.focus();
		inputElm.select();

		//adjustInputDivPos();
	}
	
	
	function processTabStyle(tabIdxToChange){
		selectedTab = tabIdxToChange;
		tabElm = getElmById('tab_element_'+(tabIdxToChange));
		
	}
	

	/*----------------------------------------------------------------------------
	*/
	function processTabValue( tabText ){
		if ( tabText.length>tabMaxLength ) 
			return (tabText.substr(0,tabMaxLength) + "..." );
		else 
			return (tabText);
	}
	/*----------------------------------------------------------------------------
	*/
	function completeTabTextChange()	{
		
		
		
		divElm	 = getElmById('tabDivTextElm');
		formElm  = getElmById('tabFormTextElm');
		inputElm = getElmById('tabInputTextElm');
		
		
	//	alert("sel:"+selectedTab);
		
		tabElm = getElmById('tab_element_'+(selectedTab));
		
		//alert(inputElm.value);
		if ( (inputElm) && ( inputElm.value  == '') ) killTabElm(selectedTab);
		
		//updateTabValue();
		
		//update tab value
		
		//alert(inputElm.value);
		tabElm.childNodes.item(1).firstChild.nodeValue = processTabValue(inputElm.value);
		
		//remove the edit tab 
		
		tabElm.removeChild(divElm);
		
		//tabElm.lastChild.style.display='block';
		
		if ( getHorizontalSpaceLeft() < newTabWidth )	{
			adjustTabOrder();
		}
		
		//not working
		updateTabDataAjax();;
		tabsEditMode = false;
		
		return false;
		
	}

	/*----------------------------------------------------------------------------
	*/
	function getHorizontalSpaceLeft(){
		var sumWidth = 0;
		for ( i=0; i<barHorizontal.childNodes.length ; i++ )	{
			sumWidth += barHorizontal.childNodes.item(i).offsetWidth;
		}
		return (barHorizontal.offsetWidth - sumWidth);
	}
	
	/*----------------------------------------------------------------------------
	*/
	function getElmFromBar( whichBar , whichElm )	{

		switch ( whichElm ) {
			case 0: 	return whichBar.lastChild.id;
			case 1: 	return whichBar.firstChild.id;
			default: 	return whichBar.childNodes(whichElm).id;
		}
	}

	/*----------------------------------------------------------------------------
			Moved tabs between the vertical and the horizontal tab
	*/
	function transferTab( elm ){
		elmLabel = getTabText(elm);
		elmHref  = elm.childNodes.item(2).firstChild.nodeValue;
	
		if ((elm.className=='ll')||(elm.className=='llhl'))	{
			rmDiv 	= barHorizontal;
			isVertBar = true;
		} else {
			rmDiv	= barVertical;
			isVertBar = false;
		}
		tabCreate(elmLabel , elmHref , -1 , isVertBar );
		rmDiv.removeChild(elm);
	}

	/*----------------------------------------------------------------------------
	*/
	function verticalNotEmpty () {
		return ( ( barVertical.firstChild ) && ( barVertical.firstChild.className == 'll2' ) );
	}
/*----------------------------------------------------------------------------
*/
	function adjustTabOrder1()	{
//alert('adjust');
		lastHorElm 		= barHorizontal.lastChild;

		while ( (firstVertElm	= barVertical.firstChild ) && 
			( getHorizontalSpaceLeft() > ( parseInt(newTabWidth) + parseInt(firstVertElm.offsetWidth) ) ) ) {
//			transferTab( firstVertElm );

			moveObj = popElm(barVertical);
			tabCreate( moveObj.label , moveObj.href , -1 , false);
		} 

		while ( getHorizontalSpaceLeft() < newTabWidth ) {
			transferTab( lastHorElm );
		} 
		
		transferDivElm = getElmById('transferDiv');
		transferDivElm.style.display = ( verticalNotEmpty()) ? 'block' : 'none';
	}

	/*----------------------------------------------------------------------------
	*/
	function adjustTabOrder() {
		adjustTabOrder1();
		//adjustTabOrder1();
	}

	/*----------------------------------------------------------------------------
			Transfer divs between vertical and horizontal bar (or vice versa).
	*/
	function transDivAction ( anElm ) {
		elmLabel = getTabText(anElm);
		elmHref  = anElm.childNodes.item(2).firstChild.nodeValue;

		firstCreate 	=	popElm( barVertical );
		secondCreate 	=	popElm( barHorizontal);

		tabCreate( firstCreate.label , firstCreate.href 	, -1 , false);
		tabCreate( secondCreate.label , secondCreate.href ,  -1 , false);
		
		adjustTabOrder();
	}
	/*----------------------------------------------------------------------------
	*/
	function adjustInputDivPos(){
		inputElm = document.getElementById('tabChangeDiv');
		tabElm = getElmById('tab_element_'+(selectedTab));

		offsetTopAdjust = ( tabElm.className == 'll2' ) ? 22 : 3 ;
		offsetLeftAdjust = ( tabElm.className == 'll2' ) ? 37 : 87;
		inputElm.style.display='block';
		inputElm.style.left=(parseInt(tabElm.offsetLeft) + parseInt(offsetLeftAdjust)) + "px";
		inputElm.style.top=(parseInt(tabElm.offsetTop) + parseInt(offsetTopAdjust))+ "px";
	}
	/*----------------------------------------------------------------------------
	*/

	function adjustClassNames(){
		horLength = barHorizontal.childNodes.length;
		for ( i=0 ; i < horLength ; i++ ) {
			barHorizontal.childNodes.className = 'll';
		}
		vertLength = barHorizontal.childNodes.length;
		for ( i=0 ; i < vertLength ; i++ ) {
			barVertical.childNodes.className = 'll2';
		}
	}
/*----------------------------------------------------------------------------
	Creates a new tab element with all sub-elements
		
		tabText		string		tab Label 
		tabUrl		string		URL corresponding to a single click of the tab
		tabId			string		
		isVertBar	boolean		is the tab to be created in the vertical bar
*/
	function tabCreate( tabText , tabUrl , tabId , isVertBar )	{

		var tabListElm;
		var tabElm = document.createElement('div');
		tabText = processTabValue( tabText );
	//	alert('creating tab');
		
	//	barHorizontal = getElmById('horizontalDivId');
	//	barVertical		= getElmById('verticalDivId');
if ( tabUrl == '' )	{ tabUrl = document.location; }

		if ( ( getHorizontalSpaceLeft() < newTabWidth ) || isVertBar ) {
			tabListElm = barVertical;
			tabListElm.style.display= ( isPageLoad ) ? 'none' : 'block';
			tabElm.className = 'll2';
		}	else {
			tabListElm = barHorizontal;
			if ( tabUrl == document.location ) {
			tabElm.className = 'llhl';
			} else {
				//alert(tabUrl+":"+document.location);
				tabElm.className = 'll';
			}
			
		}
		
	
		
		
	

		if ( tabId > -1 ) 	{ curTabId = tabId; }	
		else 								{ curTabId = nextTabId++; }

		tabElmId = 'tab_element_'+(curTabId);
		tabElm.id = tabElmId;

		a = new Function ("evt" , "killTabElm('" + curTabId + "')" );
		leftImgElm	= document.createElement("img");
		leftImgElm.id='leftImg_' + curTabId;
		leftImgElm.className = 'leftImgElm';
		leftImgElm	= document.createElement("img");
		leftImgElm.onclick = a;
		
		//choose left
		if ( tabListElm == barHorizontal ) {
		
			
			if ( tabUrl == document.location ) {
				
				//tabberElm = getElmById('tab_element_1');
				//tabberElm.style.display='block';
				
				
				leftImgElm.src	="../../../../images/tabs/newtab_on_left.gif";
				
			} else {
				
				leftImgElm.src	="../../../../images/tabs/newtab_left.gif";
			}
			
		
		
		} else {
			leftImgElm.src	="../../../../images/tabs/close.gif";
			
		}
		rightImgElm 		= document.createElement("img");
		rightImgElm.className = 'rightImgElm';
		rightImgElm.id='rightImg_' + curTabId;
		
		//choose right
		//alert(curTabId);
		if ( tabListElm == barHorizontal ) {
		
			
		if ( tabUrl == document.location ) {
			
			rightImgElm.src	="../../../../images/tabs/newtab_on_right.gif";	
			
		} else {
					rightImgElm.src	="../../../../images/tabs/newtab_right.gif";	

			
		}
				
		
		
		}	
	
		
		anchorElm = document.createElement('a');
		anchorElm.className = 'tabAnchor';
		anchorElm.id = 'tab_anchor_' + curTabId;
		anchorElm.href= tabUrl;
		anchorElm.style.cursor='pointer';
		anchorElm.innerHTML = '';
		anchorElm.onClick= "return true";
		
		if ( navigator.appName == 'Microsoft Internet Explorer' ) 
			b = new Function ("evt" , "handleTabClick('" + curTabId + "', event );");
		if ( navigator.appName == 'Netscape' )	 
			b = new Function ("evt" , "handleTabClick('" + curTabId + "', evt);");

		tabElm.onclick = b;
		tabElm.ondblclick = b;

		tabTextSpanElm = createElm('span');
		tabTextSpanElm.appendChild(document.createTextNode( tabText ));	
		tabTextSpanElm.className='tabTextSpan';
		tabTextSpanElm.id='tabTextSpan_' + curTabId;

		tabHrefSpanElm = createElm('span');
		tabHrefSpanElm.appendChild(document.createTextNode( tabUrl ));	
		
		if ( tabListElm == barHorizontal ) {
		tabHrefSpanElm.className='tabTextSpan';
		} else {
		tabHrefSpanElm.className='tabVTextSpan';
		}
		
		tabHrefSpanElm.style.display='none';
		tabHrefSpanElm.id='tabHrefSpan_' + curTabId;

		tabElm.appendChild(leftImgElm);	
		tabElm.appendChild(tabTextSpanElm);
		tabElm.appendChild(tabHrefSpanElm);	

		if ( tabListElm == barHorizontal ) { tabElm.appendChild(rightImgElm);	 }

		if ( isVertBar ) {
			tabListElm.insertBefore( tabElm, tabListElm.firstChild);
		}	else {
			tabListElm.appendChild(tabElm);
		}

		//Sortable.destroy('horizontalDivId');
		Sortable.create('horizontalDivId' , {tag: 'div', constraint:'horizontal' , overlap:'horizontal' , containment:['horizontalDivId'],
				onUpdate:function(aa){ updateSortable(); } , 
				onChange:function( empty ){ tabsEditMode=true;} }
		);

		//Sortable.destroy('verticalDivId');
		Sortable.create('verticalDivId' , {tag:'div' , only:'ll2' , constraint:'vertical' , overlap:'vertical',  containment:['verticalDivId'] ,
				onUpdate:function(aa){ updateSortable(); } ,
				onChange:function( empty ){ tabsEditMode=true;} } 
		);
	}

/*----------------------------------------------------------------------------
*/
	function updateSortable ()	{
		tabsEditMode=true;
		adjustTabOrder();
		updateTabDataAjax();;
	}
/*----------------------------------------------------------------------------
*/
	function collectTabData()	{
		horCount = barHorizontal.childNodes.length;
		vertCount = barVertical.childNodes.length;
		var tabDefs ='';	
		tabSep	= ";-;";
		recSep = ";;;";	
		
		for ( i=0 ; i<horCount ; i++ )	{
			tabElm = barHorizontal.childNodes.item(i);
			if (( tabElm.className == 'll' )||(tabElm.className == 'llhl') )	{
				tabDefs += getTabText( tabElm ) + recSep + tabElm.childNodes.item(2).firstChild.nodeValue + tabSep;
			}
		}
		for ( i=0 ; i<vertCount ; i++ )	{
			tabElm = barVertical.childNodes.item(i);
			if ( tabElm.className == 'll2' )	{
				tabDefs += getTabText(tabElm) + recSep + tabElm.childNodes.item(2).firstChild.nodeValue + tabSep;
			}
		}
		return(tabDefs);
	}

/*----------------------------------------------------------------------------
*/
	function killTabElm( elmIdx ) {
				
		elm = document.getElementById( 'tab_element_' + elmIdx );
		parentElm = document.getElementById(elm.parentNode.id);
		parentElm.removeChild(elm);
	
		adjustTabOrder();
		updateTabDataAjax();
	}

/*----------------------------------------------------------------------------
*/
	function	storeTabsAjax() {
		if(oDomDoc.readyState == 4) {
			var xmlparent=oDomDoc.childNodes.item(0).childNodes;
			if ( xmlparent.length>0 ) {
				for ( var i=0; i<xmlparent.length ; i++)	{
				/*
					if ( xmlparent.item(i).tagName == 'record' ) {
						xmlrecordattribs = xmlparent.item(i).attributes;
						sUrl = (xmlrecordattribs.getNamedItem('URL').nodeValue);
						sId = (xmlrecordattribs.getNamedItem('Id').nodeValue);
					}
				*/
					if ( xmlparent.item(i).tagName == 'statistics' ) {
						xmlrecordattribs = xmlparent.item(i).attributes;
						error = (xmlrecordattribs.getNamedItem('error').nodeValue);
						if ( error != '' ) {
							alert("Ajax Call returned Error: " + error);
						}	
					}
				}
			}
		}
	}

/*----------------------------------------------------------------------------
*/
	function updateTabDataAjax() {
		csvOfUrls	= collectTabData();
		oDomDoc 	= Sarissa.getDomDocument();
		oDomDoc.async	= true;
		oDomDoc.onreadystatechange = storeTabsAjax;
		oDomDoc.load("http://www.productdose.com/includes/slidingTabsAjax.php?Url=" + csvOfUrls);
	}

/*----------------------------------------------------------------------------
*/


