var winnerXSL = null;
var selectedUVmagning = null;
var currentIDX = -1;

function loadWeekWinner()
{
  new net.ContentLoader('weekwinner/functions/getWeekWinner.php?&date ='+new Date(),parseWeekWinner);
}
function parseWeekWinner()
{
  var xmlDoc = this.req.responseXML;
  var el = document.getElementById('LayerWeek');
  if (winnerXSL != null)
  {
    if (window.ActiveXObject)
    {
      el.innerHTML = xmlDoc.transformNode(winnerXSL);
    // alert(el.innerHTML);
    }
    else
    {
      var xsltProcessor = new XSLTProcessor();
      xsltProcessor.importStylesheet(winnerXSL);
      var fragment = xsltProcessor.transformToFragment(xmlDoc,document);

      el.innerHTML='';
      el.appendChild(fragment);
    //  alert(el.innerHTML);

    }
  }
}

function showWinner(idx)
{
  closeFunction =  closeShowAll;
  
  if (idx)
  {
    currentIDX = idx;
    // url,onload,onerror,method,params,contentType,notify
    //  alert("calling notify");
    new net.ContentLoader('weekwinner/functions/getWeekWinnerOrder.php?eid='+idx+'&date ='+new Date(),parseWeekWinnerAllEvent);
  }
  else
  {
    new net.ContentLoader('weekwinner/functions/getWeekWinnerOrder.php?&date ='+new Date(),parseWeekWinnerAllEvent);
  }

  var el = document.getElementById("LayerTastingShowAll");
  //alert(el);
  if (el != null)
  {
    var vis = el.style.visibility;
    //if (vis == "hidden")
    {
      var elheight=el.offsetHeight;
      el.innerHTML="<center><img class=\"va\" src=\"images/wait26trans.gif\" style=\"position:relative;top:"+((elheight/2)-30)+"px;\" /></center>";
      el.style.visibility="visible";
      el.style.zIndex = "10";
    }
  //else
  // {
  //  el.style.visibility="hidden";
  //}
  }
  
  setBlackShade();
  
}

function parseWeekWinnerAllEvent()
{
  
  var xmlDoc = this.req.responseText;
  var el = document.getElementById('LayerTastingShowAll');
  //alert(tastingXSL);
  if (el != null)
  {
    el.innerHTML = xmlDoc;
  }
  if (currentIDX != -1)
  {
    var eid = currentIDX;
    currentIDX = -1;
    //  alert("eid:"+eid);
    var ttable = document.getElementById('UVScroller');
    if (ttable != null)
    {
      var currentIdx = -1;

      var trs = ttable.childNodes;
      //    alert(currentIdx);
      for(i=0 ; i < trs.length ; i++)
      {
        var row = trs[i];
        //      alert("nodeType:"+row.nodeType);
        if (row.nodeType == 1)
        {
          //       alert(row.getAttribute("event_id"));
          if (row.getAttribute("event_id") == eid)
          {
            currentIdx = i;
          }
        }
      }
      // alert(currentIdx);
      if (currentIdx == -1)
      {
        showWinnerAllEvent(eid);

      }
      else
      {
        var selEl = trs[currentIdx];
        //alert(selEl);
        if (selEl != null)
        {
          // alert("event_id:"+selEl.getAttribute("id"));
          showWinnerAllEvent(selEl.getAttribute("id"));
        }
      }
    }
  }
}

function showWinnerAllEvent(idx)
{
  //alert(idx);
  var el = document.getElementById("LayerTastingShowAllEvent");
  if (el != null)
  {
    // var vis = el.style.visibility;
    el.style.left="434px";
    var elheight=el.offsetHeight;
      el.innerHTML="<center><img class=\"va\" src=\"images/wait26trans.gif\" style=\"position:relative;top:"+((elheight/2)-30)+"px;\" /></center>";
      el.style.visibility="visible";
    el.style.zIndex = "10";
  }
  //alert(idx.startsWith("event_")+"_"+idx);
  if (selectedUVmagning != null && selectedUVmagning.id.startsWith("eventUV_"))
  {
    selectedUVmagning.style.background="white";
  }
  var tdEl= document.getElementById(idx);
  if (tdEl != null)
  {
    if (idx.startsWith("eventUV_"))
    {
      tdEl.style.background="#CCCCCC";
    }
    //alert('weekwinner/functions/getWinnerEvent2.php?eventid='+tdEl.getAttribute("event_id")+'&date ='+new Date());
    new net.ContentLoader('weekwinner/functions/getWinnerEvent2.php?eventid='+tdEl.getAttribute("event_id")+'&date ='+new Date(),parseWinnerAllEventShow);

  }

  selectedUVmagning = tdEl;
}

function parseWinnerAllEventShow()
{
  var xmlDoc = this.req.responseText;
  // alert(xmlDoc);
  var el = document.getElementById('LayerTastingShowAllEvent');
  el.innerHTML = xmlDoc;
}

function showAllWinnerNext(startoffset,maxevents)
{
  //alert(startoffset);
  if (startoffset >= 0 && startoffset <= maxevents)
  {
    new net.ContentLoader('weekwinner/functions/getWeekWinnerOrder.php?startoffset='+startoffset+'&date ='+new Date(),parseWeekWinnerAllEvent);
  }
}



function selectNextWinnerShowAll(direction)
{
  //alert("selectNextTastingShowAll");
  if (selectedUVmagning != null)
  {

    //var children = document.getElementById('TastingScroller-Container').childNodes;
    //alert(children.length);

    var ttable = document.getElementById('UVScroller');
    if (ttable != null)
    {
      var currentIdx = -1;

      var trs = ttable.childNodes;
      for(i=0 ; i < trs.length ; i++)
      {
        var row = trs[i];
        if (row.nodeType == 1)
        {
          if (row.id == selectedUVmagning.id)
          {
            currentIdx = i;
          }
        }
      }
      if (currentIdx >= 0)
      {
        
        {
          var selEl = trs[currentIdx+direction];
          while (selEl != null && selEl.nodeType != 1)
          {
            currentIdx+=direction;
            selEl = trs[currentIdx+direction];
          }
          if (selEl != null)
          {
            //alert(selEl.id+"_"+selEl.getAttribute("event_id"));
            if (selEl.getAttribute("event_id") != null)
            {
              this.showWinnerAllEvent(selEl.id);
            }
            else
            {
              this.showWinnerAllEvent("eventUV_0");
            }
          }
        }
      }
      else if (currentIdx == -1 && direction>0)
      {
        var selID = selectedUVmagning.id;
        var iCount = selID.substring(selID.indexOf("_")+1);
        //alert("event_"+(parseInt(iCount)+1));
        if (parseInt(iCount) > ttable.childNodes.length)
        {
          this.showWinnerAllEvent("eventUV_0");
        }
        else{
          this.showWinnerAllEvent("eventUV_"+(parseInt(iCount)+1));
        }
      }

      else if (currentIdx == -1 && direction < 0)
      {
        this.showWinnerAllEvent("eventUV_0");
      }

    }
  }
}

function loadWinnerXSL(stylesheet)
{
  var xsl = null;
  if(window.ActiveXObject)
  {
    

    // Load XSL
    xsl = new ActiveXObject("MSXML2.DOMDocument");
    xsl.async = false
    xsl.load(stylesheet)
  }
  if(document.implementation && document.implementation.createDocument)
  {

    // load the xslt file
    var myXMLHTTPRequest = new XMLHttpRequest();
    myXMLHTTPRequest.open("GET", stylesheet, false);
    myXMLHTTPRequest.send(null);

    xsl = myXMLHTTPRequest.responseXML;
  }
  winnerXSL = xsl;
  loadWeekWinner();
  //return xsl;
}



