_LOE._classes.InnovationStage = new Class({
	Implements: [Events, Options, _LOE._libs.jQuery],
	options: {
		
	},
	cancelAutoScroll : false,
	
	initialize: function(options) {
		$('EntertainmentInfo').getElement('.entertainmentInfo').addClass('active');
		$(window).scrollTo(0,0);
		this.setJQuery();
		this.setOptions(options);
		this.onScroll = false;
		this.viewPort = this.jQuery(window);
		this.stageElement = this.jQuery('#EntertainmentStage');
		this.setupContent();
		this.setupImgs();
		this.resizeWindow();
		this.viewPort.resize(this.resizeWindow.bind(this));
	},
	setupContent: function() {
		//this.content = this.jQuery('#subPageWrapper');
		this.content = this.jQuery('#content'); // mod bs
		this.contentH = this.content.position();
		this.contentOffset = this.content.offset().top;
		this.contentButton = this.jQuery('#contentButton');
		this.contentButton.show();
		this.contentButton.click(this.showContent.bind(this));
		this.win = this.jQuery(window);
		this.win.scroll(this.toggleButton.bind(this));
		
		//this.jQuery(document).mouseleave(this.clearIntval.bind(this));
		//this.jQuery(document).mouseenter(this.setIntval.bind(this));
	},
	setupImgs: function() {
		var self = this;
		this.stageImgs = this.stageElement.find('img.entertainmentImg');
		this.header = this.jQuery('#header');
		this.teaserLeft = this.jQuery('#teaserLeft');
		this.teaserLeft.show();
		this.teaserLeft.click(function(){self.cancelAutoScroll = true;});
		this.teaserLeft.click(this.showPrev.bind(this));
		this.teaserRight = this.jQuery('#teaserRight');
		this.teaserRight.show();
		this.teaserRight.click(function(){self.cancelAutoScroll = true;});
		this.teaserRight.click(this.showNext.bind(this));
		
		var firstImg = this.stageImgs.first(),
		  otherImg = this.stageImgs.not(firstImg);
		firstImg.toggleClass('active');
		if(firstImg.data('color') == 'white') this.toggleWhite();
		otherImg.each(function(i, e) {
		var img = this.jQuery(e);
		img.css('left', -3000);
		}.bind(this));
		this.setIntval();
	},
	clearIntval: function() {
		clearInterval(this.interval);
		this.interval = false;
		this.onScroll = true;
	},
	setIntval: function() {	
		if(!this.interval && !this.cancelAutoScroll) this.interval = window.setInterval(this.showNext.bind(this), 6000);
		this.onScroll = false;
	},
	showNext: function() {
		if(this.onScroll == true) return;
		this.clearIntval();
		var actImg = this.stageElement.find('img.active');
		var firstImg = this.stageImgs.first('img');
		var lastImg = this.stageImgs.last('img');
		var nextImg = actImg.next('img.entertainmentImg');
		
		if(nextImg.length == 0) nextImg = firstImg;
		nextImg.css({
		  'width': actImg.width(),
		  'height': actImg.height(),
		  'left': actImg.width()
		});
		actImg.toggleClass('active').animate({'left': -actImg.width()}, 1000, 'linear');
		nextImg.toggleClass('active').animate({'left': 0}, 1000, 'linear', this.fadeComplete.bind(this));
		if((nextImg.data('color') == 'white' && !this.header.hasClass('white')) || (nextImg.data('color') == 'black' && this.header.hasClass('white'))) {
			this.toggleWhite();
		}
		this.showInfo(nextImg.data('entertainment'));
	},
	showPrev: function() {
		if(this.onScroll == true) return;
	    this.clearIntval();
	    var actImg = this.stageElement.find('img.active'),
	        firstImg = this.stageImgs.first('img'),
	        lastImg = this.stageImgs.last('img'),
	        prevImg = actImg.prev('img.entertainmentImg');
	    if(prevImg.length == 0) prevImg = lastImg;
	    prevImg.css({
	      'width': actImg.width(),
	      'height': actImg.height(),
	      'left': -actImg.width()
	    });
	    actImg.toggleClass('active').animate({'left': actImg.width()}, 1000, 'linear');
	    prevImg.toggleClass('active').animate({'left': 0}, 1000, 'linear', this.fadeComplete.bind(this));
	    if((prevImg.data('color') == 'white' && !this.header.hasClass('white')) || (prevImg.data('color') == 'black' && this.header.hasClass('white'))) {
	        this.toggleWhite();
	    }
	    this.showInfo(prevImg.data('entertainment'));
	},
	fadeComplete: function() {
		this.setIntval();
	},
	toggleWhite: function() {
		this.header.toggleClass('white');
    	this.contentButton.toggleClass('white');
    	this.jQuery('#teaserLeft').toggleClass('white');
    	this.jQuery('#teaserRight').toggleClass('white');
	},
	showInfo: function(infoID) {
		var actInfoBox = this.jQuery('div.entertainmentInfo.active');
	    var allInfoBox = this.jQuery('div.entertainmentInfo');
	    var nextInfoBox = this.jQuery('div.entertainmentInfo[data-entertainment=' + infoID + ']');
	    actInfoBox.fadeOut(1000, function() {allInfoBox.removeClass('active');});
	    nextInfoBox.fadeIn(1000, function() {nextInfoBox.toggleClass('active');});
	},
	showContent: function(e) {
		this.jQuery('html, body').animate({scrollTop: this.content.offset().top + 'px'}, 500, this.toggleButton.bind(this));
	},
	toggleButton: function() {

		
	  var winPos = this.win.scrollTop();
		if(winPos <= this.contentOffset) {
		  this.contentButton.fadeIn();
		  this.teaserLeft.fadeIn();
		  this.teaserRight.fadeIn();
		} else {
		  this.contentButton.fadeOut();
		  this.teaserLeft.fadeOut();
		  this.teaserRight.fadeOut();
		}
		
		/* fix stageplus */
		/*
		if(jQuery('#EntertainmentInfo .entertainmentInfo.active').has('.stageitemplus').length){
			var calculatedHeight = this.viewPort.height().toInt() - this.contentH.top;
			var correctPlusHeight = jQuery('#EntertainmentInfo .entertainmentInfo.active .stageitemplus').height();
			if(correctPlusHeight>calculatedHeight){
				this.content.css('top', correctPlusHeight);
			}
		}else{
			var calculatedHeight = this.viewPort.height().toInt() - this.contentH.top;
			this.content.css('top', calculatedHeight);
		}
		*/
	},
	resizeWindow: function() {
		var onTopImg = this.stageElement.find('.active.entertainmentImg'),
			viewPortW = this.viewPort.width().toInt(),
			viewPortH = this.viewPort.height().toInt(),
			viewPortHB = viewPortH/2 + viewPortH;
		if(viewPortW <= 1006) {
			if(window.console) console.log('Width');
		}
		this.stageElement.width(viewPortW);
		//this.stageElement.css({"margin-left": -(viewPortW)});
		this.stageElement.height(viewPortH);
		onTopImg.height(viewPortH);
		onTopImg.width('auto');
		if(viewPortW > onTopImg.width()) {
			onTopImg.width(viewPortW);
			onTopImg.height('auto');
			
		}
		this.content.css('top', viewPortH - this.contentH.top);
		
		/* fix stageplus */
		/*
		if(jQuery('#EntertainmentInfo .entertainmentInfo.active').has('.stageitemplus').length){
			var calculatedHeight = this.viewPort.height().toInt() - this.contentH.top;
			var correctPlusHeight = jQuery('#EntertainmentInfo .entertainmentInfo.active .stageitemplus').height();
			if(correctPlusHeight>calculatedHeight){
				this.content.css('top', correctPlusHeight);
			}
		}	
		*/
		this.fireEvent('resizeWindow');
	}
});

