/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
jQuery.fn.center = function()
{
    this.css("position","absolute");
    var top = (jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() -200;
    if (top < 0) top = 0;
    this.css("top", top + "px");
   // this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
    return this;
}
jQuery.extend({
	 getURLParam: function(strParamName){
		  var strReturn = "";
		  var strHref = window.location.href;
		  var bFound=false;
		  
		  var cmpstring = strParamName + "=";
		  var cmplen = cmpstring.length;

		  if ( strHref.indexOf("?") > -1 ){
			var strQueryString = strHref.substr(strHref.indexOf("?")+1);
			var aQueryString = strQueryString.split("&");
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			  if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				bFound=true;
				break;
			  }
			  
			}
		  }
		  if (bFound==false) return null;
		  return strReturn;
		}
});

var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//alert('popup1 geladen');
	//loads popup only if it is disabled
	jQuery('#popupContact').center();
	if(popupStatus==0){
		jQuery("#backgroundPopup").css({
			"opacity": "0.7",
			"filter": "alpha(opacity=70)"
			
		});
		jQuery("#backgroundPopup").fadeIn("slow");
		jQuery("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}

}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = (jQuery("#popupContact").height())/2;
	var popupWidth = jQuery("#popupContact").width()/2;
	

	//centering
	jQuery("#popupContact").css({
		"position": "absolute",
		"top": top,
		"left": left,
		"width": '500px'
			});
	//only need force for IE6
	
	jQuery("#backgroundPopup").css({
		"height": windowHeight
	});
	
}
	var clicked = 0;

//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
	var showform = jQuery.getURLParam("tx_drepdfgen_pi1[showform]");
	var showform2 = jQuery.getURLParam("tx_drepdfgen_pi1%5Bshowform%5D");
	var showform3 = document.location.href.indexOf('downloadcase/pdf');

	if(showform == 1 || showform2 == 1 || showform3 > 0){
		jQuery('#popupContact').center();
		
		//load popup
		loadPopup();
	}
	


	jQuery("#commentForm").validate();
	//LOADING POPUP
	//Click the button event!
	jQuery(".pdf").click(function(){
		//centering with css
		//centerPopup();
		jQuery('#popupContact').center();

		//load popup
		loadPopup();
		return false;

		
	});
				
	//CLOSING POPUP
	//Click the x event!
	jQuery(".popupContactClose").click(function(){
		disablePopup();
		var clicked = 0;
	});
		jQuery("#popupContactClose").click(function(){
		disablePopup();
		var clicked = 0;
	});
	jQuery(".close").click(function(){
		disablePopup();
		var clicked = 0;
	});
	//Click out event!
	jQuery("#backgroundPopup").click(function(){
		disablePopup();
		var clicked = 0;
	});
	//Press Escape event!
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

