var prize_id;
var prize_description;
var skill_question_id;
var skill_question;

var skill_timer;
var skill_timer_id;

// instant win functions
function instantWinCheck(idValue, winCallback, loseCallback) {
	$.post(api_paths.instant_win, {id:idValue}, function(data) {
		if (data.success == "false") {
			// throw event to continue as normal
			omniture("?event=event12");
			prize_description = "0";
			loseCallback();
		} else if (data.success == "true") {
			omniture("?event=event11");
			// get prize info and skill testing question deets
			skill_question_id = data.skill_question_id;
			skill_question = data.skill_question;
			prize_id = data.prize_id;
			prize_description = data.prize_description;
			winCallback(data.prize_id, data.prize_description);
		}
	}, "json");
}

function claimPrizeLoaded(responseText, textStatus, XMLHttpRequest) {
	if (contestType == "AR") {
		$("#html-content").css("top","50px");
	}
	// push along prize_description, etc (kiss mark)
	var prizeClass = "prize-" + prize_description;
	if (lang == "fr") prizeClass += "-fr";
	$("#html-content .congratulations .prize").attr("class", prizeClass);
	
	htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest);
}

function skillTestingQuestionLoaded(responseText, textStatus, XMLHttpRequest) {
	$("#html-content").css("top","100px");
	skill_timer = 0;
	skill_timer_id = window.setInterval("skillTestingQuestionTimerUpdate()", 1000);
	$("#html-content #question").html(skill_question);
	
	htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest);
}

function skillTestingQuestionTimerUpdate() {
	skill_timer++;
	var timerValue = (skill_timeout * 60) - skill_timer;
	var minutes = Math.floor(timerValue / 60);
	var seconds = timerValue - (minutes * 60);
	$("#timer").html(minutes + ":" + ((seconds < 10) ? "0" + seconds : seconds));
	if (minutes <= 0 && seconds <= 0) {
		window.clearInterval(skill_timer_id);
	}
}

function skillTestingQuestionSubmit() {
	var answer = $("#html-content form [name=answer]").val();
	$.post(api_paths.skill_testing, {skill_question_answer:answer}, function(data) {
		if (data.success == "false") {
			htmlEvent("skillTestingQuestionWrong");
		} else if (data.success == "true") {
			htmlEvent("instantWinClaimForm");
		}
	}, "json");
}

function skillTestingQuestionWrongLoaded(responseText, textStatus, XMLHttpRequest) {
	if (contestType == "AR") {
		$(".enter-upc").remove();
		$(".close").remove();
	} else {
		$(".continue").remove();		
	}
	
	htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest);
}

function instantWinClaimFormLoaded(responseText, textStatus, XMLHttpRequest) {
	if (contestDOB) {
		$("#html-content form #dob").val(contestDOB);
	}
	if (contestProvince) {
		$("#html-content form #province").val(contestProvince);
	}
	omniture("?event=event14");
	if (contestType == "AR") {
		$("#instant-win").addClass("ar");
		$("#html-content .container").css("height","400px");
		$("#html-content").css("top","50px");
		$("#slider-bg").css("margin-left", "570px");
		$("#slider").css("margin-left", "569px");
	}
	
	htmlContentLoaded(responseText, textStatus, XMLHttpRequest);
}

function instantWinClaimFormSubmit() {
	$.post(api_paths.instant_win_form, $("#html-content form").serialize(), function(data) {
		if (data.success == "false") {
			// show errors, etc
			if ("errors" in data) {
				showRegistrationErrors(data.errors);
			}			
		} else if (data.success == "true") {
			omniture("?event=event15");
			// on to next step, reset slider to normal position
			$("#html-content .container").css("height","425px");
			$("#html-content").css("top","100px");
			$("#slider-bg").css("margin-left", "643px");
			$("#slider").css("margin-left", "642px");
			htmlEvent("instantWinPrint");
		}
	}, "json");
}

function instantWinPrintLoaded(responseText, textStatus, XMLHttpRequest) {	
	if (contestType == "AR") {
		$(".enter-upc").remove();
	}
	
	htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest);
}

function instantWinComplete() {
	omniture("?event=event4");
	if (contestType == "AR") {
		$("#html-content .container-static").css("visibility","visible");
		arResponse("claimYourPrize", "true");
		$("#html-content .container-full").hide();
	} else if (contestType == "UPC") {
		setLocation('/home');
	}
}

function instantWinFailed() {
	htmlEvent("instantWinNonWinner");
}

function instantWinNonWinnerLoaded(responseText, textStatus, XMLHttpRequest) {
	if (contestType == "AR") {
		$(".enter-upc").remove();
	}
	omniture("?event=event4");	
	htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest);
}