var popup = {
    current: null,
	init: function()
	{
		$('#popup-background').click(popup.close);
	},
	open: function(id)
	{
        if (this.current != null)
        {
            this.close();
        }
        this.showBackground();
        this.current = $('#popup-wrapper');

        var self = this;
		
        $.get(websiteUrl + 'ajax/gallery-popup/' + id, function(response)
        {
            $('#popup-wrapper').html(response);
            
            var top = $(window).scrollTop();
            if (self.current.height() < ($(window).height() - 100))
            {
                top += ($(window).height() - self.current.height()) / 2;
            }
            else
            {
                top += 50;
            }

            self.current
                .css('left', (($(document).width()/2)-(self.current.width()/2)) + 'px')
                .css('top', top + 'px')
                .show();

            setupGalleryPopup();
            popup.showBackground();
			$('.close').click(popup.close); 
        });
        popup.init();
	},
	close: function()
	{
        if  (popup.current != null)
        {
            popup.current.find('.close').unbind('click');
            popup.current.hide();
			if ($timer!=null)
			{
				clearTimeout($timer);
				$timer = null;
			}
        }
        popup.hideBackground();
	},
    showBackground: function()
    {
      $('#popup-background').css('height', $(document).height() + 40).show();
    },
    hideBackground: function()
    {
        $('#popup-background').hide();
    }
}
