function wopen(url, width, height) {
	if(!width) width = 50;
	if(!height) height = 50;
	window.open(url, '', "location=no,menubar=no,status=no,titlebar=no,toolbar=no,scrollbars=yes,width=" + width+",height=" + height);
}

var gallery = function() {
	//User vars
	this._directory  = '/img/content/';
	this._idRegular = 'photo_regular';
	this._loading = '/img/loading.gif';
	this._normalBorder = '#636363';
	this._overBorder   = '#CE0000';
	this._tbPrefix = 'tb_';
	this._aPrefix = 'a_';
	this._globalname = 'window.g';

	//System vars
	this._preloaded = false;

	this._names        = new Array();
	this._ids          = new Array();
 	this._img          = new Array();
	this._imgEnlarged  = new Array();
	this._imgObjs      = new Array();
	this._descriptions = new Array();

	this._flag = new Array();
	this._timeout = null;
	this._imgLoading = null;

	/* -------------- Methods -------------- */

	this.add = function(photo, enlarged, id, description) {

		this._names.push(photo);
		this._ids.push(id);
		this._imgEnlarged.push(enlarged);
		this._descriptions.push(description);

		imgToLoad = (this._directory + photo);
		this._img.push(imgToLoad);
	}

	this.preload = function () {
		for(i=0; i<this._names.length; i++) {
			this._imgObjs[i] = new Image();
			this._imgObjs[i].src = this._img[i];
			this._flag[i] = false;
		}

		this._preloaded = true;
	}

	this._clear = function() {
		for(j=0; j<this._flag.length; j++) {
			if(this._flag[j] == true) {
				this._flag[j] = false;
				stImg = document.getElementById(this._tbPrefix+ this._ids[j]);
				stImg.style.borderColor = this._normalBorder;
			}
		}
	}

	this.show = function(photo) {
			if(this._timeout) {
				clearTimeout(this._timeout);
				this._timeout = null;
			}

			loc = -1;
			for(i=0; i<this._names.length; i++) {
				if(this._names[i] == photo) loc = i;
			}
			if(loc == -1) return false; // Image not found

			this._clear();

			tbImg = document.getElementById(this._tbPrefix + this._ids[loc]);
			tbImg.style.borderColor = this._overBorder;
			this._flag[loc] = true;

			aImg = document.getElementById(this._aPrefix + this._ids[loc]);
			aImg.blur();

			img = document.getElementById(this._idRegular);
			if(this._imgObjs[loc].complete) {
				img.src = this._imgObjs[loc].src;
			} else {
				img.src = this._imgLoading.src;
				this._timeout = setTimeout(this._globalname + ".show('" + photo + "')", 5000);
			}
		return true;
	}

	this.openCurrent = function () {
		loc = 0;
		for(j=0; j<this._flag.length; j++) {
			if(this._flag[j] == true)  loc = j;
		}

		wopen('/popup.php' + this._directory + this._imgEnlarged[loc]);
	}
	
	this.loadCurrent = function () {
		loc = 0;
		for(j=0; j<this._flag.length; j++) {
			if(this._flag[j] == true)  loc = j;
		}

		wopen('/getfile.php' + this._directory + this._imgEnlarged[loc]);
	}	

	this.getCurrentDescription = function () {
		loc = 0;
		for(j=0; j<this._flag.length; j++) {
			if(this._flag[j] == true)  loc = j;
		}

		return this._descriptions[loc];
	}

	this.init = function () {
		this._imgLoading = new Image();
		this._imgLoading.src = this._loading;


		this.preload();
		this.show(this._names[0]);
	}
}
