jQuery(document).ready(function(){

	jQuery.fn.exists = function(){return (jQuery(this).length > 0);}

	if(jQuery(".contactFormOpener").exists()){
		new MsContactFormHandler();
	}

});

function MsContactFormHandler(){
	this.ref = jQuery(".contactFormOpener").first();
	this.init();
}

MsContactFormHandler.prototype = {
	init : function(){
		var This = this;
		
		this.ref.click(function(){
			
      This.loadForm(this.id.substr(2));
			return false;
			
			//TEMP CZ
      /*id = this.id.substr(2);
      Shadowbox.open({
	     content:  'http://aberdeen.dk/wp-content/plugins/microsite/contactForm.php?id='+id,
	     player:  'iframe',
	     //content:  '',
	     //player:  'html',
	     width: 575,
	     height: 460
	    });
	    This.addFormHandler(id);
      */

		});
		
		
		
		jQuery("body").mousedown(function(){
			jQuery("#shadowBox").hide();
			//$("body").css("height", "auto");
		});
		
		
	},
	
	loadForm : function(id){
		var This = this;
		jQuery('<div id="shadowBox" style="">').appendTo(jQuery("body"));
		jQuery("#shadowBox").load("http://aberdeen.dk/wp-content/plugins/microsite/contactForm.php",{id:id},function(){ // FB: SKAL ÆNDRES TIL NORMAL URL
			jQuery("#shadowBox").addClass("active").show();
			This.addFormHandler(id);
		});
		
		jQuery("#shadowBox").mousedown(function(e){
			e.stopPropagation();
		});
		
		//$("body").css("height", "1000px");
		
	},
	
	addFormHandler : function(id){
		jQuery("#shadowBox form").submit(function(){
			jQuery.post("http://aberdeen.dk/wp-content/plugins/microsite/contactForm.php",jQuery(this).serialize()+"&submitId="+id,function(data){
				jQuery("#shadowBox form").slideToggle("medium",function(){
					jQuery("#shadowBox form").html(data).slideToggle("fast");
					jQuery("#shadowBox").delay(2000).fadeOut();		
				});
			});
			
			return false;
		});
	}
}
