var xmlHttp = createXmlHttpRequestObject();
var test = true;
var dataPath = "/functions/";

// JavaScript Document
function createXmlHttpRequestObject() {	
	// zde bude reference na objek XMLHttpRequest
	var xmlHttp;
	// toto by melo fungovat ve vsech prohlizecich mimo IE6 a starsi
	try {
		// pokusi se vyvorit objekt XMLHttpRequest
		xmlHttp = new XMLHttpRequest();		
	}
	catch (e) 	{
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
										'MSXML2.XMLHTTP.5.0',
										'MSXML2.XMLHTTP.4.0',
										'MSXML2.XMLHTTP.3.0',
										'MSXML2.XMLHTTP',
										'Microsoft.XMLHTTP');
		//vyzkousej vsechna prog id, dokud nektere nebude funkcni
		for(var i=0; i < XmlHttpVersions.lenght && !xmlHttp; i++) {
			try {
				//pokusi se vytvorit objekt XmlHttpRequest
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e) {}
		}
	}
	// vrati vytvoreny objekt nebo zobrazi chybovou zpravu
	if (!xmlHttp) {
		 // alert("Error creating the XMLHttpRequest object.");
	}
	else {
		return xmlHttp;
	}
}

function processZobrazDetailFotky(id) {
  //pokracuj jen neni-li xmlHttp prazdne
  if(xmlHttp) {
	try {
	  setOpacity('foto-detail', alpha = 0.2 );
	  //pokus se pripojit k serveru
	  params = "id=" + id;
	  //inicijuje asynchronni pozadavek HTTP
	  xmlHttp.open("GET", dataPath + "ajax-galerie.php?" + params, true);
	  xmlHttp.onreadystatechange = handleRequestStateChange;
	  xmlHttp.send(null);
		
	}
	// v pripade selhani zobraz chybu
	catch(e) {
		//alert("Can`t connect to server: \n" + e.toString());
	}
  }
}


function handleRequestStateChange() {
  //je li readyState 4 muzeme precist odpoved ze serveru
  if(xmlHttp.readyState == 4) {		
	//pokracuj pouze, je-li stav HTTP "OK"
	if(xmlHttp.status == 200) {			
		try {
		  //zpracuj odpoved ze serveru
		  handleServerResponse();
		}
		catch(e) {
		  //zobraz chybovou zpravu
		  //alert ("Error reading the response: " + e.toString());
		}
	}
	else  {
	  // zobraz zpravu o stavu
	  //alert("There was a problem retrieving the data: \n" + xmlHttp.statusText);
	}
  }
}

function handleServerResponse() {
  var xmlResponse = xmlHttp.responseXML;
  
  //ziska element dokumentu XML
  xmlRoot = xmlResponse.documentElement;
  
  var foto = xmlRoot.getElementsByTagName("foto").item(0).firstChild.data;
  if(xmlRoot.getElementsByTagName("nazev").item(0).hasChildNodes()) {
  	var nazev = xmlRoot.getElementsByTagName("nazev").item(0).firstChild.data;
  }
  
  if(xmlRoot.getElementsByTagName("popis").item(0).hasChildNodes()) {
  	var popis = xmlRoot.getElementsByTagName("popis").item(0).firstChild.data;
  }

  html  = '<img src="'+ foto +'" alt="' + nazev + '" onclick="PPOpen(\''+ foto +'\', \'' + nazev +'\', \'' + popis +'\');" />';
  
  var divFotoDetail = document.getElementById("foto-detail");
  
  //pridej obsah do elementu <div>
  divFotoDetail.innerHTML = html;
  setOpacity('foto-detail', alpha = 1);
 
}


function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}

function rawurlencode (str) {
    str = (str+'').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A');
}
