/* POPUPS */

var sh_popupClass = new Class.create({
    content: '',
    title: null,
    ret:null,
    type:'',
    width:300,
    position:'top',
    cursorChanged:false,
    onconfirmok: function(){},
    onconfirmcancel: function(){},
    onpromptok:function(){},
    onpromptcancel:function(){},
    onalertshown:function(){},
    initialize: function() {
    },
    _setParams: function(content,params){
        this.content = content;
        if(params != undefined){
            if(params.width != undefined && params.width > 20){
                this.width = params.width;
            }
            if(params.title != undefined){
                this.title = params.title;
            }
            if(params.onconfirmok != undefined){
                this.onconfirmok = params.onconfirmok;
            }
            if(params.onconfirmcancel != undefined){
                this.onconfirmcancel = params.onconfirmcancel;
            }
            if(params.onpromptok != undefined){
                this.onpromptok = params.onpromptok;
            }
            if(params.onpromptcancel != undefined){
                this.onpromptcancel = params.onpromptcancel;
            }
            if(params.onalertshown != undefined){
                this.onalertshown = params.onalertshown;
            }
            if(params.onafterhide != undefined){
                this.onafterhide = params.onafterhide;
            }
            if(params.icon != undefined){
                this.icon = params.icon;
            }
        }
    },
    resizeToContent: function(){
        Modalbox.resizeToContent();
    },
    wait: function(content, params) {
        this.type = 'wait';
        this._setParams(content, params);
        this.cursorChanged = true;
        document.body.style.cursor = 'wait';
        if(this.title == null){
            this.title = document.title;
        }
        if(this.icon != undefined){
            this.title = '<img src="'+this.icon+'"/>'+this.title;
        }
        content = '<div>'+this.content+'</div>';
        Modalbox.show(content, {
            title: this.title,
            width: this.width,
            closeValue: '',
            overlayClose: false
        });
    },
    message: function(div, params){
        this.type = 'message';
        var content = '';
        if($(div) == '[object HTMLDivElement]'){
            content = $(div).innerHTML;
        }else{
            content = div;
        }
        this._setParams('<div>'+content+'</div>', params);
        if(this.title == null){
            this.title = document.title;
        }
        if(this.icon != undefined){
            this.title = '<img src="'+this.icon+'"/>'+this.title;
        }

        Modalbox.show(this.content, {
            title: this.title,
            width: this.width,
            closeValue: '',
            overlayClose: true
        });
    },
    hide: function() {
        if(this.cursorChanged){
            document.body.style.cursor = 'auto';
            this.cursorChanged = false;
        }
        Modalbox.hide();
    },
    alert: function(content, params) {
        this.type = 'alert';
        this._setParams(content, params);
        if(this.title == null){
            this.title = document.title;
        }
        if(this.icon != undefined){
            this.title = '<img src="'+this.icon+'"/>'+this.title;
        }
        content = '<div>'+this.content+'</div><div class="popup_buttons"><input class="popup_button popup_button_ok" type="button" onclick="sh_popup.setRet(true);" value="OK"/></div>';
        Modalbox.show(content, {
            title: this.title,
            width: this.width,
            overlayClose: false
        });
    },
    confirm: function(content, params){
        this.type='confirm';
        this._setParams(content, params);
        if(this.title == null){
            this.title = document.title;
        }
        if(this.icon != undefined){
            this.title = '<img src="'+this.icon+'"/>'+this.title;
        }else{
            this.title = '<img src="/images/shared/icons/help.png"/>'+this.title;
        }
        content = '<div>'+this.content+'</div><br /><br />';
        content += '<div class="popup_buttons"><input onclick="sh_popup.setRet(true);" type="button" class="popup_button popup_button_ok" value="OK"/>&#160;<input type="button" value="Annuler" class="popup_button popup_button_cancel" onclick="sh_popup.setRet(false);"/></div>';
        
        Modalbox.show(content, {
            title: this.title,
            width: this.width,
            overlayClose: false,
            closeValue: '',
            onShow:function(){
            },
            afterHide:function(){
            }
        });
    },
    prompt: function(content, value, params){
        this.type='prompt';
        this._setParams(content, params);
        if(this.title == null){
            this.title = document.title;
        }
        if(this.icon != undefined){
            this.title = '<img src="'+this.icon+'"/>'+this.title;
        }
        content = '<div>'+this.content+'</div>';
        content += '<div class="popup_buttons"><input id="sh_popup_promptInput" value="'+value+'"/><br /><br />\
<input onclick="sh_popup.setRet($(\'sh_popup_promptInput\').value);" type="button" class="popup_button popup_button_ok" value="OK"/>&#160;\
<input type="button" value="Annuler" class="popup_button popup_button_cancel" onclick="sh_popup.setRet(false);"/></div>';

        Modalbox.show(content, {
            title: this.title,
            width: this.width,
            overlayClose: false,
            closeValue: '',
            onShow:function(){
            },
            afterHide:function(){
            }
        });
    },
    setPromptValue: function(value){
        $('sh_popup_promptInput').value = value;
    },
    setRet: function(ret){
        this.ret = ret;
        if(this.type == 'alert' && this.onalertshown != undefined){
            this.onalertshown();
        }else if(this.type == 'confirm'){
            if(this.ret && this.onconfirmok != undefined){
                this.onconfirmok();
            }else if(this.onconfirmcancel != undefined){
                this.onconfirmcancel();
            }
        }else if(this.type == 'prompt'){
            if(this.ret != false && this.onpromptok != undefined){
                this.onpromptok(this.ret);
            }else if(this.onpromptcancel != undefined){
                this.onpromptcancel();
            }
        }
        Modalbox.hide();
    }
});

var sh_popup = new sh_popupClass();

/* POPUPS END */

/* POPINS */
var sh_popinClass = new Class.create({
    url:'',
    className:"alphacube",
    width:500,
    okLabel:'Close',
    _setParams: function(params){
        if(params != undefined){
            if(params.width != undefined && params.width > 20){
                this.width = params.width;
            }
            if(params.url != undefined){
                this.url = params.url;
            }
            if(params.okLabel != undefined){
                this.okLabel = params.okLabel;
            }
            if(params.className != undefined){
                this.className = params.className;
            }
        }
    },
    show: function(content, params) {
        this._setParams(params);

        Dialog.alert(
        {
            url: this.url
            },

            {
            className: this.className,
            width:this.width,
            okLabel: this.okLabel
            }
        );
    }
});

var sh_popin = new sh_popinClass();

/* END OF POPINS */

var sh_loaded_css = new Array();
function sh_load_css(cssFile){
    if(sh_loaded_css[cssFile] == undefined){
        Scriptaculous.load('../'+cssFile);
        //alert('../'+cssFile);
        /*
        var file=document.createElement("link")
        file.setAttribute("rel", "stylesheet")
        file.setAttribute("type", "text/css")
        file.setAttribute("href", cssFile)
        $$("head")[0].appendChild(file)*/
        sh_loaded_css[cssFile] = true;
    }
}

sh_load_css('window/themes/alert.css');
//sh_load_css('window/themes/alert.css');


/* Tab arrays */
Event.observe(window, 'load', function() {

    /* TAB GROUPS */
    $$('.tabGroup_containerTitle').each(function(el){
        Event.observe(el, 'click', function(clicked){
            var theClass = el.ancestors()[0].id;
            $$('.'+theClass).each(function(el){
                el.removeClassName('selected');
            });
            $$('.'+theClass+'_titles').each(function(el){
                el.removeClassName('selected');
            });
            $(el.id).addClassName('selected');
            $(el.id+'_content').addClassName('selected');
        });
    });
});


/* taken from http://rule52.com/2008/06/css-rule-page-load/ until -----
var CSSRules = function() {
    // create a stylesheet
    var stylesheet = document.createElement("style");
    stylesheet.setAttribute("type", "text/css");
    document.getElementsByTagName("head")[0].appendChild(stylesheet);
    stylesheet = document.styleSheets[document.styleSheets.length - 1];
    return {
        add: function(selector, rule) {
            // the Microsoft method
            if (stylesheet.addRule) {
                stylesheet.addRule(selector, rule, 0);
            // the standard method
            } else if (stylesheet.insertRule) {
                // safari 2 apparently ignores this
                stylesheet.insertRule(selector.concat('{' + rule + '}'), 0);
            }
        }
    };
}();*/
