﻿function SimplePopup(Content, Options, ID)
{
    var Instance = this;
    this.ID = ID;
    this.Content = Content;
    
    //OPÇÕES PERSONALIZADAS PASSADAS NO CONSTRUTOR
    this.Options = (Options) ? Options : {};
    
    //OPÇÕES DEFAULT DA GRID
    this.OptionsDefault = {
        Width : "300",
        Top : 0,
        Left : 0,
        Center : true,
        Title : "",
        CloseOnClick : false, 
        CloseOnClickOut : null, 
        ShowDisabler : true, 
        PositionFromOBJ : null,
        //Eventos : {
        OnLoad : function(){},
        OnClose : function(){},
        //}
        //Styles : {
        BtnCloseClass : "SimplePopup_Default_BtnClose",
        ContainerClass : "SimplePopup_Default_Container",
        ContentClass : "SimplePopup_Default_Content",
        HeaderClass : "SimplePopup_Default_Header"
        //}
    };

    //PARA RECUPERAR UMA OPTION, UTLIZAR ESTE MÉTODO
    var GetOption = function(Opt)
    {
        return ( Instance.Options != null &&  Instance.Options[Opt] != null) ? Instance.Options[Opt] : Instance.OptionsDefault[Opt];
    };
    var EvtClose = function(){
        var Target = getTarget(event);
        if(Target != GetOption("CloseOnClickOut"))
        {
            Instance.Close();
        }        
    };
    var Create = function(ID) {
        var Container = document.createElement("DIV");
        Container.className = GetOption("ContainerClass");
        Container.id = GetOption("ContainerClass") + "_" + ID;
        Container.style.width = GetOption("Width") + "px";
        Container.style.zIndex = 1000;
        Container.style.visibility = "hidden";

        var Content = document.createElement("DIV");
        Content.className = GetOption("ContentClass");
        Content.id = GetOption("ContentClass") + "_" + ID;
        Content.innerHTML = Instance.Content;

        var Header = document.createElement("DIV");
        Header.className = GetOption("HeaderClass");
        Header.id = GetOption("HeaderClass") + "_" + ID;
        Header.innerHTML = "<span style='float:left'>" + GetOption("Title") + "</span>";

        var BtnClose = document.createElement("DIV");
        BtnClose.className = GetOption("BtnCloseClass");
        BtnClose.id = GetOption("BtnCloseClass") + "_" + ID;
        BtnClose.innerHTML = "&nbsp;";
        BtnClose.onclick = function() {
            Instance.Close();
        };

        Header.appendChild(BtnClose);
        Container.appendChild(Header);
        Container.appendChild(Content);

        document.body.appendChild(Container);
        if (GetOption("ShowDisabler")) {
            documentDisabler.add("SimplePopup", Container);
        }
        if (GetOption("CloseOnClickOut") != null) {
            addEvent(document, "click", EvtClose);
        }

        if (GetOption("CloseOnClick")) {
            addEvent(Content, "click", function() {
                Instance.Close();
            });
        }
        (
            function(Container) {
                setTimeout(function() {
                    var XY = ValidatePosition(GetPosition(Container), Container);
                    Container.style.top = XY.y + "px";
                    Container.style.left = XY.x + "px";
                    Container.style.visibility = "visible";
                }, 200);
            }
        )(Container);

        GetOption("OnLoad")(Content);
        Container = null;
        Content = null;
        Header = null;
        BtnClose = null;
    };
    
    var GetPosition = function(Popup){
        
        if(GetOption("Center"))
        {
            return {"x": (DOCWidth() / 2) - (OBJWidth(Popup) / 2), "y": (DOCHeight() / 2) - (OBJHeight(Popup) / 2)};
        }
        else if(GetOption("PositionFromOBJ") != null)
        {
            return {"x": OBJX(GetOption("PositionFromOBJ")), "y": OBJY(GetOption("PositionFromOBJ"))};                
        }
        else
        {
            return {"x" : GetOption("Left"), "y" : GetOption("Top")};
        }
    };
    var ValidatePosition = function(Position, Popup)
    {
        if(Position.x + OBJWidth(Popup) > DOCWidth())
        {
            Position.x = DOCWidth() - (OBJWidth(Popup) + 5);
        }
        
        if(Position.y + OBJHeight(Popup) > DOCHeight())
        {
            Position.y = DOCHeight() - (OBJHeight(Popup) + 5);
        }        
        return Position;
    };
    Create(this.ID);
    
    this.Close = function(){
        var Popup = document.getElementById(GetOption("ContainerClass") + "_" + Instance.ID);
        if(GetOption("ShowDisabler"))
        {
            documentDisabler.remove("SimplePopup", Popup);
        }        
        if(Popup != null)
        {
            document.body.removeChild(Popup);
        }
        removeEvent(document, "click", EvtClose);
        GetOption("OnClose")();
    };
}


//FUNCOES AUXILIARES
function addEvent(obj, type, fn) {
    if (obj.attachEvent) {
        obj.attachEvent('on' + type, fn);
    } else {
        obj.addEventListener(type, fn, false);
    }
}

function removeEvent(obj, type, fn) {
    if (obj.detachEvent) {
        obj.detachEvent('on' + type, fn);
    } else {
        obj.removeEventListener(type, fn, false);
    }
}

function getTarget(e){
    e?evt=e:evt=event;
    return evt.target?evt.target:evt.srcElement;
}
var DOCWidth = function(){
    var width = 0;
    if(typeof( window.innerWidth ) == 'number' ) {
        width = window.innerWidth;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        width = document.documentElement.clientWidth;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        width = document.body.clientWidth;
    }
    return width;
};

var DOCHeight = function(){
    var height = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        height = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        
        height = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        
        height = document.body.clientHeight;
    }
    return height;
};
var OBJWidth = function(elem){
    function _Convert(val) {
        if (!val) {return;}
        val = val.replace("px","");
        if (isNaN(val)) {return 0;}
        return parseInt(val);
    }
    var currentStyle;
    if (elem.currentStyle) { currentStyle = elem.currentStyle; }
    else if (window.getComputedStyle) {	currentStyle = document.defaultView.getComputedStyle(elem, null); }
    else { currentStyle = elem.style; }
    return (elem.offsetWidth -
        _Convert(currentStyle.marginLeft) -
        _Convert(currentStyle.marginRight) -
        _Convert(currentStyle.borderLeftWidth) -
        _Convert(currentStyle.borderRightWidth));        
};
var OBJHeight = function(elem){
    function _Convert(val) {
        if (!val) {return;}
        val = val.replace("px","");
        if (isNaN(val)) {return 0;}
        return parseInt(val);
    }
    var currentStyle;
    if (elem.currentStyle) { currentStyle = elem.currentStyle; }
    else if (window.getComputedStyle) {	currentStyle = document.defaultView.getComputedStyle(elem, null); }
    else { currentStyle = elem.style; }
    return (elem.offsetHeight -
        _Convert(currentStyle.marginTop) -
        _Convert(currentStyle.marginBottom) -
        _Convert(currentStyle.borderTopWidth) -
        _Convert(currentStyle.borderBottomWidth));	
};
var OBJX = function(obj){
    var curleft = 0;
    if(obj.offsetParent){
        while(1){
            curleft += obj.offsetLeft;
            if(!obj.offsetParent){
                break;
            }
            obj = obj.offsetParent;
        }
    }else if(obj.x){
        curleft += obj.x;
    }
    return curleft;
};
var OBJY = function(obj){
    var curtop = 0;
    if(obj.offsetParent){
        while(1){
            curtop += obj.offsetTop;
            if(!obj.offsetParent){
                break;
            }
            obj = obj.offsetParent;
        }
    }else if(obj.y){
        curtop += obj.y;
    }
    return curtop;        
};