// Please include the jQuery script before this one

var WP_WIDTH = 750; 
var WP_HEIGHT = 440; 
var WP_pathToImage = "images/ajax-loader.gif";
var WP_USE_DEBUG = false;

$(document).ready(function(){   
	imgLoader = new Image();// preload image
	imgLoader.src = WP_pathToImage;
});

function Win_DetectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function debugMsg(msg) { if (WP_USE_DEBUG) alert(msg); }

/// Setup the scene for showing a popup


function Win_InitializePopup(frm)
{
    Win_RemovePopup();
    //if IE 6
	if (typeof document.body.style.maxHeight === "undefined") 
	{
		$("body","html").css({height: "100%", width: "100%"});
		$("html").css("overflow","hidden");
		if (document.getElementById("WP_HideSelect") === null) 
			$("body").append("<iframe id='WP_HideSelect'></iframe>");
    }
    
	if(document.getElementById("WP_overlay") === null)
	{
	    if (frm === null)
		    $("body").append("<div id='WP_overlay'></div><div id='WP_window'></div>");
		else
		    $(frm).append("<div id='WP_overlay'></div><div id='WP_window'></div>");
		$("#WP_overlay").click(Win_RemovePopup);
	}
	
	if(Win_DetectMacXFF())
	{
		$("#WP_overlay").addClass("WP_overlayMacFFBGHack");//use png overlay so hide flash
	}
	else
	{
		$("#WP_overlay").addClass("WP_overlayBG");//use background and opacity
	}	
}

function Win_ShowPopupStatic(caption, elementID, showCloseBtn, useForm)
{
    var parentForms = $('#'+elementID).parents("form");
    
    if ((!useForm) || (parentForms.length === 0))
    {
        Win_InitializePopup(null);
        debugMsg('init popup no form');
    }
    else
    {
        Win_InitializePopup(parentForms[0]);
        debugMsg('init popup with form');
    }
    
	if(caption===null){caption="";}
	
    debugMsg('adding loader to the body');
	$("body").append("<div id='WP_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
	$('#WP_load').show();//show loader
	

	contentW = WP_WIDTH - 30;
	contentH = WP_HEIGHT - 45;
	
	var closeLink = "";
	
	if (showCloseBtn)
	    closeLink = "<div id='WP_closeWindow'> <a href='#' id='WP_closeWindowButton' >Esc</a></div>";
	else
    	$("#WP_overlay").unbind("click");
    	
    debugMsg('adding window');
    if($("#WP_window").css("display") != "block")
    {
		$("#WP_window").append("<div id='WP_title'><div id='WP_windowTitle'><span>"+caption+"</span></div>"+
		closeLink+
		"</div><div id='WP_windowContent' style='width:"+contentW+"px;height:"+contentH+"px'></div>");
	}
	
    debugMsg('close button');
	if (showCloseBtn)
	    document.onkeydown = function(e){ 	
		    if (e == null) { // ie
			    keycode = event.keyCode;
		    } else { // mozilla
			    keycode = e.which;
		    }
		    if(keycode == 27){ // close
			    Win_RemovePopup();
		    }
	    };	
	
	$("#WP_closeWindowButton").click(Win_RemovePopup);

    debugMsg('adding children to the window content');
    // Add all children into the window	
	$('#'+elementID).children().each(function(){
        $("#WP_windowContent")[0].appendChild(this);
	});

    // When it is being unloaded, we move them back.
	$("#WP_window").unload(function () {
	    $("#WP_windowContent").children().each(function(){
            $('#'+elementID)[0].appendChild(this);
	    });
	});
	
    debugMsg('setting position');
	Win_SetPosition();
	$("#WP_load").remove();
    debugMsg('moving back');
	$("#WP_window").css({display:"block"}); 
}

//helper functions below
function Win_ShowIFrame(){
	$("#WP_load").remove();
	$("#WP_window").css({display:"block"});
}

function Win_RemovePopup() {
 	$("#WP_imageOff").unbind("click");
	$("#WP_closeWindowButton").unbind("click");
	$('#WP_window').trigger("unload").unbind("unload");
	$("#WP_window").fadeOut("fast",function(){$('#WP_window,#WP_overlay,#WP_HideSelect').remove();});
	$("#WP_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function Win_SetPosition() {
$("#WP_window").css({marginLeft: '-' + parseInt((WP_WIDTH / 2),10) + 'px', width: WP_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#WP_window").css({marginTop: '-' + parseInt((WP_HEIGHT / 2),10) + 'px'});
	}
}