var category;
var container; 
var imgcount = 0;

function viewImage(category, id) {
	var previewImg = document.getElementById("preview-img");
	if(previewImg == null) { alert("Please wait untl all images have loaded."); return; }
	previewImg.src = "../images/" + category + "/medium/" + id + ".jpg";
}

function preloadImages(cat, images) {
	category = cat;
	imgcount = images.length;

	if(getCookie(category) == "") {
		if(container == null) { container = document.getElementById("container"); }
		container.innerHTML = "<center><img src='../images/spinner.gif'><br/>Loading images. " + (imgcount) + " to go. Please wait...</center>";
		for (i = 0; i < imgcount; i++) {
			var u = "../images/" + category + "/medium/" + images[i] + ".jpg";
			var img = new ajaxObject(u, imageLoaded);
			img.update();
		}
	} else {
		showImage();
	}
}
	
function showImage() {
	var prefix = category.substring(0,1);
	if(category == 'flaneur') { prefix = category.substring(0,1) + category.substring(1,2); }
	if(container == null) { container = document.getElementById("container"); }
	container.innerHTML = "<center><img id='preview-img' src='../images/" + category + "/medium/" + prefix + "1.jpg'></center>";
	if(getCookie(category) == "") {
		setCookie(category, true, 1);
	}
}

function imageLoaded(responseStatus) {
	if(responseStatus == 200) {
		imgcount--;
		if(imgcount == 0) { showImage(); return; }
		if(container == null) { container = document.getElementById("container"); }
		container.innerHTML = "<center><img src='../images/spinner.gif'><br/>Loading images. " + (imgcount) + " to go. Please wait...</center>"
	}
}	

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function createXMLHttpRequest() {
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  	try { return new XMLHttpRequest(); } catch(e) {}
  	alert("XMLHttpRequest not supported");
  	return null;
}

function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;

  this.update = function(passData,postMethod) { 
    if (that.updating==true) { return false; }
    //that.updating=true;                       
    var AJAX = null;
	AJAX = createXMLHttpRequest();                                           
    if (AJAX==null) {                             
      return false;                               
    } else {
      AJAX.onreadystatechange = function() {  
        if (AJAX.readyState==4) {  
          that.updating=false;                
          that.callback(AJAX.status);        
          delete AJAX;                 
        }                                                      
      }                                                         
      if (postMethod=='POST') {
        var uri=urlCall+'?'+timestamp.getTime();
        AJAX.open("POST", uri, true);
        AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        AJAX.send(passData);
      } else {
		if(passData != null) { 
	        var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1); 
		} else {
			var uri=urlCall;
		}
        AJAX.open("GET", uri, true);                             
        AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;
  this.callback = callbackFunction || function () { };
}
