var active = false;
var intrv;
var tmt;
$(document).ready(function(){
	
	try {
		if (session) {
			intrv  = setInterval("check_session()",29000);	
		}
	}
	catch (e) {}
	
	
	$('#partners-login').hover(function() {								
								if (!active) {		
									$('#partners-login').attr('class','partners-active');								
									$('#login-content').show();
								}
								active = true;
								clearTimeout(tmt);
							},
							function() {
								 active = false;
								 tmt = setTimeout("hide_popup()",800);								
							});
	
	$('#login-content').hover(function() {
								active=true;
								clearTimeout(tmt);
							},
							function() {
								active=false;
								tmt = setTimeout("hide_popup()",800);													
							});
	
	$('#login-submit').click(function() {
									  
									  $.post("login.php", { username: $('#username').val(), password: $('#passwd').val(), ts: Date.parse(new Date()) }, function(data){
											var msg;
											if (data.trim().substring(0,2)==='ok') {
												intrv  = setInterval("check_session()",29000);
												msg	   = data.trim().substring(3);
												$('#msg').css('color','#00a804');
												valid  = true;
											}
											else {
												msg	   = data.trim();
												$('#msg').css('color','#e10303');
												valid  = false;
											}
											$('#msg').empty().text(msg).hide();
											$('#login-form').fadeOut("fast").oneTime("5100ms", function() {
																$(this).fadeIn("fast");									  
																});
											
											$('#msg').oneTime("600ms",function() {
															$(this).fadeIn("fast")				
																			}).oneTime("4500ms", function() {
																			 if (valid) document.location = "partners.php";
																			 $(this).fadeOut("fast");
																			});
											
											
										});
									  	return false;
									  });
	
});
	
check_session = function() {
	$.post("session.php", { ts: Date.parse(new Date()) }, function(data) {
			if (data.trim()!='ok') {
				clearInterval(intrv);
			}
		});
}

hide_popup = function () {
	if (!active) {
		$('#login-content').hide();		
		$('#partners-login').attr('class','');
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
