//MoM for OFF!
//meter img width: 509px
//meter left width: 21px
//meter right width: 21px
//meter actual width: 467px 
var scale = 11; //meter actual width div total scale (467/44)
var upper_bounds = 488;
var right_bounds = 412;
var left_bounds = 21;
var dDuration = 500;
var dFps = 18;
var total_q = 8;
var base_url = '/meter/';
var dPre = 'dyn';
var pPre = 'product';
var qPre = 'q';
var tPre = 'tip';
var m = 'meter_product_wrapper';
var d = 'dyn_wrapper';
var rec_arr = [];
var count_txt = new Element('span',{ id: 'no_questions' });

//if (!console) {
//	var console = {};
//	console.log = function() { /*alert(console.log) */ };
//}

Array.prototype.removeDuplicate = function()
{
	// Here we remove duplicate values from first array
	var array4 = new Array;
	for(var i=0; i<this.length; i++) 
	{
	    var xx = true;
		for(var j=i+1; j<this.length; j++)
		{
			if(this[i] == this[j])
				xx = false;		
		}	
		if(xx == true)
			array4.push(this[i]);
	}
	return array4;
}

// global functions 
function singleFadeOut(el) {
	//console.log('s fade out: ', el);
	var fade = $(el).effects({duration: dDuration, fps: dFps, transition: Fx.Transitions.Sine.easeOut});
	fade.start({'opacity': [1,0]});
}

function singleFadeIn(el) {
	//console.log('single fade in el: ', el);
	var fade = $(el).effects({duration: dDuration, fps: dFps, transition: Fx.Transitions.Sine.easeOut});
	if ($(el).hasClass('hide')) {
		$(el).removeClass('hide');	
	}
	$(el).setStyles(
		{
			display:'block',
			opacity: 0
		}
	);
	fade.start({ 'opacity': [0,1] });
}
// end global functions


function mom_init() {
	$('btn_intro').setStyle('opacity', 0);
	$$('ol li','#dyn_wrapper','#meter_product_wrapper','.tip').setStyle('opacity', 0);
	$('mom_quiz').reset();
	$('dyn_wrapper').setStyle('left', 0);
//	$$('#dyn_wrapper').setStyle('opacity', 0);
//	$$('#meter_product_wrapper').setStyle('opacity', 0);
//	$$('.tip').setStyle('opacity', 0);
	var tmpCount_txt = ''//'Question 0 of ' + total_q;
	var meter_width = $('meter').getCoordinates().width;
	var marker_width = $('meter_marker').getCoordinates().width;
	var center_marker = ((meter_width - marker_width)/2) - 30;
	var mosquito_width = center_marker + (marker_width/2);
	
	var mover = $('meter_marker').effects(
			{
				duration: dDuration,
				fps: dFps,
				transition: Fx.Transitions.Sine.easeOut
			}
		);
	var wider = $('meter_mosquitoes').effects(
			{
				duration: dDuration,
				fps: dFps,
				transition: Fx.Transitions.Sine.easeOut
			}
		);

	mover.start({ 'left': [0, center_marker] });
	wider.start({ 'width': [0, mosquito_width] }).addEvent('onComplete', function() {
		singleFadeIn('btn_intro')
	});
	$(count_txt).setHTML(tmpCount_txt).injectInside('meter_div');
}

function mom_domready() {
	var total = 0;
	var count = 0;
	var n = 0;
	
	$('btn_intro').addEvent('click', function(e) {
		var e = new Event(e); 
		n = 1;
		fadeOut('copy_intro',n);
		var tmpQ = qPre + n;
		var qH = $(tmpQ).getProperty('height');
		singleFadeOut('main_img');
		$('meter_tip').setStyle('display','block');
		e.preventDefault();
		return false;     
	});

	$ES('#mom_quiz input').addEvent('click', function(e) {
		count = this.value.toInt();
		dynFadeIn(n); 
		if (count > 0) { 
			showTout(n);
		} else { 
			//if product recommender is showing, hides PR, then shows regular footer
			hideTout(n); 
		}
	});

	$$('#btn_next', '#btn_finish').addEvent('click', function(e) {
		var e = new Event(e);
		var btn_id = this.getAttribute('id');
		var oldN = n;
		(n <= total_q) ? n++ : (n > total_q) ? n = null : n = null;
		if (n > total_q) oldN = n-1;
		singleFadeOut(btn_id);
		fadeOut(oldN, n);
		singleFadeOut('dyn_wrapper');
		moveMoM(count);
		if (count > 0) {
			var tmpRec = 'a' + oldN + '_rec';
			if ($(tmpRec)) {
				var toPush = $(tmpRec).value;
				if(!rec_arr.contains(toPush)) {
					rec_arr.push(toPush);
				}
			}
		}
		e.preventDefault();
		return false;
	});

	function moveMoM(amount) {
		//console.log('move: ', amount);
		total += amount;
		$('total').value = total;
		var mover_pos = ($('meter_marker').getStyle('left').toInt() < right_bounds) ? $('meter_marker').getStyle('left').toInt() : right_bounds;
		var mos_width = $('meter_mosquitoes').getStyle('width').toInt();
		var mover_amount = mover_pos + (amount * scale);
		var new_mos_width = ((mos_width + (amount * scale)) < upper_bounds) ? mos_width + (amount * scale) : upper_bounds;
		var mover = $('meter_marker').effects(
				{
					duration: dDuration,
					fps: dFps,
					transition: Fx.Transitions.Sine.easeOut
				}
			);
		var wider = $('meter_mosquitoes').effects(
				{
					duration: dDuration,
					fps: dFps,
					transition: Fx.Transitions.Sine.easeOut
				}
			);
		mover.start({ 'left': [mover_pos, mover_amount] });
		wider.start({ 'width': [mos_width, new_mos_width] });
	}

	function fadeOut(el,n) {
		if (n <= total_q || $type(n) == 'string') {
			//console.log('el: %a, n: %b', el, n);
			//console.log('rec_arr: ', rec_arr);
			var tempEl = ($type(el) == 'string') ? el : qPre + el;
			var tEl    = ($type(el) == 'string') ? el : tPre + el;
	
			var tmpArr = [tempEl,tEl]
	
			var nEl = ($type(n) == 'number' && n <= total_q) ? n : ($type(n) == 'string') ? n : null;
			tmpArr.each(function(a) {
				//console.log('arr: ', a);
				var fade = $(a).effects(
					{
						duration: dDuration,
						fps: dFps,
						transition: Fx.Transitions.Sine.easeOut
					}
				);
				fade.start({'opacity': [1, 0]}).addEvent('onComplete', function() {
					$(a).setStyle('display','none');
					if ($type(el) == 'number') hideTout();
					fadeIn(nEl);
				});
			});
		}
		if ($('btn_next').getStyle('display') == 'block') {
			$('btn_next').setStyles({
				display: 'none'
			});
		}
		if (n > total_q) {
			fadeOut('meter_bot_wrapper','recommender');
			$('content_wrapper').setStyle('background','#ffaf2a');
			recommender();
		}
	}
	
	function fadeIn (el) {
		//console.log(el);
		if ($defined(el)) {
			var tempEl = ($type(el) == 'string') ? el : qPre + el;
			var tEl    = ($type(el) == 'string') ? el : tPre + el;
			
			if ($type(el) == 'number') {
				var tmpCountTxt = 'Question ' + el + ' of ' + total_q;
				$(count_txt).setProperty('id','q_count');
				$(count_txt).setHTML(tmpCountTxt);
			} else {
				
			}
			
			var tmpArr = [tempEl,tEl];
	
			$(m).removeClass('hide');
			tmpArr.each(function(a) {
				var fade = $(a).effects({duration: dDuration, fps: dFps, transition: Fx.Transitions.Sine.easeOut});
				$(a).setStyles({
					display: 'block',
					opacity: 0
				});
				fade.start({'opacity': [0, 1]}).addEvent('onComplete',function() {

				});
			});
		}
	}

	function dynFadeIn(el) {
		var dynEl = dPre + el;
		var fade = $(d).effects({duration: dDuration, fps: dFps, transition: Fx.Transitions.Sine.easeOut});
		var dynH = $(dynEl).getCoordinates().height;
		$$('.dyn').removeClass('block');
		$$('.dyn').addClass('hide');
		$(dynEl).removeClass('hide');
		$(dynEl).addClass('block');
		//console.log('dyn height: ', $(dynEl).getCoordinates());
		//console.log('2nd fade dyn: ', dynEl);
		$(d).removeClass('hide');
		if ($(d).getStyle('opacity') == 0) {
			fade.start({'opacity': [0, 1]}).addEvent('onComplete',function() {
				var tmpQ = qPre + el;
				var qH = $(tmpQ).getStyle('height').toInt() + 20 + 'px';
				if ($('btn_next').getStyle('display') == 'none' && el < total_q) {
					$('btn_next').setStyles({
						display: 'block',
						marginTop: qH
					});
					singleFadeIn('btn_next');
				} else if (el == total_q) {
					$('btn_finish').setStyles({
						display: 'block',
						marginTop: qH
					});
					singleFadeIn('btn_finish');
				}
			});			
		}
	}

	function showTout(el) {
		var pEl  = pPre + el;
		var tmpP = $(pEl).getChildren();

var a = 'pEl: ' +  pEl + '\n';
a += 'tmpP: ' + tmpP.length;
//alert(a)
		$$('.product').setStyle('visibility','hidden');
		if (tmpP.length > 0) {
	//		alert('here');
			$('intro_bot').setStyle('display','none');
			$(pEl).setStyle('visibility','visible');
			$(pEl).removeClass('hide');
			var fade = $(m).effects({duration: dDuration, fps: dFps, transition: Fx.Transitions.Sine.easeOut});
			$(m).removeClass('hide');
			if ($(m).getStyle('opacity') == 0) {
				$(m).setStyle('height','150px');
				fade.start({'opacity': [0, 1]});
			}
		}
	}
	
	function hideTout() {
		var fade = $(m).effects({duration: dDuration, fps: dFps, transition: Fx.Transitions.Sine.easeOut});
		if ($(m).getStyle('opacity') == 1) {
			fade.start({'opacity': [1, 0]}).addEvent('onComplete',function() {
				$(m).addClass('hide');
				$(m).setStyle('height','0');
				singleFadeIn('intro_bot');
				$$('.product').setStyle('visibility','hidden');
			});
		}
	}
	
	function recommender() {
		$$('.aP').each(function(index) {
			var indexValue = index.getProperty('value');
			rec_arr.include(indexValue);
		});
		//console.log('included arr values: ', rec_arr);
		var ex = rec_arr.removeDuplicate();
		//console.log('cleaned arr values: ', rec_arr);
//		a += 'Products recommended: ' +  + '\n';
		var result = (total < 1) ? 'low' : (total > 0 && total < 12) ? 'medium' : 'high';
		var mos_img = 'mosquito_love_' + result;
		var mos_txt = 'You scored ' + result + ' as a mosquito magnet';
		var back_to_quiz = new Element('a', { 
								id: 'return',
								href: base_url,
								title: 'Return to Questions'
							});
		var mos_quiz = new Element('p',{ id: 'mos_based_txt', 'class': 'score_txt' });
		var mos_score = new Element('p',{ id: 'mos_score_txt', 'class': 'score_txt' });
		var mos_score_txt = '';
		$(count_txt).remove();
		$(back_to_quiz).setHTML('Return to Questions').injectInside('meter_div');
		$(mos_quiz).setHTML(mos_score_txt).injectAfter(mos_img);
//		$(mos_score).setHTML(mos_txt).injectAfter(mos_quiz);
		$(mos_img).removeClass('hide');
		ex.reverse();
		ex.each(function(index) {
			var tmpIndex = 'recommended_' + index;
			var grad = new Element('div',{ 'class': 'bot_gradient' });
			//console.log('ex: ', tmpIndex);
			$(tmpIndex).injectTop('recommended_products');
			grad.injectAfter(tmpIndex);
		})
	
	/*
	function recommender() {
		$$('.aP').each(function(index) {
			var indexValue = index.getProperty('value');
			rec_arr.include(indexValue);
		});
		console.log('included arr values: ', rec_arr);
		var ex = rec_arr.removeDuplicate();
		console.log('cleaned arr values: ', rec_arr);
//		a += 'Products recommended: ' +  + '\n';
		var result = (total < 1) ? 'low' : (total > 0 && total < 12) ? 'medium' : 'high';
		var mos_img = 'mosquito_love_' + result;
		var mos_txt = 'You scored ' + result + ' as a mosquito magnet';
		var back_to_quiz = new Element('a', { 
								id: 'return',
								href: base_url,
								title: 'Return to Questions'
							});
		var mos_quiz = new Element('p',{ id: 'mos_based_txt', 'class': 'score_txt' });
		var mos_score = new Element('p',{ id: 'mos_score_txt', 'class': 'score_txt' });
		var mos_score_txt = 'Based on the quiz:<br />' + mos_txt;
		$(count_txt).remove();
		$(back_to_quiz).setHTML('Return to Questions').injectInside('meter_div');
		$(mos_quiz).setHTML(mos_score_txt).injectAfter(mos_img);
//		$(mos_score).setHTML(mos_txt).injectAfter(mos_quiz);
		$(mos_img).removeClass('hide');
		ex.reverse();
		ex.each(function(index) {
			var tmpIndex = 'recommended_' + index;
			var grad = new Element('div',{ 'class': 'bot_gradient' });
			console.log('ex: ', tmpIndex);
			$(tmpIndex).injectTop('recommended_products');
			grad.injectAfter(tmpIndex);
		})*/
		
	}
}

window.addEvent('load', function() {
	mom_init();
});

window.addEvent('domready', function() {
	mom_domready();
});