/*
Multiple small galleries (on the same page, as long as there's a different gid for each gallery)
base code Taken from Noa's website
Liran Oz 2007
*/

ImagePre.prototype.url;
ImagePre.prototype.img;
ImagePre.prototype.done;
ImagePre.prototype.unhide;


function ImagePre(url, gid)
{

	disable(gid);
	this.img_url = url;
	this.done = false;
	this.img = new Image();

	this.img.onload = function() {
		this.done = true;
		showImage(gid);
		enable(gid);
	};

	this.img.src = url;
}



var images = new Array();
var flags = new Array();
var gidx = new Array();
var cidx = new Array();


function createNewGallery(gid)
{
	images[gid] = new Array();
	flags[gid] = false;
	gidx[gid] = 0;
	cidx[gid] = 0;
}

function addImage(gid, thumb, url, title)
{
	for (i=0; i<gidx[gid]; i++) {
		if (images[gid][i][0] == url) {
			return;
		}
	}

	images[gid][gidx[gid]] = new Array();
	images[gid][gidx[gid]][0]= url;
	images[gid][gidx[gid]][1] = thumb;
	images[gid][gidx[gid]][2] = null;
	images[gid][gidx[gid]++][3] = title;

	if (gidx[gid] == 1) {
		images[gid][0][2] = new ImagePre(images[gid][0][1], gid);
		//Explorer fix (clicking back on iFrame)
		document[gid].src = thumb;
		if (images[gid][0][3] != ' ' && images[gid][0][3] != null) {
			var obj = document.getElementById(gid+'_title');
			if (obj)
				obj.innerHTML = images[gid][0][3];
		}
	}

}

function enable(gid)
{
	flags[gid] = true;
}

function disable(gid)
{
	flags[gid] = false;
}


function showImage(gid)
{
	if (images[gid][cidx[gid]][2] == null && flags[gid] == true) {
		images[gid][cidx[gid]][2] = new ImagePre(images[gid][cidx[gid]][1], gid);	//preload it
	}
	else {	//just display
		document[gid].src = images[gid][cidx[gid]][1];
	}

	var ref = document.getElementById(gid+"_a");
	if (ref) {
		ref.href = images[gid][cidx[gid]][0];
	}
/*
	var obj = document.getElementById(gid+'_title');
	if (!obj)
		return;
	var ttl;
	if (images[gid][cidx[gid]][3] == null || images[gid][cidx[gid]][3] == ' ' || images[gid][cidx[gid]][3] == '')
		ttl = _gallery_title;
	else
		ttl = images[gid][cidx[gid]][3].substr(0, 25);
	obj.innerHTML = ttl;
*/

}

function nextImage(gid)
{
	if (!flags[gid])
		return;

	if (cidx[gid] >= gidx[gid]-1)
		cidx[gid] = 0;

	else
		cidx[gid]++;

 	showImage(gid);
}

function prevImage(gid)
{
	if (!flags[gid])
		return;


	if (cidx[gid] <= 0)
		cidx[gid] = gidx[gid]-1

	else
		cidx[gid]--;

 	showImage(gid);
}

var __lighboxOverrideLst = null;
var __lightboxOverrideNum = 0;
function openLightBox2(gid) {
	__lighboxOverrideLst = [];
	for (var i=0; i<images[gid].length; i++) {
		__lighboxOverrideLst.push([images[gid][i][0], ""]);
	}	
	
	__lightboxOverrideNum = cidx[gid];
	//lightbox will pop itself now
}

function openLightBox(gid)
{
	/*Nice kombina...*/
	if (images) {
		parent.Lightbox.prototype.newImageList();
		parent.images = new Array();
		parent.images = images;
	}

	var objLink = parent.document.createElement('a');
	objLink.setAttribute('href', images[gid][cidx[gid]][0]);
	objLink.setAttribute('rel', 'lightbox');
	objLink.setAttribute('title', '');
	parent.Lightbox.prototype.start(objLink);
}

