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

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]['current_price'] != null)
        ids = ids + '(' + auctions[auctionid]['current_price'] + ')';
      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;
  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;
      if(auctions[auctionid])
      {
	      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 + localTimeDiff);
  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')
    {
      auction['time_left'] = 0;
      if(pause)
      {
        auction['time_left'] = auction['remaining'];
      }
      else
      {
        auction['time_left'] = auction['endtime'] - currentTime.getTime();
      }
      if(auction['time_left'] < 0)
        auction['time_left'] = 0;
      if(auction['is_blocked'] == '1' && auction['block_endtime'] != null)
      {
      	auction['time_left'] = auction['remaining'];
      	var block_time_left = auction['block_endtime'] - currentTime.getTime();
      	if(block_time_left < 0)
      		block_time_left = 0;
      	var seconds = Math.floor(block_time_left / 1000);
      	setAuctionData(auctionid, 'block_countdown', seconds + ' Sekunde' + (seconds == 1 ? '' : 'n'));
      }

      if(pause == '0' || pausecounter % 4 >= 2)
	  		setAuctionData(auctionid, 'countdown', countdown_readable(auction['time_left']));
			else
    	  setAuctionData(auctionid, 'countdown', 'PAUSE');
	  }
	}
	setTimeout("refreshEndTimes()", 1000);
}

function refreshAuctions()
{
	for(var auctionid in auctions)
  {
    var auction = auctions[auctionid];

    if(auction['endtime'] != null && auction['status'] == '1')
    {
	  	if(auction['is_blocked'] != null)
	     	showAuctionData(auctionid, 'blocked', auction['is_blocked'] == '1');
     	showAuctionData(auctionid, 'preis_text_box', auction['is_blocked'] != '1' || auction['is_blocked_for_me'] != '0');
     	showAuctionData(auctionid, 'preis_text_row', auction['is_blocked'] != '1' || auction['is_blocked_for_me'] != '0');
     	if(auction['is_blocked'] == '0')
     	{
	    	formatPrice(auctionid, '');
	    }
     	else if(auction['is_blocked_for_me'] == '1')
     	{
	    	formatPrice(auctionid, 'aktiv');
     	}
     	showAuctionData(auctionid, 'details_button', auction['is_blocked_for_me'] != '1');
     	showAuctionData(auctionid, 'blocked_for_me', auction['is_blocked_for_me'] == '1');
     	showAuctionData(auctionid, 'blocked_for_me2', auction['is_blocked_for_me'] == '1');
      if(auction['is_blocked'] == '1')
      {
      	block_countdown(auctionid);
      }
      else if(auction['time_left'] <= 30000)
      {
        highlight_countdown(auctionid);
      }
      else
        unhighlight_countdown(auctionid);
    }
    else if(auction['endtime'] != null && auction['status'] != null)
    {
     	showAuctionData(auctionid, 'details_button', true);
     	showAuctionData(auctionid, 'preis_text_box', true);
     	showAuctionData(auctionid, 'preis_text_row', true);
     	showAuctionData(auctionid, 'blocked_for_me', false);
     	showAuctionData(auctionid, 'blocked_for_me2', false);
     	showAuctionData(auctionid, 'blocked', false);
    }

		if(auction['should_display_maxbidder'] != null)
			showAuctionData(auctionid, 'bidder', auction['should_display_maxbidder'] == '1');
    if(auction['should_display_maxbidder'] == '1')
      setAuctionData(auctionid, 'bidder_name', auction['bidder']);

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

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

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

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

			if(auction['current_price'] == '???,??')
			{
				if(document.getElementById('auction_'+auctionid+'_preis'))
					document.getElementById('auction_'+auctionid+'_preis').style.textDecoration = 'blink';
				if(document.getElementById('auction2_'+auctionid+'_preis'))
					document.getElementById('auction2_'+auctionid+'_preis').style.textDecoration = 'blink';
			}
			else
			{
				if(document.getElementById('auction_'+auctionid+'_preis'))
					document.getElementById('auction_'+auctionid+'_preis').style.textDecoration = '';
				if(document.getElementById('auction2_'+auctionid+'_preis'))
					document.getElementById('auction2_'+auctionid+'_preis').style.textDecoration = '';
			}
    }

    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['should_display_auction_ended_message'] == '1');
      showAuctionData(auctionid, 'gewonnen', auction['is_winner'] == '1');

      showAuctionData(auctionid, 'bieten', auction['should_display_bidbutton'] == '1');
      showAuctionData(auctionid, 'maxbidder', auction['should_display_maxbidder_message'] == '1');
    }

    if(auction['is_sold'] == '1')
    {
    	formatPrice(auctionid, 'verkauft');
	 		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'] == '1')
	 			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" /> Kaufabwicklung starten!</a> ]</div>');
	 		if(auction['ersparnis_prozent'])
		  	setAuctionData(auctionid, 'price_post', '<div class="auction_price_post">Ersparnis: <div class="auction_row_ersparnis">'+auction['ersparnis_prozent']+'%</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("refreshAuctions()", 1000);
}

function formatPrice(auctionid, style)
{
	if(document.getElementById('auction_'+auctionid+'_preis_text_box'))
		document.getElementById('auction_'+auctionid+'_preis_text_box').className = 'mini_auction_box_price' + (style ? '_' + style : '');
	if(document.getElementById('auction_'+auctionid+'_preis_text_row'))
		document.getElementById('auction_'+auctionid+'_preis_text_row').className = 'auction_row_price' + (style ? '_' + style : '');
}

function countdown_readable(time_left)
{
  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;
  return hours+':'+minutes+':'+seconds;
}

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))
  	if(document.getElementById('auction_'+auctionid+'_'+elementname).style.display != (value ? 'block' : 'none'))
	    document.getElementById('auction_'+auctionid+'_'+elementname).style.display = (value ? 'block' : 'none');
  if(document.getElementById('auction2_'+auctionid+'_'+elementname))
  	if(document.getElementById('auction2_'+auctionid+'_'+elementname).style.display != (value ? 'block' : 'none'))
	    document.getElementById('auction2_'+auctionid+'_'+elementname).style.display = (value ? 'block' : 'none');
}

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

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;
  if(auctions[auctionid]['is_blocked'] != null)
  	return;
  auctions[auctionid]['highlight']++;
  if(document.getElementById('auction_'+auctionid+'_preis_text'))
    document.getElementById('auction_'+auctionid+'_preis_text').className = 'auction_box_price_hl';
  if(document.getElementById('auction_'+auctionid+'_preis_text_box'))
    document.getElementById('auction_'+auctionid+'_preis_text_box').className = 'auction_box_price_hl';
  if(document.getElementById('auction_'+auctionid+'_preis_text_row'))
    document.getElementById('auction_'+auctionid+'_preis_text_row').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('auction_'+auctionid+'_preis_text_box'))
      document.getElementById('auction_'+auctionid+'_preis_text_box').className = 'auction_box_price';
    if(document.getElementById('auction_'+auctionid+'_preis_text_row'))
      document.getElementById('auction_'+auctionid+'_preis_text_row').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';
}