/**
 *   GPopout
 *
 *   Copyright (C) 2010  Emiliano Mateu
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 **/

function GPopout(cfg){
    this.window             = null;
    this.cfg                = null;	
    this.html               = null;
    this.body               = null;
    this.popoutLayer        = null;
    this.popoutWrapper      = null;
    this.popoutLoading      = null;
    this.popoutBox          = null;
    this.docHeight          = null;
    this.ie                 = null;
    this.ie6                = null;
    this.ie7                = null;
    this.ie6Layer           = null;
    var _this               = this;

    this.init = function(){
        _this.setCfg(cfg);
        _this.window            = jQuery(window);
        _this.html 		= jQuery("html");
        _this.body 		= jQuery("body");
        _this.docHeight         = jQuery(document).height();
        _this.ie                = jQuery.browser.msie;
        _this.ie6		= jQuery.browser.msie && jQuery.browser.version.substr(0, 1) == 6;
        _this.ie7		= jQuery.browser.msie && jQuery.browser.version.substr(0, 1) == 7;
    };

    this.setupPopout = function(x, y){
        var posX = x;
        var posY = y;

        _this.body.append(_this.structure());
        
        _this.popoutBody            = jQuery("#"+_this.cfg.id.popoutBody);
        _this.popoutLayer           = jQuery("#"+_this.cfg.id.popoutLayer);
        _this.popoutWrapper         = jQuery("#"+_this.cfg.id.popoutWrapper);
        _this.ie6Layer              = jQuery("#"+_this.cfg.id.popoutIe6Layer);

        if (_this.ie) {
            _this.popoutBox         = jQuery("#"+_this.cfg.id.popoutBox);
            _this.popoutWrapper.width(_this.popoutBox.outerWidth());
        }

        if (posX == null || posX == "")  posX = parseInt((_this.body.outerWidth()-_this.popoutWrapper.outerWidth())/2);
        if (posY == null || posY == "")  posY = parseInt((_this.window.height()-_this.popoutWrapper.outerHeight())/2);

        if (_this.cfg.position == "absolute") posY += parseInt(_this.window.scrollTop());
        
        _this.popoutWrapper.css({
            left: posX+"px",
            top: posY+"px"
        });

        if (!_this.cfg.pageScroll || _this.ie6){
            _this.html.css({
                height      : _this.docHeight+"px",
                overflow    : "hidden"
            });
        }

        if (_this.cfg.opacity < 1) _this.popoutLayer.css("opacity", _this.cfg.opacity);
        _this.popoutLayer.hide();
        _this.popoutWrapper.hide();

        if (_this.ie6){
            _this.ie6Layer.hide();
            _this.popoutWrapper.css("position", "absolute");
        }

        jQuery("#"+_this.cfg.id.popoutClose).click(function(){
            _this.close();
        });

        if(_this.cfg.closeOnClick){
            jQuery("#"+_this.cfg.id.popoutLayer).click(function(){
                _this.close();
            });
        }
    };

    this.close = function(){
        eval("(_this.popoutWrapper."+_this.cfg.hideFx+"("+_this.cfg.hideFxDelay+"))");
        _this.popoutLayer.hide();

        if (!_this.cfg.pageScroll || _this.ie6){
            _this.html.css({
                height      : "auto",
                overflow    : "auto"
            });
            if (_this.ie6) _this.html.css("overflow-x", "hidden");
        }

        setTimeout(function(){
            _this.popoutLayer.remove();
            _this.popoutWrapper.remove();
            if (_this.ie6){
                _this.ie6Layer.remove();
            }
        },(_this.cfg.hideFxDelay+300));
    };

    this.show = function(html, clase, x, y){
        _this.setupPopout(x,y);

        if (clase != null || clase != ""){
            _this.popoutWrapper.addClass(clase);
        }
        _this.popoutBody.html(html);

        if (_this.ie6){
            jQuery("#"+_this.cfg.id.popoutIe6Layer).show();
        }
        _this.popoutLayer.show();
        eval("(_this.popoutWrapper."+_this.cfg.showFx+"("+_this.cfg.showFxDelay+"))");
        //_this.popoutWrapper.fadeIn(_this.cfg.showFxDelay);
    };

    this.load = function(html){
        _this.popoutBody.html(html);
    };

    this.showLoading = function(x, y){
        var posX = x;
        var posY = y;
        _this.popoutLoading = jQuery("#"+_this.cfg.id.popoutLoading);

        if (_this.ie6) _this.popoutLoading.css("position", "absolute");

        if (posX == null || posX == "")  posX = parseInt((_this.body.width()-_this.popoutLoading.width())/2)+"px";
        if (posY == null || posY == "")  posY = "50%";

        _this.popoutLoading.css({
            left: posX,
            top: posY
        });

        _this.popoutLoading.show();
    };

    this.hideLoading = function(){
        _this.popoutLoading.hide();
    };

    this.structure = function(){
        var html =  '\
                    <div id="'+_this.cfg.id.popoutLayer+'" style="height:'+_this.docHeight+'px; position: absolute; z-index:99995; top: 0; left:0; width:100%;"></div>\
                    <div id="'+_this.cfg.id.popoutWrapper+'" style="position:'+_this.cfg.position+'; z-index: 99997;">\
                        <div id="'+_this.cfg.id.popoutLoading+'" style="display: none; position:'+_this.cfg.position+'; z-index: 99999;">'+_this.cfg.loadingText+'</div>\
                        <div id="'+_this.cfg.id.popoutBox+'">\
                            <div id="'+_this.cfg.id.popoutHeader+'">\
                                <div id="'+_this.cfg.id.popoutClose+'" title="'+_this.cfg.closeTitleText+'">'+_this.cfg.closeText+'</div>\
                            </div>\
                            <div id="'+_this.cfg.id.popoutBody+'"></div>\
                        </div>\
                    </div>\
            ';

        if (_this.ie6){
            _this.body.append("<iframe id='"+_this.cfg.id.popoutIe6Layer+"' src='javascript:false;' scrolling='no' frameborder='0' style='width: 100%; height: " +_this.doc_height+ "px; position:absolute; top:0; left:0; z-index:997;  filter: alpha(opacity=0);' />")
        }

        return html;
    };
    
    this.setCfg = function(cfg){
        if (cfg == null) cfg = {};
        _this.cfg = {
            closeOnClick    : (cfg.closeOnClick != null)    ? cfg.closeOnClick : true,
            pageScroll      : (cfg.pageScroll != null)      ? cfg.pageScroll : true,
            position        : (cfg.position != null)        ? cfg.position : "fixed",
            showFx          : (cfg.showFx != null)          ? cfg.showFx : "fadeIn",
            showFxDelay     : (cfg.showFxDelay != null)     ? cfg.showFxDelay : 500,
            hideFx          : (cfg.hideFx != null)          ? cfg.hideFx : "fadeOut",
            hideFxDelay     : (cfg.hideFxDelay != null)     ? cfg.hideFxDelay : 500,
            closeText       : (cfg.closeText != null)       ? cfg.closeText : "Cerrar",
            closeTitleText  : (cfg.closeTitleText != null)  ? cfg.closeTitleText : "Cerrar",
            loadingText     : (cfg.loadingText != null)     ? cfg.loadingText : "Cargando...",
            opacity         : (cfg.opacity != null)         ? parseFloat(cfg.opacity) : 0.5,
            id              : {
                popoutLayer     : (cfg.popoutLayer != null)     ? cfg.popoutLayer : "popoutLayer",
                popoutWrapper   : (cfg.popoutWrapper != null)   ? cfg.popoutWrapper : "popoutWrapper",
                popoutLoading   : (cfg.popoutLoading != null)   ? cfg.popoutLoading : "popoutLoading",
                popoutBox       : (cfg.popoutBox != null)       ? cfg.popoutBox : "popoutBox",
                popoutHeader    : (cfg.popoutHeader != null)    ? cfg.popoutHeader : "popoutHeader",
                popoutClose     : (cfg.popoutClose != null)     ? cfg.popoutClose : "popoutClose",
                popoutBody      : (cfg.popoutBody != null)      ? cfg.popoutBody : "popoutBody",
                popoutIe6Layer  : (cfg.popoutIe6Layer != null)  ? cfg.popoutIe6Layer : "popoutIe6Layer"
            }
        };
    }

    this.init();
}

