var mailing_list = {
	incoming_links : {
		init : function() {
			this.refer_a_friend.init();
			this.profile_completion();
			this.thank_you();
		},
		refer_a_friend : {
			init : function() {			
				var _hash = window.location.hash;
				if (_hash.indexOf('#refer_a_friend=') !== -1) {
					var split_hash = _hash.substr(_hash.indexOf('=') + 1).replace('%40', '@').replace(/%7C/gi, '|').split('|');
					sessvars.referer_fname = split_hash[1];
					sessvars.referer_lname = split_hash[2];
					sessvars.referer_email = split_hash[0];
					$('<a href="#">temp</a>').optPopup('http://www.groomstars.com/index.jsp?content=email-refer', { 
						iframe: true,
						height: 250,
						width: 500
					}).click();
				}
			},
			form : function() {
				var session = window.parent.sessvars;
				$('input#referer_fname').val(session.referer_fname);
				$('input#referer_lname').val(session.referer_lname);
				$('input#referer_email').val(session.referer_email);
				$('button#submit_refer').live('click', function() {
					var container = $('div#popup_container', window.parent.document);
					var iframe = $('div#popup_container iframe', window.parent.document);
					container.css({
						height: 500,
						marginTop: -250
					});
					iframe.css({
						height: 500
					});
				});
			}
		},
		profile_completion : function() {
			var _hash = window.location.hash;
			if (_hash.indexOf('#profile_completion=') !== -1) {
				sessvars.email_address = _hash.split(/=/i)[1].replace('%40', '@');
				$('<a href="#">temp</a>').optPopup('http://www.groomstars.com/index.jsp?content=email-profile', { 
					iframe: true,
					height: 560,
					width: 500
				}).click();
			}
		},
		thank_you : function() {
			var _hash = window.location.hash;
			if (_hash.indexOf('#thank_you') !== -1) {
				$('<a href="#">temp</a>').optPopup('http://www.groomstars.com/email-thankyou', {
					iframe: true,
					height: 500,
					width: 500
				}).click();
			}
		}
	},
	pop_down : function() {
		var popdown = $('div#mailing_list_popdown'),
				close_button = $('a#close_button_popdown');
		setTimeout(function() {
			if ($.cookie('groomstars_mailing_list') !== 'true') {
				close_button.click(function() {
					popdown.animate({
						top: -250
					}, 800);
					return false;
				});
				$.cookie('groomstars_mailing_list', 'true', { expires: 60 });
				popdown.animate({
					top: 116
				}, 500);
			}
		}, 3000);
		$('#email_subscribe_popdown').keyup(function() {
			$('#email_subscribe').val($(this).val());
		});
		$('#submit_email_popdown').click(function() {
			$('#close_button_popdown').click();
			$('#submit_email').click();
		});
	},
	header : function() {
		mailing_list.incoming_links.init();
		var email_input = $('input#email_subscribe');
		email_input.focus(function() {
			if ($(this).val() === 'enter email address') {
				$(this).val('');
			}
		})
		$('.ui-widget-overlay').live('click', function() {
			$('#mailing_list_wrap').dialog('close');
		});
		$('button#submit_email').click(function() {
			sessvars.email_address = email_input.val();
			$('#mailing_list_wrap').dialog({
				modal: true,
				width: 500,
				height: 300,
				position: 'center',
				draggable: false
			});
			$('form#mailing_list_head').submit();
		});
//		this.profile_from_contact();
		this.pop_down();
	},
	profile_from_contact : function() {
		if (typeof(sessvars.joined_list) !== 'undefined') {
			$('input#email_subscribe').val(sessvars.email_address);
		}
		$('#submit_email').click();
	},
	profile : function() {
		var session = window.parent.sessvars;
		if (typeof(session.email_address) !== 'undefined') {
			$('input#email_address').val(session.email_address);
		}
		$('select#relationship_status').change(function() {
			var selected = $('select#relationship_status option:selected').text(),
					a_date = $('div#anniversary_date');
			if ((selected === 'Engaged' || selected === 'Married') && a_date.is(':hidden')) {
				a_date.slideDown();
			}
			else if ((selected !== 'Engaged' || selected !== 'Married') && a_date.is(':visible')) {
				a_date.slideUp();
				$('input#a_date_input').val('');
			}
		});
		$('input.date_picker').datepicker({
			changeMonth: true,
			changeYear: true,
			yearRange: '1910:2010'
		});
		//sessvars.$.clearMem();
	},
	validate : function() {
		var is_valid = true,
				isNewsletterSelected = true,
				errorString = '',
				x = document.OptinForm;
		if (x.field6.value == "") {
			errorString = errorString + "First Name\n";
			is_valid = false;
		}
		if (x.field7.value == "") {
			errorString = errorString + "Last Name\n";
			is_valid = false;
		}
		if (!$("input[@name='field9']:checked").val()) {
			errorString = errorString + "Gender\n";
			is_valid = false;
		}
		if (x.field14.value == "") {
			errorString = errorString + "Zip Code\n";
			is_valid = false;
		}
		if (!$("input[@name='field12']:checked").val()) {
			errorString = errorString + "Groomstars Blog\n";
			is_valid = false;
		}
		if (!$("input[@name='field13']:checked").val()) {
			errorString = errorString + "Sweepstakes\n";
			is_valid = false;
		}
		else {
			if (x.UEmail.value.indexOf("@") == -1 || x.UEmail.value.indexOf(".") == -1) {
				errorString = errorString + 'Invalid email address.';
				is_valid = false;
			}
		}
		if (is_valid == true && isNewsletterSelected == true) {
			return true;
		}
		else {
			if (isNewsletterSelected == false) {
				errorString = errorString + "----------------------\nPlease select at least one subscription";
			}
			alert("Following fields are required:\n\n" + errorString);
			return false;
		}
	}
};

$(function() {

});
