if ( typeof(SNI.DIY.Quiz) == 'undefined' ) { SNI.DIY.Quiz = {}; };

SNI.DIY.Quiz.answer_choices = [];
SNI.DIY.Quiz.answer_text = [];


SNI.DIY.Quiz.init = function( url, w, h, options ) {
	var defaults = {
		width: 800,
		height: 600,
		resize: 0,
		scrolls: 1,
		status: 0,
		toolbar: 0,
		location: 0,
		menu: 0
	};
	var options = jQuery.extend(defaults, options);
	var popupWin = window.open(url, 'popWin', 'width=' + w + ', height=' + h + ', resizable=' + options.resize + ', scrollbars=' + options.scrolls + ', status=' + options.status + ', toolbar=' + options.toolbar + ', location=' + options.location + ', menubar=' + options.menu);
};
SNI.DIY.Quiz.debug = function( msg ) {
	if ( window.console ) { console.debug(msg); } else { alert(msg); };
};
SNI.DIY.Quiz.backToLastQuestion = function(goBack) {
	alert('It appears the last question was not properlly recorded.  You will now be redirected to the last question.  Please submit your last answer again.');
	history.go(goBack);
};
SNI.DIY.Quiz.setQuestionPage = function( options ) {
	var defaults = {
		num						: -1,
		labelID_prepend			: '#answer',
		previewID_prepend		: '',
		previewWindowWidth		: 515,
		previewWindowHeight		: 515,
		showPreviewWindowMenu	: 1,
		form					: '#frm_quiz',
		preview_urls			: [],
		goBack					: -1
	};
	var options = $.extend(defaults, options);
	if ( options.num == -1 ) { return; };
	//if ( options.num > 1 ) {
	//	if ( opener.SNI.DIY.Quiz.answer_choices.length < (options.num - 1) ) {
	//		SNI.DIY.Quiz.backToLastQuestion(options.goBack);
	//	}
	//};
	if ( options.previewID_prepend ) {
		for ( var i=0; i<options.preview_urls.length; i++ ) {
			var obj = $(options.previewID_prepend + (i + 1)).get(0);
			obj.url = options.preview_urls[i];
			obj.w = options.previewWindowWidth;
			obj.h = options.previewWindowHeight;
			obj.m = options.showPreviewWindowMenu;
			$(options.previewID_prepend + (i + 1))
				.css('zIndex', 1000)
				.click(function() {
					SNI.Util.popup2(this.url, this.w, this.h, this.m);
				});
		};
	};
	$(options.form).submit(function() { return SNI.DIY.Quiz.validate(options.num, options.labelID_prepend, options.form); });
	$('a', options.form).click(function() { $('[type=radio]', $(this).next()).attr('checked', true); });
};
SNI.DIY.Quiz.setupTiePage = function( options ) {
	var defaults = {
		num						: 0,
		form					: null,
		labelPrependID			: '',
		questionWrapPrependID	: '',
		previewID_prepend		: '#btn_preview',
		preview_urls			: []
	};
	var options = jQuery.extend(defaults, options);
	if ( !options.form ) { SNI.DIY.Quiz.debug('Form was not set properly.  Use option [form]'); return; }
	if ( !options.num ) { SNI.DIY.Quiz.debug('Page number was not set.  Use option [num]'); return; }
	if ( !options.labelPrependID ) { SNI.DIY.Quiz.debug('Label prepend id was not set.  Use option [labelPrependID]'); return; }
	if ( options.previewID_prepend ) {
		for ( var i=0; i<options.preview_urls.length; i++ ) {
			var obj = $(options.previewID_prepend + (i + 1)).get(0);
			obj.url = options.preview_urls[i];
			obj.w = options.previewWindowWidth;
			obj.h = options.previewWindowHeight;
			obj.m = options.showPreviewWindowMenu;
			$(options.previewID_prepend + (i + 1))
				.css('zIndex', 1000)
				.click(function() {
					SNI.Util.popup2(this.url, this.w, this.h, this.m);
				});
		}
	}
	var aryTie = opener.tiedAnswers.toString().split(',');
	for ( var i=0; i<aryTie.length; i++ ) {
		if ( jQuery(options.questionWrapPrependID + (parseInt(aryTie[i]) + 1)).get(0) ) {
			jQuery(options.questionWrapPrependID + (parseInt(aryTie[i]) + 1)).show();
		}
	}
	$('a', options.form).click(function() { $('[type=radio]', $(this).next()).attr('checked', true); });
	jQuery(options.form).submit(function() {
		return SNI.DIY.Quiz.validate(options.num, options.labelPrependID, options.form);
	});
};
SNI.DIY.Quiz.validate = function( num, labelID, form ) {
	var chosen = false;
	var allRadios = $('[type=radio]', form);
	$.each(allRadios, function(i) {
		if ( $(this).get(0).checked ) {
			opener.SNI.DIY.Quiz.answer_choices[num - 1] = $(this).val().toUpperCase();
			if ( labelID ) { opener.SNI.DIY.Quiz.answer_text[num - 1] = $(labelID + (i + 1)).html(); }
			var answer_choices = opener.SNI.DIY.Quiz.answer_choices.length != 0 ? opener.SNI.DIY.Quiz.answer_choices : [];
			var answer_text = opener.SNI.DIY.Quiz.answer_text.length != 0 ? opener.SNI.DIY.Quiz.answer_text : [];
			opener.SNI.DIY.Quiz.answer_choices = answer_choices;
			opener.SNI.DIY.Quiz.answer_text = answer_text;
			chosen = true;
		}
	});
	if ( !chosen ) {
		alert("Please choose an answer to continue.");
		return false;
	}
};
SNI.DIY.Quiz.getNumCorrect = function( options ) {
	var defaults = {
		correct_answers: [],
		obj_total: '',
		goBack: -1,
		beratementSplit: [4, 7, 11],
		numCorrectObj: '',
		beraterHeaderObj: '',
		beraterDescriptionObj: '',
		beraterHeaders: [],
		beraterDescriptions: []
	};
	var options = $.extend(defaults, options);
	//if ( opener.SNI.DIY.Quiz.answer_choices.length < options.correct_answers.length ) {
	//	$.quizUtils.backToLastQuestion(options.goBack);
	//}
	if ( opener.SNI.DIY.Quiz.answer_choices.length < 10 ) {
		for ( var i=opener.SNI.DIY.Quiz.answer_choices.length - 1; i<9; i++ ) {
			opener.SNI.DIY.Quiz.answer_choices[i] = 'c';
		}
	}
	var numCorrect = 0;
	for ( var i=0; i<options.correct_answers.length; i++ ) {
		if ( opener.SNI.DIY.Quiz.answer_choices[i].toLowerCase() == options.correct_answers[i].toLowerCase() ) {
			numCorrect++;
		}
	}
	var ret;
	/* loop through options.beratementSplit and see what range we're in */
		for ( var i=0; i<options.beratementSplit.length; i++ ) {
			if ( numCorrect < options.beratementSplit[i] ) {
				jQuery(options.beraterHeaderObj).html(options.beraterHeaders[i]);
				jQuery(options.beraterDescriptionObj).html(options.beraterDescriptions[i]);
				ret = options.beratementSplit[i]
				break;
			}
		}

	$(options.obj_total).html(numCorrect.toString());
	return ret;
};
SNI.DIY.Quiz.getMaxOfNumberSet = function() {
	var largest = arguments[0];
	var tmp = [];
	var mArrayCounter = 0;
	for ( i=0; i<arguments.length; i++ ) {
		if ( arguments[i] >= largest ) {
			if ( arguments[i] > largest ) {
				tmp = [];
				mArrayCounter = 0;
			}
			tmp[mArrayCounter] = i;
			largest = arguments[i];
			mArrayCounter++;
		}
	}
	return tmp;
};
SNI.DIY.Quiz.getNumOccurrences = function( lookFor, lookIn ) {
	var numCount = 0;
	for ( var i=0; i<lookIn.length; i++ ) {
		if ( lookIn.charAt(i) == lookFor ) {
			numCount++;
		}
	}
	return parseInt(numCount);
};
SNI.DIY.Quiz.getPersonality = function( options ) {
	var defaults = {
		numQuestions: 10,
		choices: ['a', 'b', 'c', 'd'],
		tieURL: '',
		beraterHeaderObj: '',
		beraterDescriptionObj: '',
		beraterHeaders: [],
		beraterDescriptions: []
	};
	var cookieValue = paramStr = '';
	var options = jQuery.extend(defaults, options);
	for ( var i=0; i<(options.numQuestions + 1); i++ ) {
		if ( opener.SNI.DIY.Quiz.answer_choices[i] != null )
			cookieValue += opener.SNI.DIY.Quiz.answer_choices[i] + ',';
	}
	for ( i=0; i<options.choices.length; i++ ) {
		eval('var num' + options.choices[i] + ' = SNI.DIY.Quiz.getNumOccurrences(\'' + options.choices[i].toLowerCase() + '\', cookieValue.toLowerCase());');
		paramStr += 'num' + options.choices[i] + ', ';
	}
	eval('var theHighest = SNI.DIY.Quiz.getMaxOfNumberSet(' + paramStr.substring(0, paramStr.length - 2) + ');');
	if (theHighest.length > 1) {
		opener.tiedAnswers = theHighest;
		if ( options.tieURL ) window.location.href = options.tieURL;
	}
	jQuery(options.beraterHeaderObj).html(options.beraterHeaders[parseInt(theHighest)]);
	jQuery(options.beraterDescriptionObj).html(options.beraterDescriptions[parseInt(theHighest)]);
	return theHighest;
};
