var currlottoGameCard=0;
var lottoGameCard=[];
var gamesPlayed=0;


/*
lottoGameCard.push({
	pickLimit: 4,
	numChoices: 24,
	gameID: 1,
	status: 0,
	adLen: 10
});

lottoGameCard.push({
	pickLimit: 5,
	numChoices: 36,
	gameID: 2,
	status: 0,
	adLen: 15
});

lottoGameCard.push({
	pickLimit: 6,
	numChoices: 48,
	gameID: 3,
	status: 0,
	adLen: 20	
});

lottoGameCard.push({
	pickLimit: 7,
	numChoices: 56,
	gameID: 4,
	status: 0,
	adLen: 30		
});
*/

var myPicks=[];

function buildCard(inOrdinal, bAmimate){
	var totalPicks = lottoGameCard[currlottoGameCard].pickLimit;
	currlottoGameCard=inOrdinal;
	lottoGameCardErase();
	$(".lottoGameCardButton, .lottoGameCard, #lottoPathAd").hide();	
	$(".lottoGameCardSubmitWrap a").attr("data-clicked", "false");
	
	//Populate lotto game instruction balloon
	$(".lottoGameBalloonText").text(totalPicks);
	$(".lottoGameBalloon").fadeIn();	
	
	//console.log("currlottoGameCard: " + currlottoGameCard)
	if(lottoGameCard[currlottoGameCard].numChoices>56){
		alert("PCH ERROR: Maximum number of choices is 56!");
		lottoGameCard[currlottoGameCard].numChoices=56;
	}
	
	$(".lottoGameCard").attr("id", "lottoGame"+lottoGameCard[currlottoGameCard].gameID);
	
	
	//check game card position
	if ($(".lottoGameCard").css("left") < 0)
	{
		$(".lottoGameCard").css("left","0");
	}
	
	$(".lottoGameCard").fadeIn("slow", function(){
		$(".lottoGamePicks").show();
		$(".lottoGameCol2 ol").html("");
		for(var iN=1;iN<=lottoGameCard[currlottoGameCard].numChoices;iN++){
			$(".lottoGameCol2 ol").append('<li><a href="#" onclick="return lottoPickMe('+iN+')" id="lottoGameCardPick'+iN+'"><span>'+iN+'</span></a></li>');
		}
		
		lottoGameStatusBar();
		$(".lottoGameCardButton").show();	
		
		if(bAmimate){
			$("#lottoGame"+lottoGameCard[currlottoGameCard].gameID).css({left:$("#lottoGame"+lottoGameCard[currlottoGameCard].gameID).width()+100})
			$("#lottoGame"+lottoGameCard[currlottoGameCard].gameID).animate({left: "0px"}, 555)						
		}
	})
	
	$("#lottoGameRegistration input,#lottoGameRegistration select").focus( function(){
						$(this).parent().addClass("focus");
	});
	
	$("#lottoGameRegistration input,#lottoGameRegistration select").blur( function(){
								$(this).parent().removeClass("focus");
	});

	//Tracking Code: Coremetrics
	pch.lotto.trackingCode("LottoGameOnLoadEvent");	
	
	return false;
}

function lottoPickMe(inNum, bAnimate){

	bAnimate = (typeof bAnimate == "undefined") ? true: bAnimate;

	var myPickLoc=-1;
	for(var iM=0; iM<myPicks.length; iM++){
		if(myPicks[iM]==inNum){
			myPickLoc=iM
			break;
		}
	}
	
	
	//if($("#lottoGameCardPick"+inNum).hasClass("picked")){
	if(myPickLoc>-1){		
		
		//console.log("kill "+inNum+": " + Date())		

		myPicks.splice(iM,1);
		//console.log(myPicks);
		//lottoGameCardDestoryBalls(inNum);

		$("#lottoGameCardBigBall"+inNum).remove();
		var $effectsWrapper = $(".ui-effects-wrapper",".lottoGamePicks");
		$effectsWrapper
			.each(function(){
				if ($(this).children().size() == 0)
				{
					$(this).remove();
				}
			});

		$("#lottoGameCardPick"+inNum).removeClass("picked");		
		$(".lottoGameCardSubmitWrap").hide();
	}else{
		if(myPicks.length<lottoGameCard[currlottoGameCard].pickLimit){
			//console.log("pick "+inNum+": " + Date())		
			myPicks.push(inNum)
			$("#lottoGameCardPick"+inNum).addClass("picked");	
			
			$(".lottoGamePicks").append('<p class="lottoGameCardBigBall" id="lottoGameCardBigBall'+inNum+'">'+inNum+'</p>')
			
			
			if(bAnimate)
			{
				$("#lottoGameCardBigBall"+inNum).effect("bounce");
				//$("#lottoGameCardBigBall"+inNum).slideUp("fast", function(){
			}
																					 
			//})
			
			
			if(parseInt(myPicks.length)==parseInt(lottoGameCard[currlottoGameCard].pickLimit)) $(".lottoGameCardSubmitWrap").show();
			
		}else{
			/*
			$("#lottoGameDone").fadeOut("fast",function(){
				$("#lottoGameDone").fadeIn("fast",function(){
					window.setTimeout(function(){$("#lottoGameDone").fadeOut("slow")},555);										   
				})
			})
			*/
			
			var showValue = ($("body").hasClass("ie")) ? "inline": "block";
			$("#lottoGameDone").css("display", showValue);
			window.setTimeout("$('#lottoGameDone').fadeOut('slow')", 1500);

			//console.log("sorry, only "+ lottoGameCard[currlottoGameCard].pickLimit +" picks on this card " + inNum);	
		}
	}
	
	lottoGameStatusBar();
	return false;	
}


function lottoGameCardErase(bUserPressedBtn){

	bUserPressedBtn = (typeof bUserPressedBtn == "undefined") ? false: bUserPressedBtn;
	
	$(".lottoGameCol2 li a").removeClass("picked");	
	lottoGameCardDestoryBalls(0);
	$(".lottoGameCardSubmitWrap").hide();
	myPicks=[];
	lottoGameStatusBar();
	
	if (bUserPressedBtn)
	{
		//Tracking Code: Coremetrics
		pch.lotto.trackingCode("EraseButtonClickEvent");
	}

	return false;
}

function lottoGameCardDestoryBalls(inNum){	
	if(!inNum){
		$(".lottoGameCardBigBall,.lottoGamePicks .ui-effects-wrapper").remove(); 
	}else{
		$("#lottoGameCardBigBall"+inNum).hide("fast",function(){
			if($("#lottoGameCardBigBall"+inNum).parent().hasClass(".ui-effects-wrapper")){
				$("#lottoGameCardBigBall"+inNum).parent().remove(); 				
			}else{
				$("#lottoGameCardBigBall"+inNum).remove(); 				
			}

		})
	}
}

function lottoGameCardQuickPick(bUserPressedBtn){

	bUserPressedBtn = (typeof bUserPressedBtn == "undefined") ? false: bUserPressedBtn;

	lottoGameCardErase();

	var arrQuick=[];
	for(iC=1;iC<=lottoGameCard[currlottoGameCard].numChoices;iC++){
		arrQuick.push(iC)
	}

	for(iQ=0;iQ<lottoGameCard[currlottoGameCard].pickLimit;iQ++){
		randomnumber=Math.floor(Math.random()*arrQuick.length)
		//console.log("rand: " + randomnumber);
		lottoPickMe(arrQuick[randomnumber], false)
		arrQuick.splice(randomnumber, 1);					
	}

	//animate group of lotto balls
	$(".lottoGameCardBigBall").show();
	$(".lottoGamePicks")
		.stop(true,true)
		.css("top","0")
		.effect("bounce");
	
	if (bUserPressedBtn)
	{
		//Tracking Code: Coremetrics
		pch.lotto.trackingCode("QuickPickClickEvent");
	}

	return false;								   
}

function lottoGameCardSubmit(){
	
	$(".lottoGameCardSubmitWrap a").attr("data-clicked", "true");
	
	if ($(".lottoGameCardSubmitWrap a").attr("data-clicked") == "clicked")
	{
		return false;
	}
	

	$(".lottoGameCardSubmitWrap").fadeOut();	
	iWhere = -$("#lottoGame"+lottoGameCard[currlottoGameCard].gameID).width()-100

	var cntr = $( ".lottoGameCardBigBall" ).length
	$( ".lottoGameCardBigBall" ).each(function(){
		$( this ).animate({left: iWhere}, 555, function(){

			$(".lottoGameBalloon").fadeOut();
			cntr--;
			if(cntr==0){
				$("#lottoGame"+lottoGameCard[currlottoGameCard].gameID).animate({left: iWhere}, 555, function(){
					$("#gameMessage").html("Loading...");																							

					//User logged in?
					if (!pch.lotto.customerData.emailAddress)
					{
						//Show registration form
						lottoGameAnimateReg();

						//Tracking Code: eDialogConversion
						pch.lotto.trackingCode("OnSubmitClickEventUnknownUserNoCookie");											
					}
					else 
					{											
						
						//Check if user registered without playing game first
						if (pch.lotto.customerData.currGameCardIndex == pch.lotto.NO_GAME_PLAY)
						{
							pch.lotto.customerData.currGameCardIndex = 0;
						}
						
						//submit game
						pch.lotto.webServices.SubmitEntry({
							'contestId': pch.lotto.lottoGameCard[pch.lotto.customerData.currGameCardIndex].gameID,
							'selectedNumbersAsString': myPicks.join(',')
						});

						//Tracking Code: eDialogConversion
						pch.lotto.trackingCode("OnSubmitClickEventUserKnownWithCookie");
					};

				}) //end animate
			} //endif
		})//end animate
	});


	
	//Tracking Code: Coremetrics
	pch.lotto.trackingCode("LottoGameOnSumbitEvent");
	
	return false;
}

function lottoGameAnimateReg(bUserLoginWithoutGamePlay){
	bUserLoginWithoutGamePlay = (typeof bUserLoginWithoutGamePlay == "undefined") ? false: bUserLoginWithoutGamePlay;
	if (bUserLoginWithoutGamePlay)
	{
		pch.lotto.customerData.currGameCardIndex = pch.lotto.NO_GAME_PLAY;
		pch.lotto.init(null,false);		
	}

	$(".lottoGameCardButton, .lottoGameCard, #lottoPathAd, .lottoGamePicks").hide();	
		
	
	$("#lottoGameRegistration").show(1,function(){
		$("#lottoGameRegistration").animate({left: "0px"}, 555, function(){
			$("#gameMessage").html("Please Register");																		 
			//lottoGameAdCountDown(true);
		});	
	});

	if ($("body").hasClass("ie6"))
	{
		$("#lottoGameRegistration input")
			.bind("noEvents",function(e) {
					e.stopPropagation();
					e.preventDefault();
			})			
			.click(function() { $(this).trigger("noEvents")})		
			.focus(function() { $(this).trigger("noEvents")})
			.blur(function() { $(this).trigger("noEvents")})
			.change(function() { $(this).trigger("noEvents")});
	}

	//DFA Conversion
	pch.lotto.trackingCode("DFA_PopulateRegistrationForm");	
}

function lottoGameHideReg(callback) {
	iWhere = -$("#lottoPathWrap").width()

	$("#lottoGameRegistration").animate({left: iWhere}, 555, 
			function() { 
				$("#lottoGameRegistration").hide(1); 
				
				if (typeof callback != "undefined")
				{
					callback();
				}
			}
	);
}

function lottoGameSubmitReg(){

	//Place any needed functionality related to game card submission here
	
	//Hide the game card
	lottoGameHideReg(function() {																			  
		$("#gameMessage").html("Loading...");
		//Handled by SubmitEntry webservice call
		//lottoGameAnimateAd();
	});	
	return false;
}

function lottoGameAnimateAd(){
	$(".lottoGameCard, .lottoGamePicks").hide();
	$("#lottoPathAd").show(1,function(){
		$("#lottoPathAd").animate({left: "0px"}, 555, function(){
			lottoGameAdCountDown(true);
		});	
	});
}


var adTime;
function lottoGameAdCountDown(bStart){
	if(bStart) adTime = lottoGameCard[currlottoGameCard].adLen
	else adTime--;
	
	var strSpacer =""
	if(adTime<10) strSpacer="<span>0</span>"
	
	$("#gameMessage").html(strSpacer+adTime + " seconds remaining");
	
	if(adTime > 0){
		window.setTimeout(function(){lottoGameAdCountDown(false)},1000);
	}else{
		loadNextGameCard();
	}
}

function loadNextGameCard() {
	gamesPlayed = pch.lotto.customerData.gamesPlayed.length;
	if(gamesPlayed>lottoGameCard.length){
		//alert("ALL DONE!");
		$(".lottoGameCard").hide();
	}
	
	//Taken out. What if more game cards where added?
	//currlottoGameCard++					
	//if(currlottoGameCard>3) currlottoGameCard=0;
	
	
	//iWhere = -$("#lottoPathWrap").width();
	
	buildCard(currlottoGameCard,true);
	$("#gameMessage").html("Loading...");
}

function lottoGameStatusBar(bDisabled){
	
	var noGamePlayRegistration = false;

	//Backwards compatibility
	lottoGameCard = pch.lotto.lottoGameCard;
	currlottoGameCard = (pch.lotto.customerData.currGameCardIndex == pch.lotto.NO_GAME_PLAY) ? 0: currlottoGameCard;
	bDisabled = (typeof bDisabled == "undefined") ? false: bDisabled;
	
	var pL = lottoGameCard[currlottoGameCard].pickLimit-myPicks.length;
	var pT = lottoGameCard[currlottoGameCard].pickLimit;
	
	if (!bDisabled)
	{
		$("#gameMessage").html(pL + " picks out of " + pT + " remaining");
	}
	
	var strHTML=""
	
	var totalGames = pch.lotto.customerData.totalGames;
	gamesPlayed =  pch.lotto.customerData.gamesPlayed.length;	

	for(iX=0;iX<totalGames;iX++){
		strHTML+='<span';

		if (!bDisabled)
		{
			if(gamesPlayed>iX){
				strHTML+=' class="done"';
			}else if(gamesPlayed==iX){
				strHTML+=' class="curr"';			
			}
		}
		strHTML+='>Game '+(iX+1)+'</span>'
	}
	
	$("#pathStatus").html(strHTML)
		
}