/**
 * 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 resizable
 * 2 scrollbars
 * 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
var dhtml_popup;
var popup_selection;
var popup_html_selection;
var popup_src;
var popup_rng;
function doPopup(popup_file, popup_width, popup_height, check_selection){
    if (check_selection !== false) {
        if (selection_handler_check_selection(check_selection) == false) {
            return;
        }
    }

    try {
        popup_src            = selection_handler_get_source();
        popup_rng            = selection_handler_get_range(popup_src);
        popup_selection      = selection_handler_get_selection();
        popup_html_selection = selection_handler_get_html_selection(popup_src);
    }
    catch (e) {
        ; // no wysiwyg here
    }

    dhtml_popup = dhtmlwindow.open(
            "dhtml_popup"
          , "iframe"
          , popup_file
          , "Popup"
          , "width=" + popup_width + "px," +
            "height=" + popup_height + "px," +
            "resize=1,scrolling=1,center=1"
          , "popup"
    );

    if (popup_file.indexOf("popup/upload.php") != -1) {
        dhtml_popup.onclose=function(){
            parent.location=parent.location;
            return true;
        }
    }
}

function openCmsRefInsertor(insertor_type) {
    doPopup(
          "insertor.php?avnsp=insertor&insertor_type=" + insertor_type
        , 620
        , 560
    );
}

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

// 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();
}

