var gallery = {

	init : function(){
		this.eCtrlPrev = document.getElementById('backward_gallery'); 
		this.eCtrlNext = document.getElementById('forward_gallery');
		this.ePhotoList = document.getElementById('gallery_photo');
		this.eDescriptionList = document.getElementById('gallery_description');
		this.iPos = 0;

		this.bPresentAnimatimg = false;
		this.bCalculateWidth = true;
		
		this.checkWidth();
		this.checkStructure();
		this.attachEvents();
	}, 
	
	attachEvents : function(){
		var that = this;
		Common.Event.add(this.eCtrlNext, 'click', function(){ if(that.bPresentAnimatimg == false){gallery.moteGallery();} });
		Common.Event.add(this.eCtrlPrev, 'click', function(){ if(that.bPresentAnimatimg == false){gallery.moteGallery('backward');} });
	}, 
	
	updateIPos : function(){
		for(i = 0, a = this.ePhotoList.firstChild.childNodes.length; i < a ; i++ ){
			aItem = this.ePhotoList.firstChild.childNodes
			if(Common.Class.match(aItem[i], 'selected')){
				this.iPos = i;
			}
		}
	},

	nextIPos : function(){
		if(this.iPos >= this.ePhotoList.firstChild.childNodes.length-1)
                        return 0;
                else
                        return this.iPos+1;
	},
	
	checkWidth : function(){
		if(this.bCalculateWidth){
			this.calculateWidth(this.ePhotoList.firstChild);
			this.calculateWidth(this.eDescriptionList.firstChild);
			this.bCalculateWidth = false;
		}
	},
	
	calculateWidth : function(obj){
		var countWidth = 0;
		for(i = 0, a = obj.childNodes.length; i < a ; i++){
			countWidth += obj.childNodes[i].offsetWidth;
		}

		obj.style.width = countWidth + 'px';
	},
	
	checkStructure : function(){
		var i = this.iPos,
			aItem = this.ePhotoList.firstChild.childNodes,
			aItem = aItem[i];


		if(!aItem.previousSibling || !aItem.nextSibling){
			this.deleteClear(this.eDescriptionList);

			this.updateStructure(this.ePhotoList, i);
			this.updateStructure(this.eDescriptionList, i);
			
			this.insertClear(this.eDescriptionList);
		}

		
	},
	
	updateStructure : function(obj, i){

		var eTarget = obj.firstChild,
			eFirstChild = eTarget.firstChild,
			eLastChild = eTarget.lastChild;

		if(i == 0){
			eTarget.insertBefore(eLastChild, eFirstChild);
		} else {
			eTarget.appendChild(eFirstChild);
		}
		
		this.updateIPos();
		this.updateOffset(obj);
	},

	deleteClear : function(obj){
		var mTarget = obj.firstChild.childNodes;
		
		for(i= 0, c = mTarget.length; i < c; i++){
			if(Common.Class.match(mTarget[i], 'clear')){
				obj.firstChild.removeChild(mTarget[i]);
			}
		}
	},
	
	insertClear : function(obj){
		var eTarget = obj.firstChild,
			elem = document.createElement('li');
		Common.Class.add(elem, 'clear');
		eTarget.appendChild(elem);
	},
	
	updateOffset : function(obj){
		var target = obj.firstChild,
			model = target.childNodes,
			model = model[this.iPos];
			
		target.style.left = '-' + model.offsetLeft + 'px';
	},
	
	moteGallery : function(sDirection){
		this.disableControls(1);
		
		var that = this,
			i = this.iPos,
			photoTarget = this.ePhotoList.firstChild,
			photoTmp = photoTarget.childNodes,
			photoWidth = this.ePhotoList.firstChild.firstChild.offsetWidth,
			photoStart = 0 - (photoWidth * i),
			photoEnd = sDirection ? photoStart + photoWidth : photoStart - photoWidth ,
			descriptionTarget = this.eDescriptionList.firstChild,
			descriptionTmp = descriptionTarget.childNodes,
			descriptionWidth = this.eDescriptionList.firstChild.firstChild.offsetWidth,
			descriptionStart = 0 - (descriptionWidth * i),
			descriptionEnd = sDirection ? descriptionStart + descriptionWidth : descriptionStart - descriptionWidth;


		Common.Class.remove(photoTmp[i], 'selected');
		Common.Class.remove(descriptionTmp[i], 'selected');

		Common.Class.add(
			sDirection ? photoTmp[i - 1] : photoTmp[i+1]
			, 'selected'
		);
		
		Common.Class.add(
			sDirection ? descriptionTmp[i - 1] : descriptionTmp[i+1]
			, 'selected'
		);


		var photoAnimate = new Tween(photoTarget, 'left', EEQ.Cubic.easeOut, photoStart, photoEnd, 20);
		var descriptionAnimate = new Tween(descriptionTarget, 'left', EEQ.Cubic.easeOut, descriptionStart, descriptionEnd, 20);


		photoAnimate.onMotionFinished = function() {
			that.updateIPos();
			that.checkStructure();
			that.disableControls();
		};
	},
	

	disableControls : function(b){
		if(b){
			this.bPresentAnimatimg = true;
			Common.Class.add(this.eCtrlPrev, 'disabled');
			Common.Class.add(this.eCtrlNext, 'disabled');
		} else {
			this.bPresentAnimatimg = false
			Common.Class.remove(this.eCtrlPrev, 'disabled');
			Common.Class.remove(this.eCtrlNext, 'disabled');
		}
	}
	
};




var saveMe = {
	init : function(){
		document.getElementById('header').style.display = 'none';
		document.getElementById('balloon').style.display = 'none';
		document.getElementById('navigation').style.display = 'none';
	}
}
Common.Event.add(window, "load", function(){
    try { gallery.init(); } catch (e) {}
});


