﻿// SpinSix
// LINCS site.lightbox.js
// @author	Nicholas Boll
// @copy	Spinsix 2010

// Site lightboxes
var Lightbox = {
	init: function()
	{
		// Add any events
		Lightbox.bind();
	},
	
	bind: function()
	{
		// Ajaxify the embedded form
		$('#cboxLoadedContent form').bind('submit', Lightbox.Events.ajaxifyForm);
	}
};

Lightbox.Events = {
	
	ajaxifyForm: function (e)
	{
		$(this).ajaxSubmit({
			success: Lightbox.Events.ajaxifyFormSuccess,
			error: function (xhr, ajaxOptions, thrownError) { alert(thrownError); },
			beforeSubmit: function () {
			}
		});
		
		e.preventDefault();
	},
	
	// The form has been submitted.  It should be checked for errors
	ajaxifyFormSuccess: function (responseText, statusText)
	{
		var div = document.createElement('div');
		div.innerHTML = responseText;
		$content = $('#content', div);
		if ($content.length == 0)
		{
			$content = $(div);
		}
		
		// check for validation errors or success confirmation
		if ($('.validation-summary-errors, .validation-summary-success', $content) && !$('#juice', $content).length)
		{
			$('#cboxLoadedContent').html($content.html());
			// re-initialize the lightbox
			$.fn.colorbox.resize();
			$.event.trigger('cbox_complete');
			Lightbox.init();
		}
		else
		{
			$.fn.colorbox.close();
			$('#content').html($content.html());
			Quote.init();
		}
	}
}