
warning = {
    zIndex: 30,
    show: function(txt,title){
        var title = title||'Сообщение!';
        var theHeight = 40;
        var theWidth  = 200;
        var overlay= createOverlay(warning.zIndex++);
        var wrn =$(Builder.node('div',{
                                'class':'warning'
                                },[
                                   Builder.node(
                                                'div',
                                                {'class':'warning-title'},[
                                                        Builder._text(title)
                                                                                                    ]),
                                                Builder.node('div',{},[
                                                    Builder.node('div',{
                                                        'class':'warning-body'
                                                    },[Builder._text(txt)])])]
                            ));

        var w = document.viewport.getWidth();
        var h = document.viewport.getHeight();
        var s = document.viewport.getScrollOffsets().top;

        var vPosition = h/2+s - theHeight/2;
        var hPosition = w/2 - theWidth/2;

        wrn.setStyle('position: absolute; z-index: '+warning.zIndex
                     +'; top:' + vPosition + 'px; left:' + hPosition + 'px;'
                     + ' width: ' + theWidth + 'px; cursor: pointer;'

                     );




        warning.zIndex++;
         new Effect.Opacity(wrn, { from: 0.0, to: 1.0, duration: 1});
        setTimeout(function(){document.body.appendChild(wrn);},100);


        function remove(){
            var o = overlay;
            var w = wrn;
               new Effect.Opacity(w, { from: 1.0, to: 0.0, duration: 1.2,afterFinish:function(){
                                        w.parentNode.removeChild(w);
                                        o.remove();

                                    }});

        }




        //new Draggable(wrn);
        wrn.onclick = remove;
        overlay.onclick = remove;




    }
}

message = function(text,title){
    this.show(text,title);
}

message.prototype = {
    show: function(txt,title){
        var title = title||'Сообщение!';
        var theHeight = 40;
        var theWidth  = 200;

        var wrn =$(Builder.node('div',{
                                'class':'warning'
                                },[
                                   Builder.node(
                                                'div',
                                                {'class':'warning-title'},[
                                                        Builder._text(title)
                                                                                                    ]),
                                                Builder.node('div',{},[
                                                    Builder.node('div',{
                                                        'class':'warning-body'
                                                    },[Builder._text(txt)])])]
                            ));

        var w = document.viewport.getWidth();
        var h = document.viewport.getHeight();
        var s = document.viewport.getScrollOffsets().top;

        var vPosition = h/2+s - theHeight/2;
        var hPosition = w/2 - theWidth/2;

        wrn.setStyle('position: absolute; z-index: '+warning.zIndex
                     +'; top:' + vPosition + 'px; left:' + hPosition + 'px;'
                     + ' width: ' + theWidth + 'px; cursor: pointer;'

                     );




        warning.zIndex++;




         new Effect.Opacity(wrn, { from: 0.0, to: 1.0, duration: 0.5,afterFinish: function(){
            var w = wrn;
               new Effect.Opacity(w, { from: 1.0, to: 0.0, duration: 1.5,afterFinish:function(){
                                        w.parentNode.removeChild(w);
              //                          o.remove();

                                    }});
            }});
        setTimeout(function(){document.body.appendChild(wrn);},100);









    }
}




function createOverlay(zIndex){

    zIndex = zIndex||5;

    var overlay = $(document.createElement('div'));
            /*  overlay.setStyle('opacity:0.4; filter:alpha(opacity=40, style=0);position: absolute; top: 0; left: 0; z-index: '+zIndex+'; width: 100%; height: 2000px; background-color: #88A; ');*/
            overlay.className = 'overlayer';
            overlay.style.zIndex = zIndex;

    document.body.appendChild(overlay);
    overlay.remove = function(){
        //console.log(' Removing overlay');
        var ov = overlay;
        try{document.body.removeChild(ov);}catch(e){}
    }

    return overlay;

}


function dateFormat(theDate){

   // theDate = parseInt(theDate);
    theDate = new Date(theDate);

    var day      = theDate.getDate();
    var month    = theDate.getMonth();
    var year     = theDate.getFullYear();
    var hour     = theDate.getHours();
    var minutes  = theDate.getMinutes();

    return day + '.' + month + '.' + year + ' ' + hour + ':' + minutes;

}



function buildField(type,name,caption,value,id){
    var type = type||text;
    var name = name||type+getRandom(10000);
    var value = value||'';
    var id=id||name+getRandom(10000);
    var caption = caption||'';
    return Builder.node('div',{'class':'comment-row'},[
                            Builder.node('span',{'class':'comment-caption'},caption+':'),
                            Builder.node('input',{'class':'comment-input',
                                         name:name,id:id,type:type,value:value
                                         })
            ]);

}

function getRandom(num){
    var rnd = Math.round(Math.random()*num)+1;
    return rnd;
}


function jWindow(confObject){

    this.body=document.createElement('div');

    for(i in confObject){
        this[i] = confObject[i];
    }
    this.init();
}

jWindow.prototype = {
    id:             '',
    'class':        'warning',
    bodyclass:      'warning-body',
    captionclass:   'warning-title',
    caption:        '',
    modal:          false,
    draggable:      false,
    closable:       true,
    dom:            null,
    body:           null,
    height:         100,
    width:          200,
    duration:       0.5,


    /**
     * Observers
     **/
    onCreate: function(){},
    afterClose: function(){},
    beforeClose: function(){},

    /**
     * initialization function
     */
    init: function(){
        if(typeof(this.body)=='string'){
            var ab = $(document.createElement('div'));
            ab.innerHTML = this.body;
            this.body=ab;
        }

        var win = this;

        win.dom = $(Builder.node('div',{'class':win['class']},[
                                   Builder.node('div',{'class':win.captionclass},[Builder.node(
                                                'div',
                                                {'class':'x-caption'},win.caption)]),
                                                Builder.node('div',{'class':win.bodyclass},
                                                                 [win.body]

                                                )
                    ]));

        var w = document.viewport.getWidth();
        var h = document.viewport.getHeight();
        var s = document.viewport.getScrollOffsets().top;

        var vPosition = h/2+s - this.height/2;
        var hPosition = w/2 - this.width/2;
        warning.zIndex+=3; // prepare zIndex
        this.dom.setStyle('position: absolute; z-index: '+ warning.zIndex
                     +'; top:' + vPosition + 'px; left:' + hPosition + 'px;'
                     + ' width: ' + this.width + 'px; height:'+ this.height
                     +'px; cursor: pointer;'
                     );

        this.setOptions();

        new Effect.Opacity(this.dom, { from: 0.0, to: 1.0, duration: this.duration});
        var t = this.dom;
        var th = this;
        setTimeout(function(){document.body.appendChild(t);
                   th.onCreate(th);
                   },30);
        //this.onCreate(this);

    },
    setOptions: function(){
        if(this.draggable){
            new Draggable(this.dom);
        }
        if(this.modal){
            this.overlay = createOverlay(warning.zIndex-1);
        }
        if(this.closable){
            var caption = $(this.dom.firstChild);
            var x = Builder.node('div',{'class':'close-button'});
            caption.appendChild(x);
        }
        var wind = this;
        x.onclick=function(){wind.close();};
    },
    close: function(){
        this.beforeClose(this);
        var w=this.dom;
        var o = this.overlay||document.createElement('div');
        new Effect.Opacity(w, { from: 1.0, to: 0.0, duration: 0.8,
                           afterFinish:function(){
                                        w.parentNode.removeChild(w);
                                       o.remove();
                                    }});
        this.afterClose(this);
        this.dom = null;
    }
}






































function showLoader(){
    var w = document.viewport.getWidth();
    var h = document.viewport.getHeight();
    var s = document.viewport.getScrollOffsets().top;
    var overlay =createOverlay(warning.zIndex++);

    dvPosition = h/2+s-25;
    dhPosition = w/2 - 25;

    var loader = $(Builder.node('div'));

    loader.setStyle('position: absolute; z-index: '+ ( warning.zIndex++ )
                     +'; top:' + dvPosition + 'px; left:' + dhPosition + 'px;'
                     + ' width: 54px; height: 55px; background: url(/img/loader.gif); ');
    document.body.appendChild(loader);

    var ov = overlay;
    var lo = loader;
    var returner = {close:function(){
        var shadow = ov;
         new Effect.Opacity(lo , { from: 1.0, to: 0.0, duration: 0.5,afterFinish:
                            function(){
                                shadow.remove();
                                try{lo.parentNode.removeChild(lo);}catch(e){}
                            }
                            });
        }};
    return returner;
}













/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/*              Console implementation                                   */
if(!console){
    var console = window.console = {
        log: function (param){},
        warn: function (param){},
        group: function (param){},
        groupEnd: function (param){},
        info: function (param){},
        debug: function (param){},
        error: function (param){},
        assert: function (param){},
        dir: function (param){},
        dirxml: function (param){},
        trace: function (param){},
        time: function (param){},
        timeEnd: function (param){},
        profile: function (param){},
        count: function (param){}
    }
    //window.debugger = {}
}else{
    console.info('Detected, that console enabled!');
}

function getCooks(name){
    var r = new RegExp('\s?'+name+'=([^;]*)');

    var val = document.cookie.match(r);
   try{
    return val[1]?val[1]:null;
   }catch(e){
       return null;
   }
}

function setCooks(theName,theValue){
        new Ajax.Request('/sc.php',{
            method: 'GET',
            parameters:{
                name: theName,
                value: theValue
            },
            onSuccess:function(t){
               /// console.log(document.cookie);
            }
            });
}

