$(function() {
	$.ajax({
	type: 'GET',
	url: '?open=popup',
	cache: false,
	success: show,
	dataType: 'json',
	error: function(x) {
	//alert(x);
    }
});
});

function _getWindowHeight() {
	if (self.innerHeight) 
		return self.innerHeight; // browsers
	else if (document.documentElement && document.documentElement.clientHeight) 
		return document.documentElement.clientHeight; 	
	else if (document.body) 
		return document.body.clientHeight; // other IE
	return false;
}

function _getBodyHeight() {
	if (window.innerHeight && window.scrollMaxY)
		return window.innerHeight + window.scrollMaxY;
	else if (document.body.scrollHeight > document.body.offsetHeight)
		return document.body.scrollHeight;
	else  
		return document.body.offsetHeight;
	return false;
}

function getHeight() {
    var windowHeight = _getWindowHeight();	var bodyHeight = _getBodyHeight();
	if (bodyHeight > windowHeight) {
        return bodyHeight;
	}
    return windowHeight;
}

function show(data)
{
    if(null != data) {
        var img = new Image();
        img.onload = function() {
						var cords = (_getWindowHeight() - this.height)/2;
						if(cords < 20){
							cords = '20';
						}
            $('select').hide();
            $('<div id="popupBackgroundDiv" style="height: '+getHeight()+'px"></div>').appendTo('body').show();
            $('<div id="popupDiv" style="width:'+this.width+'px; height:'+this.height+'px; margin-top:'+cords+'px"></div>')
					 .appendTo('#popupBackgroundDiv').append($('<a href="'+(data.url?data.url:'')+'"></a>').append(this)).show();
            var url = $('<h3><a href="#" style="position:relative;font-size:1.3em;text-decoration:none;top:-'+(this.height+15)+'px;left:'+this.width+'px;">X</a></h3>');
            $(this).show('slow',
                function() {
                    url.appendTo('#popupDiv').click(
                        function() {$('#popupBackgroundDiv').hide('slow',
                            function() {$('#popupBackgroundDiv').remove();}
                        );
                        $('select').show();
                        return false;
                    }
                 );
                }).click(
                function() {$('#popupBackgroundDiv').hide('slow',
                    function() {$('#popupBackgroundDiv').remove();});
                 $('select').show();
                } 
            );
        }
        img.src = data.filename;
    }
}
