var gallery = function() {

	//User vars

	this._directory  = ' ';

	this._idRegular = 'photo_regular';

	this._loading = '/img/ajax-loader.gif';

	this._normalBorder = '#B6B6B6';

	this._overBorder   = '#717171';

	this._tbPrefix = 'tb_';

	this._aPrefix = 'a_';

	this._globalname = 'window.g';

	this._needPreload = true;

	//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.preloadOne(i);

		}

		this._preloaded = true;

	}



	this.preloadOne = function (i) {

        this._imgObjs[i] = new Image();

        this._imgObjs[i].src = this._img[i];

 		this._flag[i] = false;

	}

	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

			if(!this._preloaded) this.preloadOne(loc);

			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._imgObjs[loc].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;

		}

//		alert(this._imgEnlarged[loc].replace('http://system.www.ostwest.local/', ''));
//		wopen('http://site.www.ostwest.local/en/popup/?img=' + this._imgEnlarged[loc].src.replace('http://site.www.ostwest.local/', ''));

	}

	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;

		if(this._needPreload) this.preload();

		this.show(this._names[0]);

	}

}
