(function() {

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

	var init = function() {
		setupLocations();	
		setupCredits();
		setupSubscribe();
	};
	
	var setupLocations = function() {
		var opener = $('#locations-open');	
		var closer = $('#locations-close');	
		var locationsHolder = $('#locations-holder');	

		opener.click(function() {
			locationsHolder.animate({ height: 550}, 700)
		});

		closer.click(function() {
			locationsHolder.animate({ height: 0}, 700)
		});
	};

	var setupSubscribe = function() {
		var form = $("#signup-form");
		var textBox = form.find("input[type='text']");
		var button = form.find("input[type='submit']");

		button.click(function(e) { 
			e.preventDefault();

			textBox.css("border-color","#000");
			form.find('.subscribe-error').remove();

			form.submitAjax(function(data) {
				
				var isSucess = data.sucess;
				
				if(isSucess) {
					textBox.val('Subscribed ✓');
					textBox.css("color", "#090");
					textBox.click(function(e) {
						textBox.val("");
						textBox.css("color", "#000");
					});
				}
				else {
					textBox.css("border-color","#C00");
					var errMsg = $('<div class="subscribe-error" />');
					errMsg.html(data.response.Message);
					form.append(errMsg);
				}
			});
		});
	};

	var setupCredits = function() {
		var credits = $('#credits');
		var title = credits.attr("data-title");
		var originalText = credits.html();

		credits.hover(function() {
			credits.html(title);
		},function() {
			credits.html(originalText);
		});
		
	};

})();

// ADDONS *************


//Submit a form asynchronously with jQuery post
//Updated for jQuery 1.3.2 (attr selectors and disabled filter)
jQuery.fn.submitAjax = function(responseHandler) {
	var params = {};

	$(this).find("input[checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='submit'], option[selected], textarea")
	.not(':disabled')
	.each(function() { params[this.name || this.id || this.parentNode.name || this.parentNode.id] = this.value; });

	var url = $(this).attr("action");

	$.post(url, params, responseHandler);

	return this;
}

