$(document).ready(function(){  
	//LOADING POPUP  
	//Click the button event!  
	$("#media_form_link").click(function(){  
		//centering with css  
		center_media_popup();  
		//load popup  
		load_media_popup();
		reset_media_popup();
		reset_media_values();
		// return false;
	});

	//CLOSING POPUP  
	//Click the x event!  
	$("#media_form_close").click(function(){  
		disable_media_popup();
	});  
	
	$("#mediaThankyouclose").click(function(){  
		disable_media_thankyou_popup();
	});
	
	//Click out event!  
	$("#background_media_form").click(function(){  
		disable_media_popup();
		disable_media_thankyou_popup();
	});  
	//Press Escape event!  
	$(document).keypress(function(e){  
		if(e.keyCode==27 && popupStatus==1){  
			disable_media_popup();  
		}  
	});
	
	// hijack the form submit
	$('#id_submit_media').bind('click',function(){
		reset_media_popup();
		$.post('/enquiry_form',$('#media_form form').serialize(),function(data){

			var result = eval(data);
			
			// if the post was a success then display thank you popover
			if(result.success){
				$("#media_form").fadeOut("slow");
				media_form_status = 0;
				center_media_thankyou_popup();
				load_media_thankyou_popup();
			}
			else {
				jQuery.each(result.errors, function(i, val) {
					if (val.length) {
						$('#media_form #id_' + i).css('background-color','#ff617e');
            			$('#id_' + i).after(val);
					};					
				});
			}
			// if not mark incorrect fields on the form
			},'json');

			return false;
		});
});


/*
============================

MEDIA POPUP

============================
*/

function reset_media_values(){
	$('#media_form input:text').each(function(index,elm){
		$(elm).val('');
	});
	
	$('#media_form input:checkbox').each(function(index,elm){
		$(elm).attr('checked', false);
	});
	$('#media_form textarea').val('');
}

function reset_media_popup(){
	$('#media_form input:text').each(function(index,elm){
		$(elm).css('background-color','#ffffff');
	});
	$('#media_form textarea').css('background-color','#ffffff');
	$('ul.errorlist').remove();
}

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var media_form_status = 0;

//loading popup with jQuery magic!  
function load_media_popup(){  
	//loads popup only if it is disabled  
	if(media_form_status==0){  
		$("#background_media_form").css({  
			"opacity": "0.7"  
		});  
		$("#background_media_form").fadeIn("slow");  
		$("#media_form").fadeIn("slow");  
		media_form_status = 1;  
	}  
}


//disabling popup with jQuery magic!  
function disable_media_popup(){  
	//disables popup only if it is enabled  
	if(media_form_status==1){  
		$("#background_media_form").fadeOut("slow");  
		$("#media_form").fadeOut("slow");
		media_form_status = 0;  
	}  
}


//centering popup  
function center_media_popup(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#media_form").height();  
	var popupWidth = $("#media_form").width();  
	
	var top = windowHeight/2-popupHeight/2;
	var left = windowWidth/2-popupWidth/2;
	
	if (top < 0) {
		top = 20;
	}
	
	//centering  
	$("#media_form").css({  
		"position": "absolute",  
		"top": top,  
		"left": left  
	});  
	//only need force for IE6  

	$("#background_media_form").css({  
		"height": windowHeight,
		"width": windowWidth
	}); 

}


/*
===========================

Thank you

============================
*/

//centering popup  
function center_media_thankyou_popup(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#mediaThankyou").height();  
	var popupWidth = $("#mediaThankyou").width();  
	
	var top = windowHeight/2-popupHeight/2;
	var left = windowWidth/2-popupWidth/2;
	
	if (top < 0) {
		top = 20;
	}
	
	//centering  
	$("#mediaThankyou").css({  
		"position": "absolute",  
		"top": top,  
		"left": left  
	});  
	//only need force for IE6  

	$("#background_media_form").css({  
		"height": windowHeight,
		"width": windowWidth
	}); 

}

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var media_thankyou_status = 0;

//loading popup with jQuery magic!  
function load_media_thankyou_popup(){  
	//loads popup only if it is disabled  
	if(media_thankyou_status==0){  
		$("#background_media_form").css({  
			"opacity": "0.7"  
		});  
		$("#background_media_form").fadeIn("slow");  
		$("#mediaThankyou").fadeIn("slow");  
		media_thankyou_status = 1;  
	}  
}


//disabling popup with jQuery magic!  
function disable_media_thankyou_popup(){  
	//disables popup only if it is enabled  
	if(media_thankyou_status==1){  
		$("#background_media_form").fadeOut("slow");  
		$("#mediaThankyou").fadeOut("slow");
		media_thankyou_status = 0;  
	}  
}