var ani;

function PhotoGallery(id){
	this.id = id;
	this.prepare();
}

PhotoGallery.prototype.prepare = function(){
	this.move = document.getElementById(this.id);
	this.drag = false;
	var imgs = this.move.getElementsByTagName('img');
	this.arr = [];
	_my = this;
	document.body.onmouseup = function(){ _my.cancel() };
	for(var i=0; i < imgs.length; i++){
		imgs[i].onmousedown = function(){ _my.cancelImg() };
	}
	this.move.onmousemove = function(evt){ _my.fMove(evt) };
	this.move.onmouseover = function(evt){ _my.fMoveOver(evt) };
	this.move.onmousedown = function(evt){ _my.fMoveDown(evt) };
}

PhotoGallery.prototype.scroll = function(obj){
	ani = setInterval(function(){_my.aniScroll(obj.id)}, 10)
}

PhotoGallery.prototype.aniScroll = function(id){
	var vLeft = this.move.offsetLeft;
	switch(id){
		case 'Left':
			if(this.move.offsetLeft < 0)
				this.move.style.left = vLeft + 5 + "px";
			break;
		case 'Right':
			if(this.move.offsetLeft > -(this.move.offsetWidth - this.move.parentNode.offsetWidth))
				this.move.style.left = vLeft - 5 + "px";
			break;
	}
}

PhotoGallery.prototype.fMoveDown = function(evt){
	if(evt = checkEvent(evt)){
		if(!this.drag && !this.img && this.move.offsetWidth > this.move.parentNode.offsetWidth){
			this.pos = {x: evt.clientX, y: evt.clientY, move: this.move.offsetLeft}
			this.drag = true;
		}
	}
}

PhotoGallery.prototype.fMoveOver = function(evt){
	this.img = false;
}

PhotoGallery.prototype.fMove = function(evt){
	if(evt = checkEvent(evt)){
		if(this.drag){
			this.arr.push(evt.clientX);
			if(this.arr.length > 2)
				this.arr.shift();
			this.move.style.left = this.pos.move-(this.pos.x-evt.clientX) + "px";
			if(this.move.offsetLeft > 0)
				this.move.style.left = "0px";
			if(this.move.offsetLeft < -(this.move.offsetWidth - this.move.parentNode.offsetWidth))
				this.move.style.left = -(this.move.offsetWidth - this.move.parentNode.offsetWidth) + "px";
		}
	}
}

PhotoGallery.prototype.cancelImg = function(){
	this.drag = false;
	this.img = true;
}

PhotoGallery.prototype.cancel = function(){
	clearInterval(ani);
	if(this.drag){
		this.drag = false;
		if(this.arr[0] && this.arr[1]){
			var diff = this.arr[1]-this.arr[0];
			this.arr = [];
			var t = new Tween(this.move, 'left', Math.easeOutQuart, this.move.offsetLeft, this.move.offsetLeft+(diff*30), 50);
			t.onMotionChanged = function(){
				if((this.target.offsetLeft > 70) || (this.target.offsetLeft < -(this.target.offsetWidth - this.target.parentNode.offsetWidth)-70)){
					this.onMotionFinished();
					this.constructor._remove(this);
				}
			}
			t.onMotionFinished = function(){
				if(this.target.offsetLeft > 0)
					var t = new Tween(this.target, 'left', Math.easeInQuad, this.target.offsetLeft, 0, 20);
				if(this.target.offsetLeft < -(this.target.offsetWidth - this.target.parentNode.offsetWidth))
					var t = new Tween(this.target, 'left', Math.easeInQuad, this.target.offsetLeft, -(this.target.offsetWidth - this.target.parentNode.offsetWidth), 20);
			}
		}
	}
}


var ph = new PhotoGallery('Photo');

var oA = document.getElementById('Photo').getElementsByTagName('a');

var photo = document.getElementById('pic').getElementsByTagName("td")[0];
for(var i=0; i<oA.length; i++){
 addEvent(oA[i], 'click', showPhoto);
}

function showPhoto(evt){
	if(evt = checkEvent(evt)){
		var parent = evt.target;

		do{
			if(parent.tagName == "A")
				break
		}while(parent = parent.parentNode)

		var oAs = parent.parentNode.getElementsByTagName("a");
		for(var i=0; i<oAs.length; i++){
			oAs[i].className = "";
			if(oAs[i] == parent)
				oAs[i].className = "nocur";
		}

		photo.innerHTML = '';
		var img = document.createElement("img");
		img.setAttribute("src", parent.href);
		photo.appendChild(img);

		if(-evt.target.offsetLeft > ph.move.offsetLeft)
			var tw = new Tween(ph.move, 'left', Math.easeInQuad, ph.move.offsetLeft, -evt.target.offsetLeft, 10);
		
		if((evt.target.offsetLeft+evt.target.offsetWidth) > (ph.move.parentNode.offsetWidth-ph.move.offsetLeft)){
			var wt = new Tween(ph.move, 'left', Math.easeInQuad, ph.move.offsetLeft, ph.move.offsetLeft-(evt.target.offsetLeft+evt.target.offsetWidth)+(ph.move.parentNode.offsetWidth-ph.move.offsetLeft), Math.round(((evt.target.offsetLeft+evt.target.offsetWidth)-(ph.move.parentNode.offsetWidth-ph.move.offsetLeft))/90*10));
		}
		
		cancelEvent(evt);
		return false;
	}
}
