

// ===========================================
// = Initialize MillerCoors Shared Framework =
// ===========================================

var mcf = new mcFramework('wrapper','av_container');
mcf.mcSetSiteCode('mgd64');
mcf.mcSetAVContent('Please enter your date of birth to continue.');
mcf.mcSetCallbackFunction(initPage);
mcf.mcSetAVTag(avtag);
mcf.mcSetAVFailTag(avfailtag);
//mcf.mcSetAVFailForward(false); //Uncomment this line to temporarily disable forwarding to thecoolspot.com

mcf.mcLaunchPrivacy = function() {
	var name = Math.round( 9999*Math.random( ) ) + new Date( ).getTime( );
  var win = window.open( "/legal/privacy-policy.html","FooterLinks" + name,"toolbar=0,menubar=0,width=640,height=500,status=0,resizable=1,scrollbars=1" );
  if( !win ) { getSwf(swfId).openWindowFromSwf( "/legal/privacy-policy.html" ); }
  else { winNew.focus( ); }
}

mcf.mcLaunchTerms = function() {
	var name = Math.round( 9999*Math.random( ) ) + new Date( ).getTime( );
  var win = window.open( "/legal/terms-conditions.html","FooterLinks" + name,"toolbar=0,menubar=0,width=640,height=500,status=0,resizable=1,scrollbars=1" );
  if( !win ) { getSwf(swfId).openWindowFromSwf( "/legal/terms-conditions.html" ); }
  else { winNew.focus( ); }
}

// ================================
// = AV and AV Fail Tag Functions =
// ================================

function avtag() {
	trackPage('pagename=2009 MGD64 AV: Home,channel=2009 MGD64 AV,floodlight=mgd64263,atlas=Homepage_ageveri_image');
}

function avfailtag() {
	trackPage('pagename=2009 MGD64 AV: Fail,channel=2009 MGD64 AV');
}

// ============================
// = On Content Load Function =
// ============================

function initPage() {
	var params = {
		quality: "high",
		scale: "noscale",
		wmode: "opaque",
		allowscriptaccess: "always",
		bgcolor: "#FFFFFF"
	};
	var flashvars = {
		siteXML: "xml/site.xml"
	};
	var attributes = {id:"content"};
	swfobject.embedSWF("main.swf", "content", "100%", "100%", "9.0.124", "expressInstall.swf", flashvars, params, attributes);
	swffit.fit("content",1000,650,2000,1500);
	
}
function setFlashToHome() {
	var flashMovie=getSwf("content");
	flashMovie.setAddressToHome();
}

// =========================
// = Overlay Functionality =
// =========================

function launchOverlay(curOverlay) {
	centerDiv('locator');
	overlay.show();
}

// Live Servlet Url: ???
// Stage Servlet: http://stage.locator.mgd64.com/APLocator.aspx
// Live API Key: ABQIAAAADEfJitKfoB_llxtJ0rnnRRRb3kwNYQPP1xvvoiT9riYhx8FbOxQgMTmvOS7R9JkyiH1saOXf3-izjw
// Stage API Key: ABQIAAAADEfJitKfoB_llxtJ0rnnRRT53z8mcN83kGiMmHozY09lTTwp_BSvFgztQkS7FlL7p1Dlleypf8QeGQ

var overlay = {
	show: function() {
		var flashvars = {
			widget: 'false',
			baseURL: 'http://locator.mgd64.com/',
			apiKey: 'ABQIAAAADEfJitKfoB_llxtJ0rnnRRRb3kwNYQPP1xvvoiT9riYhx8FbOxQgMTmvOS7R9JkyiH1saOXf3-izjw'
		}
		var attributes = {}
		var params = {
			wmode: "transparent",
			align: "middle",
			scale: "showall",
			allowFullScreen: "false"
		}
		swfobject.embedSWF("assets/swf/mgd64_locator.swf", "locator_flash", "750", "540", "9.0.124", "expressInstall.swf", flashvars, params, attributes);
		
		$('#locator').show();
	},
	hide: function() {
		$('#locator').hide();
		setFlashToHome();
	}
}


// ================
// = Spring Into Action =
// ================

function launchSignUp() {
	centerDiv('signup');
	$('#signup').show();
	trackPage('pagename=2009 MGD 64 E-mail Sign Up: Home,channel=2009 MGD 64 E-mail Sign Up,floodlight=mgd64149,atlas=Homepage_EmailSignUpLink_image');
}


/* ========================== */
/* = Holiday Overlay Launch = */
/* ========================== */

function launchSpringIntoAction() {
	centerDiv('SpringIntoAction');
	$('#SpringIntoAction').show();
	$('#SpringIntoAction_intro').show();
	trackPage('pagename=2010 MGD64 Spring Into Action: Intro,channel=2010 MGD64 Spring Into Action');
}
function showSpringIntoActionForm(){
	$('#SpringIntoAction_intro').hide();
	$('#SpringIntoAction_form').show(); 
	$('#SpringIntoActionForm').show();
	trackPage('pagename=2010 MGD64 Spring Into Action: Form,channel=2010 MGD64 Spring Into Action');
}

function closeSpringIntoAction(){
	$('#SpringIntoAction_intro').hide();
	$('#SpringIntoAction_form').hide();
	
	$('#SpringIntoAction_entered').hide(); 
	$('#SpringIntoAction_download').hide(); 
	$('#SpringIntoAction').hide();
	$('.formField').val('');
	$('#mcMobileInput').hide();
	$('#errorBox').hide();
	$('label').show();
	$('label').each(function() {
		var for_attr = $(this).attr('for');
		var labelName = $(this)
	    $("input#" + for_attr + "[type='text']").focus(function()
		{
			labelName.hide();
		});
																
	});
	
	resetCheckboxes();
	$("input[type='text']").removeClass("error");
	$("#SweepsForm1_ddlState").css("background-color", "#ffffff");
	$("#officialRulesLabel").css("color", "#000000");
	$("#mcErrorPrompt").css("background-color", "transparent");
	$("#mcErrorPrompt").css("border", "none");
	
	setFlashToHome();
	
}

function launchResolutionsPromo() {
	centerDiv('holidayResolutions');
	$('#holidayResolutions').show();
	$('#holidayResolutions_intro').show();
	trackPage('pagename=2010 MGD64 Resolutions: Home,channel=2010 MGD64 Resolutions');
}
function showResolutionsForm(state){
	$('#holidayResolutions_intro').hide();
	$('#holidayResolutions_form').show(); 
	if(state == "California")
	{
		trackPage('pagename=2010 MGD64 Resolutions: Form CA,channel=2010 MGD64 Resolutions');
	}
	else
	{
		trackPage('pagename=2010 MGD64 Resolutions: Form National,channel=2010 MGD64 Resolutions');
	}
}



function resetHolidayForm()
{
	  $("#mcSharedForm").remove();
	 $("#mcframe").remove();
	 $("#nationalDisclaimerTitle").show();
	 $("#nationalDisclaimerTitle").show();
	 $("#nationalDisclaimer").show();
	 $("#californiaDisclaimer").show();
	 $("#californiaDisclaimerTitle").show();
	 $("#holidaySuccess").hide();
	 $("#nationalHeader").show();
	 $("#californiaHeader").show();
	 
	 $("#selectState").val("");
	 
	 $("#disclaimer").css("top","440px"); 
}

//form success handling
function mcfSuccess(mcfResponse, mcfState, mcfMagazine)
{
	
	$("#nationalHeader").hide();
	$("#californiaHeader").hide();
	
	$("#loading").hide();
	
	$("#holidaySuccess").show();
	
	$("#californiaEntered").hide();
	$("#californiaSuccess").hide();
	$("#nationalSuccess").hide();
	$("#nationalEntered").hide();
	
	
	if(mcfResponse == "success" && mcfState == "CA")
	{
		trackPage('pagename=2010 MGD64 Resolutions: Confirmation CA,channel=2010 MGD64 Resolutions');
		
		$("#californiaSuccess").show();

		if(mcfMagazine == "MensFitness")
		{
			
			$("#magazineLink").attr("href", "http://www.mensfitness.com/bca");
		}
		else
		{
			$("#magazineLink").attr("href", "http://www.shape.com/bca");
		}
	}
	
	else if(mcfResponse != "success" && mcfState == "CA")
	{
		trackPage('pagename=2010 MGD64 Resolutions: Already Entered CA,channel=2010 MGD64 Resolutions');
		$("#californiaEntered").show();
	}
	else if(mcfResponse == "success" && mcfState != "CA")
	{
		$("#nationalSuccess").show();
		trackPage('pagename=2010 MGD64 Resolutions: Confirmation National,channel=2010 MGD64 Resolutions');
	}
	else if(mcfResponse != "success" && mcfState != "CA")
	{
		$("#nationalEntered").show();
		trackPage('pagename=2010 MGD64 Resolutions: Already Entered National,channel=2010 MGD64 Resolutions');
	}
	
}


$(document).ready(function() {


    $("label").inFieldLabels();
    validate();

    $("#signup_form").validate({
        submitHandler: function(form) {
            $('#signup_confirm').show();
            trackPage('pagename=2009 MGD 64 E-mail Sign Up: Confirmation,channel=2009 MGD 64 E-mail Sign Up');
        },
        invalidHandler: function(form, validator) {
            $('#error_message').html('<div style="color: #E6260F;text-weight:bold;">Please correct the highlighted fields below.</div>');
        }
    });


    $("#moptin").bind("click", function() {
        if ($('#moptin').attr('checked')) {
            $('#mobile_confirmation').show();
        }
    });

    $("#accept").bind("click", function() {
        $('#mobile_confirmation').hide();
    });

    $("#decline").bind("click", function() {
        $('#moptin').attr('checked', '')
        $('#mobile_confirmation').hide();
    });

    $("#signup_close").bind("click", function() {
        $('#signup').hide();
        setFlashToHome();
    });

    $("#holidayPromo_close").bind("click", function() {
        $('#holidayPromo').hide();

        setFlashToHome();
    });

    $("#holidayResolutions_close").bind("click", function() {
        $('#holidayResolutions').hide();

        setFlashToHome();
    });

    $("#selectState").change(function() {
        var theSelectedState = $("select option:selected").text();
        if (theSelectedState != "State") {
            //set state for form
            mcfSelectedState = theSelectedState;


            //display form
            if (theSelectedState == "California") {
                $("#nationalHeader").hide();
                $("#nationalDisclaimerTitle").hide();
                $("#nationalDisclaimer").hide();

            }
            else {
                $("#californiaHeader").hide();
                $("#californiaDisclaimerTitle").hide();
                $("#californiaDisclaimer").hide();
            }
            $("#disclaimer").css("top", "490px");

            //initalize form and send xml location
            mcff.TheForm("http://mgd64.com/TestForm/form.xml");

            showResolutionsForm(theSelectedState);
        }

        
    });

});

// ==============
// = Center Div =
// ==============

function centerDiv(div) {
	var div = $('#' + div);
	var height = $(window).height();
	var width = $(document).width();

  div.css({
  	'left' : width/2 - (div.width() / 2),  // half width - half element width
  	'top' : height/2 - (div.height() / 2), // similar
  	'z-index' : 15                        // make sure element is on top
  });
}



// ==========================
// = Popup Window Functions =
// ==========================

var swfId = "mbc_mainSwf";

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

function launchActiveTerms() {
  var name = Math.round( 9999*Math.random( ) ) + new Date( ).getTime( );
  var win = window.open("active_terms.html","FooterLinks" + name,"toolbar=0,menubar=0,width=400,height=325,status=0,resizable=1,scrollbars=1" );
  if( !win ) { getSwf(swfId).openWindowFromSwf( "active_terms.html" ); }
  else { winNew.focus( ); }
}

function getWidget() {
	window.open("locator/get_widget.html", "getwidget","status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=0,width=400,height=420");
	trackPage('pagename=MGD64 Product Locator: Get Widget,channel=MGD 64 Product Locator');
}
// ==========================
// = Popup Window Functions =
// ==========================

var swfId = "mbc_mainSwf";

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

function launchActiveTerms() {
  var name = Math.round( 9999*Math.random( ) ) + new Date( ).getTime( );
  var win = window.open("active_terms.html","FooterLinks" + name,"toolbar=0,menubar=0,width=400,height=325,status=0,resizable=1,scrollbars=1" );
  if( !win ) { getSwf(swfId).openWindowFromSwf( "active_terms.html" ); }
  else { winNew.focus( ); }
}

function getWidget() {
	window.open("locator/get_widget.html", "getwidget","status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=0,width=400,height=420");
	trackPage('pagename=MGD64 Product Locator: Get Widget,channel=MGD 64 Product Locator');
}
