var lang = "en";

var sliderSize;
var percent;
var containerHeight;
var scrollHeight;
var isIE6;
var isIE7;

var contestType = "AR"; // "AR" or "UPC"
var contestHost; // for UPC flow use
var contestProvince;
var contestDOB;

// for gallery use
var lastPage = 1;
var lastSort = "views";
var currentVideo = "";

var galleryMode = false;

var RecaptchaOptions = {theme:"blackglass"};

function initDentyne() {
	lang = $("html").attr("lang");
	if (lang == "fr") {
		html_paths = html_paths_fr;
	}
	
	isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	isIE7 = /msie|MSIE 7/.test(navigator.userAgent);
	
	var lockout = $.cookie('Dentyne_lockout'); // check for age verification lockout
	
	var vars = {lang:lang,lockout:lockout};
	var params = { menu:"false", allowFullScreen:"true", allowScriptAccess:"sameDomain", wmode:"opaque" };
	var attributes = { id:'flash-object', name:'flash-object' }; // give an id to the flash object

	swfobject.embedSWF(flash_paths.main, "flash-content", "100%", "100%", "9.0.115", "swf/playerProductInstall.swf", vars, params, attributes, flashStatus );
	
	$(window).resize(positionHTML);
	window.setInterval(positionHTML, 2000);
	positionHTML();
	
	omniture("?pageName=home");
	
//	window.setTimeout(htmlEvent, 1000, "instantWinNonWinner");
}

function flashStatus(e) {
	if (!e.success) {
		flashInstallFailed();
	} else if (e.success) {
	}
}

function flashInstallFailed() {
	var noFlashURL = "mobile/contest.php?lang=" + lang
	
	document.location = noFlashURL;
}

function positionHTML() {
	var xPos;
	if (galleryMode) {
		xPos = parseInt(($(document).width() - $("#html-content").width() - 39) / 2) + "px";
	} else {
		xPos = parseInt(($(document).width() - $("#html-content").width()) / 2) + "px";
	}
	$("#html-content").css("left", xPos);
}

function setLocation(hash) {
	SWFAddress.setValue(hash);
}

function htmlEvent(event, value) {
	galleryMode = false;

	switch(event) {
		case "showContest":
			thisMovie("flash-object").sizeBackground("405");
			showPage(html_paths.webcam_select, "full", webcamSelectLoaded, true);
			break;
		case "showContestAR":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.ar_registration, "standard", contestARLoaded);
			break;
		case "showContestUPC":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.upc_registration, "standard", contestUPCLoaded);
			break;
		case "showARInstructions":
			thisMovie("flash-object").sizeBackground("335");
			showPage(html_paths.ar_instructions, "full", instructionsLoaded);
			break;
		case "showAR":
			showPage(html_paths.ar_container, "static", arLoaded);
			break;
		case "claimPrize":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.instantwin_congratulations, "full", claimPrizeLoaded);
			break;
		case "showARConsentForm":
			showPage(html_paths.ar_video_consent, "standard", consentFormLoaded);
			break;
		case "skillTestingQuestion":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.instantwin_skilltestingquestion, "full", skillTestingQuestionLoaded);
			break;
		case "skillTestingQuestionWrong":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.instantwin_skilltestingwrong, "full", skillTestingQuestionWrongLoaded);
			break;
		case "instantWinClaimForm":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.instantwin_form, "standard", instantWinClaimFormLoaded);
			break;
		case "instantWinPrint":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.instantwin_print, "full", instantWinPrintLoaded);
			break;
		case "instantWinNonWinner":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.instantwin_nonwinner, "full", instantWinNonWinnerLoaded);
			break;
		case "upcInstantWinCheck":
			thisMovie("flash-object").sizeBackground("465");
			showPage(html_paths.upc_instantwin_check, "full", upcInstantWinCheckLoaded);
			break;
		case "showGallery":
			galleryMode = true;
			var galleryURL = html_paths.gallery + "?p=" + lastPage + "&s=" + lastSort;
			showPage(galleryURL, "full", galleryLoaded, true);
			break;
		case "showGalleryVideo":
			galleryMode = true;
			showPage(html_paths.gallery_video, "full", galleryVideoLoaded, true);
			break;
		default:
	}
}

// run jQuery getScript to lazy load omniture tracking
function omniture(params) {
	$.getScript(omniture_paths.track + params);
}

/*		case "contestStepTwo":
			if (contestType == "AR") {
				htmlEvent("showContestAR");
			} else if (contestType == "UPC") {
				htmlEvent("showContestUPC");
			}
			break;
		case "contestStepThree":
			if (contestType == "AR") {
				htmlEvent("showARInstructions");
			} else if (contestType == "UPC") {
				htmlEvent("upcInstantWinCheck");
			}
			break;*/

function flashEvent(event, value) {
	switch(event) {
		case "closeContent":
			closeHTMLContent();
			break;
		case "ageVerification":
			ageVerification(value[0], value[1], value[2]);
			break;
		case "ageLockOut":
			ageLockOut();
			break;
		case "contestStepOne":
			skipContest();
			//htmlEvent("showARInstructions");
			break;
		case "claimPrize":
			if (prize_description == "0") {
				htmlEvent("instantWinNonWinner");
			} else {
				htmlEvent("claimPrize");				
			}
			break;
		case "showAR":
			htmlEvent("showAR");
			break;
		case "showGallery":
			htmlEvent("showGallery");
			break;
		case "showGalleryVideo":
			htmlEvent("showGalleryVideo");
			break;
		case "certified":
			omniture("?event=event13");
			break;
		default:
	}
}

function ageVerification(province, dob, valid) {
	contestProvince = province.toUpperCase();
	contestDOB = dob.replace(/[\[\]]/g, "");
	omniture("?dob=" + contestDOB + "&province=" + province + "&language=" + lang.toUpperCase());
	if (valid == "false") {
		ageLockOut();
	}
}

function ageLockOut() {
	if ($.cookie('Dentyne_lockout') != "true") {
		var date = new Date();
		date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
		$.cookie('Dentyne_lockout', 'true', { path: '/', expires: date });
	}
}

function showPage(url, size, callback, fade) {
	$("#html-content .container").hide();
	$("#html-content .container-full").hide();
	$("#html-content .container-static").css("visibility","hidden");

	if (size == "full") {
		$("#html-content .container-full").html("");
		$("#html-content .container-full").load(url, "", callback);
		$("#html-content .container-full").show();
		if (fade) {
			$("#html-content").fadeIn();
		} else {
			$("#html-content").show();
		}
	} else if (size == "static") {
		// if this gets opened, it stays open until dismissed explicitly
		$("#html-content .container-static").html("");
		$("#html-content .container-static").load(url, "", callback);
		$("#html-content .container-static").show();
		$("#html-content .container-static").css("visibility","visible");
		if (fade) {
			$("#html-content").fadeIn();
		} else {
			$("#html-content").show();
		}
	} else {
		$("#html-content .container .scroll").html("");
		$("#html-content .container .scroll").load(url, "", callback);
		$("#html-content .container").show();
		if (fade) {
			$("#html-content").fadeIn();
		} else {
			$("#html-content").show();
		}
	}
}

function htmlContentLoaded(responseText, textStatus, XMLHttpRequest) {
	$("#html-content #handle").css("top", "0px");
	$("#html-content .scroll").css("margin-top", "0px");

	containerHeight = $("#html-content  .container").height();
	scrollHeight = $("#html-content .scroll").height();
	
	// alert(containerHeight);
	// alert(scrollHeight);
	
	if (scrollHeight <= containerHeight && !isIE6) {
		$("#html-content #slider-bg").css("visibility", "hidden");
		$("#html-content #slider").css("visibility", "hidden");
	} else {
		$("#html-content #slider-bg").css("visibility", "visible");
		$("#html-content #slider").css("visibility", "visible");		
	}
	
	$("#html-content #handle").draggable({containment:'parent',axis:"y",drag:htmlScrollPositionStart,stop:htmlScrollPosition});
	positionHTML();
}

function htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest) {
	$("#html-content #handle").css("top", "0px");
	$("#html-content .scroll").css("margin-top", "0px");
	$("#html-content #slider-bg").css("visibility", "hidden");
	$("#html-content #slider").css("visibility", "hidden");
	positionHTML();
}

function closeHTMLContent() {
	$("#html-content").hide();
	$("#html-content .container").hide();
	$("#html-content .container-full").hide();
	$("#html-content .container-static").hide();
}

function htmlScrollPositionStart() {
	sliderSize = $("#html-content #slider").height() - $("#html-content #handle").height();
	containerHeight = $("#html-content  .container").height();
	scrollHeight = $("#html-content .scroll").height();
	if (scrollHeight <= containerHeight && !isIE6) {
		$("#html-content #slider-bg").css("visibility", "hidden");
		$("#html-content #slider").css("visibility", "hidden");
	} else {
		$("#html-content #slider-bg").css("visibility", "visible");
		$("#html-content #slider").css("visibility", "visible");
	}

	htmlScrollPosition();
}

function htmlScrollPosition(e, ui) {
	percent = parseInt($("#html-content #handle").css("top")) / sliderSize;
	var newMargin = parseInt((containerHeight - scrollHeight) * percent) + "px";
	if (scrollHeight > containerHeight) {
		$("#html-content .scroll").css("margin-top", newMargin);
	}
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}