//if using this library, make sure a CSS that has the classes used here is included in your page

function createTab(tabLabel,tabClickHandler,tabNum,tabLeft,tabTop,tabDivIdPrefix) {
//todo: allow the CSS class to be defined on parameter -- e.g. tabActive or mmTabActive
//for now, class names are assumed to be tabDivIdPrefix + "Tab" + whatever

	//num, left and top are optional
	if (tabNum != parseInt(tabNum)) tabNum=0;
	if (tabLeft != parseInt(tabLeft)) tabLeft=0;
	if (tabTop != parseInt(tabTop)) tabTop=0;
	if (typeof(tabDivIdPrefix) != "string") tabDivIdPrefix = "";

	var tabHtm = "";

//	tabHtm += "<div id=\"" + tabDivIdPrefix + "Tab" + tabNum + "\" class=\"tab\" style=\"left:  "+ tabLeft +"px; top: "+ tabTop +"px; color:#FFFFFF\" t=0 l=0 onclick=\""+ tabClickHandler +";\" >\n";
	tabHtm += "<div id=\"" + tabDivIdPrefix + "Tab" + tabNum + "\" class=\""+ tabDivIdPrefix +"Tab\" style=\"left:  "+ tabLeft +"px; top: "+ tabTop +"px; color:#FFFFFF\" onclick=\""+ tabClickHandler +";\" >\n";
	tabHtm += "<table cellpadding='0' cellspacing = '0' border='0'>";
	tabHtm += "<tr><td id='"+tabDivIdPrefix+"Tab"+ tabNum +"imgL' class='"+tabDivIdPrefix+"TabActiveLeft'>&nbsp;</td>";
	tabHtm += "<td id='"+tabDivIdPrefix+"Tab"+ tabNum +"C' class='"+tabDivIdPrefix+"TabActiveCenter'><nobr><span id=\""+ tabDivIdPrefix +"Tab"+tabNum+"Label\" class=\""+tabDivIdPrefix+"TabtextActive\">" + tabLabel +"</span></nobr></td>";
	tabHtm += "<td id='"+tabDivIdPrefix+"Tab"+ tabNum +"imgR' class='"+tabDivIdPrefix+"TabActiveRight'>&nbsp;</td>\n";
	tabHtm += "</tr></table></div>\n ";
	
	return tabHtm;
}

function createTabGroup(labelsArray,handlersArray,leftStart,topStart,imgWd,imgHt,tabDivIdPrefix,holderDivWid,createHolderDiv,holderDivName,holderZIdx) {

//todo: allow the CSS class to be defined on parameter
	if (labelsArray.length != handlersArray.length) {
		alert("Error: there must be the same number of labels and handlers.");
	}
	if (leftStart != parseInt(leftStart)) leftStart=0;
	if (topStart != parseInt(topStart)) topStart=0;
	if (typeof(tabDivIdPrefix) != "string") tabDivIdPrefix = "";
/* even if createHolderDiv is not used, holderDivWid might be, so that the tabs will fit appropriately into
a div that already exists */
	if (holderDivWid != parseInt(holderDivWid)) holderDivWid=700;
	if (typeof(createHolderDiv) != "boolean") createHolderDiv = false;
	if (typeof(holderDivName) != "string") holderDivName = "";
	if (holderZIdx != parseInt(holderZIdx)) holderZIdx=0;
	
		
	var tabGroupHtm = "";
	var currLeft = leftStart;
	var currTop = topStart;

	if (createHolderDiv) {
		tabGroupHtm += "<div Id=\""+ holderDivName +"\" style=\"position:relative;  width:"+ holderDivWid +"; z-index:"+ holderZIdx +";\">";
	}

	for (var i = 0; i < labelsArray.length; i++) {
		tabGroupHtm += createTab(labelsArray[i],handlersArray[i],i,currLeft,currTop,tabDivIdPrefix);

		//set up left and top for next button
		currLeft = currLeft + imgWd;
		if (currLeft > holderDivWid) {
			currLeft = leftStart;
			currTop = currTop + imgHt;
		}		
	}
	
	if (createHolderDiv) {
		tabGroupHtm += "</div>";
	}
	return tabGroupHtm;
}

var tabGenerator_trySetTabAgain = null;

function setTabLook(button,classPrefix,state)
{
	clearTimeout(tabGenerator_trySetTabAgain);
	
	if (button) {
		var tabName = button.id;
		document.getElementById(tabName + "imgL").className = classPrefix + "Tab" + state + "Left";
		document.getElementById(tabName + "C").className = classPrefix + "Tab" + state + "Center";
		document.getElementById(tabName + "imgR").className = classPrefix + "Tab" + state + "Right";
		document.getElementById(tabName + "Label").className = classPrefix + "TabText" + state;
	} else {
		tabGenerator_trySetTabAgain = setTimeout("setTabLook(button,\"" + classPrefix+ "\",\"" + state + "\")",50);
		return -1;
	}
}


function setTabLookAll(classPrefix,state)
{
	var currTab;
	var tabIdx = 0;
	while (currTab = document.getElementById(classPrefix + "Tab" + tabIdx)) {
		setTabLook(currTab,classPrefix,state);
		tabIdx++;
	}
}

var tabGenerator_tryArrangeAgain = null;

function arrangeTabs(classPrefix,tabCt,divWd, imgHt) {
//arranges tabs to the top-left of "Tab0" -- do not call until tabs have been written to browser!
clearTimeout(tabGenerator_tryArrangeAgain);

	var tabZero = document.getElementById(classPrefix + "Tab0");
	if (tabZero == null) {
		tabGenerator_tryArrangeAgain = setTimeout("arrangeTabs(\"" + classPrefix+ "\"," + tabCt + "," + divWd+ "," + imgHt + ")",50);
		return -1;
	}
	var currTop = CssUtils_getTop(tabZero);
	var startLeft = CssUtils_getLeft(tabZero);

	for (var i = 1; i < tabCt; i++) {
		currTab = document.getElementById(classPrefix + "Tab" + i);
		prevTab = document.getElementById(classPrefix + "Tab" + (i - 1));
		if (CssUtils_getWidthNum(prevTab) == -1) {
			//width not attainable, try again in 50 ms
			tabGenerator_tryArrangeAgain = setTimeout("arrangeTabs(\"" + classPrefix+ "\"," + tabCt + "," + divWd+ "," + imgHt + ")",50);
			return -1;
		}

		CssUtils_setLeft(currTab,CssUtils_getLeft(prevTab) + CssUtils_getWidthNum(prevTab));
//alert("previous tab is at "+ CssUtils_getLeft(prevTab) +" and is " + CssUtils_getWidthNum(prevTab) + " pixels wide");
		if (CssUtils_getLeft(currTab) + CssUtils_getWidthNum(currTab)> divWd) {
			CssUtils_setLeft(currTab,startLeft);
			currTop += imgHt;
		}
		CssUtils_setTop(currTab,currTop);
	}
}


function getTabFromLabel(labelStr,classPrefix,labelArray) {
//find labelStr in listingDetailLabelArray
	for (var i = 0; i < labelArray.length; i++) {
		if (labelArray[i] == labelStr) {
			return document.getElementById(classPrefix + "Tab" + i);
		}
	}
	return -1;
}





