$(document).ready(function(){
	
	$('div#nickname-tooltip[title]').tooltip({ effect: 'slide'});
	
	$('input[type=radio]').bind('click', function(event) {
		
		if ( event.currentTarget.checked ) { console.log(event.currentTarget.checked); event.currentTarget.checked = false; }
	});
	
});

function vote(assetRoot, mascotPairs)
{
	/* === PROPERTIES ========================================================================================================= */
	var _assetRoot = assetRoot;
	var _captchaCodeValue = "";
	var _expressInstallPath = "false";
	var _mascotPairs = mascotPairs;
	var _nicknameValue = "";
	var _swfObj = null;
	
	/* === OBJECTS ============================================================================================================ */
	var _captchaCode = null;
	var _nickname = null;
	
	/* === PUBLIC METHODS ===================================================================================================== */
	this.handleClick = handleClick;
	this.init = init;
	this.validate = validate;
	
	/* === PUBLIC METHOD DEFINITIONS ========================================================================================== */
	
	/*
	METHOD: handleClick
	ARGS:	radioClick [obj] - The DOM node representing the clicked radio button.
			swfId [string] - The name of the SWF to target.
	DESC:	Called when the user clicks a checkbox, this method passes the radio button's value into the SWF  and triggers
			the corect mascot's happy dance to play.
	*/
	function handleClick(radioClick, swfId, spotlightId1, spotlightId2)
	{
		_spotlight1 = document.getElementById(spotlightId1);
		_spotlight2 = document.getElementById(spotlightId2);
		_swfObj = document.getElementById(swfId);
		
		if ( _spotlight1 !== null && _spotlight2 !== null ) {
			_spotlight1.style.visibility="visible";
			_spotlight2.style.visibility="hidden";
		}
		
		if ( _swfObj.submitVote !== undefined ) {
			_swfObj.submitVote(radioClick.value);
		}
	}
	
	/*
	METHOD: init
	ARGS:	none
	DESC:	Initializes the object and calls any required sub-functions.
	*/
	function init()
	{
		// tell swfs that the javascript is loaded
		jsReady = true;
		
		// hack for Firefox Flash 8 first instance of swfObject detection
		//loadSwf("/assets/swf/vote_hero.swf", "noFlashMessage", 10, 10, "9.0.0");
		
		// load the mascot video SWFs
		loadMascotVideos();
	}
	
	/*
	METHOD: validate
	ARGS:	form [object] - A reference to a form object.
	DESC:	Verify that the passed form object contains all the required
			fields and that those fields have been filled in appropriately.
	*/
	function validate(form)
	{
		var errorText = "The fields listed below were either left blank or found to contain errors. Please check them and try again.\n";
		var returnObj = true;
		
		if( form.weekId.value == 15 ) {
			
			// validate matchup pair one
			if (form.matchup_1[0].checked == false && form.matchup_1[1].checked == false) {
				errorText = errorText + "\n" + "Matchups";
				returnObj = false;
			}
			
		} else if(form.weekId.value == 13 || form.weekId.value == 14) {
			
			// validate matchup pair one and two
			if (
				(form.matchup_1[0].checked == false && form.matchup_1[1].checked == false) && 
				(form.matchup_2[0].checked == false && form.matchup_2[1].checked == false)
			) {
				errorText = errorText + "\n" + "Matchups";
				returnObj = false;
			}
			
		} else {
			
			// validate matchup pair one
			if (
				(form.matchup1[0].checked == false && form.matchup1[1].checked == false) &&
				(form.matchup2[0].checked == false && form.matchup2[1].checked == false) &&
				(form.matchup3[0].checked == false && form.matchup3[1].checked == false) &&
				(form.matchup4[0].checked == false && form.matchup4[1].checked == false) &&
				(form.matchup5[0].checked == false && form.matchup5[1].checked == false) &&
				(form.matchup6[0].checked == false && form.matchup6[1].checked == false) &&
				(form.matchup7[0].checked == false && form.matchup7[1].checked == false) &&
				(form.matchup8[0].checked == false && form.matchup8[1].checked == false)
			) {
				errorText = errorText + "\n" + "Match-ups: You must vote in at least one match-up.";
				returnObj = false;
			}
			
		}
		
		// validate nickname
		if (form.nickname == null || !stringCheck(form.nickname.value) || form.nickname.value == "Enter your nickname for the season") {
			errorText = errorText + "\n" + "Nickname: Enter your nickname for the season.";
			returnObj = false;
		}
		
		// validate captcha string
		if (form.recaptcha_response_field == null || !stringCheck(form.recaptcha_response_field.value)) {
			errorText = errorText + "\n" + "CAPTCHA Code: This field is required.";
			returnObj = false;
		}
		
		// if there are errors, send the alert
		if (!returnObj) {
			alert(errorText);
		}
		
		return returnObj;
	}
	
	/* === PRIVATE METHOD DEFINITIONS ========================================================================================= */
	
	/*
	METHOD: loadMascotVideos
	ARGS:	none
	DESC:	Loop over the _mascotPairs array and load the mascot pair SWFS into the DOM.
	*/
	function loadMascotVideos()
	{
		var _attributes = null;
		var _flashvars = null;
		var _height = null;
		var _i = null;
		var _params = null;
		var _swfUrl = null;
		var _targetId = null;
		var _version = null;
		var _width = null;
		var _mascotSWFName = "voteMain.swf";
		var _targetPrefix = "heroSwfContainer";
		
		for (_i = 0; _i < _mascotPairs.length; _i++)
		{
			_attributes = { id: _targetPrefix + (_i + 1), name: _targetPrefix + (_i + 1) };
			_flashvars = {
				team1_name: _mascotPairs[_i].team1_id,
				team2_name: _mascotPairs[_i].team2_id,
				team1_win_SWF: _assetRoot + "/schools/" + _mascotPairs[_i].team1_name + "/swf/" + _mascotSWFName,
				team1_lose_SWF: "",
				team2_win_SWF: _assetRoot + "/schools/" + _mascotPairs[_i].team2_name + "/swf/" + _mascotSWFName,
				team2_lose_SWF: ""
			};
			_height = 245;
			_width = 325;
			_params = { allowscriptaccess: "always", wmode: "transparent" };
			_swfUrl = _assetRoot + "/swf/voting.swf";
			_targetId = _targetPrefix + (_i + 1);
			_version = "9.0.0";
			
			loadSwf(_swfUrl, _targetId, _width, _height, _version, _flashvars, _params, _attributes);
		}
	}
	
	/*
	METHOD: loadSwf
	ARGS:	swfUrl [string] - A relative or absolute URL to the SWF to load.
			targetId [string] - The ID of the DOM node to replace with the SWF.
			width [integer] - The width of the SWF.
			height [integer] - The height of the SWF.
			version [string] - The target version of the Flash Player.
			flashvars [object] - Any data required by the SWF to properly execute.
			params [object] - Any parameters required by the <embed /> tag to properly load the SWF.
			attributes [object] - Any attributes required by the <embed /> tag to properly load the SWF.
	DESC:	Load a specific SWF into the DOM via SWFOBject.
	*/
	function loadSwf(swfUrl, targetId, width, height, version, flashvars, params, attributes)
	{
		swfobject.embedSWF(swfUrl, targetId, width, height, version, _expressInstallPath, flashvars, params, attributes);
	}
}