/**
 * Rayge Popups Javascript Class
 *
 * $Rayge: rayge/js/popups.js,v 1.3 2005/08/25 00:55:34 fhelly Exp $
 * $Id: Popups.js 68 2006-03-21 17:47:07Z François Helly $
 *
 * $Revision: 54 $
 * $Date: 2006-04-07 15:24:11 +0200 (Fri, 07 Apr 2006) $
 * $Author: $
 *
 * Copyright 2004-2006 Francois Helly <fhelly@bebop-design.net>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @version $Revision: 54 $
 * @package Rayge
 */

var popups = new Array();

/**
 * Constructs the popup object and sets its attributes.
 *
 * @param string name                The popup window name
 * @param integer|string top         The left position of the window, in pixels,
 *                                   relatively to the client screen,
 *                                   the 'center' value means the popup will
 *                                   be verticaly centered in the screen
 * @param mixed integer|string left  The left position of the window, in pixels,
 *                                   relatively to the client screen,
 *                                   the 'center' value means the popup will
 *                                   be horizontaly centered in the screen
 * @param integer width              The width of the popup window, in pixels
 * @param integer height             The height of the popup window, in pixels
 * @param string options             A formatted string containing any window
 *                                   required options (like 'scrollbars=yes')
 */
function Popups(name, top, left, width, height, options)
{
    this._name    = name;
    this._width   = width;
    this._height  = height;
    this._top     = top;
    this._left    = left;

    if (this._top == 'center') {
        var h = screen.availHeight||screen.innerheight||600;
        if (height > h) {
            this._top = 10;
            this._height = h - 40;
        } else {
            this._top = ((h - height)/2) - 20;
        }
    }
    if (this._left == 'center') {
        var w = screen.availWidth||screen.innerwidth||800;
        if (width > w) {
            this._left = 10;
            this._width = w - 20;
        } else {
            this._left = (w - width)/2;
        }
    }
    this._params = 'width='+ this._width +',height='+ this._height +',' ;
    this._params += 'left' ;
    this._params += '=' + this._left +',';
    this._params += 'top' ;
    this._params += '=' + this._top + ''+ options;

    this.popen = function(href, relative)
    {
        if (typeof(relative) == 'undefined') {
            relative = new Boolean(false);
        }
        if (typeof(window.name) != 'undefined' && this._name == window.name) {
            this._win = eval('self');
            window.location.href = href;
        } else if (parent.opener && this._name == parent.opener.name) {
            this._win = eval('parent.opener');
            var loaded = this._isloaded(parent.opener.location, href, relative);
            if (loaded == -1) {
                parent.opener.location.href = href;
            }
            if (loaded == 0) {
                parent.opener.location.hash = href.substring(href.lastIndexOf('#'));
            }
            parent.opener.focus();
        } else if (this._win && this._win.closed) {
            this._win = window.open(href, target=this._name, this._params);
            this._win.focus();
        } else if (this._win && this._win.open) {
            var loaded = this._isloaded(this._win.location, href, relative);
            if (loaded == -1) {
                this._win.location.href = href;
            }
            if (loaded == 0) {
                this._win.location.hash = href.substring(href.lastIndexOf('#'));
            }
            this._win.focus();
        } else {
            this._win = window.open(href, target=this._name, this._params);
            this._win.focus();
        }
    }

    /**
     * Define if we have to reload the popup location or not.
     *
     * @param string location   The loaded location
     * @param string newurl     The new location
     * @param integer relative  Compare relative or absolute location,
     *                          or focus on opened popup.
     *
     * @return integer  -1 : load the new url
     *                  0 : yes but not at the same hash we have to change the popup location.hash
     *                  1 : yes we only need to focus on popup
     */
    this._isloaded = function(location, newurl, relative)
    {
        if (relative == -1) {
            return 1;
        }
        var host   = location.host;
        var pathname = (relative) ?
            location.pathname.substr((location.pathname.lastIndexOf('/'))+1) : location.pathname;
        var path   = (newurl.indexOf('//') != -1) ?
            newurl.substr(newurl.indexOf('//')+2) : newurl;
        path       = path.replace(host, '');
        if (location.search == '' &&
            location.hash == '' &&
            path == pathname) {
            return 1;
        }
        if (location.search == '' &&
            location.hash != '' &&
            path == pathname.substring(0, pathname.lastIndexOf('#'))) {
            return 0;
        }
        if (path.lastIndexOf('#') != -1) {
            var hpos = path.lastIndexOf('#');
            if (path.substring(0, hpos) == pathname+location.search) {
                if (path.substr(hpos) == location.hash) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
        if (path.lastIndexOf('?') != -1) {
            var spos = path.lastIndexOf('?');
            if (path.substring(0, spos) == pathname &&
                path.substr(spos) == location.search) {
                return 1;
            }
        }
        return -1;
    }

    this.pclose = function()
    {
        if (parent.opener && parent.opener.popups[this._name]) {
            parent.opener.popups[this._name].pclose();
        } else {
            if (this._win && this._win.closed) return;
            if (this._win && this._win.open) this._win.close();
        }
    }

}
