Wiking.widgets.LoginScreen = (function(){
    function Control(){
        Wiking.widgets.BlackScreen.draw();
        Wiking.getMap().addControl(this);
    }
    Control.prototype=new GControl();
    Control.prototype.selectable = function(){return false;}
    Control.prototype.printable = function(){return true;}
    Control.prototype.initialize = function(map){
        var ie = Prototype.Browser.IE ? 10 : 0;
        var html = '<div style="float:left;font-size:1px;width:8px;height:8px;background:url(\'wimg/clefttop.png\')"></div>'+
                       '<div style="float:left;font-size:1px;background:url(\'wimg/ctop.png\');height:8px;width:'+(261-16)+'px"></div>'+
                       '<div style="float:left;font-size:1px;width:8px;height:8px;background:url(\'wimg/crighttop.png\')"></div>'+
                     '<div style="float:left;clear:left;">'+
                       '<div style="float:left;width:8px;background:url(\'wimg/cleft.png\');height:'+(210-16+ie)+'px"></div>'+
                       '<div style="float:left;background:#fff;height:'+(210-16+ie)+'px;width:'+(261-16)+'px">'+
                        //TREŚĆ
                           '<div style="padding:10px;">'+
                                '<strong>Logowanie do panelu użytkownika</strong>'+
                                '<div style="height:1px;font-size:1px;border-bottom:1px solid #ccc;margin:10px 0;"></div>'+
                                'Login:<br />'+
                                '<input id="LoginScreenLogin" type="text" style="border: 1px solid #ccc;padding:3px;margin:3px 0;width:210px;"><br />'+
                                'Hasło:<br />'+
                                '<input id="LoginScreenPass" type="password" style="border: 1px solid #ccc;padding:3px;margin:3px 0;width:210px;"><br />'+ 
                                '<button id="loginscreenbutton" style="padding:4px 10px;margin-top:10px;float:right;margin-right:8px;cursor:pointer;">Zaloguj się</button>'+                                
                           '</div>'+
                       '</div>'+
                       '<div style="float:left;font-size:1px;width:8px;background:url(\'wimg/cright.png\');height:'+(210-16+ie)+'px"></div>'+
                        //font size ie fix
                   '</div>'+
                   '<div style="float:left;clear:left;">'+
                       '<div style="float:left;font-size:1px;width:8px;height:8px;background:url(\'wimg/cleftbottom.png\')"></div>'+
                       '<div style="float:left;font-size:1px;background:url(\'wimg/cbottom.png\');height:8px;width:'+(261-16)+'px"></div>'+
                       '<div style="float:left;font-size:1px;width:8px;height:8px;background:url(\'wimg/crightbottom.png\')"></div>'+
                   '</div>';
        var div = document.createElement('div');
        div.innerHTML=html;
        div.style.zIndex="300";
        map.getContainer().appendChild(div);
        GEvent.bindDom(document.getElementById('loginscreenbutton'),'click',this,this.continue_);
        return div;
    }
    Control.prototype.continue_ = function(){
        var login = document.getElementById('LoginScreenLogin').value;
        var pass = document.getElementById('LoginScreenPass').value;
        var self=this;
        new Ajax.Request('/thingy/auth',{
            method:'GET',
            parameters:{
                u:login,
                p:pass
            },
            onSuccess:function(e){
                var json = e.responseJSON;
                if (typeof(json.user)!="undefined"){
                    document.cookie="ta="+json.user+"#$#"+json.hash;
                    Username = json.user;
                    self.clear_();
                    stage1();
                }
            }
        });
    }
    Control.prototype.draw_ = function(){  
        Wiking.widgets.BlackScreen.draw();
        Wiking.getMap().addControl(this);
    }
    Control.prototype.clear_ = function(){
        Wiking.widgets.BlackScreen.clear();
        Wiking.getMap().removeControl(this);
    }
    Control.prototype.getDefaultPosition = function(){
        var size = Wiking.getMap().getSize();
        return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(size.width/2-137,size.height/2-105));
    }; 
    return Control;
})();
Wiking.widgets.LoginScreen.login = function(){
    var login = document.getElementById('LoginScreenLogin').value;
    var pass = document.getElementById('LoginScreenPass').value;
    new Ajax.Request('/thingy/auth',{
        method:'GET',
        parameters:{
            u:login,
            p:pass
        },
        onSuccess:function(e){
            var json = e.responseJSON;
            if (typeof(json.user)!="undefined"){
                document.cookie="ta="+json.user+"#$#"+json.hash;
                //Wiking.Event.trigger({e:'AUTHORIZED'});
                document.location.reload(true); // TODO
            }
        }
    });
}
