﻿var popup = {

    visible: false,
    grabbed: null,
    grabbedX: 0,
    grabbedY: 0,
    
    
    show: function(popupId,fadedBG) {
        this.grabbed = null;
    
        if(fadedBG)
        {   
            $("#popup_bg").css('filter', 'alpha(opacity=50)');
            $("#popup_bg").fadeIn(500);
        }
            
        var popup = document.getElementById('popup_'+popupId);
        var height = popup.offsetHeight;
        var width = popup.offsetWidth;
        
        var cookieX = main.readCookie("popup_" + popupId + "_x");
        var cookieY = main.readCookie("popup_" + popupId + "_y");
       
        if(main.browser!="IE6")
        {
            if(cookieX==null || cookieY==null)
                $("#popup_"+popupId).css("top",$(window).height()/2-height/2 + "px").css("left",$(window).width()/2-width/2 + "px").css("visibility","visible");
            else
                $("#popup_"+popupId).css("top",cookieY + "px").css("left",cookieX + "px").css("visibility","visible");
        }
        else
        {
            $("#popup_"+popupId).css("top",$(window).height()/2-height/2+$(window).scrollTop() + "px").css("left",$(window).width()/2-width/2 + "px").css("visibility","visible");
        }
        this.visible = true;
    },
    
    hide: function(popupId) {
        $("#popup_bg").fadeOut(500);
        $("#popup_"+popupId).css("visibility","hidden");
        this.visible = false;
        this.grabbed = null;
    },

    grab: function (popupId) {
        this.grabbed = 'popup_'+popupId;
        this.grabbedX = main.mouseX-($("#"+this.grabbed).css("left")).replace("px","");
        this.grabbedY = main.mouseY-($("#"+this.grabbed).css("top")).replace("px","");
    },
        
    release: function() {
        main.createCookie(this.grabbed+"_x",(main.mouseX-this.grabbedX),1);
        main.createCookie(this.grabbed+"_y",(main.mouseY-this.grabbedY),1);
        this.grabbed = null;
       

    },
    
    move: function() {
        $("#"+this.grabbed).css("left",(main.mouseX-this.grabbedX)+"px").css("top",(main.mouseY-this.grabbedY)+"px");
        return false;
    }
};