(function(){
    JC.addEvent(window, "load", function(){
        var b = JC.id("o");
        JC.setAttr(b, "class", "jsEn");
		//get venue id
		var currentId = JC.id("venueNumber").value;
		startAjax(currentId);
    });
	
function startAjax(id){
	var http = JC.old.createQCObject();
	http.open('get','../scripts/ajaxVenuePhotos.php?venueId='+id+'',true);
	http.onreadystatechange = function(){
		if (http.readyState == 4 && http.status == 200) {
			var response = http.responseText;
			ajax = new doSomething(response);
		}
	};
  	http.send(null); 	
}

var doSomething = function (r){
	this.r =  eval("(" + r + ")");
	this.photoAmt = this.r.length - 1;
	this.photoNo = 0;//ini
	if (this.photoAmt > 1) {
		this.image = JC.id("vImage");
		this.imagePath = "i/restaurant/";
		var internal = this;
		var para = JC.old.cEle("p");
		var link = JC.old.cEle("a");
		var span = JC.old.cEle("span");
		var text = JC.old.tNode("Next photo");
		var numberText = JC.old.tNode((this.photoNo+1) +" of "+ (this.photoAmt + 1) + " ");
		span.id = "photoNumbers";
		link.id = "photoLink";
		link.href = "#";
		para.id = "venuePhotoLink";
		JC.old.aChild(numberText,span);
		JC.old.aChild(text,link);
		JC.old.aChild(span,para);
		JC.old.aChild(link,para);
		JC.old.aChild(para,JC.id("venuePhotos"));
		JC.addEvent(JC.id("photoLink"),"click",JC.bindFunction(this,this.changeImage));
	}else{
		return;
	}
};
doSomething.prototype = {
	changeImage:function()		{
		//if we are on the last photo need to go to the first one;
		if(this.photoNo == this.photoAmt){
			this.photoNo = 0;
		}else{this.photoNo++;}
		//set image attr and text		
		JC.id("photoNumbers").childNodes[0].nodeValue = (this.photoNo+1) +" of "+ (this.photoAmt + 1)+ " ";
		this.image.src = this.imagePath + this.r[this.photoNo]['photo_name'];
		this.image.alt = this.r[this.photoNo]['photo_text_long'];
	}
};
})();
