var ss = null;
var idTimerEnerCAN = 0;
var mousex = 0, mousey = 0;
//var okToShowEnerCANHelp = true;

/**
 * SlideShowController Class
 */
var SlideShowController = new Class({
	Implements: [Options, Events],
	options: {
		slides: [],
		wrap: true,
		defaultUpTime: 8
	},
	slides: [],
	idTimer: null,
	currentBG: 1,
	now: 0,
	running: true,
	buttons: [],
	currentUpTime: 8,

	initialize: function(options){
		this.setOptions(options);
		this.addSlides(this.options.slides);
		if(this.slides.length) this.showSlide(0);
	},
	addSlides: function(slides){
		$$(slides).each(function(slide){
			this.slides.include($(slide));
			//slide.addEvent('click', this.cycleForward.bind(this));
		}, this);
		if (slides.length > 0) {
			for(var x=0; x < slides.length; x++) {
				var btn = new Element('div',{text:(x+1),'class':'btn'});
				if (x==0) btn.addClass('selected');
				btn.addEvent('click',function(evt) {
					this.onBtnClick(evt.target.get('text')-1)
				}.bind(this));
				this.buttons[x] = btn;
				$('slideShowControls').adopt(btn);
			}
			var playStop = new Element('div',{'id':'playPauseBtn','class':'onPause'});
			playStop.addEvent('click',function(evt) {
				this.toggleTimer();
			}.bind(this));
			this.buttons[slides.length] = playStop;
			$('slideShowControls').adopt(playStop);
		}
		//console.log(slides);
	},
	addSlide: function(slide){
		this.addSlides(Array.from($(slide)));
	},
	cycleForward: function(){
		var toShow=0;
		if(this.now < this.slides.length-1) toShow = this.now+1;
		this.showSlide(toShow);
	},
	cycleBack: function(){
		var toShow=this.slides.length-1;
		if(this.now > 0) toShow = this.now-1;
		this.showSlide(toShow);
	},
	showSlide: function(iToShow){
		if (this.fading) return;
		this.selectButton(iToShow);
		var now = this.now;	    
		var currentSlide = this.slides[now];
		var slide = this.slides[iToShow];
		var itemprop = JSON.decode(slide.getProperty('itemprop'));
		var upTime = (itemprop && itemprop.sec) ? itemprop.sec : this.options.defaultUpTime;
		var bg = (itemprop && itemprop.bg) ? itemprop.bg : null;

		var fadeIn = function (s){
			this.fading = true;
			s.setStyles({
				display:'block',
				visibility: 'visible',
				opacity: 0
			});
			s.get('tween').start('opacity', 1).chain(function(){
				this.fading = false;
				this.setBackground(bg);
				this.fireEvent('onShow', [slide, iToShow]);
			}.bind(this));
		}.bind(this);
		if(slide) {
			if($chk(now) && now != iToShow){
				this.fading = true;
				currentSlide.get('tween').start('opacity', 0).chain(function(){
					currentSlide.setStyle('display', 'none');
				}.bind(this));
				fadeIn(slide);
			} else fadeIn(slide);
			this.now = iToShow;
		}
		this.currentUpTime = upTime;
		this.prepareNextSlide(upTime);
	},
	selectButton: function(bToShow){
		this.buttons[this.now].removeClass('selected');
		this.buttons[bToShow].addClass('selected');
	},
	onBtnClick: function(iToShow) {
		this.clearTimer();
		this.setRunning(false);
		this.showSlide(iToShow);
	},
	setRunning: function(isRunning){
		this.running = isRunning;
		this.setPlayBtn(this.running);
	},
	prepareNextSlide: function(sec){
		this.clearTimer();
		if (sec > 0 && this.running)
			this.idTimer = setTimeout("ss.cycleForward();",sec*1000);
	},
	clearTimer: function(){
		if (this.idTimer) clearTimeout(this.idTimer);
	},
	stopTimer: function(){
		this.clearTimer();
		this.setRunning(false);
	},
	startTimer: function(){
		this.clearTimer();
		this.setRunning(true);
		this.prepareNextSlide(this.currentUpTime);
	},
	toggleTimer: function() {
		if (this.running) this.stopTimer();
		else this.startTimer();
	},
	setPlayBtn: function(toPlause) {
		if (toPlause == undefined) toPlause = true;
		if (toPlause) {
			$('playPauseBtn').addClass('onPause');
		} else {
			$('playPauseBtn').removeClass('onPause');
		}
	},
	setBackground: function(bg) {
		var currDIV = $('bg_placeholder'+this.currentBG);
		var currClass = currDIV.getProperty('class');
		if (currClass.length > 0) {
			currDIV.setStyle('z-index',1);
			currDIV.addClass('fadingOut');
			setTimeout("$('bg_placeholder"+this.currentBG+"').setProperty('class','');",1100);
		}
		
		if (!bg) bg = 'defaultBG';
		this.currentBG = (this.currentBG == 1) ? 2 : 1 ;
		var newDIV = $('bg_placeholder'+this.currentBG);
		newDIV.setStyle('z-index',2);
		newDIV.addClass(bg);
	}
});

var showEnerCANHelp = function(){ $('enerCANHelp').fade('in'); }

window.addEvent('load',function() {
	ss = new SlideShowController({
		slides: $$('div.automaticSlide')
	});
	
	$('enerCanLabel').addClass('enercanHover').addEvent('click',function(e){
		$('enerCan_modelInput').focus();
		e.target.fade('hide');
	});
	$('enerCan_modelInput').addEvents({
		focus: function(){
			$('enerCanLabel').fade('hide');
		},
		blur: function(e){
			if (e.target.value.length == 0) {
			$('enerCanLabel').fade('show');
			}
		}
	});
	$('enerCANzone').addEvent('mouseover',showEnerCANHelp);
	$('enerCANzone').getElement('h5').addEvent('click',showEnerCANHelp);
	$('enerCANHelp').addEvents({
		mouseout:function(){$('enerCANHelp').fade('out');},
		click:function(){
			$('enerCANzone').removeEvent('mouseover',showEnerCANHelp);
			$('enerCANHelp').fade('out');
		}
	}).fade('hide');
});
