var HQgallery = {
	objMain:null,
	arrIMGs:null,
	arrDescriptions:null,
	leftIMGcon:null,
	centerIMGcon:null,
	rightIMGcon:null,
	leftIMG:null,
	centerIMG:null,
	rightIMG:null,
	descContainer:null,
	thumbHeight:120,
	imgCount:null,
	currentPicture:0,
	opValue:0,
	resInterval:0,
	init:function() {
		this.objMain = document.getElementById('hq_gallery');
		this.createIMGarray();
		/* Left container */
		this.leftIMGcon = document.createElement('div');
		this.leftIMGcon.setAttribute('id', 'left_img');
		/* Central container */
		this.centerIMGcon = document.createElement('div');
		this.centerIMGcon.setAttribute('id', 'center_img');
		/* Right container */
		this.rightIMGcon = document.createElement('div');
		this.rightIMGcon.setAttribute('id', 'right_img');
		
		this.objMain.appendChild(this.leftIMGcon);
		this.objMain.appendChild(this.centerIMGcon);
		this.objMain.appendChild(this.rightIMGcon);
		
		this.initGallery();
	},
	prepare:function() {
		var tmpObj = document.getElementsByTagName('head')[0];
		var newStyle = document.createElement('link');
		newStyle.setAttribute('rel', 'stylesheet');
		newStyle.setAttribute('href', 'hq_gallery.css');
		newStyle.setAttribute('type', 'text/css');
		tmpObj.appendChild(newStyle);
	},
	createIMGarray:function() {
		this.arrObjIMGs = new Array();
		this.arrIMGs = new Array();
		this.arrDescriptions = new Array();
		this.arrObjIMGs = this.objMain.getElementsByTagName('img');
		for(var i=0; i<this.arrObjIMGs.length; i++) {
			this.arrIMGs[i] = this.arrObjIMGs[i].getAttribute('src');
			this.arrDescriptions[i] = this.arrObjIMGs[i].getAttribute('alt');
		}	
		this.imgCount = i;
		this.objMain.innerHTML = '';
	},
	getResizedSize:function(objImage) {
		var tmpArray = new Array();
		tmpArray[0] = objImage.width;
		tmpArray[1] = objImage.height;
		alert(tmpArray[0]);
	},
	initGallery:function() {
		/* Left thumbnail */
		this.leftIMG = new Image();
		this.leftIMG.src = this.arrIMGs[this.imgCount-1];
		extender.setOpacity(this.leftIMG, 5);
		this.leftIMG.onmouseover = function() {
			extender.setOpacity(HQgallery.leftIMG, 7);
		}
		this.leftIMG.onmouseout = function() {
			extender.setOpacity(HQgallery.leftIMG, 5);
		}
		this.leftIMG.onclick = function() {
			HQgallery.galBackward();
		}
		
		/* Center */
		this.centerIMG = new Image();
		this.centerIMG.src = this.arrIMGs[0];
		this.centerIMG.style.marginTop = '-' + this.centerIMG.height/2 + 'px';
		
		/* Right */
		this.rightIMG = new Image();
		this.rightIMG.src = this.arrIMGs[1];
		extender.setOpacity(this.rightIMG, 5);
		this.rightIMG.onmouseover = function() {
			extender.setOpacity(HQgallery.rightIMG, 7);
		}
		this.rightIMG.onmouseout = function() {
			extender.setOpacity(HQgallery.rightIMG, 5);
		}
		this.rightIMG.onclick = function() {
			HQgallery.galForward();
		}
		
		/* Description */
		this.descContainer = document.createElement('div');
		this.descContainer.setAttribute('id', 'description');
		this.descContainer.innerHTML = '1 - ' + this.arrDescriptions[0];
		
		this.leftIMGcon.appendChild(this.leftIMG);
		this.centerIMGcon.appendChild(this.centerIMG);
		this.rightIMGcon.appendChild(this.rightIMG);
		this.objMain.appendChild(this.descContainer);
	},
	galForward:function() {
		// extender.setOpacity(this.centerIMG, 0);
		clearInterval(HQgallery.resInterval);
		this.opValue = 0;
		if(this.currentPicture == this.imgCount-1) {
			this.centerIMG.src = this.arrIMGs[0];
			this.rightIMG.src = this.arrIMGs[1];
			this.leftIMG.src= this.arrIMGs[this.imgCount-1];
			this.currentPicture = 0;
		}
		else {
			this.leftIMG.src= this.arrIMGs[this.currentPicture];
			this.centerIMG.src = this.arrIMGs[++this.currentPicture];
			if(this.currentPicture == this.imgCount-1)
				this.rightIMG.src = this.arrIMGs[0];
			else
				this.rightIMG.src = this.arrIMGs[this.currentPicture+1];
		}
		this.finishChange();
	},
	galBackward:function() {
		// extender.setOpacity(this.centerIMG, 0);
		clearInterval(HQgallery.resInterval);
		this.opValue = 0;
		
		if (this.currentPicture == 0) {
			this.centerIMG.src = this.arrIMGs[this.imgCount-1];
			this.currentPicture = this.imgCount-1;
			this.leftIMG.src = this.arrIMGs[this.imgCount-2];
			this.rightIMG.src = this.arrIMGs[0];
		}
		else {
			this.centerIMG.src = this.arrIMGs[--this.currentPicture];
			this.rightIMG.src = this.arrIMGs[this.currentPicture+1];
			
			if (this.currentPicture == 0) 
				this.leftIMG.src = this.arrIMGs[this.imgCount-1];
			else 
				this.leftIMG.src = this.arrIMGs[this.currentPicture - 1];
		}
		this.finishChange();
	},
	fadeIn:function() {
		if(this.opValue < 100) {
			extender.setOpacity(this.centerIMG, (this.opValue+4)/10);
		}
		else {
			clearInterval(HQgallery.resInterval);
			this.opValue = 0;
		}
	},
	finishChange:function() {
		this.centerIMG.style.marginTop = '-' + this.centerIMG.height/2 + 'px';
		// this.resInterval = setInterval(function() { HQgallery.fadeIn(); }, 30);
		this.descContainer.innerHTML = (this.currentPicture+1) + ' - ' + this.arrDescriptions[this.currentPicture];
	}
}
