var auctions = new Array();
pause = 0;
pausecounter = 0;
timeDiff = 1268651332000 - new Date().getTime();

last_ad_time = new Date().getTime();

var xmlHttp = null;
if(typeof XMLHttpRequest != 'undefined')
{
  xmlHttp = new XMLHttpRequest();
}
if(!xmlHttp)
{
  try
  {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e)
  {
    try
    {
      xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e)
    {
      xmlHttp  = null;
    }
  }
}

function requestAuctionData()
{
  if(xmlHttp)
  {
    var ids = '';
    for(var auctionid in auctions)
    {
      ids = ids + auctionid;
      if(auctions[auctionid]['maxbid'] != null && auctions[auctionid]['maxbid'] != auctions[auctionid]['next_bid_step'])
        ids = ids + '(' + auctions[auctionid]['maxbid'] + ')';
      ids = ids + ',';
    }
    var request = '/js/ajax_data.xml?auctions='+ids+'';
    if(pause == '0' && new Date().getTime() - last_ad_time > 10000)
    {
    	last_ad_time = new Date().getTime();
    	request += '&newad=1';
    }
    xmlHttp.open('GET', request, true);
    xmlHttp.onreadystatechange = function()
      {
        if(xmlHttp.readyState == 4)
        {
          if(xmlHttp.status == 200)
            parseAuctionData();
          setTimeout("requestAuctionData()", 500);
        }
      };
    xmlHttp.send(null);
  }
}

function parseAuctionData()
{
  var xml = xmlHttp.responseXML;
//alert(xmlHttp.responseText);
//alert(xmlHttp.getAllResponseHeaders());
//alert(xml.getElementsByTagName('curTime')[0]);
  var curTime = xml.getElementsByTagName('curTime')[0].firstChild.data;
  timeDiff = curTime - new Date().getTime();
  if(pause != xml.getElementsByTagName('pause')[0].firstChild.data)
  {
    pause = xml.getElementsByTagName('pause')[0].firstChild.data;
    if(document.getElementById('rotationsbox'))
      document.getElementById('rotationsbox').style.display = (pause == '1' ? 'none' : 'block');
    if(document.getElementById('pausebox'))
      document.getElementById('pausebox').style.display = (pause == '1' ? 'block' : 'none');
  }
  var xmlAuctions = xml.getElementsByTagName('auction');
  if(xmlAuctions)
    for(var i = 0; i < xmlAuctions.length; i++)
    {
      var xmlAuction = xmlAuctions[i];
      var auctionid = xmlAuction.getElementsByTagName('id')[0].firstChild.data;
      for(var i2 = 0; i2 < xmlAuction.childNodes.length; i2++)
        if(xmlAuction.childNodes[i2].firstChild)
          auctions[auctionid][xmlAuction.childNodes[i2].nodeName] = xmlAuction.childNodes[i2].firstChild.data;
      if(auctions[auctionid]['remaining'])
        auctions[auctionid]['endtime'] = 1 * curTime + 1 * auctions[auctionid]['remaining'];
      var xmlGebote = xmlAuction.getElementsByTagName('gebot');
      if(xmlGebote)
      {
        var gebote = new Array();
        var geboteZ = 0;
        var geboteCount = xmlGebote.length;
        for(var i3 = 0; i3 < geboteCount; i3++)
        {
          var xmlGebot = xmlGebote[i3];
          if(xmlGebot.childNodes[0].firstChild && xmlGebot.getElementsByTagName('gebot'))
          {
            var gebot = xmlGebot.getElementsByTagName('gebot')[0].firstChild.data;
            var bieter = xmlGebot.getElementsByTagName('bieter')[0].firstChild.data;
            var typ = xmlGebot.getElementsByTagName('typ')[0].firstChild.data;
            gebote[geboteZ] = new Array();
            gebote[geboteZ]['gebot'] = gebot;
            gebote[geboteZ]['bieter'] = bieter;
            gebote[geboteZ]['typ'] = typ;
            geboteZ++;
          }
        }
        if(geboteZ > 0)
          for(var g = 9; g >= geboteZ; g--)
          {
            moveAuctionData(auctionid, 'gebote_'+(g-geboteZ)+'_gebot', 'gebote_'+g+'_gebot');
            moveAuctionData(auctionid, 'gebote_'+(g-geboteZ)+'_bieter', 'gebote_'+g+'_bieter');
            moveAuctionData(auctionid, 'gebote_'+(g-geboteZ)+'_typ', 'gebote_'+g+'_typ');
          }
        for(var g = 0; g < geboteZ; g++)
        {
          setAuctionData(auctionid, 'gebote_'+g+'_gebot', gebote[g]['gebot']+'&nbsp;&euro;');
          setAuctionData(auctionid, 'gebote_'+g+'_bieter', gebote[g]['bieter']);
          setAuctionData(auctionid, 'gebote_'+g+'_typ', gebote[g]['typ']);
        }
      }
    }
  if(xml.getElementsByTagName('kontostand')[0] && document.getElementById('kontostand'))
    document.getElementById('kontostand').innerHTML = xml.getElementsByTagName('kontostand')[0].firstChild.data;
  if(xml.getElementsByTagName('ad')[0] && document.getElementById('rotationsbox'))
    document.getElementById('rotationsbox').innerHTML = xml.getElementsByTagName('ad')[0].firstChild.data;
}

  function refreshClock()
  {
    var currentTime = new Date();
    currentTime.setTime(currentTime.getTime() + timeDiff);
    if(document.getElementById('clock'))
    {
      var hours = currentTime.getHours();
			var minutes = currentTime.getMinutes();
      var seconds = currentTime.getSeconds();
      if(hours < 10) { hours = '0' + hours; }
      if(minutes < 10) { minutes = '0' + minutes; }
      if(seconds < 10) { seconds = '0' + seconds; }
      document.getElementById('clock').innerHTML = hours+':'+minutes+':'+seconds;
    }
    setTimeout("refreshClock()", 1000);
  }
  
  setTimeout("refreshClock()", 1000);

  function refreshEndTimes()
  {
	  var currentTime = new Date();
    currentTime.setTime(currentTime.getTime() + timeDiff);
  	pausecounter++;
    for(var auctionid in auctions)
    {
      var auction = auctions[auctionid];

      if(auction['endtime'] != null && auction['status'] == '1')
      {
        var time_left = 0;
        if(pause)
        {
          time_left = auction['remaining'];
        }
        else
        {
          time_left = auction['endtime'] - currentTime.getTime();
        }
        if(time_left < 0)
          time_left = 0;
        var seconds = Math.floor(time_left / 1000);
        var minutes = Math.floor(seconds / 60 % 60);
        var hours = Math.floor(seconds / 60 / 60);
        seconds = seconds % 60;
        if(seconds < 10) seconds = '0'+seconds;
        if(minutes < 10) minutes = '0'+minutes;
        if(hours < 10) hours = '0'+hours;
        if(pause == '0' || pausecounter % 4 >= 2)
	        setAuctionData(auctionid, 'countdown', hours+':'+minutes+':'+seconds);
	      else
	        setAuctionData(auctionid, 'countdown', 'PAUSE');
        if(time_left <= 30000)
        {
          highlight_countdown(auctionid);
        }
        else
          unhighlight_countdown(auctionid);
      }

      if(auction['bidder'] != null)
      {
        showAuctionData(auctionid, 'bidder', true);
        setAuctionData(auctionid, 'bidder_name', auction['bidder']);
      }

      if(auction['ueberboten'] != null)
        showAuctionData(auctionid, 'ueberboten', auction['ueberboten'] == '1');

      if(auction['maxbid'] != null)
      {
        if(document.getElementById('auction_'+auctionid+'_preis') && document.getElementById('auction_'+auctionid+'_preis').innerHTML != auction['maxbid']
        || document.getElementById('auction2_'+auctionid+'_preis') && document.getElementById('auction2_'+auctionid+'_preis').innerHTML != auction['maxbid'])
          highlight_price(auctionid);

        setAuctionData(auctionid, 'preis', auction['maxbid']);
        setAuctionData(auctionid, 'preis2', auction['maxbid']);
      }

      if(auction['bidcosts'] != null)
        setAuctionData(auctionid, 'gebotepreis', auction['bidcosts']);

      if(auction['ersparnis'] != null)
        setAuctionData(auctionid, 'ersparnis', auction['ersparnis']);

      if(auction['ersparnis_prozent'] != null)
        setAuctionData(auctionid, 'ersparnis_prozent', auction['ersparnis_prozent']);

      if(auction['minprice_info'] != null)
        showAuctionData(auctionid, 'minprice_info', auction['minprice_info']);

      if(auction['status'] != null)
      {
        showAuctionData(auctionid, 'aktiv', auction['status'] == '1');
        showAuctionData(auctionid, 'beendet', auction['status'] == 'f' && auction['is_winner'] != '1');
        showAuctionData(auctionid, 'gewonnen', auction['status'] == 'f' && auction['is_winner'] == '1');

        showAuctionData(auctionid, 'bieten', auction['status'] == '1' && auction['is_max_bidder'] != '1');
        showAuctionData(auctionid, 'maxbidder', auction['status'] == '1' && auction['is_max_bidder'] == '1');
      }

      if(auction['status'] == 'f' && auction['winner'] != null)
      {
	 	  	setAuctionData(auctionid, 'image_verkauft_box', '<img src="/images/verkauft.png" width="150" height="70" border="0" />');
	 	  	setAuctionData(auctionid, 'image_mini_row_verkauft_box', '<img src="/images/verkauft.png" width="55" height="26" border="0" />');
	 	  	setAuctionData(auctionid, 'image_row_verkauft_box', '<img src="/images/verkauft.png" width="90" height="42" border="0" />');
	 	  	if(auction['is_winner'])
	 	  		setAuctionData(auctionid, 'bidder_pre', 'Herzlichen&nbsp;Glückwunsch,');
	 	  	if(auction['prize_confirmation_needed'])
	 	  		setAuctionData(auctionid, 'bidder_post', '<div class="auction_gewinnbestaetigen">[ <a href="/gewinnbestaetigung.php?auctionid='+auctionid+'"><img src="/images/ok.gif" alt="" border="0" /> Gewinn best&auml;tigen</a> ]</div>');
	 	  	if(auction['ersparnis_prozent'])
		 	  	setAuctionData(auctionid, 'price_post', '<div class="auction_price_post"><div class="auction_row_ersparnis">'+auction['ersparnis_prozent']+'% gespart</div></div>');
	 	  	showAuctionData(auctionid, 'countdown', false);
	 	  	setAuctionData(auctionid, 'verkauft', '<div class="auction_verkauft_fuer">verkauft f&uuml;r</div>');
      }
      if(auction['status'] == 'f')
      	refreshAuctionLink(auctionid, auction['url']);

      if(auction['next_bid_step'] != null && document.getElementById('auction_'+auctionid+'_bid_field'))
      {
        document.getElementById('auction_'+auctionid+'_bid_field').value = auction['next_bid_step'];
      }

    }
    setTimeout("refreshEndTimes()", 1000);
  }
  
  function refreshAuctionLink(auctionid, url)
  {
    if(document.getElementById('auction_'+auctionid+'_link_1'))
      document.getElementById('auction_'+auctionid+'_link_1').href = url;
    if(document.getElementById('auction_'+auctionid+'_link_2'))
      document.getElementById('auction_'+auctionid+'_link_2').href = url;
    if(document.getElementById('auction_'+auctionid+'_link_3'))
      document.getElementById('auction_'+auctionid+'_link_3').href = url;
  }

  function setAuctionData(auctionid, elementname, value)
  {
    if(document.getElementById('auction_'+auctionid+'_'+elementname))
      document.getElementById('auction_'+auctionid+'_'+elementname).innerHTML = value;
    if(document.getElementById('auction2_'+auctionid+'_'+elementname))
      document.getElementById('auction2_'+auctionid+'_'+elementname).innerHTML = value;
  }

  function moveAuctionData(auctionid, from, to)
  {
    if(document.getElementById('auction_'+auctionid+'_'+from) && document.getElementById('auction_'+auctionid+'_'+to))
      document.getElementById('auction_'+auctionid+'_'+to).innerHTML = document.getElementById('auction_'+auctionid+'_'+from).innerHTML;
    if(document.getElementById('auction2_'+auctionid+'_'+from) && document.getElementById('auction_'+auctionid+'_'+to))
      document.getElementById('auction2_'+auctionid+'_'+to).innerHTML = document.getElementById('auction_'+auctionid+'_'+from).innerHTML;
  }

  function showAuctionData(auctionid, elementname, value)
  {
    if(document.getElementById('auction_'+auctionid+'_'+elementname))
      document.getElementById('auction_'+auctionid+'_'+elementname).style.display = (value ? 'block' : 'none');
    if(document.getElementById('auction2_'+auctionid+'_'+elementname))
      document.getElementById('auction2_'+auctionid+'_'+elementname).style.display = (value ? 'block' : 'none');
  }

  function highlight_countdown(auctionid)
  {
    if(document.getElementById('auction_'+auctionid+'_countdown'))
      document.getElementById('auction_'+auctionid+'_countdown').className = 'auction_countdown_hl';
    if(document.getElementById('auction2_'+auctionid+'_countdown'))
      document.getElementById('auction2_'+auctionid+'_countdown').className = 'auction_countdown_hl';
  }

  function unhighlight_countdown(auctionid)
  {
    if(document.getElementById('auction_'+auctionid+'_countdown'))
      document.getElementById('auction_'+auctionid+'_countdown').className = '';
    if(document.getElementById('auction2_'+auctionid+'_countdown'))
      document.getElementById('auction2_'+auctionid+'_countdown').className = '';
  }

  function highlight_price(auctionid)
  {
    if(!auctions[auctionid]['highlight'])
      auctions[auctionid]['highlight'] = 0;
    auctions[auctionid]['highlight']++;
    if(document.getElementById('auction_'+auctionid+'_preis_text'))
      document.getElementById('auction_'+auctionid+'_preis_text').className = 'auction_box_price_hl';
    if(document.getElementById('auction2_'+auctionid+'_preis_text'))
      document.getElementById('auction2_'+auctionid+'_preis_text').className = 'auction_box_price_hl';
    setTimeout("unhighlight_price("+auctionid+")", 500);
  }

  function unhighlight_price(auctionid)
  {
    if(--auctions[auctionid]['highlight'] == 0)
    {
      if(document.getElementById('auction_'+auctionid+'_preis_text'))
        document.getElementById('auction_'+auctionid+'_preis_text').className = 'auction_box_price';
      if(document.getElementById('auction2_'+auctionid+'_preis_text'))
        document.getElementById('auction2_'+auctionid+'_preis_text').className = 'auction_box_price';
    }
  }
  
  function switchtopauction(auctionid)
  {
    if(!document.getElementById('auction_top_'+auctionid))
      return false;
    for(var i=1; i<=5; i++)
    {
      if(document.getElementById('top_auction_link_'+i))
        document.getElementById('top_auction_link_'+i).className = 'top_auction_link_inactive';
      if(document.getElementById('auction_top_'+i))
        document.getElementById('auction_top_'+i).style.display = 'none';
    }
    if(document.getElementById('top_auction_link_'+auctionid))
      document.getElementById('top_auction_link_'+auctionid).className = 'top_auction_link_active';
    if(document.getElementById('auction_top_'+auctionid))
      document.getElementById('auction_top_'+auctionid).style.display = 'block';
  }
  
  var mouseOverCatImg = false;
  var mouseOverCatList = false;

  function showCategories(catImg)
  {
 		if(catImg)
			mouseOverCatImg = true;
 		else
 	  	mouseOverCatList = true;
 	  if(document.getElementById('catlist'))
			document.getElementById('catlist').style.display = 'block';
  }

  function hideCategories(catImg)
	{
 		if(catImg)
			mouseOverCatImg = false;
 		else
 	  	mouseOverCatList = false;
		if(!mouseOverCatImg && !mouseOverCatList && document.getElementById('catlist'))
			document.getElementById('catlist').style.display = 'none';
  }
  
  function showHide(box)
  {
 	  if(document.getElementById(box))
 	    if(document.getElementById(box).style.display == 'block')
  			document.getElementById(box).style.display = 'none';
  		else
  			document.getElementById(box).style.display = 'block';
  }