/*************************************\
|** TabManager
|**
\*************************************/

var tabFlap = new Array();
var activeTab = null;
var wObjOfTab = null;
var tabManagerInitiated = false;

//init array of visible tabs
var visibleTabs = new Array();
for(var ppp = 0; ppp < belenusHome.maxTabsSize; ppp++) {
  visibleTabs[ppp] = -1;
}

// obj functions
//lockable not in use currently
function appendTab(tabName,tabURL,tabCaption,lockable,duplicate,scrolling) {
  var tabIndex = tabFlap.length;
  var adding = true;
  for(Tabs in visibleTabs) {
    if(tabFlap[visibleTabs[Tabs]]) {
      if(tabFlap[visibleTabs[Tabs]][0] == tabName) {
        if(tabFlap[visibleTabs[Tabs]][4] != 0) {
          adding = false;
          if(tabFlap[visibleTabs[Tabs]][4] == 1) {
            wObjOfTab.document.getElementById("tab_"+Tabs).setAttribute("src",tabFlap[visibleTabs[Tabs]][1]);
          }
          showTab(Tabs,wObjOfTab);
          return wObjOfTab.frames["tab_"+Tabs];
        }
      }
    }
  }
  
  if(adding) {
    tabFlap[tabIndex] = new Array();
    tabFlap[tabIndex][99] = false; // the blocked attribute
    tabFlap[tabIndex][0] = tabName;
    tabFlap[tabIndex][1] = tabURL;
    tabFlap[tabIndex][2] = tabCaption; // usedCaption for displaying in the registry
    tabFlap[tabIndex][98] = tabCaption; //orgCaption
    tabFlap[tabIndex][3] = lockable;
    tabFlap[tabIndex][4] = duplicate; // 0 == yes, 1 == false (reload url), 2 == false (don't reload url) 
    tabFlap[tabIndex][5] = scrolling;
    return addTab(tabIndex,wObjOfTab); // return window object of the frame
  }
}

/*
function getCaption(tabIndex,captionKey) {
	if(tabFlap[tabIndex][2].indexOf("shortTabCaption") != -1 && tabFlap[tabIndex][2].indexOf("longTabCaption") != -1 ) {
		if(captionKey == "shortTabCaption") return belenusHome.getQueryField(tabFlap[tabIndex][2],"shortTabCaption");
		else if(captionKey == "longTabCaption") return belenusHome.getQueryField(tabFlap[tabIndex][2],"longTabCaption");
	} else {
		return tabFlap[tabIndex][2]; 
	}
}
*/
function getCaption(cap,captionKey) {
		if(cap.indexOf("shortTabCaption") != -1 && cap.indexOf("longTabCaption") != -1 ) {
			if(captionKey == "shortTabCaption") return belenusHome.getQueryField(cap,"shortTabCaption");
			else if(captionKey == "longTabCaption") return belenusHome.getQueryField(cap,"longTabCaption");
		} else {
			return cap; 
		}
}

function addTab(tabIndex,wObj) {
  var addPos = -1;
  if(eval(visibleTabs.join("+")) == "-"+(belenusHome.maxTabsSize)) startOpenTabAnimation();
  for(i in visibleTabs) {
    if(visibleTabs[i] == -1) {
      addPos = i;
      visibleTabs[i] = tabIndex;
      break;
    }
  }
  if(addPos != -1) {
    if (belenusHome.browserName == "Internet Explorer") {
      var frmProp = wObj.document.createElement("frame");
      frmProp.setAttribute("scrolling", tabFlap[tabIndex][5]);
      frmProp.setAttribute("src",tabFlap[tabIndex][1]);
      //frmProp.setAttribute("blocked","false"); not implemented
      frmProp.setAttribute("tabIndex",tabIndex);
      frmProp.setAttribute("id","tab_"+addPos);
      frmProp.setAttribute("name","tab_"+addPos);
      wObj.document.getElementById("tab_includer").replaceChild(frmProp,wObj.document.getElementById("tab_"+addPos));
    } else {
    	
      wObj.document.getElementById("tab_"+addPos).setAttribute("src",tabFlap[tabIndex][1]);
      wObj.document.getElementById("tab_"+addPos).setAttribute("scrolling",tabFlap[tabIndex][5]);
      wObj.document.getElementById("tab_"+addPos).setAttribute("tabIndex",tabIndex);
      wObj.document.getElementById("tab_"+addPos).setAttribute("id","tab_"+addPos);
      wObj.document.getElementById("tab_"+addPos).setAttribute("name","tab_"+addPos);
    }
    
    showTab(addPos,wObj);
    return wObj.frames['tab_'+addPos]; // return window object of the frame
  } else {
  	var askMaxTabs = confirm(belenusHome.ld['maxTabs_ResetAllSearchResults'])
    if(askMaxTabs != undef && askMaxTabs != false) {
    	belenusHome.TAM.closeAllTabsUnless('basket',belenusHome.wObjOfTab);
    	addTab(tabIndex,wObj);
    }
  }
}

function showTab(tab,wObj) {
	belenusHome.startOpenTabAnimation();
  tab = parseInt(tab);
  activeTab = tab;
  var nFA = "";
  for(var TaB = 0; TaB < belenusHome.maxTabsSize; TaB++) {
    if(TaB == tab) {
      nFA += "*,";
    } else {
      nFA += "0,"
    }
  }  
  nFA = nFA.substring(0,nFA.length-1);
  wObj.document.getElementById("tab_includer").setAttribute("cols",nFA);
  modifyTabsBar(tab,wObj);
  //wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = tabFlap[visibleTabs[tab]][2];
//  wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = getCaption(tabFlap[visibleTabs[tab]][2], "longTabCaption");
	try {
		var xxx = getCaption(tabFlap[visibleTabs[tab]][2], "longTabCaption");
		wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = eval(xxx);
	} catch (e) {
		wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = getCaption(tabFlap[visibleTabs[tab]][2], "longTabCaption");
	}
}

function closeAllTabsUnless(tabName,wObj) {
	for(d in visibleTabs) {
		if(visibleTabs[d] != -1) {
			if(tabFlap[visibleTabs[d]][0] != tabName) closeTab(d,wObj);
		}
	}
}


function closeAllTabs(wObj) {
	var askCloseAllTabs = confirm(belenusHome.ld['reallyCloseAllTabs']);
	if(askCloseAllTabs != undef && askCloseAllTabs != false) {
		for(d in visibleTabs) {
			closeTab(d,wObj);
		}
	}
}

function closeTab(tab,wObj) {
  if(tab != null) {
    var runningIndex = 0;
    var newVisibleTabs = new Array();
    for(tabIndex in visibleTabs) {
      if(tabIndex == tab) {
      	wObj.document.getElementById("tab_"+tab).setAttribute("src",'about:blank');
        visibleTabs[tabIndex] = -1;
        break;
      }
    }
    modifyTabsBar(tab,wObj);
    reorganizeVisibleTabs();
    if(getNextUsefullTab(tab) != null) showTab(getNextUsefullTab(tab),wObj);
    else tabRegEmpty(wObj); // none tab found
  }
  if(getNextUsefullTab(tab) == null) {
    startCloseTabAnimation();
  }
}

function showTabs(tabIndex,wObj) {
  if(tabIndex < belenusHome.maxTabsSize) {
    activeTab = tabIndex;
    var currentSetting = wObj.document.getElementById("tab_includer").getAttribute("cols");
    var nFA = ""; // new frame attribute (the rows attribute)
    for(var TaB = 0; TaB < belenusHome.maxTabsSize; TaB++) {
      if(TaB == tabIndex) {
        nFA += "*,";
      } else {
        nFA += "0,"
      }
    }
    nFA = nFA.substring(0,nFA.length-1);
    wObj.document.getElementById("tab_includer").setAttribute("cols",nFA);
    modifyTabsBar(tabIndex,wObj);
    //wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = tabFlap[tabIndex][2];
    wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = getCaption(tabFlap[tabIndex][2], "longTabCaption");
  }
}


function getNextUsefullTab(currentActiveTab) {
  var found = null;
  if(currentActiveTab == null) {
    for(i in visibleTab) {
      if(visibleTab[i] != -1) {
        found = i;
        break;
      }
    }
    
  } else {
  
    if(currentActiveTab == 0) {
      for(j in visibleTabs) {
        if(visibleTabs[j] != -1) {
          found = j;
          break;
        }
      }
    }
    if(currentActiveTab == visibleTabs.length-1) {
      for(var j = eval(visibleTabs.length-1); j >= 0; j--) {
        if(visibleTabs[j] != -1) {
          found = j;
          break;
        }
      }
    } else { // isn't the first or the last.....
      var firstTryAlright = false;
      for(var k = eval(currentActiveTab-1); k >= 0; k--) {
        if(visibleTabs[k] != -1) {
          found = k;
          firstTryAlright = true;
          break;
        }
      }
      if(!firstTryAlright) {
        for(var b = eval(currentActiveTab+1); b < visibleTabs.length; b++) {
          if(visibleTabs[b] != -1) {
            found = b;
            break;
          }
        }
      }
    }
  }
  return found;
}

function initTabs(wObj) {
  wObjOfTab = wObj;
  var colsProp = "";
  for(k in visibleTabs) colsProp += "0,";
  if(colsProp.length > 0) {
    colsProp = colsProp.substring(0,colsProp.length-3).concat(",*");
    wObj.document.getElementById("tab_includer").setAttribute("cols",colsProp);
    /*
    for(h in visibleTabs) {
      var newFrame = wObj.document.createElement('frame');
      newFrame.setAttribute("src","about:blank");
      newFrame.setAttribute("blocked","false");
      newFrame.setAttribute("scrolling","1");
      newFrame.setAttribute("id","tab_"+h);
      newFrame.setAttribute("name","tab_"+h);
      wObj.document.getElementById("tab_includer").appendChild(newFrame);
    }
    */
  }
  belenusHome.tabManagerInitiated = true;
}

function tabRegEmpty(wObj) {
  /*var newFrame = wObj.document.createElement('frame');
  newFrame.setAttribute("src","empty_tabs_bar.html");
  newFrame.setAttribute("id","tab_0");
  wObj.document.getElementById("tab_includer").setAttribute("cols","0,0,0,0,0,0,*");
  wObj.document.getElementById("tab_includer").appendChild(newFrame);
  wObj.document.getElementById("tab_includer").replaceChild(newFrame,wObj.document.getElementById("tab_0"));
  wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = "&nbsp;";
  */
}

function startCloseTabAnimation() {
  belenusHome.startCloseTabAnimationNow();
}

function startOpenTabAnimation() {
  belenusHome.startOpenTabAnimationNow();
}

function reorganizeVisibleTabs() {
}
function updateCaptions(wObj) {
	var anyTabAvailable = false;
	for(abcd in visibleTabs) if(visibleTabs[abcd] != "-1") { anyTabAvailable=true;break; }
	if(anyTabAvailable) {
	  modifyTabsBar(activeTab,wObj);
		try {
			var xxx = getCaption(tabFlap[visibleTabs[activeTab]][2], "longTabCaption");
			wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = eval(xxx);
		} catch (e) {
			wObj.frames.caption_bar.document.getElementById("caption_space").innerHTML = getCaption(tabFlap[visibleTabs[activeTab]][2], "longTabCaption");
		}
	}
}


// the obj
function TabManager() {
  this.getCaption = getCaption;
  this.appendTab = appendTab;
  this.closeTab = closeTab;
  this.closeAllTabs = closeAllTabs;
  this.closeAllTabsUnless = closeAllTabsUnless;
  this.showTab = showTab;
  this.updateCaptions = updateCaptions;
}
// make the object... init the WindowManager.....
belenusHome.TAM = new TabManager();
