﻿var BannerRotate = {

	init : function(){
		var that = this;
		if(document.getElementById("common_banner")){
			this.nBannerList = document.getElementById("banner_list").childNodes;
			this.currentBanner = document.getElementById("current_banner").firstChild;
			if(this.nBannerList.length > 1){
				this.getBannerStructure();
				setInterval(function(){
					that.rotateBanner()
				}, 15000);
			}
			this.pos = 0;
		}
	},

	getBannerStructure : function(){
		var bannerStructure = this.currentBanner.childNodes;
		this.topControl = bannerStructure[0].lastChild;
		this.middleControl = bannerStructure[1].firstChild;
		this.bottomControl = bannerStructure[2].firstChild;
		this.incutControl = bannerStructure[3].firstChild;
	},

	rotateBanner : function(){
		this.checkBannerIndex();
		this.rotateText();
		this.rotateImage();
	},

	rotateText : function(){
		var that = this;
		var hideTopConrol = new Tween(this.topControl, 'opacity', EEQ.linear, 1, 0, 30),
			hideBottomConrol = new Tween(this.bottomControl, 'opacity', EEQ.linear, 1, 0, 30),
			hideIncutControl = new Tween(this.incutControl, 'opacity', EEQ.linear, 1, 0, 30);

		hideTopConrol.onMotionFinished = function(){
			
			Common.Class.add(that.topControl, 'invisible');
			Common.Class.add(that.bottomControl, 'invisible');
			Common.Class.add(that.incutControl, 'invisible');
			
			that.showNewTextInner(that.topControl, 'top');
			that.showNewTextInner(that.bottomControl, 'bottom');
			that.showNewTextInner(that.incutControl, 'incut');
		}

	},

	rotateImage : function(){
		var that = this;
		var newInner = this.getNewInner('middle').firstChild.cloneNode(true),
			newHref = this.getNewInner('middle').href;
			
		Common.Class.add(newInner, 'fade');
		document.getElementById('main_content_banner_incut_control').style.paddingLeft = newInner.width + 'px';
		
		this.middleControl.firstChild.href = newHref;
		this.middleControl.firstChild.insertBefore(newInner, this.middleControl.firstChild.firstChild);
		
		var hideMiddleConrol = new Tween(this.middleControl.firstChild.lastChild, 'opacity', EEQ.linear, 1, 0, 60);
		
		hideMiddleConrol.onMotionFinished = function(){
			Common.Class.remove(that.middleControl.firstChild.firstChild, 'fade');
			that.middleControl.firstChild.removeChild(that.middleControl.firstChild.lastChild);

		}
	},

	showNewTextInner : function(control, target){
		var that = this;
		control.innerHTML = this.getNewInner(target);
		setTimeout(function(){
			Common.Class.remove(control, 'invisible')
		}, 20);
		var showNewControl = new Tween(control, 'opacity', EEQ.linear, 0, 1, 70);
		showNewControl.onMotionFinished = function(){
			control.style.filter = '';
		}
	},

	getNewInner : function(target){
		var bannerStructure = this.getNewBanner().childNodes;
		if(target == 'top'){
			return bannerStructure[0].innerHTML;
		} else if(target == 'middle'){
			return bannerStructure[1].firstChild;
		} else if(target == 'bottom') {
			return bannerStructure[2].innerHTML;
		} else if(target == 'incut') {
			return bannerStructure[3].innerHTML;
		}
	},


	getNewBanner : function(){
		return this.nBannerList[this.pos];
	},
	
	checkBannerIndex : function(){
		this.nBannerList[this.pos + 1] ? ++this.pos : this.pos = 0;
	}

};


Common.Event.add(window, "load", function(){ BannerRotate.init(); });
