function layeredLogin(){
    //alert('op=Entrar&unickname='+$('#uname').val()+'&upasswd='+$('#upasswd').val());
    $('#loginErrorMsg').hide();
    if(loginHiddenFields.charAt(0)=='/')loginHiddenFields=loginHiddenFields.substr(1,loginHiddenFields.length-1);
    $.ajax({
        url:'gen.pl',
        data:'p=users&op=ajaxlogin&uop=userlogin&unickname='+escape($('#uname_layered').val())+'&upasswd='+escape($('#upasswd_layered').val()),
        type:'POST',
        cache:false,
        dataType: 'application/x-www-form-urlencoded;charset=ISO-8859-1',
        beforeSend: function() {$('#loginLoadingIcon').show()},
        error: function(){ },
        success:function(data){
            $('#loginLoadingIcon').hide();
            switch(data){
                case 'NOK':
                    $('#loginErrorMsg').show();
                    break;
                default:
                    closeMeUp();
                    window.location.href=baseurl+loginHiddenFields;
                    break;
            }		
        }
    })
}

function closeMeUp(){
    $('#loginDiv').slideUp('slow');
}

function layeredLogout(){
    if(returnURL.charAt(0)=='/')returnURL=returnURL.substr(1,returnURL.length-1);
    window.location.href=baseurl+returnURL;
}

function getObj(id){
  return document.getElementById(id);
}

function onFocusLayer(id){
  var obj=getObj(id);
  if(obj.value==obj.defaultValue){
    obj.value="";
  }
}

function onBlurLayer(id){
  var obj=getObj(id);
  if(obj.value==""){
    obj.value=obj.defaultValue;
  }
}

function showHideLoginBox(){
    var leftValue=parseInt($('.divLoginTop').offset().left-310+$('.divLoginTop').width())-13, topValue=parseInt($('.divLoginTop').offset().top+$('.divLoginTop').height()+2);
    if($('#loginDiv').length>0){
        $('#loginDiv').css({'top':topValue,'left':leftValue})
        $('#loginDiv').slideToggle('slow');
    }else{
        $('body').append(
            /*'<style type="text/css">'+
                '#loginDiv *{font-family:\'Trebuchet MS\';font-weight:normal;font-size:12px;text-decoration:none}'+
                '#loginDiv a:hover{text-decoration:underline !important}'+
            '</style>'+*/
            '<div id="loginDiv" style="position:absolute;width:310px;height:147px;padding:5px;border:2px solid #dc0000;display:none;z-index:100;display:none;background-color:#f5f5f3">'+
                '<div class="w100 ohidden" style="height:147px;position:relative">'+
                    '<div id="loginErrorMsg" style="position:absolute;top:2px;left:40px;width:230px;display:none;color:#dc0000;text-align:center;line-height:13px;font-size:11px">Os dados introduzidos estão incorrectos.<br>Por favor, verifique e tente novamente.</div>'+
                    '<div id="loginClose" onclick="closeMeUp()" class="pointer" style="top:5px;right:5px;position:absolute"><img src="/i/ic_closeLogin.gif" alt="Fechar" style="width:15px;height:15px"></div>'+
                    '<div class="fleft ohidden mleft20 mbot10" style="margin-top:5px">'+
                        '<div class="fleft ohidden">'+
                            '<input name="op" type="hidden" value="Entrar">'+
                            '<div class="fleft ohidden" style="width:270px;margin-top:25px">'+
                                '<div class="ohidden">'+
                                    '<div class="fleft mright10 mbot5 bold f12" style="width:105px;text-align:right;margin-top:3px">Nick:</div> '+
                                    '<div class="fright bgwhite pleft5 ptop3 pbot3 bgray mbot5">'+
                                        '<input id="uname_layered" type="text" value="email/nickname" name="unickname" onfocus="onFocusLayer(this.id.toString())" onblur="onBlurLayer(this.id.toString())" style="width:140px;border: 1px solid #CCC;padding: 2px;">'+
                                    '</div>'+
                                '</div>'+
                                '<div class="ohidden">'+
                                    '<div class="fleft mright10 mbot5 bold f12" style="width:105px;text-align:right;margin-top:3px">Palavra-chave:</div> '+
                                    '<div class="fright bgwhite pleft5 ptop3 pbot3 bgray mbot5">'+
                                        '<input id="upasswd_layered" type="password" maxlength="40" value="........" name="upasswd" onkeydown="if(event.keyCode==13) layeredLogin();" onfocus="onFocusLayer(this.id.toString())" onblur="onBlurLayer(this.id.toString())" style="width:140px;border: 1px solid #CCC;padding: 2px;">'+
                                    '</div>'+
                                '</div>'+
                                '<div class="ohidden cboth pointer" style="float:right;margin-bottom:3px;margin-right:0px;margin-top:5px"><a class="editButtons" href="javascript:layeredLogin()" style="display:block;text-decoration:none"><img src="/i/bt_login.png" alt="Login" style="border:0px"></a></div>'+
                                '<div class="fright cboth">'+
                                    '<div class="fright"><a href="/gen.pl?p=users&op=asknewpasswd&sid=as.sections/11" rel="nofollow" style="margin-left:15px;color:#333;font-weight:bold;font-size:12px">Recuperar password</a></div>'+
                                    '<div class="fright"><a href="/gen.pl?p=users&op=new&sid=as.sections/11" rel="nofollow" style="color:#333;font-weight:bold;font-size:12px">Registo</a></div>'+
                                '</div>'+
                            '</div>'+
                        '</div>'+
                        '<div id="loginLoadingIcon" class="fleft" style="margin-top:90px;margin-left:10px;display:none">'+
                            '<img src="/i/ic_loginLoading.gif" alt="Em processamento" style="width:25px;height:25px">'+
                        '</div>'+
                    '</div>'+
                '</div>'+
            '</div>'
        );
        $('#loginDiv').css({'top':topValue,'left':leftValue});
        $('#loginDiv').slideToggle('slow');
    }
}

$(function(){
    if($('#loginDiv').length>0){
        var leftValue=parseInt($('.divLoginTop').offset().left-310+$('.divLoginTop').width())-13, topValue=parseInt($('.divLoginTop').offset().top+$('.divLoginTop').height()+2);
        $('#loginDiv').css({'top':topValue,'left':leftValue});
    }
})

