//Title:			Smashbox Email Modal Windows	
//Created:		11/15/2010
//Author:			Jeff Dennis
//Version:		1.0.1 - 12/10/2010

/* * * * * * * * * * * * * * * * * * * * * * * *
 *
 *   Editable Parameters
 *
 * * * * * * * * * * * * * * * * * * * * * * * */
 
//var DomainName = 'sbsandbox.explorewebdev.com';
var DomainName = 'smashbox.com';

if(!modal_firstName) {
	var modal_firstName = "";
}
//var modal_firstName = "<%=getCurrentAttribute('customer','firstname','')%>";

//set modal dimensions
var modalHeight = 300;
var modalWidth = 500;

//thank you modal dimensions
var TYmodalHeight = 244;

//delay modal slide in (in seconds)
var modalDelay = 4;

//time for window to be shown before it automatically slides out (in seconds)
//this must be greater than the modal delay value
var interactionTriggerTime = 12;

//set these to true to automatically close the modals if there is no interaction													 
var modalAutoClose = true;		
var tyModalAutoClose = true;

//when set to false, clicking in the email input field will stop the slide out animation. if set to true, it will have the opposite affect.
var inputOverride = false;

/* * * * * * * * * * * * * * * * * * * * * * * *
 *
 *   Editable Content
 *
 * * * * * * * * * * * * * * * * * * * * * * * */
 
//set modal display to false
var modalDisplay = false;

//load copy array
var Line = new Array();
Line[0] = 'Yes! I want the scoop from Smashbox on exclusive beauty tips, easy tutorials, must-have products and step-by-step videos.';
Line[1] = 'Yes! I want the scoop from Smashbox on exclusive beauty tips, easy tutorials, must-have products and step-by-step videos.';
Line[2] = 'Send me the inside scoop on all the cool happenings at Smashbox, from exclusive online-only promotions to the hottest beauty tips and product launches.';
Line[3] = 'Send me the inside scoop on all the cool happenings at Smashbox, from exclusive online-only promotions to the hottest beauty tips and product launches.';
Line[4] = 'Yes! I want the inside scoop from Smashbox on online-only promotions, limited-time sales and exclusive product launches.';
Line[5] = 'Yes I am! I want to be the first to know about the fabulous new products, online-only kits and exclusive offers from Smashbox.';

//load header image array
var Header = new Array();
Header[0] = 'OptInHeader1.jpg';
Header[1] = 'OptInHeader2.jpg';
Header[2] = 'OptInHeader3.jpg';
Header[3] = 'OptInHeader4.jpg';
Header[4] = 'OptInHeader5.jpg';
Header[5] = 'OptInHeader6.jpg';

var modalArray = new Array();
modalArray[0] = [Header[0], Header[1], Header[2], Header[3], Header[4], Header[5]];
modalArray[1] = [Line[0], Line[1], Line[2], Line[3], Line[4], Line[5]];

function setContent(PageTitle) {
	var emailN;
	var title = PageTitle.toLowerCase();
	switch(title)
	{
		//Offers page, Insider Secrets, Best Sellers, and What's New pages
		case 'smashbox offers':
			emailN = 4;
			modalDisplay = true;
			break;
		case 'smashbox.com > best sellers':
			emailN = 5;
			modalDisplay = true;
			break;
		case 'tips':
			emailN = randomContent(0,1);
			modalDisplay = true;
			break;
		case 'what\'s new':
			emailN = randomContent(2,3);
			modalDisplay = true;
			break;
		//Product Categories / Sub-Categories
		//
		case 'best sellers':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'blush':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'body':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'body makeup | smashbox cosmetics - offical web store':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'bronzer':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'brows':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'brush cleaner':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'cheeks':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'concealer':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'concealer/brightener':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'egift certificate':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'eye liner':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'eye shadow':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'eyes':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'face':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'face makeup | smashbox cosmetics - official web store':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'foundation':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'highlighter':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'just added':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'kits':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'lip gloss':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'lip pencil':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'lip plumper':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'lip primer':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'lip treatment':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'lips':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'lipstick':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'makeup brushes and tools | smashbox cosmetics - official web store':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'makeup page':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'mascara':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'powder':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'primer':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'smashbox cosmetics | award winners makeup':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'smashbox cosmetics | multi-use makeup | official web store':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'smashbox cosmetics collections':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'smashbox cosmetics curtain-call | sales and special offers | smashbox cosmetics - official web store':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'smashbox kits | smashbox makeup kits | smashbox cosmetics':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'smashbox.com > cheeks makeup products | blushes, palettes & more':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'smashbox.com > eyes products | eye liners , eye shadows , mascaras & more':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'smashbox.com > lips | lip pencil , lip gloss , lip palettes & more':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'tinted moisturizer':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'tools & bags':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'travel':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		case 'view all':
			emailN = randomContent(2,3,5);
			modalDisplay = true;
			break;
		//
		//Default to
		default:
			emailN = false;
			modalDisplay = false;
			break;
	}
	
	return {'emailN':emailN, 'modalDisplay':modalDisplay};
}

/* * * * * * * * * * * * * * * * * * * * * * * *
 *
 *   Misc. Modal Functions
 *
 * * * * * * * * * * * * * * * * * * * * * * * */

function randomContent(x,y,z) {
	var n = (z) ? 3 : 2;
	var rand = Math.floor(Math.random()*n);
	var val;
	if(rand==0){val=x}
	if(rand==1){val=y}
	if(rand==2){val=z}
	return val;
}

function createIframe(email)
{
	var iframe = document.createElement('iframe');
	iframe.src = 'http://ebm.cheetahmail.com/r/regf2?aid=2086076959&a=1&n=2&BEHAVIOR_10=2&email=' + email;
	iframe.width = 1;
	iframe.height = 1;
	iframe.frameborder = 0;
	document.body.appendChild(iframe);
}

/* * * * * * * * * * * * * * * * * * * * * * * *
 *
 *   Jquery Code
 *
 * * * * * * * * * * * * * * * * * * * * * * * */
 
 $(document).ready(function() {
													 
//get browser height and width
	var getHeight = $(window).height();
	var getWidth = $(window).width();
	
	var halfWidth = getWidth/2;
	var halfModal = modalWidth/2;
	var modalLeft = halfWidth-halfModal;
	
	var halfHeight = getHeight/2;
	var halfModalH = modalHeight/2;
	var hideHeight = modalHeight*(-1);
	var modalTop = halfHeight-halfModalH;
	
//modal content
	var contentID = setContent(document.title);
	
//create HTML element for modal popup
	function createModal() {
		
		$('body').prepend('<div id="EM_modal"><div id="EM_bar"><div id="EM_close"></div></div></div>');
		
		$('#EM_modal').append('<img id="EM_header" src="http://smashbox.s3.amazonaws.com/site/files/emailModal/img/'+ modalArray[0][contentID.emailN] +'" />');
		$('#EM_modal').append('<p id="EM_blurb" align="center">'+modalArray[1][contentID.emailN] +'</p>');
		$('#EM_modal').append('<form id="EM_signupForm" name="form" action=""></form><br />');
		//$('#EM_modal').append('<p id="EM_disclaimer" class="small" align="center">By providing your information, you acknowledge that you have read, understand and agree to our <a class="pink" href="#" target="_blank">Terms &amp; Conditions</a> and <a class="pink" href="#" target="_blank">Privacy Policy</a>.</p>');
		
		//form elements
		$('#EM_signupForm').append('<label for="EM_emailinput" class="small">Email Address:</label><br />');
		$('#EM_signupForm').append('<input id="EM_emailinput" type="text" name="EMAIL" value="Sign up and save $15!" maxlength="4000" /><br />');
		$('#EM_signupForm').append('<div id="EM_submitCtr"><img id="EM_submit" name="submit" alt="submit" src="http://smashbox.s3.amazonaws.com/site/files/emailModal/img/OptInSubmit.jpg" /><!--input type="button" name="submit" value="submit" id="EM_submit" /--></div>');
		
		//modal container styles
		$('#EM_modal').css('font-family','Arial, Verdana, Helvetica, sans-serif');
		$('#EM_modal').css('font-size','14px');
		$('#EM_modal').css('width',modalWidth);
		$('#EM_modal').css('height',modalHeight);
		$('#EM_modal').css('background-color','#fff');
		$('#EM_modal').css('border','4px solid #000');
		$('#EM_modal').css('display','none');
		$('#EM_modal').css('position','absolute');
		$('#EM_modal').css('top',hideHeight);
		$('#EM_modal').css('left',modalLeft);
		$('#EM_modal').css('z-index','9998');
		$('.pink').css('color','#c06');
		$('.small').css('font-size','11px');
		
		//modal top bar styles
		$('#EM_bar').css('display','block');
		$('#EM_bar').css('background','#000');
		$('#EM_bar').css('height','25px');
		$('#EM_bar').css('width',modalWidth);
		
		//close button styles
		$('#EM_close').html('<img src="http://smashbox.s3.amazonaws.com/site/files/emailModal/img/OptInClose.jpg" alt="close" />');
		$('#EM_close').css('display','block');
		$('#EM_close').css('background','#000');
		$('#EM_close').css('height','25px');
		$('#EM_close').css('width','25px');
		$('#EM_close').css('float','right');
		$('#EM_close').css('cursor','pointer');
		$('#EM_close').css('z-index','9999');
		
		//form element styles
		$('#EM_signupForm').css('width','430px');
		$('#EM_signupForm').css('margin-left','auto');
		$('#EM_signupForm').css('margin-right','auto');
		
		$('#EM_emailinput').css('font-size','16px');
		$('#EM_emailinput').css('width','430px');
		$('#EM_emailinput').css('border','1px solid #000');
		$('#EM_emailinput').css('color','#c06');
		
		$('#EM_submitCtr').css('width','119px');
		$('#EM_submitCtr').css('margin-top','14px');
		$('#EM_submitCtr').css('margin-left','auto');
		$('#EM_submitCtr').css('margin-right','auto');
		
		$('#EM_submit').css('cursor','pointer');
		
		$('#EM_blurb').css('width','460px');
		$('#EM_blurb').css('margin-left','auto');
		$('#EM_blurb').css('margin-right','auto');
		
		$('#EM_disclaimer').css('width','460px');
		$('#EM_disclaimer').css('margin-left','auto');
		$('#EM_disclaimer').css('margin-right','auto');
	}
	
	function createTYModal() {
		
		$('body').prepend('<div id="EM_ty"><div id="EM_ty_bar"><div id="EM_ty_close"></div></div></div>');
		
		$('#EM_ty').append('<img id="EM_ty_header" src="http://smashbox.s3.amazonaws.com/site/files/emailModal/img/ThankYouHeader.jpg" />');
		$('#EM_ty').append('<p align="center">Thanks for signing up at Smashbox.com! Check your inbox soon for inside access to the hottest beauty trends, exclusive online promotions and must-have new products.</p>');
		
		//styles for thank you modal
		$('#EM_ty').css('font-family','Arial, Verdana, Helvetica, sans-serif');
		$('#EM_ty').css('font-size','14px');
		$('#EM_ty').css('width',modalWidth);
		$('#EM_ty').css('height',TYmodalHeight);
		$('#EM_ty').css('background-color','#fff');
		$('#EM_ty').css('border','4px solid #000');
		$('#EM_ty').css('display','none');
		$('#EM_ty').css('position','absolute');
		$('#EM_ty').css('top',modalTop);
		$('#EM_ty').css('left',modalLeft);
		$('#EM_ty').css('z-index','9998');
		
		$('#EM_ty_bar').css('display','block');
		$('#EM_ty_bar').css('background','#000');
		$('#EM_ty_bar').css('height','25px');
		$('#EM_ty_bar').css('width',modalWidth);
		
		//close button styles
		$('#EM_ty_close').html('<img src="http://smashbox.s3.amazonaws.com/site/files/emailModal/img/OptInClose.jpg" alt="close" />');
		$('#EM_ty_close').css('display','block');
		$('#EM_ty_close').css('background','#000');
		$('#EM_ty_close').css('height','25px');
		$('#EM_ty_close').css('width','25px');
		$('#EM_ty_close').css('float','right');
		$('#EM_ty_close').css('cursor','pointer');
		$('#EM_ty_close').css('z-index','9999');
	}
	
//detect logged in users
//if a user is logged into smashbox.com, a cookie will be set that will prevent them from viewing the modal window
	if(modal_firstName != null && modal_firstName != "") {
		$.cookie('SBEmailSignupOptOut', 'optOut:true;', {expires: 1, domain: DomainName});
	}
	
//modal window
//if no cookie is detected or the cookie is null, trigger this animation that will show the email modal
	if(($.cookie('SBEmailSignupOptOut') == null || $.cookie('SBEmailSignupOptOut') == '') &&  contentID.modalDisplay == true) {
		
		createModal();
		
		$('#EM_modal').delay(modalDelay*1000).animate({
			opacity: 1.0,
			top: modalTop,
			height: modalHeight
		}, 600);
		
		if($.cookie('SBEmailSignupView') != null && $.cookie('SBEmailSignupView') != '') {
			$.cookie('SBEmailSignupOptOut', 'optOut:true;', {expires: 14, domain: DomainName});
		}
		
		closeModal();
	}
	
//stop and restart auto close
//if a user clicks inside the modal window, the slide away animation is stopped. when focus is lost on the window, the animation timer restarts
	$('#EM_modal').mouseover(function() {
		modalAutoClose = false;
	});
	
	$('#EM_modal').mouseout(function () {
		if(modalAutoClose == false) {
			modalAutoClose = true;
			closeModal();
		}
	});
	
//close button
//if a user clicks on the 'x' button, the cookie will be set to prevent the user from viewing the modal again for 14 days and will trigger the animation to close the modal
	$('#EM_close').click(function() {
		//cookies
		$.cookie('SBEmailSignupOptOut', 'optOut:true;', {expires: 14, domain: DomainName});
		//stop auto close
		modalAutoClose = false;
		//animation
		$('#EM_modal').animate({
			top: hideHeight-10
		}, 600);
	});
	
//close modal animation function
	function closeModal() {
		setTimeout(function () { 
			if(modalAutoClose == true && inputOverride == false) {
				//cookies
				$.cookie('SBEmailSignupView', 'viewed:1', {expires: 1, domain: DomainName});
				//
				$('#EM_modal').animate({
					top: hideHeight-10
				}, 600);
			}
		},(interactionTriggerTime)*1000);
	}
	
//populate the inital hint value in the email field and clear the value when a user clicks in the email field
	$('#EM_emailinput').click(function() {
		if($('#EM_emailinput').val() == "Sign up and save $15!") {
			$('#EM_emailinput').val("");
			inputOverride = true;
		}
	});
	
//if the user clicks out of the email field without entering a value, re-populate the inital hint value
	$('#EM_emailinput').blur(function() {
		if($('#EM_emailinput').val() == null || $('#EM_emailinput').val() == "") {
			$('#EM_emailinput').val("Sign up and save $15!");
			inputOverride = false;
			closeModal();
		} else {
			
		}
	});
	
//submit button
//upon click, validation will run checking for a valid email address and will send an ajax request with the email address to the server
	$('#EM_submit').click(function() {
		if($('#EM_emailinput').attr('value').indexOf('@') != -1){
			if($('#EM_emailinput').attr('value').indexOf('.', $('#EM_emailinput').attr('value').indexOf('@')) != -1){
				//cookies
				$.cookie('SBEmailSignupOptOut', 'optOut:true;', {expires: 365, domain: DomainName});

				var emailAddress = $('#EM_emailinput').attr('value');
				
				createIframe(emailAddress);
				triggerTY();
				
				
			} else {
				alert('Your email address is not correctly formatted. Please format your email address and re-submit.');
				return false;
			}
		} else {
			alert('Your email address is not correctly formatted. Please format your email address and re-submit.');
			return false;
		}
	});
	
//trigger thank you modal function
//upon success of the ajax request, this function is triggered to fade out the email modal and display the thank you message modal
	function triggerTY() {
		createTYModal();
		$('#EM_modal').fadeOut();
		$('#EM_ty').delay(500).fadeIn();
		
		closeTYmodal();
		
		//thank you message close button
		//when a user clicks on the close button in the thank you modal, a cookie will be set to prevent viewing for 1 year amd will trigger the animation to close the window
		$('#EM_ty_close').click(function() {
			//$.cookie('SBEmailSignupOptOut', 'optOut:true;', {expires: 365, domain: DomainName});
			//animation
			$('#EM_ty').animate({
				top: hideHeight
			}, 600);
		});
	}
	
	function closeTYmodal() {
		setTimeout(function () { 
			if(tyModalAutoClose == true) {
				//alert(modalAutoClose);
				$('#EM_ty').animate({
					top: hideHeight
				}, 600);
			}
		},interactionTriggerTime*1000);
	}
});
