﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

// namespace atlas
var atlas = atlas || {};

atlas.JoinNotificationList = {
	// set up lightbox
	initialize: function() {
		$('#joinNotificationListLightbox').fancybox({
			'overlayShow': true,
			'autoDimensions': true,
			'scrolling': 'no'
		});

		// create "OK" event handlers
		$(function() {
			$('#txtEmail').searchbox('Your Email Address', 'colorGray', atlas.JoinNotificationList.validateEmail);
		});

		$('#joinNotificationListSubmit').click(function() {
			atlas.JoinNotificationList.validateEmail();
			return false;
		});

		$('#joinLists').attr('disabled', true);
		$('#joinLists').fadeTo('fast', 0.5);
		$('#joinLists').css('cursor', 'default');

		$('#joinLists').click(function() {
			$('#joinForm').hide();
			$('#joinMessage').html('Processing your submission...');
			$('#joinMessage').show();
			atlas.JoinNotificationList.subscribe();
			return false;
		});
	},

	subscribe: function() {

		var inputs = $('input ', $('.notificationLists'));
		for (var i = 0; i < inputs.length; i++) {
			var input = inputs.get(i);
			if (input.checked) {
				var parms = JSON.stringify({
					'emailAddress': $('#txtEmail').val(),
					'id': input.value
				});

				$.ajax({
					type: "POST",
					url: "/Services/MailingList.asmx/Subscribe",
					data: parms,
					dataType: "json",
					contentType: "application/json; charset=utf-8",
					success: function(result) {
						$('#joinMessage').html(result.d);
						$('#joinMessage').show();
						$('#divEmailError').hide();
						$('#divEmailIntro').html(result.d);
					}
				});
			}
		}
	},

	renderLists: function(lists) {

		$('.notificationLists').empty();
		jQuery.map(lists, function(i) {
			var input = $('<input value="' + i[0] + '" id="list' + i[0] + '" type="checkbox" />');
			var label = $('<label for="list' + i[0] + '">' + i[1] + '</label>');
			var theDiv = $('<div></div>');

			theDiv.append(input);
			theDiv.append(label);
			$('.notificationLists').append(theDiv);
		});

		$('input', $('.notificationLists')).change(function(i) {

			if ($('input:checked', $('.notificationLists')).length > 0) {
				$('#joinLists').attr('disabled', false);
				$('#joinLists').fadeTo('fast', 1);
				$('#joinLists').css('cursor', 'pointer');
			}
			else {
				$('#joinLists').attr('disabled', true);
				$('#joinLists').fadeTo('fast', 0.5);
				$('#joinLists').css('cursor', 'default');
			}
		});
	},

	// validate email before showing lightbox
	validateEmail: function() {

		var theEmail = $('#txtEmail').val();
		if (velir.atlas.isValidEmail(theEmail)) {

			$('#joinForm').show();
			$('#joinMessage').hide();
			$('#joinNotificationListSubmit').hide();
			$('#loadMapSpinner').show();

			$.ajax({
				type: "POST",
				url: "/Services/MailingList.asmx/GetMailingLists",
				dataType: "json",
				contentType: "application/json; charset=utf-8",
				success: function(result) {
					atlas.JoinNotificationList.renderLists(result.d);
					$('#joinNotificationListLightbox').click();
					$('#joinNotificationListSubmit').show();
					$('#loadMapSpinner').hide();
				}
			});
		}
		else {
			$('#divEmailError').show();
		}
	}
}

$(document).ready(function() {
	atlas.JoinNotificationList.initialize();
});
