fifik.window = {
    disabler:'overlay',
    windowActive:false,
    windowsOpened:0,
    windows:[],
    window:0,
    translation:[],
    cssClass:'window',

    init:function(lng){
        this.setTranslation(lng);
        if ($('#'+this.disabler).length==0) {
            $('<div></div>').attr({
                'id':this.disabler
            }).css({
                'position':'absolute',
                'top': 0,
                'left': 0,
                'z-index':100,
                'bottom':0,
                'height':'100%',
                'width':'100%'
            }).hide().prependTo('body')[0];
        }
    },

    setTranslation:function(lng){
        lng = (lng == undefined) ? 'default' : lng;
        if (lng == 'default') {
            this.translation['alert'] = 'Výstraha';
            this.translation['confirm'] = 'Potvrdenie';
            this.translation['but_yes'] = 'Áno';
            this.translation['but_no'] = 'Nie';
            this.translation['but_close'] = 'Zavrieť';
        }else if(lng == 'en'){
            this.translation['alert'] = 'Alert';
            this.translation['confirm'] = 'Cofirmation';
            this.translation['but_yes'] = 'Yes';
            this.translation['but_no'] = 'No';
            this.translation['but_close'] = 'Close';
        }
    },

    showDisabler:function(){
        var disabler = $('#'+this.disabler);
        if (disabler.length) {
            disabler.height($(document).height());
            disabler.show();
            this.windowActive = true;
        } else {
            this.init();
            var disabler = $('#'+this.disabler);
            disabler.height($(document).height());
            disabler.show();
            this.windowActive = true;
        }
    },

    hideDisabler:function(){
        if ($('#'+this.disabler).length>0) {
            $('#'+this.disabler).hide();
        }
        this.windowActive = false;
    },

    alert:function(message){
        if (!this.windowActive) {
            this.showDisabler();
            this.createAlert(message, 300, 200);
            $('#alert_ok').focus();
        }else{// ak uz je aktivne nejake okno, aj tak zobrazime alert
            this.createAlert(message, 300, 200);
            $('#alert_ok').focus();
        }
    },

    ajaxWindow:function(title,url,width,height){
        if (!this.windowActive) {
            this.showDisabler();
            this.ajaxBox('ajaxBox',title,url,width,height);
        }
    },

    close:function(id){
        if ($('#'+id).length > 0) {
            $('#'+id).remove();
        }
        this.hideDisabler();
    },

    confirm:function(message){
        if (!this.windowActive) {
            this.showDisabler();
            this.createConfirm(message,300,200);
            $('#confirm_yes').focus();
        }else{ // ak uz je aktivne nejake okno, aj tak zobrazime confirm
            this.createConfirm(message,300,200);
            $('#confirm_yes').focus();

        }
    },

    confirm_true:function(id){
        $('#'+id).remove();
        this.hideDisabler();
        CMSadmin.confirm_true();
    },

    confirm_false:function(id){
        $('#'+id).remove();
        this.hideDisabler();
    },

    topscroll:function() {
        return [
        // scrollSize X
        window.pageXOffset ? window.pageXOffset : (document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollLeft : document.body.scrollLeft),
        // scrollSize Y
        window.pageYOffset ? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)
        ];
    },

    createAlert:function(message,width,height){
        var id = "alert1";
        var topscroll = this.topscroll()[1];
        var top = ($('body').height() / 2) - ( height / 2) + topscroll;
        var left = ($('body').width() / 2) - ( width / 2);
        if (top <=0)top = 0;
        if (left <=0)left = 0;

        var win = $('<div></div>').attr({
            'class': this.cssClass,
            'id':id
        }).css({
            'width':width+'px',
            'position':'absolute',
            'z-index':200,
            'top' : top+'px',
            'left': left+'px'
        });

        var head = "<table id='"+ id +"_row1' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_nw'></td>\
	          <td class='"+ this.cssClass +"_n'><h6><span>"+this.translation['alert']+"</span></h6></div></td>\
	          <td class='"+ this.cssClass +"_ne'></td>\
	        </tr>\
	      </table>";

        var bottom = "<table id='"+ id +"_row3' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_sw'></td>\
	          <td class='"+ this.cssClass +"_s' style='width:"+(width-30)+"px;'>\
                    <div class=\"alertButtonsPreHolder\"><div class=\"alertButtonsHolder\" style=\"width:"+(width-30+14)+"px;\">\
                    <table cellpadding=\"0\" cellspacing=\"2\" class=\"alertButtons\">\
                        <tr>\
                            <td><a href='javascript:;' class='"+ this.cssClass +"_but ok' id='alert_ok' onclick=\"fifik.window.close('"+id+"');\">"+this.translation['but_close']+"</a></td>\
                        </tr>\
                    </table>\
                    </div></div>\
                    </td>\
	          <td class='"+ this.cssClass +"_se'></td>\
	        </tr>\
	      </table>";


        var content ="<div id=\"" + id + "_content\" class=\"" +this.cssClass + "_text\">" + message +"</div>";
        var closeDiv = "<div class='"+ this.cssClass +"_close' id='"+ id +"_close' onclick='fifik.window.close(\""+ id +"\")'>Zavrieť</div>";

        win.html(closeDiv +head+ "\
	      <table id='"+ id +"_row2' class=\"mid table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_w'></td>\
	            <td id='"+ id +"_table_content' class='"+ this.cssClass +"_content' valign='top'>\
					" + content + "\
				</td>\
	          <td class='"+ this.cssClass +"_e'></td>\
	        </tr>\
	      </table>" +bottom);
        $('body').append(win);
    },

    createConfirm: function(message,width,height) {
        var id = "confirm1";
        var topscroll = this.topscroll()[1];
        var top = ($('body').height() / 2) - ( height / 2) + topscroll;
        var left = ($('body').width() / 2) - ( width / 2);
        if (top <=0)top = 0;
        if (left <=0)left = 0;

        var dragID = 'confirmDRAG';

        var win = $('<div></div>').attr({
            'class': this.cssClass,
            'id':id
        }).css({
            'width':width+'px',
            'position':'absolute',
            'z-index':200,
            'top' : top+'px',
            'left': left+'px'
        });

        var head = "<table id='"+ id +"_row1' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_nw'></td>\
	          <td class='"+ this.cssClass +"_n'><h6><span>"+this.translation['confirm']+"</span></h6></div></td>\
	          <td class='"+ this.cssClass +"_ne'></td>\
	        </tr>\
	      </table>";

        var bottom = "<table id='"+ id +"_row3' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_sw'></td>\
	          <td class='"+ this.cssClass +"_s' style='width:"+(width-30)+"px;'>\
                    <div class=\"confirmButtonsPreHolder\"><div class=\"confirmButtonsHolder\" style=\"width:"+(width-30+14)+"px;\">\
                    <table cellpadding=\"0\" cellspacing=\"2\" class=\"confirmButtons\">\
                        <tr>\
                            <td><a href='javascript:;' class='"+ this.cssClass +"_but false' id='confirm_no' onclick=\"this.confirm_false('"+id+"');\">"+this.translation['but_no']+"</a></td>\
                            <td><a href='javascript:;' class='"+ this.cssClass +"_but true' id='confirm_yes' onclick=\"this.confirm_true('"+id+"');\">"+this.translation['but_yes']+"</a></td>\
                        </tr>\
                    </table>\
                    </div></div>\
                    </td>\
	          <td class='"+ this.cssClass +"_se'></td>\
	        </tr>\
	      </table>";

        var content ="<div class=\"" +this.cssClass + "_text\">" + message +"</div>";
        var closeDiv = "<div class='"+ this.cssClass +"_close' id='"+ id +"_close' onclick='fifik.window.close(\""+ id +"\")'> </div>";

        win.html(closeDiv +head+ "\
	      <table id='"+ id +"_row2' class=\"mid table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_w'></td>\
	            <td id='"+ id +"_table_content' class='"+ this.cssClass +"_content' valign='top'>\
					" + content + "\
				</td>\
	          <td class='"+ this.cssClass +"_e'></td>\
	        </tr>\
	      </table>" +bottom);
        $('body').append(win);
    },

    iframeBox: function(id,header,url,top,left,width,height,callBackOnFinish) {
        this.showDisabler();
        var topscroll = this.topscroll()[1];
        if (top < 0 ) top = 0;

        var iframewidth,iframeheight = 0;

        iframewidth = width-12;
        iframeheight = height-40;

        var head = "<table id='"+ id +"_row1' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_nw'></td>\
	          <td class='"+ this.cssClass +"_n'></td>\
	          <td class='"+ this.cssClass +"_ne'></td>\
	        </tr>\
	      </table>";
        if (header != '') {
            head = "<table id='"+ id +"_row1' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_nw'></td>\
	          <td class='"+ this.cssClass +"_n'><h6><span>"+header+"</span></h6></div></td>\
	          <td class='"+ this.cssClass +"_ne'></td>\
	        </tr>\
	      </table>";
        }

        var win = $('<div></div>').attr({
            'class': this.cssClass,
            'id':id
        }).css({
            'width':width+'px',
            'height':height+'px',
            'position':'absolute',
            'z-index':200,
            'top' : top+'px',
            'left': left+'px'
        });

        var winloader = "<div id=\"winloading\"><img style=\"position:relative; top:4px;\" src=\"images/admin/ajax-loader1.gif\" widht=\"20\" height=\"20\" /><a href='javascript:;' id=\"cmsloading_text\" class=\"loader\">Loading</a></div>";
        var iframe ="<iframe onload=\""+callBackOnFinish+"\" id=\"" + id + "_content\" class=\"" +this.cssClass + "_content\" src=\""+url+"\" frameborder=\"0\" width=\""+iframewidth+"\" height=\""+iframeheight+"\">"+winloader+"</iframe>";
        var closeDiv = "<div class='"+ this.cssClass +"_close' id='"+ id +"_close' onclick='fifik.window.close(\""+ id +"\")'>Zavrieť</div>";

        win.html(closeDiv + head+ "\
	      <table id='"+ id +"_row2' class=\"mid table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_w'></td>\
	            <td id='"+ id +"_table_content' class='"+ this.cssClass +"_content' valign='top'>\
			" + iframe + "\
		    </td>\
	          <td class='"+ this.cssClass +"_e'></td>\
	        </tr>\
	      </table>\
	        <table id='"+ id +"_row3' class=\"bot table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_sw'></td>\
	            <td class='"+ this.cssClass +"_s'><div class='status_bar'><span style='float:left; width:1px; height:1px'></span></div></td>\
	            <td class='"  + this.cssClass + "_se'></td>\
	        </tr>\
	      </table>");
        $('body').append(win);
    },

    ajaxBox:  function(id,header,url,top,left,width,height,params){
        this.showDisabler();
        var iStageWidth = $(document).width();
        if (top < 0 ) top = 0;
        width += 12;
        height += 10;
        if (iStageWidth < (left + width)) {
            left = iStageWidth - width;
        }
        var win = $('<div></div>').attr({
            'class': this.cssClass,
            'id':id
        }).css({
            'width':width+'px',
            'height':height+'px',
            'position':'absolute',
            'z-index':200,
            'top' : top+'px',
            'left': left+'px'
        });

        var head = "<table id='"+ id +"_row1' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_nw'></td>\
	          <td class='"+ this.cssClass +"_n'></td>\
	          <td class='"+ this.cssClass +"_ne'></td>\
	        </tr>\
	      </table>";
        if (header != '') {
            head = "<table id='"+ id +"_row1' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_nw'></td>\
	          <td class='"+ this.cssClass +"_n'><h6><span>"+header+"</span></h6></div></td>\
	          <td class='"+ this.cssClass +"_ne'></td>\
	        </tr>\
	      </table>";
        }

        var content = '<div style="padding:'+(Math.round(height/2 - 32/2))+'px 0 0 '+(Math.round(width/2 - 32/2))+'px"><img src="/www/img/red_loader.gif" width="32" height="32" alt="loading" /></div>';
        var contentHolder ="<div id=\"" + id + "-window-content\" class=\"" +this.cssClass + "_body\" style=\"height:"+height+"px\">" + content +"</div>";

        var closeDiv = "<a title='Close' class='"+ this.cssClass +"_close' id='"+ id +"_close' onclick='fifik.window.close(\""+ id +"\")'>Zavrieť</a>";

        if (id=='newLoginBox') {
            closeDiv = '';
        }

        win.html(closeDiv + head+ "\
	      <table id='"+ id +"_row2' class=\"mid table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_w'></td>\
	            <td id='"+ id +"_table_content' class='"+ this.cssClass +"_content' valign='top'>\
" + contentHolder + "\
				</td>\
	          <td class='"+ this.cssClass +"_e'></td>\
	        </tr>\
	      </table>\
	        <table id='"+ id +"_row3' class=\"bot table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_sw'></td>\
	            <td class='"+ this.cssClass +"_s'><div id='"+ id +"_bottom' class='status_bar'><span style='float:left; width:1px; height:1px'></span></div></td>\
	            <td class='"  + this.cssClass + "_se'></td>\
	        </tr>\
	      </table>\
	      <div class='"  + this.cssClass + "PRELOADER1'><div class='"  + this.cssClass + "PRELOADER2'></div></div>\
	     ");
        $('body').append(win);

        $.ajax({
            url:url,
            type: 'POST',
            data: params,
            success: function(msg) {
                $('#'+id + "-window-content").html(msg);
            }
        });
    },

    createBox: function(id,header,content,top,left,width,height){
        this.showDisabler();
        var iStageWidth = $(document).width();
        if (top < 0 ) top = 0;
        width += 12;
        height += 30;
        if (iStageWidth < (left + width)) {
            left = iStageWidth - width;
        }
        var win = $('<div></div>').attr({
            'class': this.cssClass,
            'id':id
        }).css({
            'width':width+'px',
            'height':height+'px',
            'position':'absolute',
            'z-index':200,
            'top' : top+'px',
            'left': left+'px'
        });

        var head = "<table id='"+ id +"_row1' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_nw'></td>\
	          <td class='"+ this.cssClass +"_n'></td>\
	          <td class='"+ this.cssClass +"_ne'></td>\
	        </tr>\
	      </table>";
        if (header != '') {
            head = "<table id='"+ id +"_row1' class=\"top table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_nw'></td>\
	          <td class='"+ this.cssClass +"_n'><h6><span>"+header+"</span></h6></div></td>\
	          <td class='"+ this.cssClass +"_ne'></td>\
	        </tr>\
	      </table>";


        }

        var contentHolder ="<div id=\"" + id + "-window-content\" class=\"" +this.cssClass + "_body\">" + content +"</div>";
        var closeDiv = "<a title='Close' class='"+ this.cssClass +"_close' id='"+ id +"_close' onclick='fifik.window.close(\""+ id +"\")'>Zavrieť</a>";

        if (id=='newLoginBox') {
            closeDiv = '';
        }

        win.html(closeDiv + head+ "\
	      <table id='"+ id +"_row2' class=\"mid table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_w'></td>\
	            <td id='"+ id +"_table_content' class='"+ this.cssClass +"_content' valign='top'>\
" + contentHolder + "\
				</td>\
	          <td class='"+ this.cssClass +"_e'></td>\
	        </tr>\
	      </table>\
	        <table id='"+ id +"_row3' class=\"bot table_window\">\
	        <tr>\
	          <td class='"+ this.cssClass +"_sw'></td>\
	            <td class='"+ this.cssClass +"_s'><div id='"+ id +"_bottom' class='status_bar'><span style='float:left; width:1px; height:1px'></span></div></td>\
	            <td class='"  + this.cssClass + "_se'></td>\
	        </tr>\
	      </table>\
	      <div class='"  + this.cssClass + "PRELOADER1'><div class='"  + this.cssClass + "PRELOADER2'></div></div>\
	     ");
        $('body').append(win);
    }
}
