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,
        contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-1',
		dataType: 'text',
        beforeSend: function() {},
        error: function(jqXHR, textStatus, errorThrown){},
        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;
/*	if(window.location.href.indexOf('uop=userclose') == -1) {
		if(window.location.href.indexOf('?') == -1) {
			window.location.href = window.location.href += '?uop=userclose'		
		} else {
			window.location.href = window.location.href += '&uop=userclose'		
		}
	}*/
}

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(){
	if($('#mainLoginDiv').offset()){
	    var leftValue=parseInt($('#mainLoginDiv').offset().left-310+$('#mainLoginDiv').width())-10, topValue=parseInt($('#mainLoginDiv').offset().top+$('#mainLoginDiv').height()+7);
	    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 #E86E34;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: 250px; color: rgb(220, 0, 0); text-align: center; line-height: 13px;display:none">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="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" style="width:110px;text-align:right;margin-top:5px">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 mtop10">'+
	                                    '<div class="fleft mright10 mbot5 bold taright strgBlue" style="width:110px;text-align:right;margin-top:5px">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:5px;margin-right:0px;margin-top:5px"><a class="editButtons" href="javascript:layeredLogin()" style="padding:5px;display:block;text-decoration:none">Entrar</a></div>'+
	                                '<div class="fright cboth">'+
	                                    '<div class="fright"><a href="/gen.pl?p=users&op=login" rel="nofollow" style="margin-left:15px;color:#E86E34;font-weight:bold">Recuperar password</a></div>'+
	                                    '<div class="fright"><a href="/gen.pl?p=users&amp;op=new" rel="nofollow" style="color:#E86E34;font-weight:bold">Registo</a></div>'+
	                                '</div>'+
	                            '</div>'+
	                        '</div>'+
	                        '<div id="loginLoadingIcon" class="fleft" style="margin-top:97px;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($('#mainLoginDiv').offset().left-310+$('#mainLoginDiv').width())-10, topValue=parseInt($('#mainLoginDiv').offset().top+$('#mainLoginDiv').height()+7);
        $('#loginDiv').css({'top':topValue,'left':leftValue});
    }
})
