PHOTOVIEWERAPP = function () {
	this.simple = window.XMLHttpRequest ? false : true;
	this.available = document.getElementById && !this.simple;
	this.setupdone = false;
	this.dontclose = false;
	this.position = 0;
	this.speed = 3;
	this.timeout = 20;
	this.timeoutObj;
	this.skipSizeTransition = false;
	this.skipImageTransition = false;
	this.skipInfoTransition = false;
	this.skipThreashold = this.timeout*3;
	this.o = 'photoviewer-open';
	this.c = 'photoviewer-closed';
	this.showTitle = false;
	this.showDescription = false;
	this.folder = '/images/';
	this.record = 0;
	this.current;
	this.list = [];
	this.images = {};
	this.ste;
	this.ete;
	this.view = function(id) {
		if(this.available) {
			if(!this.setupdone) this.setup();
			this.alpha = 0;
			this.imageopen = false;
			if(id != undefined) this.position = this.getListPos(id);
			this.changeClass(this.viewer, this.o);
			this.setDimensions('holder', 100, 100);
			this.setMainPosition();
			this.setSubPosition();
			this.selectImage();
			return false;
		} else {
			return true;
		}
	};
	this.selectImage = function() {
		this.imageObj = this.images['image' + this.list[this.position]];
		this.imagemoving = true;
		if(this.imageopen) {
			this.closeImage();
		} else {
			this.openImage();
		}
	};
	this.openImage = function() {
		var l = this.getItem('photoviewerlink' + this.imageObj.id);
		this.changeClass(l, 'photoviewer-currentlink');
		this.current = this.imageObj.id;
		//
		this.newWidth = this.imageObj.width;
		this.newImageHeight = this.imageObj.height;
		this.newInfoHeight = 0;
		this.hasInfo = false;
		//
		this.changeClass(this.image, this.c);
		this.imagetag.src = this.folder + this.imageObj.image;
		this.imagetag.width = this.imageObj.width;
		this.imagetag.height = this.imageObj.height;
		//
		if((this.showTitle && this.imageObj.title != '') || (this.showDescription && this.imageObj.description != '')) {
			this.changeClass(this.imageinfoholder, this.o);
			this.imageinfoholder.style.height = 'auto';
			this.imageinfoholder.style.width = this.imageObj.width + 'px';
			if(this.showTitle && this.imageObj.title != '') {
				this.changeClass(this.imageinfotitle, this.o);
				this.imageinfotitle.innerHTML = this.imageObj.title;
			} else {
				this.changeClass(this.imageinfotitle, this.c);
			}
			if(this.showDescription && this.imageObj.description != '') {
				this.changeClass(this.imageinfodescription, this.o);
				this.imageinfodescription.innerHTML = this.imageObj.description;
			} else {
				this.changeClass(this.imageinfodescription, this.c);
			}
			this.hasInfo = true;
			this.newInfoHeight = this.imageinfoholder.offsetHeight;
			if(!this.skipInfoTransition) this.imageinfoholder.style.height = '1px';
			this.changeClass(this.imageinfoholder, this.c);
		}
		this.newHeight = this.newImageHeight + this.newInfoHeight;
		//
		this.ste = new Date().getTime();
		this.sizeImage();
	}
	this.sizeImage = function() {
		if(this.skipSizeTransition) {
			this.setDimensions('holder', this.newWidth, this.newHeight);
			this.setSubPosition();
		}
		var w = parseInt(this.holder.offsetWidth)-this.widthadd;
		var h = parseInt(this.holder.offsetHeight)-this.heightadd;
		if(w != this.newWidth || h != this.newHeight) {
			if(w != this.newWidth) {
				w += (w < this.newWidth) ? Math.ceil((this.newWidth-w)/6) : Math.floor((this.newWidth-w)/this.speed);
				this.setSize('holder', 'width', w);
			}
			if(h != this.newHeight) {
				h += (h < this.newHeight) ? Math.ceil((this.newHeight-h)/6) : Math.floor((this.newHeight-h)/this.speed);
				this.setSize('holder', 'height', h);
			}
			this.setSubPosition();
			this.ete = new Date().getTime();
			if(!this.skipSizeTransition && this.ete-this.ste > this.skipThreashold) this.skipSizeTransition = true;
			this.ste = new Date().getTime();
			this.timeoutObj = setTimeout(this.name + '.sizeImage();', this.timeout);
		} else {
			this.checkImage();
		}
	}
	this.checkImage = function() {
		if(this.imagetag.complete) {
			this.changeClass(this.image, this.o);
			this.setDimensions('imageholder', this.newWidth, this.newImageHeight);
			this.setDimensions('image', this.newWidth, this.newImageHeight);
			this.setAlpha(this.imagetag);
			this.ste = new Date().getTime();
			this.showImage();
		} else {
			this.timeoutObj = setTimeout(this.name + '.checkImage();', this.timeout);
		}
	}
	this.showImage = function() {
		if(this.skipImageTransition) {
			this.alpha = 100;
			this.setAlpha(this.imagetag);
		}
		if(this.alpha < 100) {
			this.alpha += 10;
			this.setAlpha(this.imagetag);
			this.ete = new Date().getTime();
			if(!this.skipImageTransition && this.ete-this.ste > this.skipThreashold) this.skipImageTransition = true;
			this.ste = new Date().getTime();
			this.timeoutObj = setTimeout(this.name + '.showImage();', this.timeout);
		} else {
			if(this.hasInfo) {
				this.changeClass(this.imageinfoholder, this.o);
				this.ste = new Date().getTime();
				this.showInfo();
			} else {
				this.complete();
			}
		}
	}
	this.showInfo = function() {
		if(this.skipInfoTransition) {
			this.imageinfoholder.style.height = this.newInfoHeight + 'px';
		}
		var h = parseInt(this.imageinfoholder.offsetHeight);
		if(!this.skipInfoTransition && h < this.newInfoHeight) {
			h += Math.ceil((this.newInfoHeight-h)/this.speed);
			this.imageinfoholder.style.height = h + 'px';
			this.ete = new Date().getTime();
			if(!this.skipInfoTransition && this.ete-this.ste > this.skipThreashold) this.skipInfoTransition = true;
			this.ste = new Date().getTime();
			this.timeoutObj = setTimeout(this.name + '.showInfo();', this.timeout);
		} else {
			this.complete();
		}
	}
	this.complete = function() {
		var b = 100;
		var w = this.newWidth;
		var h = this.newImageHeight;
		if(this.record > 1 && !this.blockControls) {
			if(w > b*3) {
				bw = Math.floor((w-b)/2);
				cw = b;
			} else {
				bw = Math.floor((w-(b/2))/2);
				cw = w-(bw*2);
			}
			this.setButton('prev', bw, h);
			this.setButton('close', cw, h);
			this.setButton('next', bw, h);
		} else {
			this.setButton('close', w, h);
		}
		//
		this.imagemoving = false;
		this.imageopen = true;
	}
	this.setDimensions = function(o, w, h) {
		this.setSize(o, 'width', w);
		this.setSize(o, 'height', h);
	}
	this.setSize = function(o, d, s) {
		this[o].style[d] = s + 'px';
	}
	this.setButton = function(t, w, h) {
		var bObj = this['image' + t];
		var buttonObj = this['image' + t + 'button'];
		this.changeClass(bObj, this.o);
		this.changeClass(buttonObj, this.o);
		bObj.style.height = h + 'px';
		bObj.style.width = w + 'px';
		bObj.style.top = -h + 'px';
		buttonObj.style.height = h + 'px';
		buttonObj.style.width = w + 'px';
		this.changeClass(buttonObj, this.c);
	}
	//
	this.closeImage = function() {
		this.imagetag.src = '';
		this.resetItems();
		this.hideImage();
	}
	this.hideImage = function() {
		if(this.skipImageTransition) {
			this.alpha = 0;
			this.setAlpha(this.imagetag);
		}
		if(this.alpha > 0) {
			this.alpha -= 10;
			this.setAlpha(this.imagetag);
			this.timeoutObj = setTimeout(this.name + '.hideImage();', this.timeout);
		} else {
			this.imageopen = false;
			this.openImage();
		}
	}
	//
	this.showSelect = function (dir, d) {
		this.changeClass(this['image' + dir + 'button'], 'photoviewer-' + (d ? 'open' : 'closed'));
	}
	this.selectNext = function () {
		this.position = (this.position < this.record-1) ? this.position+1 : 0;
		this.selectImage();
	}
	this.selectPrev = function () {
		this.position = (this.position > 0) ? this.position-1 : this.record-1;
		this.selectImage();
	}
	this.close = function() {
		if(!this.dontclose) {
			if(this.imagemoving) {
				clearTimeout(this.timeoutObj);
			}
			this.resetItems();
			this.changeClass(this.viewer, this.c);
		}
		this.dontclose = false;
	};
	this.nothing = function() {
		this.dontclose = true;
	};
	this.resetItems = function() {
		if(this.current != 'undefined') this.changeClass(this.getItem('photoviewerlink' + this.current), '');
		this.changeClass(this.imagenext, this.c);
		this.changeClass(this.imageprev, this.c);
		this.changeClass(this.imageclose, this.c);
		this.changeClass(this.imageinfoholder, this.c);
	};
	this.setMainPosition = function() {
		this.viewer.style.top = parseInt(this.getScrollY()) + 'px';
	};
	this.setSubPosition = function() {
		this.holder.style.left = Math.max((this.getWidth()/2) - (parseInt(this.holder.offsetWidth)/2), 0) + 'px';
		this.holder.style.top = Math.max((this.getHeight()/2) - (parseInt(this.holder.offsetHeight)/2), 0) + 'px';
	};
	this.setAlpha = function(obj) {
		obj.style.opacity = this.alpha/100;
		obj.style.filter = 'alpha(opacity=' + this.alpha + ')';
	}
	this.setup = function() {
		window.onscroll = function() {
			photoviewer.setMainPosition();
			photoviewer.setSubPosition();
		};
		window.onresize = function() {
			photoviewer.setSubPosition();
		}
		//
		this.viewer = this.getItem('photoviewer');
		this.holder = this.getItem('photoviewerholder');
		this.imageholder = this.getItem('photoviewerimageholder');
		this.image = this.getItem('photoviewerimage');
		this.imagetag = this.getItem('photoviewerimagetag');
		this.imagenext = this.getItem('photoviewernext');
		this.imagenextbutton = this.getItem('photoviewernextbutton');
		this.imageprev = this.getItem('photoviewerprev');
		this.imageprevbutton = this.getItem('photoviewerprevbutton');
		this.imageclose = this.getItem('photoviewerclose');
		this.imageclosebutton = this.getItem('photoviewerclosebutton');
		this.imageinfoholder = this.getItem('photoviewerinfoholder');
		this.imageinfo = this.getItem('photoviewerinfo');
		this.imageinfotitle = this.getItem('photoviewertitle');
		this.imageinfodescription = this.getItem('photoviewerdescription');
		//
		this.setupdone = true;
	};
	this.getItem = function(obj) {
		var obj = (document.getElementById(obj) != undefined) ? document.getElementById(obj) : false;
		if (!obj && !obj.style) obj.style = obj;
		return obj;
	};
	this.changeClass = function(obj, name) {
		obj.className = name;
	};
	this.getWidth = function() {
		return (typeof(window.innerWidth) == 'number') ?  window.innerWidth : document.documentElement.clientWidth;
	};
	this.getHeight = function() {
		return (typeof(window.innerHeight) == 'number') ?  window.innerHeight : document.documentElement.clientHeight;
	};
	this.getScrollX = function() {
		return document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft;
	};
	this.getScrollY = function() {
		return document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
	};
	this.getListPos = function(id) {
		for(var i=0; i<this.record; i++) if(id == this.list[i]) return i;
	};
}
photoviewer = new PHOTOVIEWERAPP();
photoviewer.name = "photoviewer";
