var messages;
var clubs;
var tune;
var countdown;
var countdownTimer;
function init() 
{
	initMessages();
	
	if(showFlash)
	{
			$("#siteAd").addClass("showFlash");
	}
	
	
	clubs=getClubs();
	initAutocomplete();	
	$("#clubNameSelected").click(onClickClubNameSelected);

	if($("#clb_id").val()>0)
	{
		$("#clubNameSelected").html(clubs[$("#clb_id").val()][0]);
		$("#clubNameSelected").show();
		$("#clubNameInput").hide();
	}
	showErrorsAsInformation();

	var play = getCookie("tune");
	if(play==1 || play=="")
	{
		startPlayCountdown();
	}
}

function startPlayCountdown()
{
	countdown=5;
	checkCountdown();
	countdownTimer=setInterval( checkCountdown, 1000);
}

function checkCountdown()
{
	countdown--;
	$("#stopCountdown").show();
	$("#countdown").html(message_with_args("pg.welcome.tuneStartingXSeconds",countdown));
	if(countdown==0)
	{
		clearInterval(countdownTimer);
		$("#countdown").html("");
		$("#stopCountdown").hide();
		playTune();
	}
}

function stopCountdown(bStopTune)
{
	if(bStopTune)
		stopTune();
	clearInterval(countdownTimer);
	$("#countdown").html("");
	$("#stopCountdown").hide();
	return false;
}
	
function toggleTune()
{  
  if($(".soundBtn").hasClass("muted"))
  {
  	if(countdownTimer!=null)
  		stopCountdown();
  	playTune();
  }
  else
  {
  	stopTune();
  }



  return false;
}

function playTune()
{

  $(".soundBtn").removeClass("muted");
  setCookie("tune",1,365);

  tune= soundManager.createSound({
    id: 'aSound',
    url: '../../sound/tune.mp3'
  });
  tune.setVolume(45);
  tune.play();
}
function stopTune()
{
  	$(".soundBtn").addClass("muted");
  	setCookie("tune",0,365);
	if(tune!=null)
		tune.pause();
	return false;
}

function validateSearchClub()
{

	var errorText ="";
	if($("#clb_id").val()==0)
	{
		errorText+=errorText + message_with_args("errors.required", messages["com.club"]) +"<br/><br/>";
	}
	if(errorText!="")
	{
		showErrorDlg(errorText);
		return false;
	}
	else
	{
		return true;
	}
}

function validateWaitlist()
{
	var errorText ="";
	var email = $.trim($("#email").val());
	if(email=="")
	{
		errorText=errorText + message_with_args("errors.required", messages["com.email"]) +"<br/><br/>";
	}

	if(!isEmail(email))
	{
		errorText=errorText + message_with_args("errors.email", email) +"<br/><br/>";
	}
	
	if(errorText!="")
	{
		showErrorDlg(errorText);
		return false;
	}
	else
	{
		askWillSubscribe();
		return false;
	}
}


//Ask the user if they'll subscribe when the club's available
function askWillSubscribe()
{

		var myButtons = [ { text: messages["waitlist.yesWillSubscribe"],  
	                    handler:function(){$("#will_subscribe").val(true); document.forms.waitlist.submit();} }, 
	                  { text: messages["waitlist.noWontSubscribe"],  
	                    handler:function() {$("#will_subscribe").val(false); document.forms.waitlist.submit();} }
	                    ];	                  	                  

		errorDlg = new YAHOO.widget.SimpleDialog("dlg", { visible: false, width: "350px", close:false, fixedcenter:true, modal:true, draggable:false });
		errorDlg.setHeader(messages["com.information"]);
		errorDlg.setBody(messages["waitlist.askWillSubscribe"]);
		errorDlg.cfg.queueProperty("buttons", myButtons);
		
		errorDlg.render(document.body); 
		errorDlg.show();
		return false;

}

function initAutocomplete()
{
	var oACDS;
	var oAutoComp;

	var clubNames;
	
    // Instantiate first JS Array DataSource
    oACDS = new YAHOO.widget.DS_JSArray(clubs);

    // Instantiate first AutoComplete
    oAutoComp = new YAHOO.widget.AutoComplete("clubNameInput","clubNamesContainer", oACDS);
	oAutoComp.alwaysShowContainer = false;
    oAutoComp.typeAhead = false;
    oAutoComp.useShadow = false;
	oAutoComp.useIFrame = true;
    oAutoComp.minQueryLength = 1;
    oAutoComp.maxResultsDisplayed = 6;
    oAutoComp.itemSelectEvent.subscribe( clubSelect );

}

function onClickClubNameSelected()
{
	$("#clubNameSelected").hide();
	$("#clubNameInput").show();
	$("#clubNameInput").val("").focus();
	$("#clb_id").val("");
}

function clubSelect(oSelf,elItem,oData)
{
	//The club id is stored here - this is not documented in Yahoo UI, but seems to work
	var clb_id = elItem[2][1];
	var clubName = elItem[2][0];
	$("#clubNameSelected").html(clubName);
	$("#clubNameSelected").show();
	$("#clubNameInput").hide();
	$("#clb_id").val(clb_id);
}


//Checks the total before a submit is allowed
function checkTotal()
{
	if( clubPoints + calcTotal("div.hlPanel select") > 100)
	{
			showErrorDlg(messages["profileCreation.overMaxPoints"]);
			return false;
	}
	
	return true;
	
}

