var tabs = [];
var colors = [];
var bgs = [];
var tabtimer = [];
var tabcount = 0;
var tStart  = null;
var curtab = null;
var timerrun = true;
var bgmode = 0;
var bPaused = false;

function loadTabs(tabc, tabsrc, colsrc, timersrc) {
  bgmode = 0;
  timerrun = true;
  tabcount = tabc;
  colors = colsrc;
  tabtimer = timersrc;
  var html = "<div class='tabwindow'><table  border=0 cellspacing=0 cellpadding=0 vspace=0 class='tabframe'><tr>";
  for(i = 0; i <  tabcount; i++) {
    tabs[i] = tabsrc[i];
    html += "<td><div id='tabbtn" + i + "' class='tabbtn' onclick='stoptimer(); clicktab(" + (i+1) + ")'><a href='#'>" + tabsrc[i] + "</a></div></td>";
  }
  html += "</tr><tr><td colspan='" + tabcount + "'><div id='tabcontent' class='tabcontent'></div></td></tr></table></div>";
  document.getElementById("divTab").innerHTML = html;
  for(i = 0; i < tabcount; i++) {
    document.getElementById("tabbtn" + i).style.backgroundImage = "url(./tab/tab" + (i + 1) + ".gif)";
  }
  clicktab(1);

  secTimer();
}


function loadTabsbg(tabc, tabsrc, bgsrc, timersrc) {
  bgmode = 1;
  timerrun = true;
  tabcount = tabc;
  bgs = bgsrc;
  tabtimer = timersrc;
  var html = "<div class='tabwindow'><table  border=0 cellspacing=0 cellpadding=0 vspace=0 class='tabframe'><tr>";
  for(i = 0; i <  tabcount; i++) {
    tabs[i] = tabsrc[i];
    html += "<td><a href='#'><img border='0' onmouseover='this.src=\"./tab/tab" + (i + 1) + "_over.gif\";' onmouseout='this.src=\"./tab/tab" + (i + 1) + ".gif\";' id='tabbtn" + i + "' src='./tab/tab" + (i + 1) + ".gif' class='tabbtn' onclick='stoptimer(); clicktab(" + (i+1) + ")'></a></div></td>";
  }
  html += "</tr><tr><td colspan='" + tabcount + "'><div id='tabcontent' class='tabcontent'></div></td></tr></table></div>";
  document.getElementById("divTab").innerHTML = html;
/*
  for(i = 0; i < tabcount; i++) {
    document.getElementById("tabbtn" + i).style.backgroundImage = "url(./tab/tab" + (i + 1) + ".gif)";
  }
*/
  clicktab(1);

  secTimer();
}

function clicktab(tab) {
    curtab = tab;
    for(i = 0; i < tabcount; i++) {
      if(i != (tab - 1)) { 
        document.getElementById("tabbtn" + i).className = "tabbtn";

      } else {
        document.getElementById("tabbtn" + i).className = "tabbtn_pressed";
//        document.getElementById("tabbtn" + i).style.backgroundImage = "url(./tab/tab" + (i + 1) + "_pressed.gif)";
      }
    }

    document.getElementById("tabcontent").innerHTML = "<a id='aPause' href='#' style='float: right; width: 60px;' class='Arial12BBlue' onclick='pause_Click();'>Pause&nbsp;&nbsp;</a><br />" + document.getElementById("tab"+ tab).innerHTML;
    if(bgmode == 1) {
      document.getElementById("tabcontent").style.backgroundImage = "url(./tab/images/" + bgs[tab - 1] + ")";
    } else {
      document.getElementById("tabcontent").style.backgroundColor = colors[tab - 1];
    }
}

function pause_Click() {
  if(!bPaused) {
    bPaused = true;
    document.getElementById("aPause").innerHTML = "Play";
  } else {
    bPaused = false;
    document.getElementById("aPause").innerHTML = "Pause";
  }
}

function stoptimer() {
  if(timerrun == true) {
    setTimeout(resumeTimer, 60000);
  }
  timerrun = false;
}

function resumeTimer() {
  timerrun = true; 
  tStart = new Date();
  if(curtab == tabcount) curtab = 0;
  clicktab(curtab + 1);
  secTimer();
}

function secTimer() {
   if(timerrun) {
     if(bPaused) {
       setTimeout("secTimer()", 1000);
       return;
     }
     if(!tStart) tStart   = new Date();

     var   tDate = new Date();
     var   tDiff = tDate.getTime() - tStart.getTime();
     tDate.setTime(tDiff);
     if(tDate.getSeconds() >= tabtimer[curtab - 1]) {
       tStart = new Date();
       if(curtab == tabcount) curtab = 0;
       clicktab(curtab + 1);
     }
     setTimeout("secTimer()", 1000);
  }
}