
sjq(document).ready(function () {
	LEGO.VIP.init();
	LEGO.VIP.modals.init();
});

LEGO.VIP = LEGO.VIP ? LEGO.VIP : {
	init: function(){
		// Listener for openRedemptionResetModal
		if(LEGO.redemptionReset){ this.openRedemptionResetModal();}
		// Listener for openVipOptIn
		if(LEGO.openVipOptIn){
			LEGO.modal.openModal(null, {name:LEGO.modal.large, href:"/VIP/modal/vipOptingInModal.jsp"});
		}
	},
    OpenVIPWindow: function (langId) {
    	window.open("http://shopkr.lego.com/VIP/WhatIsVIP.aspx?LangId="+langId, "_new", "status=0,tollbar=0,resizable=0,location=0,width=670px,height=820px")
    },
    toggleVIPMessage: function () {
	    sjq(".DIVJoinVIPCL").css("display", "none");
	    sjq(".DIVJoinedVIPCL").css("display", "block");
    	sjq("#optInVIP").val("true");
    	document.cookie = 'OptInLEGOVIP=true'

    },
  	checkJoinVIPMessage: function () {
		if(LEGO.VIP.readCookie('OptInLEGOVIP')=='true') {
			sjq(".DIVJoinVIPCL").css("display", "none");
			sjq(".DIVJoinedVIPCL").css("display", "block");
			sjq("#optInVIP").val("true");
		}
	},
    readCookie: function(name) {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
	},
	openTermsAndConditionsFromOptInModal: function(langId) {
	// THIS IS NOT CURRENTLY BEING CALLED - LEAVING IT HERE TILL BUSINESS DECIDES WHAT TO DO
	// DONT USE THIS
	// MATT FALKOWSKI
		LEGO.modal.close()
		setTimeout(function (langId) {
			// TODO: Remove hard coded webqa reference
			LEGO.modal.openModal(null, {name:LEGO.modal.large, href:"http://shopkr.lego.com/VIP2/terms/default.aspx?LangId="+langId})
			LEGO.modal.setClose(function () {
				LEGO.modal.close();
				setTimeout(function(){
					sjq.ajax({
						url: "/VIP/modal/vipOptingInModal.jsp",
						success: function(data){
							LEGO.modal.openModal(event, {name:LEGO.modal.large, source:data})
						},
						error: function(jqXHR, textStatus, errorThrown){
							throw new Error ("LEGO.VIP.openTermsAndConditionsFromOptInModal:\nError loading async modal:\njqXHR: " + jqXHR + "\ntextStatus: " + textStatus + "\nerrorThrown: " + errorThrown);
						},
						dataType: 'html'
					})
				}, 700) // THIS TIMEOUT NEEDS TO BE LONGER THAN THE FADE OUT IN LEGO.modal..close()
			})
		},700); // THIS TIMEOUT NEEDS TO BE LONGER THAN THE FADE OUT IN LEGO.modal..close()
	},
	openRedemptionResetModal: function(){
		sjq.ajax({
			url: LEGO.currentURLPrefix + "/VIP/modal/vipRedemptionResetModal.jsp",
			context: document.body,
			success: function(html){
				LEGO.modal.openModal(null, {name:LEGO.modal.small, source:html})
			},
			error: function(jqXHR, textStatus, errorThrown){
				throw new Error ("LEGO.VIP.openRedemptionResetModal:\nError loading async modal:\njqXHR: " + jqXHR + "\ntextStatus: " + textStatus + "\nerrorThrown: " + errorThrown);
			},
			dataType: 'html'
		});
	}
}

LEGO.VIPMicrosite = LEGO.VIPMicrosite ? LEGO.VIPMicrosite : {
	setup: function (args) {
		args.iframe = sjq("#"+args.iframeId);
		args.iframe.css({visibility:"hidden"});
		sjq(args.iframe).load(function(){
			var container = args.iframe.contents().find(args.targetContainer);
			args.height = container.height() + 10; // +10 is a work around for the bad floats in the iframe
			args.width = 770;//container.width(); // hardcoded 770 as a work around for the bad floats in the iframe
			LEGO.VIPMicrosite.resizeIframe(args);
		});
	},
    resizeIframe: function (args) {
		window.setTimeout(function(){
			sjq(args.iframe).css({width:args.width,height:args.height});
			args.iframe.css({visibility:"visible"});
		},1000);
	}
}

