/**
 * 標準のalertとconfirmをYAHOO.widget.SimpleDialogを使ったものに置きかえるライブラリ
 *
 */
PadPlate.namespace('util');

(function() { 
    new PadPlate.util.Loader({
        require: ["container","dom"], // what to load
        onSuccess: function(o) {
            installAlert();
            installConfirm();
            installIframeDlg();
        }
    });

    /**
     * 標準のalertとconfirmをyui-panelを使ったものに置きかえる
     */
	function installAlert(){
        // setup alert
        window._alert = window.alert;
        window.alert = function( message, caption, callBackOnOK, callBackOnAbort ){
            if( !caption )
                caption = "Warning!";
            
            if( typeof(callBackOnOK) != 'undefined' )
                onOK = callBackOnOK;
            else
                onOK = function(){ this.hide(); this.destroy(); }
            
            if( typeof(callBackOnAbort) != 'undefined' )
                onAbort = callBackOnAbort;
            else
                onAbort = function(e){ simple_dlg.hide(); simple_dlg.destroy(); }
            var simple_dlg = new YAHOO.widget.SimpleDialog('alert', 
             { width: "300px",
               fixedcenter: true,
               visible: false,
               close: true,
               modal: true,
               underlay: "shadow",
               text: message,
               icon: YAHOO.widget.SimpleDialog.ICON_WARN,
               keylisteners: new YAHOO.util.KeyListener(document, { keys : 27 }, {
                   fn: onAbort,
                   scope:simple_dlg,
                   correctScope:true
               }),
               buttons: [ { text:"OK", handler: onOK } ]
             } );
            simple_dlg.setHeader(caption);
            simple_dlg.render(document.body);
            simple_dlg.show();
        }
    }
    
    /**
     * window.confirmをdialogウィジェットベースのものに置きかえる
     * 従来のwindow.confirmは_confirmで呼び出せる
     */
    function installConfirm(){
        window._confirm = window.confirm;
        window.confirm = function( message, caption, callBack, opt ){
            if( !caption )
                caption = "Question";
            
            if( typeof(callBack) == 'undefined' )
                callBack = function(){};
            
            var onOK = function(){ callBack("ok"); this.hide(); /** setTimeout("this.destroy()",9000);**/ this.destroy(); }
            var onCancel = function(){ callBack("cancel"); this.hide(); this.destroy(); }
            
            var confirmId = 'confirm';
            var simple_dlg = new YAHOO.widget.SimpleDialog(confirmId, 
             { width: "300px",
               fixedcenter: true,
               visible: false,
               close: true,
               modal: true,
               underlay: "shadow",
               text: message,
               icon: YAHOO.widget.SimpleDialog.ICON_HELP,
               keylisteners: new YAHOO.util.KeyListener(document, { keys : 27 }, {
                   fn: onCancel,
                   scope:simple_dlg,
                   correctScope:true
               }),
               buttons: [ { text:"はい", handler: onOK }, { text:"いいえ", handler: onCancel } ]
             } );
            simple_dlg.setHeader(caption);
            simple_dlg.render(document.body);
            if( typeof opt != 'undefined' && typeof opt.className == 'string' ){
                YAHOO.util.Dom.addClass(document.getElementById(confirmId), opt.className);
            }
            simple_dlg.show();
        };
    }
    
    /**
     * iframeで別ページを表示するダイアログのインストール
     */
    function installIframeDlg(){
        /**
         * @param name ダイアログ名
         * @param caption タイトル
         * @param url iframeで表示したいURL
         * @param opt YAHOO.widget.SimpleDialogで渡すoptと同じ内容
         *
         * nameはページ上でユニークになるので同じnameでインスタンスを作った場合、同じダイアログを操作することになります。
         * optはYAHOO.widget.SimpleDialogですが、width、height以外は特に指定する必要はありません。
         */
        PadPlate.util.IframeDlg = function(name, caption, url, opt){
            this.init.apply(this, arguments);
        };
        PadPlate.util.IframeDlg.prototype = {
            init: function(name, caption, url, opt){
                var onOK = function(){ this.hide(); this.destroy(); };

                // 設定の初期値を代入
                if( typeof opt == 'undefined')
                    opt = {};
                opt.text = '<iframe style="width:780px; height:440px; margin-top:none; padding:none;" src="'+url+'" />';
                opt.fixedcenter = opt.fixedcenter || true;
                opt.close          = opt.close || true;
                opt.modal        = opt.modal || true;
                opt.buttons     = opt.buttons || [{ text:"閉じる", handler: onOK }];
                opt.visible        = false;
                
               //var iframeDlg = new YAHOO.widget.SimpleDialog(name,opt);
               var iframeDlg = new YAHOO.widget.SimpleDialog(name,{width:"800px",height:"500px",fixedcenter:true, modal:true});
               
               var caption="<img src='../images/popupHeaderImg.gif' />";
                iframeDlg.setHeader("<div style='width:100%; background-image:none; background-color:#eeeeee; height:20px; padding-top:5px;'>"+caption+"</div>");
                iframeDlg.setBody(opt.text);
                iframeDlg.setFooter("");
                iframeDlg.render(document.body);
                
                var iframe = iframeDlg.element.getElementsByTagName('iframe')[0];
                var Dom = YAHOO.util.Dom;
                //Dom.setStyle( iframe, 'width', parseInt(opt.width)-20+'px');
                 Dom.setStyle( iframe, 'height', 440+'px');
                Dom.setStyle( iframe, 'margin-top', 1+'px');
                
                iframeDlg.show();
            }
        };
    }
})(); 
