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($('#new_user_link').offset().left-310+$('#new_user_link').width())-55, topValue=parseInt($('#new_user_link').offset().top+$('#new_user_link').height());
    if($('#loginDiv').length>0){
        $('#loginDiv').css({'top':topValue,'left':leftValue})
        $('#loginDiv').slideToggle('slow');
    }else{
        $('body').append(
            '<div id="loginDiv" style="position:absolute;width:310px;height:147px;padding:5px;border:2px solid #ec0014;display:none;z-index:100;display:none;background-color:#fff">'+
                '<div class="w100 ohidden mtop1 bgwhite" style="height:147px;position:relative">'+
                    '<div id="loginErrorMsg" class="red taleft" style="position:absolute;top:2px;left:40px;width:230px;display:none;text-align:center">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/icons/ic_closeLogin.gif" alt="Fechar" style="width:15px;height:15px"></div>'+
                    '<div class="ohidden  mleft20 mtop10 mbot10">'+
                        '<div class="fleft ohidden">'+
                            '<input name="op" type="hidden" value="Entrar">'+
                            '<div class="fleft ohidden" style="width:270px;margin-top:30px">'+
                                '<div class="ohidden">'+
                                    '<div class="fleft mright10 mbot5 bold mtop2" style="width:110px;text-align:right">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 taright strgBlue" style="width:110px;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 mtop5" style="float:right;margin-bottom:5px;margin-right:0px"><button onclick="layeredLogin()" style="width:85px;padding-top:2px">entrar &#9658;</button> </div>'+
                                '<div class="fright cboth">'+
                                    '<div class="fright mleft15"><a class="f12 dred bold" href="/gen.pl?p=users&amp;op=loginerr" rel="nofollow">Recuperar password</a></div>'+
                                    '<div class="fright"><a class="f12 dred bold" href="/gen.pl?p=users&amp;op=new" rel="nofollow">Registo</a></div>'+
                                '</div>'+
                            '</div>'+
                        '</div>'+
                        '<div id="loginLoadingIcon" class="fleft" style="margin-top:90px;margin-left:10px;display:none">'+
                            '<img src="/i/icons/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($('#new_user_link').offset().left-310+$('#new_user_link').width())-55, topValue=parseInt($('#new_user_link').offset().top+$('#new_user_link').height());
        $('#loginDiv').css({'top':topValue,'left':leftValue});
    }
})

