jQuery(document).ready(function() {

	// layer api for contactform
	main.initApi();

	// blended bg
	main.initColorBlend();

	// contact form
	main.initContactForm();

});


var main = {

	msgApi : '',

	initApi : function () {
		msgApi = jQuery("#msgDialog").overlay({
			mask: {
				color: '#fff',
				opacity: 0.9
			},

			closeOnClick: true
		});

		jQuery('#msgDialog a.close').live('click', function (e) {
			e.preventDefault();
			msgApi.overlay().close();
		});
	},

	initColorBlend : function (){
		var colors = [{param:"background-color", colorList: ["#5a2a52", "#c764bc"], duration: 5000}];

		jQuery("#mainWrapper").colorBlend(colors);
	},

	initContactForm : function () {
		// this opens the form
		jQuery('#btnContact').click(function(e){
			e.preventDefault();
			msgApi.overlay().load();
		});
			
		var contactForm = jQuery('#contactForm');

		// this handles submit and errors
		jQuery('#btnSubmit').click(function(e) {
			e.preventDefault();

			jQuery.ajax({
				type: "post",
				data: jQuery(contactForm).serialize(),
				dataType: 'json',
				url: jQuery(contactForm).attr('action'),
				timeout: 45000,
				error: function() {
					// console.log("Failed to submit - ");
				},
				success: function(r) {

					jQuery('*', contactForm).removeClass('error');

					if (r.success !== true) {
						jQuery.each(r.errors, function(e, v) {
							jQuery('*[name=' + e + '], label[for=' + e + ']', contactForm).addClass('error');
						});

					} else {
						if (r.send == true) {
							jQuery('#contactFormWrapper').fadeOut('slow', function(){
								jQuery('#contactFormWrapper').html(r.message);
								jQuery('#contactFormWrapper').fadeIn('slow', function(){
									setTimeout("msgApi.overlay().close()", 5000);
								});
							});
						}
					}
				}
			});
		});

	}

}

