// ===== Enable Sound FXs =========================================================
soundManager.url = '/Portals/_default/Skins/OERBFlashSubs/audio/soundmanager2.swf'; // override default SWF url
soundManager.debugMode = false;
soundManager.consoleOnly = false;

soundManager.onload = function() {
	soundManager.createSound('correct','/Portals/_default/Skins/OERBFlashSubs/audio/correct.mp3');
	soundManager.createSound('incorrect','/Portals/_default/Skins/OERBFlashSubs/audio/incorrect.mp3');
}

// ===== Carousel Quiz ===============================================================================
var carousels = [];
window.addEvent('domready',function(){
	$$('.carousel').each(function(el,i){
		var container = el.getElement('div.carouselContent');
		var items = el.getElements('.carouselItem');
		var controls = el.getElement('.controls');
		carousels[i] = new SlideCarousel(container,{
			items:items,
			frameSize:el.getStyle('width').toInt(),
			itemSize:items[0].getStyle('width').toInt(),
			display:container.getElement('.galleryCount'),
			prev:controls.getElement('.btnPrev'),
			next:controls.getElement('.btnNext')
		});
	});
});
var SlideCarousel = new Class({
	initialize: function(container, options) {
		this.setOptions(options);
		this.active = false;
		this.activeImage = 1;
		this.container = container;
		this.aItems = $A(this.options.items);
		this.nDisplay = this.options.display;
		this.itemSize = this.options.itemSize;
		this.itemsTotalWidth = this.aItems.length*this.itemSize;
		this.container.setStyle('width',this.itemsTotalWidth+'px');
		this.itemsTotal = this.aItems.length;
		this.pageSize = Math.floor(this.options.frameSize / this.options.itemSize);
		this.pageTotal = (this.itemsTotal%this.pageSize==0)?this.itemsTotal:(this.pageSize-(this.itemsTotal%this.pageSize)+this.itemsTotal);
		this.totalPages = Math.floor(this.pageTotal / this.pageSize);
		this.nDisplay.setHTML(this.activeImage +" of "+ this.totalPages);
		if(this.totalPages > 1){
			this.prevLink = this.options.prev;
			this.nextLink = this.options.next;
			this.prevLink.addClass('btnDisabled');
			this.nextLink.addClass('btnEnabled');
			this.prevLink.addEvent("click", function(event) {new Event(event).stop(); this.previous()}.bind(this));
			this.nextLink.addEvent("click", function(event) {new Event(event).stop(); this.next()}.bind(this));
		} else { 
			this.options.prev.remove();
			this.options.next.remove();
			this.options.display.remove();
		}
		
		this.xPos = this.container.getStyle('left').toInt();
		this.position = new Fx.Style(this.container, 'left', {
			duration: 750,
			transition:Fx.Transitions.quartInOut,
			onStart: function(){
				// disable both buttons
				this.prevLink.removeClass("btnEnabled");
				this.nextLink.removeClass("btnEnabled");
				this.prevLink.addClass("btnDisabled");
				this.nextLink.addClass("btnDisabled");
				$$('.answerCheck').getElement('.right').addClass("hide");
				$$('.answerCheck').getElement('.wrong').addClass("hide");
				this.active = true;
			}.bind(this),
			onComplete: function(){
				if(this.activeImage > 1){
					this.prevLink.removeClass("btnDisabled");
					this.prevLink.addClass("btnEnabled");
				}
				if(this.activeImage < this.totalPages){
					this.nextLink.removeClass("btnDisabled");
					this.nextLink.addClass("btnEnabled");
					this.nextLink.addClass("hide");
				}
				this.active = false;
			}.bind(this)
		});
	},
	previous: function() {
		if(this.active) return;
		return this.changeImage(this.activeImage-1);
	},
	next: function() {
		if(this.active) return;
		return this.changeImage(this.activeImage+1);
	},
	changeImage: function(imageNum) {
		if((imageNum <= 0) || (imageNum > this.totalPages)) return false;
		this.xPos = this.container.getStyle('left').toInt();
		var dir = (imageNum > this.activeImage)? -1 : 1;
		this.position.custom(this.xPos,this.xPos+((this.itemSize*this.pageSize)*dir));
		this.activeImage = imageNum;
		this.nDisplay.setHTML(this.totalPages-1);
	}
});
SlideCarousel.implement(new Options);

window.addEvent('domready',function(){
	var correctQuestions = 0;
	var answerCheck = $$('.answerCheck');
	var right = answerCheck.getElement('.right');
	var wrong = answerCheck.getElement('.wrong');
	var suggest = $$('.suggest');
	var quizIntro = $$('.quizIntro');
	
	quizIntro.addEvent('click', function(e){
		e = new Event(e);
		this.addClass('hide');
		e.stop();
	});
	
	$$('.question').each(function(el,i){
		var answer = el.getElements('li.answer');
		var photo = el.getElements('.photo');
		var nextQuestion = $$('.btnNext');
		var amtCorrect = $$('.amtCorrect');
		var answered = 0;
		var finished = el.getElement('ol');
		answer.addEvent('click', function(e){
			e = new Event(e);
			if(answered > 0) return;
			answered = answered + 1;
			photo.addClass("hide");
			nextQuestion.removeClass("hide");

			// Add correct/incorrect number and copy
			if (this.hasClass("correct")) {
				correctQuestions = correctQuestions + 1;
				right.removeClass('hide');
				soundManager.play('correct');
			} else {
				wrong.removeClass('hide');
				soundManager.play('incorrect');
			};

			// Change the final message
			if (correctQuestions < 4) {
				suggest.setHTML("You may be wasting a lot of energy - and money. Find ways you can save <a href=/Conservation/YearRound/tabid/172/Default.aspx?quiz>here</a>.");
			} else if (correctQuestions > 3 && correctQuestions < 7) {
				suggest.setHTML("You are conservation aware, but there's room to <a href=/Conservation/YearRound/tabid/172/Default.aspx?quiz>improve</a>.");
			} else if (correctQuestions > 6 && correctQuestions < 10) {
				suggest.setHTML("You are conservation-wise. Be sure to keep up with the latest <a href=/Conservation/Gasoline/tabid/135/Default.aspx?quiz>energy conservation tips</a> from OERB.");
			} else {
				suggest.setHTML("You are a conservation expert! Be sure to keep up with the latest <a href=/Conservation/Gasoline/tabid/135/Default.aspx?quiz>energy conservation tips</a> from OERB.");
			}
			
			amtCorrect.setHTML(correctQuestions);
			finished.addClass('finished');
			e.stop();
		});
	});
});
