imageBrowser =
{
	init : function (baseurl, mediaurl, imgw, idprev, idnext)
	{
		if (!document.getElementById)
			return false;
		
		this.baseURL = baseurl;
		this.mediaURL = mediaurl;
		this.imgWidth = imgw;
		this.idPrev = idprev;
		this.idNext = idnext;
		
		this.btnPrev = document.getElementById("btnPrevImg");
		this.btnNext = document.getElementById("btnNextImg");
		
		this.setUpButtons( this.idPrev, this.idNext );
	},
	
	setUpButtons : function (prev, next)
	{
		var btn;
		
		if (prev)
		{
			btn = this.getButton("Prev");
			btn.style['visibility'] = "visible";
		}
		else
		{
			this.disableButton( "Prev" );
		}
		
		if (next)
		{
			btn = this.getButton("Next");
			btn.style['visibility'] = "visible";
		}
		else
		{
			this.disableButton( "Next" );
		}
	},
	
	disableButton : function (which)
	{
		var btn = this['btn'+which];
		
		if (!btn)
			return false;
		
		btn.href = "#";
		btn.onclick = function () {return false};
		btn.style['visibility'] = "hidden";
	},
	
	getButton : function (which)
	{
		var a, imgsrc, holder, p=this;
		
		var btnName = "btn" + which;
		
		
		if (this[btnName])
		{
			a = this[btnName];
		}
		else
		{
			a = document.createElement(a);
			imgsrc = (which == "Prev") ? "assets/img/btn_arrow_left.gif" : "assets/img/btn_arrow_right.gif";
			a.innerHTML = "<img src='" + imgsrc + "' width='24' height='20' alt='photo' />";
		}
		
		a.setAttribute("href", "#");
		a.setAttribute("id", "btn"+which+"Img");
		
		holder = document.getElementById("btnHolder");
		
		switch (which)
		{
			case "Prev":
				a.onclick = function () { p.loadImage("Prev"); return false; };
				holder.insertBefore(a, holder.firstChild);
				break;
				
			case "Next":
				a.onclick = function () { p.loadImage("Next"); return false; };
				holder.appendChild(a);
				break;
		}
		
		this[btnName] = a;
		
		return a;
	},
	
	loadImage : function (which)
	{
		var p = this;
		var val = this["id"+which];
		var query = this.baseURL + "mod.ajax/part.image/id." + val + "/";
		
		var req = new AjaxRequest(val, query, function (req) { p.handleWait(req) }, function (req) { p.handleData(req, which) }, function (req) { p.handleError(req) }, false);
		req.doRequest();
	},

	handleWait : function (req)
	{
		//console.log(req);
	},

	handleError : function (req)
	{
		//console.log(req);
	},
	
	handleData : function (req, which)
	{
		var title;
		var image = req.getResponseXML().getElementsByTagName("image")[0];
		var arr, p=this, img;
		
		arr = image.getElementsByTagName("idPrevious");
		this.idPrev = (arr.length) ? image.getElementsByTagName("idPrevious")[0].firstChild.nodeValue - 0 : 0;
		
		arr = image.getElementsByTagName("idNext");
		this.idNext = (arr.length) ? image.getElementsByTagName("idNext")[0].firstChild.nodeValue - 0 : 0;
		
		arr = image.getElementsByTagName("sTitle");
		title = (arr.length) ? image.getElementsByTagName("sTitle")[0].firstChild.nodeValue : "-";
		
		arr = image.getElementsByTagName("sFile");
		if (arr.length)
		{
			img = new Image();
			img.onload = function () { p.showImage(this, title, which) };
			img.src = this.mediaURL + image.getElementsByTagName("sFile")[0].firstChild.nodeValue;
		}
		
		this.setUpButtons( this.idPrev, this.idNext );
	},
	
	showImage : function (img, title, which)
	{
		var gi, gih, newimg, tw, p=this;
		
		gih = document.getElementById("galleryImagesHolder");
		gi = document.getElementById("galleryImages");
		newimg = document.createElement("img");
		
		newimg.setAttribute("src", img.src);
		
		switch(which)
		{
			case "Prev":
				gi.insertBefore(newimg, gi.getElementsByTagName("img")[0]);
				gi.style["left"] = -this.imgWidth + "px";
				tw = new Tween( gi, "left", "px", -this.imgWidth, 0, 1000, function () { p.handleTweenEnd(which, title) } );
				break;
				
			case "Next":
				gi.appendChild(newimg);
				tw = new Tween( gi, "left", "px", 0, -this.imgWidth, 1000, function () { p.handleTweenEnd(which, title) } );
				break;
		}
	},
	
	handleTweenEnd : function (which, title)
	{
		var gi = document.getElementById("galleryImages");
		
		// set title
		document.getElementById("imageTitle").innerHTML = title;
		
		switch(which)
		{
			case "Prev":
				// remove last image
				gi.removeChild( gi.getElementsByTagName("img")[1] );
				gi.style["left"]  = "0px";
				break;
				
			case "Next":
				// remove first image
				gi.removeChild( gi.getElementsByTagName("img")[0] );
				gi.style["left"]  = "0px";
				break;
		}
	}
};