sfb = function(){
	
	// Config 
	var global;
	var currentModalWindow = null;
	var modalWindowOpen = false;
	var modalEndY = 130;
	var fadeTime = 500;
	var currentPopup = null;
	var pageOptions = {};
	var currentPageName;
	
	// SFBLocal Methods
	return{
		
		init: function(){
			global = this;
			this.popups.init();
			this.forms.init();
		},
		
		forms: function(){
			var ref;
			var signupFormWrapper = '#signup_form_wrapper';
			var signUpFormThanks = '#signup_form_thanks';
			var tellAFriendWrapper = '#taf_form_wrapper';
			var tellAFriendThanks = '#taf_form_thanks_wrapper';
			// Forms Methods
			return{
				
				init: function(){
					ref = this;
					
					// Init Form Validation
					this.initFormValidation()
					
					// Add Focus Fields
					this.addFocusEvent(signupFormWrapper);
					this.addFocusEvent(tellAFriendWrapper);
					
					$('#taf_submit_loading').hide();
					$('#signup_submit_loading').hide();
					
				}, // End init
				
				initFormValidation: function(){
					
					// Buzz Me
					$('#submit_btn').bind("click", function(){
						ref.resetFormErrors('#signup_form');
						$("#buzz_me_form").submit();
					});
					
					$('#areacode').keyup(function(event){
						global.utils.autoTab(event,$(this).attr('id'), 'num1', 3)
					});
					
					$('#num1').keyup(function(event){
						global.utils.autoTab(event,$(this).attr('id'), 'num2', 3)
					});
					
					$("#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('dob_month');
							var monthVal = month.options[month.selectedIndex].value;
							var day = document.getElementById('dob_day')
							var dayVal = day.options[day.selectedIndex].value;
							var year = document.getElementById('dob_year')
							var yearVal = year.options[year.selectedIndex].value;
							if(!ref.isOldEnough(monthVal, dayVal, yearVal, 13)){
								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
				showErrorBox: function(selector, msg){
					$(selector).find('.popup_error_message').html(msg);
					global.popups.showPopup(selector);
				}, // End showErrorBox
				onFormSubmitErrors: function(formErrors){
					var errorData = eval('(' + formErrors + ')');
					for(var i = 0; i < errorData.errors.length; i++){
						this.showError('#' + errorData.errors[i].field);
					}
				}, // 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);});
					}
					if(currentPopup) global.popups.hidePopup(currentPopup);
					$('#taf_submit_loading').hide();
					$('#signup_submit_loading').hide();
				} // End reset
			} // End Forms return
		} (), // End Forms
		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
				autoTab: function autoTab(event, field, to, len){
					if(event.keyCode == 9) return; // Return if tab key was pressed
					f1 = document.getElementById(field)
					f2 = document.getElementById(to)
					if(f1.value.length >= len) f2.focus();
				}, // End autoTab
				submitZipCodeFromFlash: function(zipCode){
					$("#zipname").attr('value', zipCode);
					$("#zipform").submit();
				}
			} // End utils return
		} () // End utils
	} // End sfb return
}() // End sfb

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(){
	sfbMain.init();
});