/**
 * Javascript class for handling popup window using DIV
 * @requires jQuery 1.2 or later
 *
 * Copyright (c) 2010 Lucky
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 */
function popupWindow(windowWidth, windowHeight, divName){
    this.width=windowWidth;
    this.height=windowHeight;
    this.div="#" + divName;
    this.isDrag=false;
    this.isResize=false;
    
    this.smokeScreen=null;
    this.smokeScreenColor="#CCCCCC";
    this.smokeScreenOpacity=0.3;
    
    this.shield=null;
    
    this.relocate=function(){
        if(this.smokeScreen!=null && this.smokeScreen!=undefined && this.smokeScreen!=""){
            $(this.smokeScreen).css({
                "top":$(document).scrollTop() + "px",
                "left":$(document).scrollLeft() + "px"
            });
        }
        
        if(this.shield!=null && this.shield!=undefined && this.shield!=""){
            $(this.shield).css({
                "left":(($(window).width()-$(this.div).width())/2)+$(document).scrollLeft() + "px",
                "top":(($(window).height()-$(this.div).height())/2)+$(document).scrollTop() + "px"
            });
        }
        
        $(this.div).css({
            "left":(($(window).width()-$(this.div).width())/2)+$(document).scrollLeft() + "px",
            "top":(($(window).height()-$(this.div).height())/2)+$(document).scrollTop() + "px"
        });
    }
    
    this.show=function(){
        if(this.smokeScreen!=null && this.smokeScreen!=undefined && this.smokeScreen!=""){
            $(this.smokeScreen).css({
                "position":"absolute",
                "background-color":this.smokeScreenColor,
                "opacity":this.smokeScreenOpacity,
                "width":$(window).width() + "px",
                "height":$(window).height() + "px"
            });
            
            $(this.smokeScreen).fadeIn("fast");
        }
        
        if(this.shield!=null && this.shield!=undefined && this.shield!=""){
            $(this.shield).css({
                "position":"absolute",
                "width":this.width + 2 + "px",
                "height":this.height + 2 + "px"
            });
            
            $(this.shield).fadeIn("fast");
        }
        
        $(this.div).css({
            "position":"absolute",
            "width":this.width + "px",
            "height":this.height + "px"
        });
        
        var me=this;
        $(me.div + " #close").bind(
            "click",
            function(){
                me.hide();
            }
        );
        
        $(this.div).fadeIn("fast");
        
        this.relocate();
    }
    
    this.hide=function(){
        if(this.smokeScreen!=null && this.smokeScreen!=undefined && this.smokeScreen!=""){
            $(this.smokeScreen).fadeOut("fast");
        }
        
        if(this.shield!=null && this.shield!=undefined && this.shield!=""){
            $(this.shield).hide();
        }
        
        $(this.div).fadeOut("fast");
    }
    
    this.setZIndex=function(zIndex){
        if(this.smokeScreen!=null && this.smokeScreen!=undefined && this.smokeScreen!=""){
            this.setSmokeScreenZIndex((zIndex-1));
        }
        
        if(this.shield!=null && this.shield!=undefined && this.shield!=""){
            this.setShieldZIndex(zIndex);
        }
        
        $(this.div).css({"z-index":zIndex});
    }
    
    this.alwaysCentered=function(){
        var me=this;
        $(window).scroll(function(){
            me.relocate();
        });
    }
    
    this.dragable=function(dragId){
        var me=this;
        
        $(me.div + " #" + dragId).bind(
            'mousedown',
            function(e){
                me.isDrag=true;
                mouseX=e.pageX;
                mouseY=e.pageY;
                divX=$(me.div).offset().left;
                divY=$(me.div).offset().top;
                $(me.div).bind(
                    'mousemove',
                    function(e){
                        if(me.isDrag==true){
                            $(me.div).css({
                                "left":(divX+e.pageX-mouseX) + "px",
                                "top":(divY+e.pageY-mouseY) + "px"
                            });
                            
                            if(me.shield!=null && me.shield!=undefined && me.shield!=""){
                                $(me.shield).css({
                                    "left":(divX+e.pageX-mouseX) + "px",
                                    "top":(divY+e.pageY-mouseY) + "px"
                                });
                            }
                        }
                    }
                );
            }
        );
        
        $(me.div + " #" + dragId).css({"cursor":"move"});
        
        $(document).bind(
            'mouseup',
            function(){
                me.isDrag=false;
            }
        );
    }
    
    this.autoTopOnClick=function(){
        var me=this;
        
        $(me.div).bind(
            "mousedown",
            function(){
                popupWindow.maxZIndex++;
                me.setZIndex(popupWindow.maxZIndex);
            }
        );
    }
    
    this.resizable=function(resizeId, minWidth, minHeight, maxWidth, maxHeight){
        if(minWidth==undefined) minWidth=160;
        if(minHeight==undefined) minHeight=120;
        if(maxWidth==undefined) maxWidth=640;
        if(maxHeight==undefined) maxHeight=480;
        
        var me=this;
        resizeId=me.div + " #" + resizeId;
        
        $(resizeId).css({
            "position":"absolute",
            "cursor":"se-resize",
            "left":(me.width-$(resizeId).width()) + "px",
            "top":(me.height-$(resizeId).height()) + "px"
        });
        
        $(resizeId).bind(
            'mousedown',
            function(e){
                var currentWidth=$(me.div).width();
                var currentHeight=$(me.div).height();
                
                me.isResize=true;
                mouseX=e.pageX;
                mouseY=e.pageY;
                $(document).bind(
                    'mousemove',
                    function(e){
                        if(me.isResize==true){
                            var width=currentWidth+e.pageX-mouseX;
                            var height=currentHeight+e.pageY-mouseY;
                            
                            if(width<minWidth) width=minWidth;
                            if(height<minHeight) height=minHeight;
                            if(width>maxWidth) width=maxWidth;
                            if(height>maxHeight) height=maxHeight;
                            
                            $(me.div).css({
                                "width":width + "px",
                                "height":height + "px"
                            });
                            
                            if(me.shield!=null){
                                $(me.shield).css({
                                    "width":width + 2 + "px",
                                    "height":height + 2 + "px"
                                });
                            }
                            
                            $(resizeId).css({
                                "left":(width-$(resizeId).width()) + "px",
                                "top":(height-$(resizeId).height()) + "px"
                            });
                        }
                    }
                );
            }
        );
        
        $(document).bind(
            'mouseup',
            function(){
                me.isResize=false;
            }
        );
    }
    
    this.setSmokeScreen=function(divId){
        this.smokeScreen="#" + divId;
    }
    
    this.setSmokeScreenZIndex=function(zIndex){
        $(this.smokeScreen).css({"z-index":zIndex});
    }
    
    this.setSmokeScreenColor=function(color){
        this.smokeScreenColor=color;
    }
    
    this.setSmokeScreenOpacity=function(opacity){
        this.smokeScreenOpacity=opacity;
    }
    
    this.setShield=function(iframeId){
        if(navigator.appName=="Microsoft Internet Explorer"){
            if(parseFloat(navigator.appVersion.split("MSIE")[1])<7){
                this.shield="#" + iframeId;
            }
        }
    }
    
    this.setShieldZIndex=function(zIndex){
        if(navigator.appName=="Microsoft Internet Explorer"){
            if(parseFloat(navigator.appVersion.split("MSIE")[1])<7){
                $(this.shield).css({"z-index":zIndex});
            }
        }
    }
}

popupWindow.maxZIndex=0;
