$(document).ready(function(){
	register_form('contactForm');
	$('#contactusContainer').dialog({
		'modal': true, 
		'resizable': false,
		'width': '600px',
		'resizable': false,
		'autoOpen': false,
		'title': 'Contact us'
	});
	jQuery.each($('span.contact'), function(i, el){$(el).click(contactusShow);});
});

function E(id){
	var e = $('#'+id);
	if(e.length)
		return e;
	else
		return false;
}

function register_form(formId){
	formId = '#' + formId;
	 var options = { 
         success: function(responseText, statusText){
			 formCallback(responseText, statusText, formId);
		 },
		 beforeSubmit: function(){
			 cleanForm(formId);
		 },
         dataType:  'json'
    }; 
	$(formId).ajaxForm(options);
}

function cleanForm(formId){

	jQuery.each($(formId + ' .errorlist'), function(i, el){
		$(el).remove();
	});
}

function formCallback(responseText, statusText, formId){
	
	if (responseText['message']){
		$(formId).hide();
		$('#contactusContainer').dialog('close');
	}
	else{
		jQuery.each(responseText['errors'], function(key, val){
			var el_id = 'id_' + key;
			E(el_id).parent().prepend($(val));
		});
	}
}


function contactusShow(){
	$('#contactusContainer').dialog('open');
	$('#id_subject').focus();
}

// overlay.show();
// $('#contactusContainer').show();
// function contactusClose(){
// 	// overlay.hide();
// 	$('#contactusContainer').hide();
// }


// var overlay;
// var overlayClass = new DUI.Class({
// 	init: function(){

// 		$('body').append($('<div/>').attr("id", 'overlay'));
// 		this.div = $('#overlay');
// 		this.div.text(' ');
// 	},
// 	setSize:function(){
// 		this.div.height($(document).height());
// 	},
// 	show:function(){
// 		this.setSize();
// 		this.div.show();
// 	},
// 	hide:function(){
// 		this.div.hide();
// 	}
// });

// overlay = new overlayClass();
// $('#contactusClose').click(contactusClose);
