/**
 * popup: this function is commonly used by each popup related functions.
 *
 * Some arguments may be added to specify some particular features of
 * the window to create.
 *
 * When call this function, the order of the given additional arguments
 * has to respect the following - yet arbitrary - order:
 *
 * [0]popup_file, [1]popup_width, [2]popup_height, [3]popup_top,
 * [4]popup_left, [5]popup_title, [6]popup_options
 *
 * Based on the unix chmod values but for 4 available settings, the options
 * arguments is used to specify additional features like the menubar option,
 * the scrollable option, etc. Each option has a value that correspond to
 * a bit position from right to popup_left:
 *
 *    1 : popup_scrollbars (int value is 1)
 *   10 : popup_resizable (int value is 2)
 *  100 : popup_status    (int value is 4)
 * 1000 : popup_menubar   (int value is 8)
 *
 * So, the following integer values gives: 
 *
 * 1 scrollable
 * 2 resizable
 * 3 scrollable and resizable
 * 4 satus
 * 5 scrollable and status
 * 6 resizable and status
 * 7 scrollable resizable and status
 * 8 menubar
 * 9 scrollable and menubar
 * ...
 * 
 */
function popup(popup_file, popup_width, popup_height){

    var popup_window;
    var popup_left;
    var popup_top;

    var popup_title     = '';

    var popup_resizable  = 0;
    var popup_scrollbars = 0;
    var popup_status     = 0;
    var popup_menubar    = 0;

    if (arguments[3]) { popup_top       = arguments[3]; }
    if (arguments[4]) { popup_left      = arguments[4]; }
    if (arguments[5]) { popup_title     = arguments[5]; }

    if (arguments[6]) {
        if (arguments[6] & 1) { popup_resizable  = 1; }
        if (arguments[6] & 2) { popup_scrollbars = 1; }
        if (arguments[6] & 4) { popup_status     = 1; }
        if (arguments[6] & 8) { popup_menubar    = 1; }
    }

    popup_left = parseInt((screen.availWidth/2) - (popup_width/2));
    popup_top  = parseInt((screen.availHeight/2) - (popup_height/2));

    var popup_features =
                "width="         + popup_width      + 
                ",height="       + popup_height     + 
                ",resizable="    + popup_resizable  +
                ",scrollbars="   + popup_scrollbars +
                ",menubar="      + popup_menubar    +
                ",status="       + popup_status     +
                ",left="         + popup_left       +
                ",top="          + popup_top        + 
                ",screenX="      + popup_left       + 
                ",screenY="      + popup_top;

    popup_window = window.open(popup_file, popup_title, popup_features);

}

// popup to view sing image
function popImg(popup_file, popup_width, popup_height){
    popup(popup_file, popup_width, popup_height);
}

// popup with fixed size
function doPopup(popup_file, popup_width, popup_height){
    popup(popup_file, popup_width, popup_height, 300, 300);
}

// popup with scroll abaility and fixed size
function doScrollablePopup(popup_file, popup_width, popup_height){
    popup(popup_file, popup_width, popup_height, 300, 300, '', 1);
}

// popup with title
function popupWithTitle(popup_file, popup_title, popup_width, popup_height){
    popup(popup_file, popup_width, popup_height, false, false, popup_title);
}

// reload self document
function reloadIt(opoself) {
    if (arguments[0])
        window.opener.document.location.reload();
    else
        window.self.document.location.reload();
}
