
	var ClosedBoxlogin = false;	
	
	$(document).ready(function(){
		
        $('#username').focus(function(){
      		if($('#username').val() == '' || $('#username').val() == $('#default_text1').val()){
				$('#username').val(''); 
			}
        });
		
        $('#username').blur(function(){
			 if($(this).val() == ''){
				 $(this).val($('#default_text1').val()); 
			 }
        });
        
		$('#password').focus(function(){
			if($(this).val() == '' || $(this).val() == $('#default_text2').val()){
				$(this).val(''); 
			}
		});
		
		$('#password').blur(function(){
			 if($(this).val() == ''){
				 $(this).val($('#default_text2').val()); 
			 }
		});
		
		$('#loginHide').click(function(){
			$('#featured').animate({height:316},450);
			$('#feat_a').fadeOut(350);
			$('#feat_b').fadeIn(350);
			$('#loginBox').fadeOut(350);
			$('#loginBox').fadeIn(350);			
		});
		
		$('#loginHideLink').click(function(){
			$('#featured').animate({height:132},450);
			$('#feat_b').fadeOut(350);
			$('#feat_a').fadeIn(350);
		});		
		
		$("#password").keypress(function(e){
			if (e.which == 13){
				setLogin('');
			}
		});
		
		$("#loginSend").click(function(){
			setLogin('');
		});
		
		$("#forgetLink").click(function(){
			$("#loginBox").hide();
			$("#forgetBox").show();
			$("#forgetMssg").hide();
			$("#forgetForm").show();
			$("#email").val('');
		});
		
		$("#forgetBtn").click(function(){
			$("#forgetWait").show();
			$("#forgetMssg, #forgetForm").hide();
			recoveryPass("#email",'');
		});
		
		$("#forgetBackBtn").click(function(){
			$("#loginBox").show();
			$("#forgetBox").hide();
		});
		
		$("#email").keypress(function(e){
			if (e.which == 13){
				$("#forgetWait").show();
				$("#forgetMssg, #forgetForm").hide();				
				recoveryPass("#email",'');
			}
		});
		
		$("#LoginBtn").click(function(){
			$("#logoutBox").hide();
			$("#mssgBox").hide();
			$("#loginBox").show();
		});
	
		$("#BackBtn").click(function(){
			$("#logoutBox").hide();
			$("#mssgBox").hide();
			$("#loginBox").show();
		});
		
		$("#forgetBackBtn1").click(function(){
			$("#forgetMssg").hide();
			$("#forgetForm").show();
		});
		
		$("#forgetBackBtn2").click(function(){
			$("#forgetMssg").hide();
			$("#loginBox").show();
		});

        $('#send_req').click(function(){
            showFloatWin('html/dialog/floatLogin.php?rel=request&u='+$('#u').val(), '', cancel_text, '',''); 
            dialog.setBackgroundContent('#000000');
        });       

        $('#send_message').click(function(){
            showFloatWin('html/dialog/floatLogin.php?rel=message&u='+$('#u').val(), '', cancel_text, '',''); 
            dialog.setBackgroundContent('#000000');
		});
	  		
	}); // End document ready
	
	function recoveryPass(elem, name){
		if(emailCheck(elem)){
			$.getJSON( URL_AJAXS + "?data=recoveryPass&email=" + $(elem).val() + "&r=" + Math.random(), 
				function(ret){
					if(ret['success'] == 'true'){
						$("#forgetBackBtn1"+name).hide();
						$("#forgetBackBtn2"+name).show();
					}else{
						$("#forgetBackBtn2"+name).hide();
						$("#forgetBackBtn1"+name).show();
					}
					$("#forgetMssgLineTwo"+name).html(ret['message']);
					$("#forgetForm"+name+", #forgetWait"+name).hide();
					$("#forgetMssg"+name).show();
				}
			);
		}else{
			$("#forgetMssgLineTwo"+name).html($('#email_not_valid').val());
			$("#forgetForm"+name+", #forgetWait"+name).hide();
			$("#forgetMssg"+name).show();
		}
	}
    
	function setLogin(name){
		if($('#username'+name).val() == '' || $('#username'+name).val() == $('#default_text1').val()){
			setErrorMssg($('#text_user_need').val(),'',name);
			return false;
		}
		if($('#password'+name).val() == '' || $('#password'+name).val() == $('#default_text2').val()){
			setErrorMssg($('#text_pass_need').val(),'',name);
			return false;				
		}
		$.ajax({
			type	: "POST",
			url		: URL_AJAXS + "?data=getLogin",
			data	: "username=" + $("#username"+name).val() + "&password=" + $("#password"+name).val() + "&rel=" + $("#type_rel").val(),
			success	: function(ret){
				if(ret == 'true'){
					var loginLocation = $('#domain').val();
					if ( $('input[id*="type_"]').size() < 2 ){
	                    if($("#type_rel").val()!=''){ 
							var type_rel = $("#type_rel").val();
							switch(type_rel){
								case 'request':
									loginLocation += 'profile/?u='+$("#u").val()+'&action='+$("#type_rel").val();
								break;
								case 'message':
									loginLocation += 'mailbox/?action='+$("#type_rel").val()+'&to='+$('#u').val();
								break;
								case 'facebookLogin':
									loginLocation = window.location.href;
								break;
							}
	                    }
					}else{
						var type_rel;
						
						$('input[id*="type_"]').each(function(){
							if ( $(this).val() != ""  ){
								type_rel = $(this).val();
							}
						})
						var loginLocation = window.location.href;
						switch(type_rel){
							case 'request':
								loginLocation += '&runJob=profile&parameters='+urlencode('action='+type_rel+'&u='+$('#u').val());
								
							break;
							case 'message':
								loginLocation += '&runJob=mailbox&parameters='+urlencode('action='+type_rel+'&to='+$('#u').val());
							break;
						}
					}
					window.location = loginLocation;	
				}else{
					setErrorMssg($('#wrong_user_pass').val(), $('#login_not_valid').val(),name);
				}
			}
		});
	}	
       
	function checkStatus(s){
		if(s == 'logout'){
			$("#loginBox").hide();
			$('#featured').animate({height:316},450);
			$('#feat_a').fadeOut(350);
			$('#feat_b').fadeIn(350);
			$("#logoutBox").show();
		}else{
			$('#featured').animate({height:316},450);
			$('#feat_a').fadeOut(350);
			$('#feat_b').fadeIn(350);
			$('#loginBox').fadeOut(350);
			$('#loginBox').fadeIn(350);			
		}
	}

	function closeBoxLogin(){
        if(ClosedBoxlogin){
			ClosedBoxlogin = true;
			$('#featured').animate({height:132},450);
			$('#feat_b').fadeOut(350);
			$('#feat_a').fadeIn(350);
			$('#mssgBox, #logoutBox, #forgetBox, #forgetWait, #forgetMssg').hide();
		}
	}
	
	
	
	function setErrorMssg(t1, t2, name){
		$('#errorLineOne'+name).html('');
		$('#errorLineTwo'+name).html('');
		
		$('#errorLineOne'+name).html(t1);
		$('#errorLineTwo'+name).html(t2);
		$('#loginBox'+name).hide();
		$('#mssgBox'+name).show();
	}
	
    function changeBackgroundColor(color){
        $('.content').css('background-color',color);
    }
	
	function urlencode( str ) {  
   
		var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];  
		var ret = str.toString();  

		var replacer = function(search, replace, str) {  
			 var tmp_arr = [];  
			tmp_arr = str.split(search);  
			 return tmp_arr.join(replace);  
		 };  

		 // The histogram is identical to the one in urldecode.  
		 histogram['!']   = '%21';  
		 histogram['%20'] = '+';  

		 // Begin with encodeURIComponent, which most resembles PHP's encoding functions  
		 ret = encodeURIComponent(ret);  

		 for (search in histogram) {  
			 replace = histogram[search];  
			 ret = replacer(search, replace, ret) // Custom replace. No regexing  
		 }  

		 // Uppercase for full PHP compatibility  
		 return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {  
			 return "%"+m2.toUpperCase();  
		 });  

		 return ret;  
	}
