var compDiv;

function openPopDiv(divId)
{
	compDiv=divId;
	centerPopup(divId);
	loadPopup(divId);
	//var scrollup = setTimeout(function(){ $( 'html, body' ).animate( { scrollTop: 1 }, 0 ); }, 250);
	if(typeof ClickTaleExec=='function')
	  ClickTaleExec("openPopDiv('"+divId+"')");
}
function closePopDiv(divId)
{
    compDiv=null;
    disablePopup(divId)

    if(typeof ClickTaleExec=='function')
      ClickTaleExec("closePopDiv('"+divId+"')");
}
function loadPopup(popDiv)
{
    $("#fade").show();
    $("div#"+popDiv).show()
}
function disablePopup(popDiv)
{
    $("#fade").hide();
    $("div#"+popDiv).hide()
}
function centerPopup(popDiv)
{
  if(!popDiv || !popDiv.length)
    return;
  
    var windowWidth=$(window).width();
    var windowHeight=$(document).height();
    var popupHeight=$("div#"+popDiv).height();
    var popupWidth=$("div#"+popDiv).width();
    $("#fade").css({"height":windowHeight,opacity:0.7,"width":windowWidth,"backgroundColor":"black","position":"absolute"});
    
    // Is this a mobile device? Push the box to the top
    if(navigator.userAgent.match(/(android|iphone|ipod|mobile)/i)){
      $("div#"+popDiv).css({"position":"absolute","top":"0px","left":((windowWidth-popupWidth)/2),"z-Index":1002});
    }
    else {
      var topp = (($(window).height() - $("#" + popDiv).height()) /2);
      if(topp < 0) topp = 0;
      
      var leftt = ((windowWidth-popupWidth)/2);
      if(leftt < 0) leftt = 0;
      
      $("div#"+popDiv).css({"position":"fixed","top":topp+"px","left":leftt +"px","z-Index":1002});
    }
	
}

$(document).ready(function()
{ 
    setTimeout(function(){centerPopup(compDiv);}, 200);
    
    $("#fade").click(function(){ closePopDiv(compDiv); });
    $(document).keypress(function(e){
    if(e.keyCode==27){ closePopDiv(compDiv)}});
    $(window).resize(function(){ centerPopup(compDiv); })
});

