/**
 * ポップアップウィンドウクラス
 * 
 * 使用例 (1)<br />
 * var pop1 = new PopupWindow('popup.html');
 * pop1.open({width:500,height:400});
 *
 * 使用例 (2)<br />
 * <a href="popup.html" onclick="(new PopupWindow(this.href)).open({width:200,height:'max'});">open</a>
 * 
 */
var PopupWindow = function(url) {
    if (url)
        this.url = url;
    this.popupwindow = null;
    this.scrollbars = 'yes';
    this.resizable = 'yes';
    this.copyhistory = 'yes';
    this.width = null;
    this.height = null;
    this.left = 'center';
    this.top = 'center';
};

PopupWindow.prototype.open = function(argv) {
    
    var option = {};
    
    if (!argv)
        argv = {};
    
    var lurl = (argv.url) ? argv.url : this.url;
    
    option.width = (argv.width) ? argv.width : this.width;
    option.height = (argv.height) ? argv.height : this.height;
    
    if (option.width && option.width == 'max') 
        option.width = screen.width;
    if (option.height && option.height == 'max') 
        option.height = screen.height;
    
    option.scrollbars = (argv.scrollbars) ? argv.scrollbars : this.scrollbars;
    option.resizable = (argv.resizable) ? argv.resizable : this.resizable;
    option.copyhistory = (argv.copyhistory) ? argv.copyhistory : this.copyhistory;
    
    option.left = (argv.left!=undefined) ? argv.left : this.left;
    if (option.left == 'center')
        option.left = (!isNaN(option.width)) ? (screen.width - option.width) / 2 : null;
        
    option.top = (argv.top!=undefined) ? argv.top : this.top;
    if (option.top == 'center')
        option.top = (!isNaN(option.height)) ? (screen.height - option.height) / 2 : null;
    
    if (option.left)
        option.screenX = option.left;
    if (option.top)
        option.screenY = option.top;
    
    if (this.popupwindow && !this.popupwindow.closed) {
        this.popupwindow.close();
    }
    
    var optionStr = '';
    for (var key in option) {
        if (option[key]!=null)
            optionStr += key+'='+option[key]+',';
    }
    
    // Output to console
    if (window.console) {
        window.console.log(optionStr);
        window.console.log('popup::'+lurl);
    }
    
    this.popupwindow = window.open(lurl, PopupWindow.getNewWindowName(), optionStr);
    return this.popupwindow;
    
};

PopupWindow.sequence = 0;
PopupWindow.getNewWindowName = function(){
    return 'win'+(PopupWindow.sequence++);
};

