// Wordpress' themes directory
var base_url = 'http://www.east180.com/wp-content/themes/east180'

$(function() {
	// Google Page Tracking
	var pageTracker = _gat._getTracker("UA-132767-17");
	pageTracker._trackPageview();

	// Appeding a caption under each img tag
	$('img').each(function() {
		$(this).after('<p class="caption">&bull; ' + $(this).attr('alt') + ' &bull;</p>')
	})
	
	// Add the container for our Flash cell
	$('#content').before('<div id="photo_cell"></div>')

	// Load in our Flash cell
	$('#photo_cell').flash({
		src:	'http://east180.com/wp-content/themes/east180/flash/photo.swf',
		width:	298,
		height:	319,
		wmode:	'transparent'
	})
	
	// This is just a nit-picky: extend the sidebar ul to the bottom of the .entry div so the red line along the left of the entry continues to the bottom	
	$('#sidebar ul').css({ height: $('.entry:first').height() })

	// Ajaxy-submission goodness
	$('#email_club').submit(function() {
		var values = fetchVars($(this))	
		addEmail(values); // Do stuff.

		return false; // Just don't submit the form normally
	})
	
	// Select all links in object with gallery ID
	$('#gallery a').lightBox();

})


// Takes a form and breaks its input tags down to key value pairs and returns them as an object
function fetchVars(form) {
	var variables = {};
	
	// For each input element in the form, create an entry with the element's name in our variables element, and attach its value
	form.find(':input').each(function() {
		variables[$(this).attr('name')] = $(this).val()
	})

	return variables;
}

// On form submit: prep the throbber object, make the request, hide the form, and show the throbber
function addEmail(form_values) {
	throbber.container = $('#email_club_container')
	$('#email_club_container').fadeOut('normal', function() {throbber.on()})

	// Post request to our inteface script. For our post vars, use fetchVars to parse the form's input itemns into an object with the form's current values
	// Come good or ill, we call up diplayReply to parse the server's response and display the relevent info
	$.ajax({
		type:		"POST",
		url:		base_url + "/includes/interface.php",
		data:		form_values,
		success:	function(data)	{ throbber.off(); displayReply(data) },
		error: 		function()		{ throbber.off(); displayReply() }
	})
}

// Throbber object, contains references to the container it's attached to, its HTML DNA, and on/off functions
var throbber = {
	container: '',
	throbber_code: "<div id='throbber' style='background:transparent url(" + base_url + "/images/ajax-loader.gif) no-repeat center center; height: 45px; display:none'></div>",
	on: function() {
		this.container.after(this.throbber_code)
		$('#throbber').slideDown('normal')
	},
	off: function() {
		$('#throbber').slideUp('normal', function() { $(this).remove() })
	}
}

// All we really need from the server response is the response type; this form parses that out from XML input
function parseReply(data) {
	return $(data).find('response:first action').text()
}

// Take the server response, check the type of response, and display a relevant message, hiding the form element if we have a terminal status (hard error or success)
function displayReply(data) {
	// Because I'm a lazy typist
	var container = $('#email_club_container') 
	var h2 = $('#email_club_container h3')
	var legend = $('#email_club_container p.legend')
	var form = $('#email_club_container form')
	var response_type;


	// If we have an XML response, the request worked; parse it and grab the reply type
	if (data) {
		response_type = parseReply(data)
	// Otherwise the request failed; let the switch below default out
	} else {
		response_type = 'error'
	} 

	// Kill the form unless we need to resumbit
	if(response_type != 'retry') {
		form.remove() 
		$('#footer .legend').css({width: '90%'})
	}

	// Print out our response message based on our reply from the server
	switch (response_type) {
		case 'success':
			h2.text('Thank you')
			legend.text("Thank you for subscribing! We've added you to our email club, so now you'll be the first to hear about upcoming events and promotions.")
		break
		case 'retry':
			h2.text('Please try again')
			legend.text("We couldn't add to our email club as your email address appears invalid. Please double-check your email address and try again.")
		break
		case 'error':
			h2.text('Error')
			legend.html("We couldn't add to our email club. Please contact the <a href='mailto:webmaster@ontheedgedesign.com'>Webmaster</a> who will be able to add you to the list.")
		break
	}

	// Show the response
	container.fadeIn('normal')
}
