﻿// SpinSix
// S6_Framework
// @author	Nicholas Boll
// @copy	Spinsix 2010

var Quote = {
	init: function ()
	{
		// call the event bindings
		Quote.bind();
		
		// For non-ajax
		Wizard.init();
	},
	
	bind: function ()
	{
		// Services wizard form
		$('#btn_submit').bind('click', Quote.Events.clickSubmit);
		
		$('#quote_form').bind('submit', Quote.Events.submitForm);
		
		// live events for the wizard
		$('.not-listed').live('click', Quote.Events.clickVehicle);
		
		$('#check_other').live('click', Quote.Events.clickOther);
		
		$('form input[type=submit], form input[type=image]').live('click', Quote.Events.clickSubmitHidden);
	}
};

Quote.Events = {

	clickSubmitHidden: function (e)
	{
		// remove previous hidden entries
		$('.hidden-submit').remove();
		
		$(this).after('<input type="hidden" name="'+$(this).attr('name')+'" value="'+$(this).attr('value')+'" class="hidden-submit" />');
	},
	
	clickSubmit: function (e)
	{
		$('#quote_form').unbind('submit', Quote.Events.submitForm);
	},
	
	clickVehicle: function (e)
	{
		$.get($(this).attr('href'), function(responseText, statusText) {
			Lightbox.Events.ajaxifyFormSuccess(responseText, statusText);
		});
		
		e.preventDefault();
	},
	
	clickOther: function (e)
	{
		$('#text_other').toggleClass('hidden');
		$.fn.colorbox.resize();
	},
	
	// Services wizard
	submitForm: function (e)
	{
		$(this).ajaxSubmit({
			beforeSubmit: Quote.Events.showRequest,
			success: Quote.Events.showResponse
		});
		
		// prevent form submission
		e.preventDefault();
	},
	
	showRequest: function (formData, jqForm, options)
	{
		// Don't need to do anything
	},
	
	showResponse: function (responseText, statusText, xhr, $form)
	{
		var div = document.createElement('div');
		div.innerHTML = responseText;
		$content = $('#content', div);
		if ($content.length == 0)
		{
			$content = $(div);
		}
		
		if (!$('#juice', $content).length)
		{
			$.fn.colorbox({
				html: $content.html(),
				onComplete: Lightbox.init,
				scrolling: false
			});
		}
		else
		{
			$('#content').html($content.html());
			Quote.init();
		}
	}
};

// initialize the site on DOM ready
$(function(){

    Quote.init();
    
});