//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
//loading popup with jQuery magic!

function loadPopup( whichID ){
	//loads popup only if it is disabled
	
	if(popupStatus==0){
		jQuery(".over_box_m").html( jQuery("."+whichID).html() );
		jQuery(".grey_box").css({
			"opacity": "0.7"
		});
		jQuery(".grey_box").fadeIn("slow");
		jQuery(".over_box").fadeIn("slow");
		popupStatus = 1;
	}
}

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

//centering popup
function centerPopup( curPos ){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery(".over_box").height();
	var popupWidth = jQuery(".over_box").width();
	//centering
	jQuery(".over_box").css({
		"position": "absolute",
		"top": curPos,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	jQuery(".grey_box").css({
		"height": windowHeight
	});
}

//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
	//LOADING POPUP
	//Click the button event!
	jQuery(".more_inf_click, #prod_ad").click(function(){
		
		//centering with css
		centerPopup( jQuery(this).offset().top );
		//load popup
		loadPopup( jQuery(this).attr('id') );
	});

	jQuery(".more_inf_click, #prod_ad").hover(
		function () {
		jQuery(this).css("cursor", "pointer");
		}, function () {
		jQuery(this).css("cursor", "default");
	}); 

	//Testimonials
	jQuery(".more_inf_test").click(function(){
		jQuery(".testimonials").css({
			"height": "auto"
		});
		jQuery(".more_inf_test").parent().css({
			"display": "none"
		});
	});

	jQuery(".more_inf_test").hover(
		function () {
		jQuery(this).css("cursor", "pointer");
		}, function () {
		jQuery(this).css("cursor", "default");
	}); 
	
	//CLOSING POPUP
	//Click the x event!
	jQuery(".over_close").click(function(){
		disablePopup();
	});

	jQuery(".over_close").hover(
		function () {
		jQuery(this).css("cursor", "pointer");
		}, function () {
		jQuery(this).css("cursor", "default");
	}); 

	//Click out event!
	jQuery(".grey_box").click(function(){
		disablePopup();
	});
	//Press Escape event!
	jQuery(document).keypress( function(e){
		if ( e.keyCode == 27 ) {
			if ( popupStatus == 1 ) {
				disablePopup();
			}
		}
	});
});
