var currItem = 0;
var timeout = 5;
var slideSpeed = 1;
var slideStep = 5;
var advBoxPadding = 2;

function startRequest()
{
  var advBox = document.getElementById("advBox");
  advBox.innerHTML = "Trwa wczytywanie danych...";
  url = "data/ogloszenia.xml";
  startGETRequest(url, onComplete, onEnd);
}

function startAdvShow()
{
  var advBox = document.getElementById("advBox");
  var divs = advBox.getElementsByTagName("div");

  if(divs.length < 1) return;

  if(++currItem >= divs.length) currItem = 0;
  if(currItem == 0)
    prevItem = divs.length - 1;
  else
    prevItem = currItem - 1;

  var currDiv = divs[currItem];
  var prevDiv = divs[prevItem];
  currDiv.style.zIndex = 1001;
  prevDiv.style.zIndex = 1000;

  xPos = parseInt(currDiv.parentNode.offsetWidth);
  currDiv.style.left = xPos + "px";
  currDiv.style.display = "block";

  slideDiv(prevItem, currItem);
}

function slideDiv(prevItem, currItem)
{
  var advBox = document.getElementById("advBox");
  var divs = advBox.getElementsByTagName("div");
  var currDiv = divs[currItem];
  var prevDiv = divs[prevItem];

  xPos = parseInt(currDiv.style.left);
  xPos -= slideStep;
  if(xPos < advBoxPadding){
    xPos = advBoxPadding;
    prevDiv.style.display = "none";
    currDiv.style.left = xPos + "px";
    setTimeout("startAdvShow();", timeout * 1000);
  }
  else{
    currDiv.style.left = xPos + "px";
    setTimeout("slideDiv(prevItem, currItem);", slideSpeed);
  }
}

function przetwarzajXML(xml){
  var advBox = document.getElementById("advBox");
  advBox.innerHTML = "";
  
  var ogloszenia = xml.documentElement.childNodes;
  
  var count = 0;
  var iloscogloszen = Math.ceil(ogloszenia.length / 2);
  for(var i = 0; i < iloscogloszen; i++){
  	if(ogloszenia[i].nodeType == 1){
		var ogloszenie = ogloszenia[i*2].childNodes;
		var div = document.createElement("div");

		if(count > 0) div.style.display = "none"
		else div.style.display = "block";
	
		div.id = "advBox" + count++;
		//div.className = "advBoxDiv";
	
		advBox.appendChild(div);
		div.style.width = (div.parentNode.clientWidth - 2 * advBoxPadding) + "px";
		div.style.height = div.parentNode.offsetHeight + "px";
	
		div.innerHTML = '<div class="title"><div class="maska"></div><p>' + ogloszenie[1].firstChild.nodeValue + '</p></div>';
		div.innerHTML += '<p>' + ogloszenie[2].firstChild.nodeValue + '</p>';
		div.innerHTML += '<div class="wiecej" style="margin: 2px 0;"><a href="' + ogloszenie[3].firstChild.nodeValue + '"><img src="img/wiecej.gif" alt="Więcej ..."></a></div>';
		
		if ((i*2+2) <= ogloszenia.length) {
      var ogloszenie = ogloszenia[i*2+1].childNodes;
      div.innerHTML += '<p>&nbsp;</p>';
      div.innerHTML += '<div class="title"><div class="maska"></div><p>' + ogloszenie[1].firstChild.nodeValue + '</p></div>';
      div.innerHTML += '<p>' + ogloszenie[2].firstChild.nodeValue + '</p>';
      div.innerHTML += '<div class="wiecej" style="margin: 2px 0;"><a href="' + ogloszenie[3].firstChild.nodeValue + '"><img src="img/wiecej.gif" alt="Więcej ..."></a></div>';
		}
	  }
  }
  
   //setTimeout("startAdvShow();", timeout * 1000);
   
   $(document).ready(function() {
		$('#advBox').cycle({ 
			fx:     'scrollDown', 
			timeout: '3000', 
		});
	});
	
}

function onComplete(text, xml){
  if (!xml || !xml.documentElement){
    //alert(text);
  }
  else if (xml.documentElement.nodeName == "parsererror"){
    //alert(text);
  }
  else{
    przetwarzajXML(xml);
	
  }
}

function onEnd()
{
}

