var LB3_CURRENT = 0;

function lb3_tab_create(conf) {

    var tabs = document.createElement("div");
    tabs.id = "lb3_tab_container";

    var length = conf.length;
    var width = 292;
    if (length >= 2) {
        width = Math.floor(316 / length) - 24;
    }

    var div = null;

    div = document.createElement("div");
    div.className = "lb3_tab_first";
    tabs.appendChild(div);

    for (var i=0; i<length; i++) {

        div = document.createElement("div");
        div.className = "lb3_tab_center";
        div.style.width = width+"px";
        div.innerHTML = conf[i].title;
        div.onclick = new Function("lb3_tab_select("+i+");");
        tabs.appendChild(div);

        if (i < length - 1) {
            div = document.createElement("div");
            div.className = "lb3_tab_separator";
            tabs.appendChild(div);
        }
    }

    div = document.createElement("div");
    div.className = "lb3_tab_last";
    tabs.appendChild(div);

    document.getElementById("livebox_tabs").appendChild(tabs);
}

function lb3_tab_select(index) {

    LB3_CURRENT = index;
    lb3_load();

    var loading = "<div style='margin: 10px; padding: 5px; text-align: center; font-size: 10px; font-weight: bold;'>"+(LB3_LANG == "de" ? "Lade..." : "Loading...")+"</div>";
    document.getElementById("livebox_body").innerHTML = loading;


    var tabs = document.getElementById("lb3_tab_container");

    var length = (tabs.childNodes.length - 1) / 2;
    for (var i=0; i<length; i++) {
        tabs.childNodes[i*2+1].className = "lb3_tab_center";
        if (i<length-1) {
            tabs.childNodes[i*2+2].className = "lb3_tab_separator";
        }
        tabs.childNodes[0].className = "lb3_tab_first";
        tabs.childNodes[length*2].className = "lb3_tab_last";
    }
    
    tabs.childNodes[index*2+1].className = "lb3_tab_center_selected";

    if (index > 0) {
        tabs.childNodes[index*2].className = "lb3_tab_separator_selected2";
    } else {
        tabs.childNodes[index*2].className = "lb3_tab_first_selected";
    }

    if (index<length-1) {
        tabs.childNodes[index*2+2].className = "lb3_tab_separator_selected";
    }
    if (index == length-1) {
        tabs.childNodes[length*2].className = "lb3_tab_last_selected";
    }
}

function lb3_load() {
    var league = LB3_LEAGUES[LB3_CURRENT]['league'];
    var url = "/livebox3/modules/frontend/ajax/load_tab.php?lang="+LB3_LANG+"&league="+league+"&rand="+(new Date().toString());
    ajax_send(url);
}

function lb3_update(data) {
    document.getElementById("livebox_body").innerHTML = data;
        
    if (typeof removeToolTips == 'function') {
      //removeToolTips();
      addToolTips();
    }
    
    //remove the old tooltips
    /*window.addEvent('domready', function() {
      if ($('.bigTipElement') != null) {
        if ($$('.bigTipElement').length > 0) {
          $$('.bigTipElement').each(function(el) {el.dispose()});
        }
      }
      if ($('.smallTipElement') != null) {
        if ($$('.smallTipElement').length > 0) {
          $$('.smallTipElement').each(function(el) {el.dispose()});
        }
      }
      //add small tooltips
      var smallTips = new Tips('.smallTip', {
        className: "smallTipElement",
        offset: {x: -90, y: -10}
      });
      //add big tooltips
      var bigTips = new Tips('.bigTip', {
        className: "bigTipElement",
        offset: {x: -65, y: 25}
      });
      //mouseovers
      if ($('.shiftBackground') != null) {
        if ($$('.shiftBackground').length > 0) {
          $$('.shiftBackground').each(function(el) {
            el.addEvents({
              'mouseover' : function(){
                el.setStyle('backgroundPosition', '0px -32px');
              },
              'mouseout'  : function(){
                el.setStyle('backgroundPosition', '0px 0px');
              }
            });
          });
        }
      }
      //for the info button
      if ($('.shiftBackgroundInfo') != null) {
        if ($$('.shiftBackgroundInfo').length > 0) {
          $$('.shiftBackgroundInfo').each(function(el) {
            el.addEvents({
              'mouseover' : function(){
                el.setStyle('backgroundPosition', '100% -15px');
              },
              'mouseout'  : function(){
                el.setStyle('backgroundPosition', '100% 0px');
              }
            });
          });
        }
      }
      
    });*/
}

function lb3_init() {
    lb3_tab_create(LB3_LEAGUES);
    var i = 0;
    for (var key in LB3_LEAGUES) {
        if (LB3_LEAGUES[key].league == LB3_DEFAULT) {
            lb3_tab_select(i);
	}
	i++;
    }
    window.setInterval("lb3_load()", 60000+Math.round(Math.random()*30000));
}

lb3_init();

