﻿/*
Quizzes
*/
function Admongo() { }

$(document).ready(function()
{
	Admongo.setQuizHandlers();
	Admongo.setClaimsHandlers();
	Admongo.setEmailAndPrint();
	Admongo.setQuizProperties();
	$(".zoomPointer").hover(Admongo.zoomHover, Admongo.zoomHoverOut);
	$('div.magGlass a').fancyZoom({ scaleImg: true, closeOnClick: true });
});

Admongo.setQuizProperties = function()
{
	$('.quiz').each(function(index)
	{
		var quiz = $(this);

		var max = Admongo.getMaximumSelections(quiz);
		if (max == 0) max = 999999;
		quiz.data('maximumSelections', max);

		var preventSelectionAtMaximum = Admongo.getPreventSelectionsAtMaximum(quiz);
		quiz.data('preventSelectionsAtMaximum', preventSelectionAtMaximum);

		quiz.data('selections', []);
	});
}

Admongo.getPreventSelectionsAtMaximum = function(quiz)
{
	var matches = /\bpreventSelection-([^\s]*)\b/.exec(quiz.attr('class'))
	if (matches != null)
	{
		var preventSelection = matches[1].toLowerCase() === 'true';
		return preventSelection;
	}
	return null;
}

Admongo.getMaximumSelections = function(quiz)
{
	var matches = /\bmax-(\d+)\b/.exec(quiz.attr('class'))
	if (matches != null)
	{
		var max = parseInt(matches[1]);
		if (!isNaN(max))
		{
			return max;
		}
	}
	return 1;
}


Admongo.setEmailAndPrint = function()
{
	$("#printEmailBottom").localScroll();
	$("#print a").click(function() { window.print(); return false; });
	$("#email a").click(function() { window.location = "/share.aspx"; return false; });
}

Admongo.zoomHover = function()
{
	$(this).find(".zoomImg").fadeIn();
}

Admongo.zoomHoverOut = function()
{
	$(this).find(".zoomImg").fadeOut();
}

Admongo.setClaimsHandlers = function()
{
	$(".claims-para p span").hover(Admongo.claimOver, Admongo.claimOut)
	$(".claims1-para").hover(Admongo.claimOver, Admongo.claimOut)
	$(".claims2-para").hover(Admongo.claimOver, Admongo.claimOut)
}

Admongo.claimOver = function()
{
	var rel = $(this).attr("rel");
	$(".claims-info div[rel='" + rel + "']").css('visibility', 'visible');
}

Admongo.claimOut = function()
{
	var rel = $(this).attr("rel");
	$(".claims-info div[rel='" + rel + "']").css('visibility', 'hidden');
}


Admongo.setQuizHandlers = function()
{
	$(".quiz ul li").click(Admongo.radio_click);
	$(".quiz .submit").click(Admongo.submitQuiz);
}

Admongo.submitQuiz = function()
{
	var $quiz = $(this).parent();

	if ($quiz.find(".on").length > 0)
	{
		var isQuizCorrect = true;

		$quiz.find('li').each(function(i)
		{
			var li = $(this)
			var isOn = li.hasClass('on');
			var isAnswer = li.hasClass('answer');

			var isLiCorrect = (isOn && isAnswer) || (!isOn && !isAnswer);

			isQuizCorrect = isQuizCorrect && isLiCorrect;
		});

		$quiz.find("li:not(.answer)").addClass("x");
		$quiz.find("li.answer").addClass("on");
		if (isQuizCorrect)
		{
			$quiz.find(".correct").show();
		}
		else
		{
			$quiz.find(".wrong").show();
		}
		$quiz.find(".show.reader").removeClass("reader");

		$quiz.find(".reason").show();
		$quiz.find(".submit").hide();
	}
}

Admongo.init_answerSet = function(answerSet)
{
	if (answerSet == null) return;

	var quiz = answerSet.parents('.quiz');

	//create selections array if it doesnt exist
	if (answerSet.data('selections') == null)
	{
		answerSet.data('selections', []);
	}

	//grab max selections from the quiz object
	if (answerSet.data('maximumSelections') == null)
	{
		var quizMax = quiz.data('maximumSelections');
		answerSet.data('maximumSelections', quizMax)
	}

	//grab the prevent value from the quiz
	if (answerSet.data('preventSelectionsAtMaximum') == null)
	{
		var quizPrevent = quiz.data('preventSelectionsAtMaximum');
		answerSet.data('preventSelectionsAtMaximum', quizPrevent);
	}

}


Admongo.radio_click = function()
{
	var item = $(this);
	var quiz = item.parents('.quiz');

	var answerSet = item.parents('.answerSet');
	if (answerSet.length == 0) answerSet = quiz;

	Admongo.init_answerSet(answerSet);


	var index = Admongo.get_index(item, answerSet);

	var selections = answerSet.data('selections');
	var maximumSelections = answerSet.data('maximumSelections');
	var preventSelectionsAtMaximum = answerSet.data('preventSelectionsAtMaximum');

	//if the currently clicked item is already selected, remove it
	if (answerSet.find('li').eq(index).hasClass('on'))
	{
		selections = Admongo.removeItemsFromArray(selections, index);
	}
	//if we havent reached the max number of items or we allow new selections to cycle through once the max is hit, add the index to the selection
	else if (selections.length < maximumSelections || !preventSelectionsAtMaximum)
	{
		selections = Admongo.addToArray(selections, index);
	}

	//if we have exceeded the max items, then remove oldest selection from array
	if (selections.length > maximumSelections) selections.shift();

	//ACTUALLY select the items
	Admongo.selectRadios(answerSet, selections);

	//reset the saved selections for reuse on next click
	answerSet.data('selections', selections);

}
Admongo.get_index = function(li, ancestor)
{
	//get all previous li in the quiz and return the length
	return ancestor.find('li').index(li);

	//return li.prevAll().add(li.parents('.col').prevAll().find('li')).length;
}

Admongo.selectRadios = function(quiz, indices)
{
	quiz.find('li').removeClass('on');
	$(indices).each(function(i)
	{
		var index = this;
		quiz.find('li').eq(index).addClass('on');
	});
}

//never used
Admongo.multipleradio_click = function()
{
	if ($(this).siblings("li.x").length == 0)
	{
		$(this).siblings("li").removeClass("on");
		$(this).toggleClass("on");
	}
}

Admongo.addToArray = function(array, item)
{
	//find out if the current item already exists
	var index = $.inArray(item, array);

	//if the item already exists, remove it from its current position
	if (index >= 0) array.splice(index, 1);

	//add item at the end of the array
	array.push(item);

	//return new array
	return array;
}

Admongo.removeItemsFromArray = function(array, items)
{
	var newArray = [];

	//if items is not an array, wrap it into an array in order to normalize the parameters
	if (typeof (items.indexOf) != 'function')
	{
		items = [items];
	}

	for (var i = 0; i < array.length; i++){
		var item = array[i];		
		Array.prototype.indexOf = function(obj){
			for(var i=0; i<this.length; i++){
				if(this[i]==obj){
					return i;
				}
			}
			return -1;
		}
		if (items.indexOf(item) == -1) newArray.push(item);
	}
	return newArray;
}

