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,
        beforeSend: function() { $('#loginLoadingIcon').show(); },
        error: function(){ },
        success:function(data){
            $('#loginLoadingIcon').hide();
            if(data == 'NOK') {
                $('#loginErrorMsg').show();
            } else {
			    closeMeUp();
				var cleanURL = window.location.href;
				cleanURL = cleanURL.replace(/(\&|\?)?uop=userlogin/,'');
				cleanURL = cleanURL.replace(/(\&|\?)?uop=userclose/,'');
			
               // window.location.href=baseurl+loginHiddenFields;
				if(cleanURL.indexOf('?') != -1){
					window.location.href  = cleanURL + "&uop=userlogin"; 
				} else {
					window.location.href = cleanURL + "?uop=userlogin"; 			
				}
				
			}
        }
    }); 
}

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

function layeredLogout(){
   /* if(returnURL.charAt(0)=='/')returnURL=returnURL.substr(1,returnURL.length-1);
    window.location.href=baseurl+returnURL;*/
	var cleanURL = window.location.href;
	cleanURL = cleanURL.replace(/(\&|\?)?uop=userlogin/,'');
	cleanURL = cleanURL.replace(/(\&|\?)?uop=userclose/,'');
	
	if(cleanURL.indexOf('?') != -1){
		window.location.href = cleanURL + "&uop=userclose"; 
	} else {
		if(cleanURL.match("\/$")) {
			window.location.href =  "/gen.pl?uop=userclose"
		} else {
			window.location.href = cleanURL + "?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(){
    var leftValue=parseInt($('#mainLoginDiv').offset().left-310+$('#mainLoginDiv').width())+10, topValue=parseInt($('#mainLoginDiv').offset().top+$('#mainLoginDiv').height()+10);
    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:145px;padding:5px;border:2px solid #3e83a9;display:none;z-index:100;display:none;background-color:#f5f5f3">'+
                '<div class="w100 ohidden mtop1" style="height:145px;position:relative">'+
                    '<div id="loginErrorMsg" class="red taleft" style="position:absolute;top:2px;left:25px;width:280px;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="ohidden  mleft20 mbot10">'+
                        '<div class="fleft ohidden">'+
                            '<input name="op" type="hidden" value="Entrar">'+
                            '<div class="fleft ohidden" style="width:270px;margin-top:30px">'+
                                '<div class="fleft mright10 mbot5 bold mtop2 taright strgBlue" style="width:110px">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:130px">'+
                                '</div>'+
                                '<div class="fleft mright10 mbot5 bold mtop10 taright strgBlue" style="width:110px">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:130px">'+
                                '</div>'+
                                '<div class="w100 ohidden">'+
                                    '<div class="ohidden cboth pointer mtop5" onclick="layeredLogin()" style="float:right;margin-bottom:5px;margin-right:0px"><div class="bt_submit tacenter ohidden"><div style="margin-top:5px" class="bold">Entrar</div></div></div>'+
                                '</div>'+
                                '<div class="w100 ohidden">'+
                                    '<div class="fright cboth mtop5">'+
                                        '<div class="fright mleft15"><a class="uline strgBlue bold" href="/gen.pl?p=users&amp;op=loginerr" rel="nofollow">Recuperar password</a></div>'+
                                        '<div class="fright"><a class="uline strgBlue bold" href="/gen.pl?p=users&amp;op=new" rel="nofollow">Registo</a></div>'+
                                    '</div>'+
                                '</div>'+
                            '</div>'+
                        '</div>'+
                        '<div id="loginLoadingIcon" class="fleft" style="margin-top:77px;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()+10);
        $('#loginDiv').css({'top':topValue,'left':leftValue});
    }
})
