SFBLocal = function()
{
	
	/********************************************************
			Config
	********************************************************/
	
	var global;
	var currentModalWindow = null;
	var modalWindowOpen = false;
	var modalEndY = 130;
	var fadeTime = 300;
	var currentPopup = null;
	
	var pageOptions = {};
	var currentPageName;
	
	// SFBLocal Methods
	
	return {
		
		init: function()
		{
			
			global = this;
			
			this.popups.init();
			this.forms.init();
			this.modal.init();
			this.page.init();
			
		},
		
		/********************************************************
			Form Functions
		********************************************************/
		
		forms: function()
		{
			
			var ref;
			
			var months = [];
			months['january'] = 1;
			months['february'] = 2;
			months['march'] = 3;
			months['april'] = 4;
			months['may'] = 5;
			months['june'] = 6;
			months['july'] = 7;
			months['august'] = 8;
			months['september'] = 9;
			months['october'] = 10;
			months['november'] = 11;
			months['december'] = 12;
			
			var signupFormWrapper = '#signup_form_wrapper';
			var signUpFormThanks = '#signup_form_thanks';
			var tellAFriendWrapper = '#taf_form_wrapper';
			var tellAFriendThanks = '#taf_form_thanks_wrapper';
			var dobCurrentDropMenu = -1;
			
			// Forms Methods
			
			return {
				
				init: function()
				{
					
					ref = this;
					
					this.reset()
					
					// Create drop select menus
					
					$('.drop_select').dropSelect();
					
					// Init Form Validation
					
					this.initFormValidation()
					
					// Add Focus Fields
					
					this.addFocusEvent(signupFormWrapper);
					this.addFocusEvent(tellAFriendWrapper);
					
					// Hide Forms
					
					this.hideForm(signupFormWrapper);
					this.hideForm(signUpFormThanks);
					this.hideForm(tellAFriendWrapper);
					this.hideForm(tellAFriendThanks);
					
					// Add listeners for close buttons
					
					$('.form_close_x').bind("click", function(){global.modal.hide('#' + $(this).parent().parent().parent().parent().attr("id"))});
					$('.form_close_x_thanks').bind("click", function(){global.modal.hide('#' + $(this).parent().parent().attr("id"))});
					
				}, // End init
				
				hideForm: function(selector)
				{
					
					if($.browser.msie && $.browser.version < 7)
					{
						
						$(selector).hide();
						
					}
					else
					{
					
						$(selector).css("top", modalEndY);
						$(selector).css("left", "-10000px");
						$(selector).fadeTo(0, 0);
						
					}
					
				}, // End hideForm
				
				initFormValidation: function()
				{
					
					// Buzz Me
					
					$('#submit_btn').bind("click", function(){
		
						ref.resetFormErrors('#signup_form');
						
						$("#buzz_me_form").submit();
						
					});
															
					$("#buzz_me_form").validate({
						focusInvalid: false,
						onfocusout: false,
						onkeyup: false,
						rules:{
							firstname:{
								required: true,
								letterswithbasicpunc: true
							},
							lastname:{
								required: true,
								letterswithbasicpunc: true
							},
							email1:{
								required: true,
								email: true
							},
							email2:{
								required: true,
								email: true,
								equalTo: '#email1'
							},
							zip:{
								required: true,
								digits: true
							},
							b_check_it_a:{
								required: true
							},
							dob_month:{
								required: true
							},
							dob_day:{
								required: true
							},
							dob_year:{
								required: true
							},
							areacode:{
								digits: true,
								required: {
									depends: function(){
										var num1 = global.utils.trim($('#num1').attr("value"));
										var num2 = global.utils.trim($('#num2').attr("value"));
										if(num1 != '' || num2 != '')
										{
											return true;
										}
									}
								},
								minlength: 3,
								maxlength: 3
							},
							num1:{
								digits: true,
								required: {
									depends: function(){
										var num1 = global.utils.trim($('#areacode').attr("value"));
										var num2 = global.utils.trim($('#num2').attr("value"));
										if(num1 != '' || num2 != '')
										{
											return true;
										}
									}
								},
								minlength: 3,
								maxlength: 3
							},
							num2:{
								digits: true,
								required: {
									depends: function(){
										var num1 = global.utils.trim($('#num1').attr("value"));
										var num2 = global.utils.trim($('#areacode').attr("value"));
										if(num1 != '' || num2 != '')
										{
											return true;
										}
									}
								},
								minlength: 4,
								maxlength: 4
							}
						},
						messages:{
							firstname:{
								required: "Please provide your first name",
								letterswithbasicpunc: "Please make sure your first name is made up of only the following characters: [A-Z] [a-z] [ . , ' - ]"
							},
							lastname:{
								required: "Please provide your last name",
								letterswithbasicpunc: "Please make sure your first name is made up of only the following characters: [A-Z] [a-z] [ . , ' - ]"
							},
							email1:{
								required: "Please enter a valid email address",
								email: "Please enter a valid email address"
							},
							email2:{
								required: "Please enter a valid email address",
								email: "Please enter a valid email address",
								equalTo: "Please make sure your emails match"
							},
							zip:{
								digits: "Please make sure your ZIP code is made up of only the following characters:  [0-9]",
								required: "Please provide your ZIP Code"
							},
							b_check_it_a:{
								required: "Please enter the security code"
							},
							dob_month:{
								required: "Please enter your date of birth month"
							},
							dob_day:{
								required: "Please enter your date of birth day"
							},
							dob_year:{
								required: "Please enter your date of birth year"
							},
							areacode:{
								required: "Please enter a valid Mobile Number",
								digits: "Please make sure your Mobile Number is made up of only the following characters: [0-9]",
								minlength: "Please enter a valid Mobile Number",
								maxlength: "Please enter a valid Mobile Number"
							},
							num1:{
								required: "Please enter a valid Mobile Number",
								digits: "Please make sure your Mobile Number is made up of only the following characters: [0-9]",
								minlength: "Please enter a valid Mobile Number",
								maxlength: "Please enter a valid Mobile Number"
							},
							num2:{
								required: "Please enter a valid Mobile Number",
								digits: "Please make sure your Mobile Number is made up of only the following characters: [0-9]",
								minlength: "Please enter a valid Mobile Number",
								maxlength: "Please enter a valid Mobile Number"
							}
						},
						highlight: function(element){
							ref.showError(element);
						},
						errorPlacement: function()
						{
							
						},
						showErrors: function(errorMap, errorList) {
							
							if(errorList.length > 0)
							{
								ref.showErrorBox('#popup_error', errorList[0].message);
								this.defaultShowErrors();
							}
						},
						submitHandler: function(form)
						{
							
							var month = document.getElementById('drop_select_dob_month');
							var monthVal = month.options[month.selectedIndex].value;
							
							var day = document.getElementById('drop_select_dob_day')
							var dayVal = day.options[day.selectedIndex].value;
							
							var year = document.getElementById('drop_select_dob_year')
							var yearVal = year.options[year.selectedIndex].value;
							
							if(!ref.isOldEnough(monthVal, dayVal, yearVal, 18))
							{
								ref.showErrorBox('#popup_error', 'You must be of a certain age to sign up for Fresh News and Offers from Subway<sup>&reg;</sup> Restaurants.');
								return;
							}
							
							$('#signup_submit_loading').show();
							form.submit();
							
						}
					});
					
					// Tell A Friend
					
					$('#taf_submit_btn_container').bind("click", function(){
		
						ref.resetFormErrors('#taf_form');
					
						$("#tell_a_friend_form").submit();
						
					});
					
					$("#tell_a_friend_form").validate({
						focusInvalid: false,
						onfocusout: false,
						onkeyup: false,
						rules:{
							firstname:{
								required: true,
								letterswithbasicpunc: true
							},
							friend1:{
								required: true,
								letterswithbasicpunc: true
							},
							email1:{
								required: true,
								email: true
							},
							friend2:{
								required:{
									depends: function(){
										if(global.utils.trim($('#taf_email2').attr("value")) != "")
										{
											return true;
										}
									}
								},
								letterswithbasicpunc: true
							},
							email2:{
								required:{
									depends: function(){
										if(global.utils.trim($('#taf_friend2').attr("value")) != "")
										{
											return true;
										}
									}
								},
								email: true
							},
							friend3:{
								required: {
									depends: function(){
										if(global.utils.trim($('#taf_email3').attr("value")) != "")
										{
											return true;
										}
									}
								},
								letterswithbasicpunc: true
							},
							email3:{
								required: {
									depends: function(){
										if(global.utils.trim($('#taf_friend3').attr("value")) != "")
										{
											return true;
										}
									}
								},
								email: true
							},
							friend4:{
								required: {
									depends: function(){
										if(global.utils.trim($('#taf_email4').attr("value")) != "")
										{
											return true;
										}
									}
								},
								letterswithbasicpunc: true
							},
							email4:{
								required: {
									depends: function(){
										if(global.utils.trim($('#taf_friend4').attr("value")) != "")
										{
											return true;
										}
									}
								},
								email: true
							}
						},
						messages:{
							firstname:{
								required: "Please provide your first name",
								letterswithbasicpunc: "Please make sure your First name is made up of only the following characters: [A-Z] [a-z] [ . , ' - ]"
							},
							friend1:{
								required: "Please provide your friends first name",
								letterswithbasicpunc: "Please make sure your friends first name is made up of only the following characters: [A-Z] [a-z] [ . , ' - ]"
							},
							email1:{
								required: "Please enter a valid email address",
								email: "Please enter a valid email address"
							},
							friend2:{
								required:"Please provide your friends first name",
								letterswithbasicpunc: "Please make sure your friends first name is made up of only the following characters: [A-Z] [a-z] [ . , ' - ]"
							},
							email2:{
								required: "Please enter a valid email address",
								email: "Please enter a valid email address"
							},
							friend3:{
								required:"Please provide your friends first name",
								letterswithbasicpunc: "Please make sure your friends first name is made up of only the following characters: [A-Z] [a-z] [ . , ' - ]"
							},
							email3:{
								required: "Please enter a valid email address",
								email: "Please enter a valid email address"
							},
							friend4:{
								required:"Please provide your friends first name",
								letterswithbasicpunc: "Please make sure your friends first name is made up of only the following characters: [A-Z] [a-z] [ . , ' - ]"
							},
							email4:{
								required: "Please enter a valid email address",
								email: "Please enter a valid email address"
							}
						},
						highlight: function(element){
							ref.showError(element);
						},
						errorPlacement: function()
						{
							
						},
						showErrors: function(errorMap, errorList){
							
							if(errorList.length > 0)
							{
								ref.showErrorBox('#taf_popup_error', errorList[0].message);
								this.defaultShowErrors();
							}
						},
						submitHandler: function(form)
						{
							
							form.submit();
							$('#taf_submit_loading').show();
							
						}
					})
					
				}, // End initFormValidation
				
				addFocusEvent: function(selector)
				{
					
					$(selector).find(':input[type="text"]').bind("focus", function(){ref.focusField(this)});
					
					this.addBlurEvent(selector);
					
				}, // End addFocusEvent
				
				addBlurEvent: function(selector)
				{
					
					$(selector).find(':input[type="text"]').bind("blur", function(){ref.blurField(this)});
					
				}, // End addBlurEvent
				
				focusField: function(element)
				{
					
					$(element).css("background-color", "#ff0");
					$(element).css("border", "1px solid #ccc");
					
				}, // End focusField
				
				blurField: function(element)
				{
					
					$(element).css("background-color", "#fff");
					$(element).css("border", "1px solid #ccc");
				
				}, // End blurField
				
				showError: function(element)
				{
					
					$(element).css("background-color", "#fbe4e5");
					$(element).css("border", "1px solid #f00");
					
				}, // End showError
				
				resetFormErrors: function(selector)
				{
					
					$(selector).find(':input[type="text"]').each(function(){ref.blurField($(this))});
					
				}, // End resetFormErrors
				
				onSignUpFormSubmit: function(data)
				{
					
					$('#signup_submit_loading').hide();
					
					if(data.error == 0)
					{
						
						// Track omniture form success
						omnitureOnNationalFormSuccess('Forms: Get News and Offers');
						
						// Show thank you message
						global.modal.crossFade(signUpFormThanks);
						
					}
					
					else
					{
						// Show errors	
						ref.onFormSubmitErrors(data);
					}
					
				}, // End onSignUpFormComplete
				
				onTellAFriendFormSubmit: function(data)
				{
					
					$('#taf_submit_loading').hide();
					
					if(data.error == 0)
					{
						
						// Track omniture form success
						omnitureOnNationalFormSuccess('Forms: Tell a Friend');
						
						// Show thank you message
						global.modal.crossFade(tellAFriendThanks);
						
					}
					
					else
					{
						// Show errors	
						ref.onFormSubmitErrors(data);
					}
					
				}, // End onTellAFriendFormSubmit
				
				showErrorBox: function(selector, msg)
				{
					
					$(selector).find('.popup_error_message').html(msg);
					
					global.popups.showPopup(selector);
					
				}, // End showErrorBox
				
				onFormSubmitErrors: function(data)
				{
					
					var errors = data.errorMsg.split(",")
					var errorMsg = ""
					
					for(var i = 0; i < errors.length; i++)
					{
						errorMsg += errors[i] + "<br/>"
					}
					
					ref.showErrorBox('#popup_error', errorMsg);
					
				}, // End showErrorBox
				
				isOldEnough: function(month, day, year, age)
				{
					
					if(month == 0 || day == 0 || year == 0)
					{
						return false;
					}
					
					var d = new Date();
					var c_year = d.getFullYear();
					var c_month = d.getMonth() + 1;
					var c_day = d.getDate();
					var min_year = c_year - age;
					
					var alertMsg = "Date: " + d + "\n";
					alertMsg += "c_year: " + c_year + "\n";
					alertMsg += "c_month: " + c_month + "\n";
					alertMsg += "c_day: " + c_day + "\n";
					alertMsg += "min_year: " + min_year + "\n";
					alertMsg += "Month: " + month + "\n";
					alertMsg += "Day: " + day + "\n";
					alertMsg += "Year: " + year + "\n";
					
					if(year > min_year)
					{
						return false;	
					}
					
					if(year == min_year)
					{
						
						if(month > c_month)
						{
							return false;
						}
						
						if(month == c_month)
						{
							
							if(day > c_day)
							{
								return false;
							}
							
						}
					}
					
					return true;
					
				},
				
				reset: function(selector)
				{
					
					if(selector)
					{
						
						$(selector).find(':input[type="text"]').attr("value", "");
						$(selector).find(':input[type="text"]').each(function(){ref.blurField(this);});
						
						$('.drop_select_sub_ul').css('top', '0px');
						$('.jScrollPaneDrag').css('top', '0px');
						
						$('.drop_select_li_first_on').attr('class', 'drop_select_li_first');
						$('.jScrollPaneContainer_on').slideUp('fast');
						
						$('#dob_month').find('.drop_select_li_first > a').html("Month").attr('rel', "{'value':'','label':'Month','index':'0'}");
						$('#dob_day').find('.drop_select_li_first > a').html("Day").attr('rel', "{'value':'','label':'Day','index':'0'}");
						$('#dob_year').find('.drop_select_li_first > a').html("Year").attr('rel', "{'value':'','label':'Year','index':'0'}");
						
					}
					
					$('select').find('option').each(function(i){
						if(i == 0)
						{
							$(this).attr('selected', 'selected');
						}
						else
						{
							$(this).attr('selected', '');
						}
						
					});
					
					if(currentPopup)
					{
						global.popups.hidePopup(currentPopup);
					}
					
					$('#taf_submit_loading').hide();
					$('#signup_submit_loading').hide();
					
				} // End reset
				
			} // End Forms return
			
		} (), // End Forms
		
		/********************************************************
			Modal Window Functions
		********************************************************/
		
		modal: function()
		{
			
			var ref;
			var overlayHTML = '<div id="modal_overlay">&nbsp;</div>';
			var overlaySelector = '#modal_overlay';
			
			return {
				
				init: function()
				{
					
					ref = this;
					
					// Overlay
					
					$('body').append(overlayHTML);
					$(overlaySelector).stop().fadeTo(0, 0);
					$(overlaySelector).css("left", "-10000px");
					
					if($.browser.msie && $.browser.version < 7)
					{
						$(overlaySelector).css("position", "absolute");
					}
					
				}, // End init
				
				show: function(selector)
				{
					
					if(modalWindowOpen)
					{
						return;
					}
					
					modalWindowOpen = true;
					
					currentPageName = s.pageName;
					
					$(overlaySelector).css("left", "0px");
					
					$(selector).css("left", "50%");
					$(selector).css("top", modalEndY);
					
					$(overlaySelector).stop().fadeTo(fadeTime, 0.6, function(){												 
						ref.onOverlayFadeInComplete(selector);
					});
					
					$(overlaySelector).bind("click", function(){ref.hide(selector);});
					
					currentModalWindow = selector;
					
				}, // End show
				
				crossFade: function(selector)
				{
					
					if($.browser.msie && $.browser.version < 7)
					{
						$(currentModalWindow).hide();
					}
					else
					{
						$(currentModalWindow).stop().fadeTo(0, 0);
					}
					
					ref.keepHidden();
					
					$(selector).css("left", "50%");
					$(selector).css("top", modalEndY);
					
					this.onOverlayFadeInComplete(selector);
					
				}, // End crossFade
				
				hide: function(selector)
				{
					
					global.forms.reset(selector)
					
					if($.browser.msie && $.browser.version < 7)
					{
						$(selector).hide();
						ref.onFormFadeOutComplete();
					}
					else
					{
						$(selector).stop().fadeTo(fadeTime, 0, function(){ref.onFormFadeOutComplete();});
					}
					
					$(overlaySelector).unbind("click");
					
					// Track new pageview, back to regular content
					trackPageView(currentPageName);
					
				}, // End hide
				
				onOverlayFadeInComplete: function(selector)
				{
					
					if($.browser.msie && $.browser.version < 7)
					{
						$(selector).show();
					}
					else
					{
						$(selector).stop().fadeTo(fadeTime, 1);
					}
					
					currentModalWindow = selector;
					
				}, // End onOverlayFadeInComplete
				
				onFormFadeOutComplete: function()
				{
					
					global.modal.keepHidden();
					
					$(overlaySelector).stop().fadeTo(fadeTime, 0, ref.removeOverlay);
					
				}, // End onFormFadeOutComplete
				
				keepHidden: function()
				{
					
					$(currentModalWindow).css("left", "-10000px");
					
				}, // End keepHidden
				
				removeOverlay: function()
				{
					
					modalWindowOpen = false;
					
					$(overlaySelector).css("left", "-10000px");
					
				} // End removeOverlay
				
			} // End Modal return
			
		} (), // End Modal
		
		popups: function()
		{
			
			var ref;
			
			return {
				
				init: function()
				{
					
					ref = this;
					
					/* Listener for popups, make the whole thing clickable to close */
					
					$('.popup_container').bind("click", function(){
						ref.hidePopup('#' + $(this).attr("id"));
					});
					
					/* DOB Popup */
					
					$('#dob_open').bind("click", function(){ref.showPopup('#popup_dob')});
					
					/* Mobile Popup*/
					
					$('#mobile_open').bind("click", function(){ref.showPopup('#popup_mobile')});
					
					/* Cåptcha Popup */
					
					$('#captcha_open').bind("click", function(){ref.showPopup('#popup_captcha')});
					
					/* Hide popups */
					
					$('.popup_container').hide();
					
				}, // End init
				
				hidePopup: function(selector)
				{
					
					if(selector == null)
					{
						return;
					}
					
					if($.browser.msie)
					{
						$(selector).hide();
					}
					else
					{
						$(selector).fadeOut('fast');
					}
					
					currentPopup = null;
					
				}, // End hidePopup
				
				showPopup: function(selector)
				{
					
					if(currentPopup) this.hidePopup(currentPopup);
					
					var marginLeft = -($(selector).width() * 0.5); 
					var marginTop = -($(selector).height() * 0.5);
					
					$(selector).css("left", "50%");
					$(selector).css("top", "50%");
					$(selector).css("margin-left", marginLeft);
					$(selector).css("margin-top", marginTop);
					
					if($.browser.msie)
					{
						$(selector).show();
					}
					else
					{
						$(selector).fadeIn('fast');
					}
					
					currentPopup = selector;
					
				} // End showPopup
				
			} // End Popups Return
			
		} (), // End Popups
		
		utils: function()
		{
			
			return {
				
				confirmationAndTarget: function(page, target)
				{
					
					var response = confirm("You are now leaving the SUBWAY"+ unescape("%AE")+" restaurants website(s)."+'\n\n'+"The policies, including the privacy policy, on the site(s) you are going to may vary from SUBWAY"+ unescape("%AE")+" restaurants policies. SUBWAY"+ unescape("%AE")+" restaurants is not responsible for the policies and practices of other companies.");
					
					if (response) OpenWin = this.open(page, target);
					
				}, // end confirmationAndTarget
				
				trim: function(str)
				{
					
					return $.trim(str);
					
				}, // End Trim
				
				getSubDomain: function()
				{
					
					var subDomain = "";
					var url = document.location.toString().toLowerCase();
					var startIndex = url.indexOf("/") + 2;
					var endIndex = url.indexOf(".");
					
					subDomain = url.substr(startIndex, endIndex - startIndex);
					
					return subDomain;
					
				} // End getSubDomain
				
			} // End utils return
			
		} (), // End utils
		
		page: function()
		{
			
			var ref;
			var COOKIE_NAME = "local";
			var cookieOptions = {expires: 30, path: "/", domain: "subwayfreshbuzz.com"};
			
			return {
				
				init: function()
				{
					
					ref = this;
					
					// Set the cookie
					
					/*
					var subdomain = global.utils.getSubDomain()
					var cookieValue = subdomain;
					cookieValue += "|true";
					
					jQuery.cookie(COOKIE_NAME, null, {});
					jQuery.cookie(COOKIE_NAME, cookieValue, cookieOptions);
					*/
					
					// Update page options
					
					var subdomain = global.utils.getSubDomain()
					
					this.setOptions(subdomain);
					
					$("#legalBtn").click(function(){
						
						var css = $("#legal").css("display")
						if(css == 'block'){
							$(this).text("View Legal")
						}
						else{
							$(this).text("Hide Legal")
						}
						
						$("#legal").slideToggle();
						
					});
					
				},
				
				setOptions: function(localID)
				{
					
					$.ajax({
						type: "GET",
						url: "/common/xml/config.xml",
						dataType: "xml",
						success: function(xml) {
							$(xml).find('localmarket').each(function(){
								
								if($(this).find("subdomain").text().toLowerCase() == localID.toLowerCase())
								{
									
									pageOptions.name = $(this).find("name").text();
									
									ref.update(pageOptions);
									
									var cookieValue = localID;
									cookieValue += "|true";
									cookieValue += "|" + $(this).find("name").text();
									
									jQuery.cookie(COOKIE_NAME, null, {});
									jQuery.cookie(COOKIE_NAME, cookieValue, cookieOptions);
									
								}
								
							});
						}
					});
					
				}, // End setPageOptions
				
				update: function(pageOptions)
				{
					
					// Header
					var headerHTML = "Welcome to your " + pageOptions.name + " SUBWAY<sup>&reg;</sup> ";
					$('#welcome').html(headerHTML);
					
				} // End updatePage
				
			} // End page return
			
		} () // End page 
		
	} // End SFBLocal return
	
}() // End SFBLocal


function SFBLocalShowPopupFromFlash(selector, omnitureID)
{
	
	SFBLocal.modal.show(selector);
	
	omnitureOnNationalFormLoad(omnitureID);
	
}

var sfbMain = {
	
	init: function()
	{
		this.createStoreLocator();
	},
	
	showPopupOverlay: function(target)
	{
		jQuery.facybox({ div: target });
	},
	
	showStoreLocator: function()
	{
		
		this.showPopupOverlay('#storeLocatorPopup');	
		$('#facybox').find('#storeLocatorFrame').attr('src', 'http://www.subway.com/applications/locator/kidsafe/index.aspx');
		
	},
	
	createStoreLocator: function()
	{
		
		var html = '<div id="storeLocatorPopup"><iframe id="storeLocatorFrame" src="#" width="550px" height="400px" frameborder="0" style="width: 550px; height: 430px; border: 0px;"></iframe></div>';
		var div = $(html).css('display', 'none');
		$('body').append(div);
		
	}
	
}

$(document).ready(function(){
	
	SFBLocal.init();
	sfbMain.init();
	
});
