var GallerycurrItem = 0;
var timeout = 5;
var slideSpeed = 1;
var slideStep = 5;
var advBoxPadding = 2;

function GallerystartRequest()
{
  var advBox = document.getElementById("advGalleryBox");
  advBox.innerHTML = "Trwa wczytywanie danych...";
  url = "data/galeriaglowna.xml";
  startGETRequest(url, onGalleryComplete, onGalleryEnd);
}

function GallerystartAdvShow()
{
  var advBox = document.getElementById("advGalleryBox");
  var divs = advBox.getElementsByTagName("div");

  if(divs.length < 1) return;

  if(++GallerycurrItem >= divs.length) GallerycurrItem = 0;
  if(GallerycurrItem == 0)
    prevItem = divs.length - 1;
  else
    prevItem = GallerycurrItem - 1;

  var currDiv = divs[GallerycurrItem];
  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";

  GalleryslideDiv(prevItem, GallerycurrItem);
}

function GalleryslideDiv(prevItem, currItem)
{
  var advBox = document.getElementById("advGalleryBox");
  var divs = advBox.getElementsByTagName("div");
  var currDiv = divs[GallerycurrItem];
  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("GallerystartAdvShow();", timeout * 1000);
  }
  else{
    currDiv.style.left = xPos + "px";
    setTimeout("GalleryslideDiv(prevItem, GallerycurrItem);", slideSpeed);
  }
}

function GalleryprzetwarzajXML(xml){
  var advBox = document.getElementById("advGalleryBox");
  advBox.innerHTML = "";
  
  var ogloszenia = xml.documentElement.childNodes;
  
  var count = 0;
  var iloscogloszen = ogloszenia.length;
  for(var i = 0; i < iloscogloszen; i++){
  	if(ogloszenia[i].nodeType == 1){
		var ogloszenie = ogloszenia[i].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[0].firstChild.nodeValue + '</p></div>';
		div.innerHTML += '<a href="przewodnik,galeria.html"><img src="img/galeriaglowna.jpeg?id=' + ogloszenie[0].firstChild.nodeValue + '"></a>';
		//div.innerHTML += '<div class="wiecej" style="margin: 2px 0;"><a href="ogloszenia,ogloszenie,' + ogloszenie[0].firstChild.nodeValue + '.html"><img src="img/wiecej.gif" alt="Więcej ..."></a></div>';
		
		
	  }
  }
   //setTimeout("startAdvShow();", timeout * 1000);
   
   $(document).ready(function() {
		$('#advGalleryBox').cycle({ 
			fx:     'scrollLeft', 
			timeout: '3700', 
		});
	});
	
}

function onGalleryComplete(text, xml){
  if (!xml || !xml.documentElement){
    alert(text);
  }
  else if (xml.documentElement.nodeName == "parsererror"){
    alert(text);
  }
  else{
    GalleryprzetwarzajXML(xml);
	
  }
}

function onGalleryEnd()
{
}
