// Open a popup and mask the site

function setHeightBackground() {
	backgroundPopupHeight = jQuery('#mainDiv').height();
    jQuery("#backgroundPopup").css({"height": backgroundPopupHeight + "px"});
}
function showPopup(id) {
	var detailDiv = jQuery("#" + id);
	var windowObj = jQuery(window);
	var windowHeight = windowObj.height();
	//alert(windowObj.width()+' --- '+detailDiv.width());
	if (!document.getElementById("backgroundPopup")) {
		jQuery("body").append("<div id='backgroundPopup'><!-- --></div>");
	}
	setHeightBackground();
	jQuery("#backgroundPopup").show();
	detailDiv.css({left: (windowObj.width() - detailDiv.width()) / 2, top: (windowObj.height() - detailDiv.height()) / 2 + windowObj.scrollTop()}).fadeIn(250);
}

function getEventYCoordinates(e) {
	 var evt=window.event || e;
	 return e.clientY;
}

function showAjaxPopup(e, popupId, requestParams) {
	var requestParamString = '';	
	var url = contextPath+'/popupController/popup?popupid='+popupId;
	var windowObj = jQuery(window);
	
	if(typeof(requestParams) !== 'undefined') {
		for(var key in requestParams) {
			requestParamString += '&'+key+'='+requestParams[key];
		}		
		url = url+=requestParamString;
	}
	//jQuery('#ajaxPopupDiv').css('top', getEventYCoordinates(e)+windowObj.scrollTop());
	
	jQuery.ajax({
        type: "GET",
        dataType: "html",
        url: url,
        cache: false,
        success : function(html) {
    	  jQuery('#ajaxPopupDiv').html(html);
    	  showAjaxPopupCallback('ajaxPopupDiv');
    	}
    });
}

function showAjaxPopupCallback(id) {
	var detailDiv = jQuery("#" + id);
	var windowObj = jQuery(window);
	var windowHeight = windowObj.height();
	//alert(windowObj.width()+' --- '+detailDiv.width());
	if (!document.getElementById("backgroundPopup")) {
		jQuery("body").append("<div id='backgroundPopup'><!-- --></div>");
	}
	jQuery("#backgroundPopup").show();
	jQuery('#ajaxPopupDiv').center();
	detailDiv.fadeIn(250);
	setHeightBackground();
	//detailDiv.css({left: (windowObj.width() - detailDiv.width()) / 2}).fadeIn(250);
}

function hideAjaxPopup() {
	jQuery('#ajaxPopupDiv').fadeOut(250, function(){jQuery(this).hide()});
	jQuery('#backgroundPopup').hide();
}

// Used in leaderboard
function togglePopupNoBackground(id, showHide) {
    if (showHide) {
        jQuery("#" + id).show().center();
    } else {
        jQuery("#" + id).hide();
    }
}
