
function loadGallery(id_product, state) {
	
   if(xmlHttp) { 

   	if(id_product) {
      id_product = encodeURIComponent(id_product);
      cache.push("id_product=" + id_product+'&state='+state);
    }
    try {
      if((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0) {
      	
      	var cacheEntry = cache.shift();
        xmlHttp.open("POST", 'index.php?m=Product_Gallery', true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = galleryHandleRequest;
        xmlHttp.send(cacheEntry);
      }
    }
    catch (e) {
      displayError(e.toString());
    }
  }
}

function galleryReadResponse() {

  var response = xmlHttp.responseText;

  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0) {
  	throw(response.length == 0 ? "Server error." : response);
  }

  var arr = response.split(':');
 
  if(arr[0] == 'one') {
  	document.getElementById('image_1').src = arr[1];	
  	document.getElementById('main_image').src = arr[2];
  	document.getElementById('image_0').style.display = 'none';
  	document.getElementById('image_2').style.display = 'none';
  	document.getElementById('galleryPrev').style.display = 'none';
  	document.getElementById('galleryNext').style.display = 'none';
  }
  else {
 	if(arr[0] == 'two'){
 		document.getElementById('image_0').style.display = 'none';
 	}
  	for(var i = 0; i < 3; i++) {
  		document.getElementById('image_' + i).src = arr[i];
  	}
  	document.getElementById('main_image').src = arr[3];
  } 
}

/*
 *	czyta odpowiedz z servera
 */

function galleryHandleRequest() {

  if(xmlHttp.readyState == 4) {
    if(xmlHttp.status == 200) {
      try {
        galleryReadResponse();
      }
      catch(e) {
        displayError(e.toString());
      }
    }
    else {
      displayError(xmlHttp.statusText);
    }
  }
}

function displayError($message) {
 
  if (showErrors) {
   
    showErrors = false;
    alert("Wystąpił błąd: \n" + $message);
    setTimeout("loadGallery();", 10000);
  }
}

function zoomImage() {
	var path = document.getElementById('main_image').src; 
	var link = "./index.php?m=Product_Gallery&e=zoomMainImage&path_img=" + path;
	window.open(link, "ZoomImg", "width=640, height=340,resizable=0,scrollbars=no,menubar=no");
}

function zoomImage2(produkt,image) {
	var link = "index.php?m=ZoomImage&produkt="+produkt+"&image="+image;
	window.open(link,"ZoomImg","width=40,height=40,resizable=0,scrollbars=nos,menubar=no");
}
						
 
