var arFlavor;

function arResponse(theQuery,theResponse) {
     thisMovie("DentyneLoader").arResponse(theQuery,theResponse);
}

function arQuery(theQuery,theValue) {
	//var answer = confirm("query:"+theQuery+"\nvalue:"+theValue);

	switch(theQuery) {
		case "getContestResult":
			if (theValue == "0") {
				arInstantWinLose();
			} else {
				instantWinCheck(theValue, arInstantWinWin, arInstantWinLose);
			}
			break;
			
		case "getRegistration":
			htmlEvent("showARConsentForm");
			break;

		case "claimYourPrize":
			htmlEvent("claimPrize");
			break;
			
		case "uploadComplete":
			postVideoID(theValue);
			break;
			
		case "putUPC":
			arFlavor = theValue;
			omniture("?flavor=" + arFlavor);
			break;
	}
}

function arOmniture(theEvent) {
	switch(theEvent) {
		case "AR_LANDING":
			omniture("?event=event23");
			break;

		case "AR_COMPLETED":
			omniture("?event=event24");
			break;

		case "AR_MARKER":
			omniture("?event=event33");
			break;

		case "AR_KISSING":
			omniture("?event=event34");
			break;
	}
}

function closeAR() {
	$("#html-content .container").css("height","425px");
	$("#html-content").css("top","100px");
	$("#slider-bg").css("margin-left", "643px");
	$("#slider").css("margin-left", "642px");
	setLocation('/home');
}

function arLoaded(responseText, textStatus, XMLHttpRequest) {
	var user_data = getUserData();
	var vars = {user_id:user_data.id,user_language:user_data.language.toUpperCase()};
	var params = { menu:"false", allowFullScreen:"true", allowScriptAccess:"sameDomain", wmode:"transparent" };
	var attributes = { id:'DentyneLoader', name:'DentyneLoader' }; // give an id to the flash object
	swfobject.embedSWF(flash_paths.ar, "ar-flash-content", "550", "400", "10", "swf/playerProductInstall.swf", vars, params, attributes);
	
	htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest);
}

function consentFormLoaded(responseText, textStatus, XMLHttpRequest) {
	$("#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 submitARConsent() {
	//alert("The form was filled out! Thanks!");
	$("#html-content form #dob").val(contestDOB);
	$("#html-content form #language").val(lang.toUpperCase());
	
	$.post(api_paths.video_consent, $("#html-content form").serialize(), function(data) {
		$(".errors").hide();
		if (data.success == "false") {
			//alert("There was an error with your form!");
			// show errors
			if ("errors" in data) {
				showRegistrationErrors(data.errors);
			}
		} else if (data.success == "true") {
			// set user data cookie
			//alert("Thanks for filling out the form! (" + data.id + ")");
			//var tMovie;
			//if (navigator.appName.indexOf("Microsoft") != -1) {
			//	tMovie = document.getElementById("DentyneLoader");
			//} else {
			//	tMovie = document["DentyneLoader"];
			//}
			//var tMsg = "";
			//alert("Have a fun time! (" + tMovie + ")");
			//for(var i in tMovie)
			//{
			//	tMsg += "i:" + i + " obj:" + tMovie[i] + "\n";
			//}
			//alert(tMsg);
			//tMovie.uploadResponse("123456789");
			//arResponse("getRegistration", data.id);
			//alert("We are really happy about you filling out that form!");
			setUserData(data.id, data.email, data.language);
			// redirect to next step
			$("#html-content .container").css("height","425px");
			$("#html-content").css("top","100px");
			$("#slider-bg").css("margin-left", "643px");
			$("#slider").css("margin-left", "642px");
			$("#html-content .container").hide();
			$("#html-content .container-static").css("visibility","visible");
			arResponse("getRegistration", data.id);
			//alert("Ready to upload?");
		}
	}, "json");
}

function arInstantWinWin(prize_id, prize_desc) {
	arResponse("getContestResult", prize_desc); // expecting 0,250,500,1000,5000 (blocking), theValue == unique id
}

function arInstantWinLose() {
	// show messaging re: didn't win, but still entered for grand prize
	// if zero, AR offers regular play again / upload controls
	arResponse("getContestResult", "0"); // expecting 0,250,500,1000,5000 (blocking), theValue == unique id	
}

function postVideoID(encoding_id) {
	$.post(api_paths.video_id, {encoding_id:encoding_id}, function(data) {
		if (data.success == "false") {
		} else if (data.success == "true") {
			omniture("?event=event10");
			arResponse("uploadComplete","true");
			thisMovie("flash-object").contestComplete(arFlavor);
		}
	}, "json");
}

function claimPrize() {
	htmlEvent("claimPrize");
}

function skillTestingQuestion() {
	htmlEvent("skillTestingQuestion");
}