var TimerId = 0;
var startfrom = new Date().getTime(); 
var Hour = 0;
var Minutes = 0;
var Seconds = 0;
var gmt = 0;

function setTimer () {
	if(TimerId == 0) {
		TimerId = setInterval(function(){
			now = new Date().getTimezoneOffset();
			now = new Date().getTime();
			Seconds = (now - startfrom) /(1000);
			Hour = Math.floor(Seconds / 3600);
			Seconds = Seconds - (Hour * 3600);
			Minutes = Math.floor(Seconds / 60);
			Seconds = Math.floor(Seconds - (Minutes * 60)); 
			$('#timer').html((Hour < 10 ? '0'+Hour : Hour)+':'+(Minutes < 10 ? '0'+Minutes : Minutes)+':<small>'+(Seconds < 10 ? '0'+Seconds : Seconds)+'</small>');
		}, 2000);
	}
}

var start = function() {
	if(!$(this).hasClass('current')) {
	$('.cpanel a').removeClass('current');
	$(this).addClass('current');

	$('#content').load('index.php',{
		'option'	 	: 'com_whr', 
		'view' 			: 'user', 
		'format'		: 'raw',
		'task'			: 'startsession',
	},function(){
		setTimer();
	});
	}
}

var stop = function() {
	clearInterval(TimerId);
	
	TimerId = 0;
	
	$('#target').load('index.php',{
		'option'	 	: 'com_whr', 
		'view' 			: 'user', 
		'format'		: 'raw',
		'task'			: 'stopsession'
	},function(){

	});

}

var logout = function() {
	$('#target').load('index.php',{
		'option'	 	: 'com_whr', 
		'view' 			: 'user', 
		'format'		: 'raw',
		'task'			: 'logout'
	},function(){
		$('#captcha').bind('click',captcha);
	});
}

var refresh = function() {
	$('#target').load('index.php',{
		'option'	 	: 'com_whr', 
		'view' 			: 'user', 
		'format'		: 'raw',
		'task'			: 'userstat',
		'datefrom'		: $('#datefrom').val(),
		'dateto'		: $('#dateto').val()
	},function(){
		$('#refresh').bind('click',refresh);
	});
}

var userstatshort = function() {
	var selector = $(this).attr('id');
	
	$('#target').load('index.php',{
		'option'	 	: 'com_whr', 
		'view' 			: 'user', 
		'format'		: 'raw',
		'task'			: selector,
		'datefrom'		: $('#datefrom').val(),
		'dateto'		: $('#dateto').val()
	}, function(){
		$('#'+selector).addClass('current');
		$('#userstat-short').bind('click', userstatshort);
		$('#userstat-long').bind('click', userstatshort);
	});
}

var userstat = function() {
	$('#target').load('index.php',{
		'option'	 	: 'com_whr', 
		'view' 			: 'user', 
		'format'		: 'raw',
		'task'			: 'userstat',
		'datefrom'		: $('#datefrom').val(),
		'dateto'		: $('#dateto').val()
	},function(){

	});
}

var captcha = function () {
	$(this).attr('src','index.php?option=com_whr&view=user&format=raw&task=captcha&time='+ new Date().getTime());
}

function CheckForm () {  
	if($('input[name=user]').length > 0 && $('input[name=user]').val().length > 0) {
	  	$('#target').load('index.php',{
			'option'	 	: 'com_whr', 
			'view' 			: 'user', 
			'user' 			: $('input[name=user]').val(),
			'userpassword' 	: $('input[name=userpassword]').val(),
			'captcha' 		: $('input[name=captcha]').val(),
			'format'		: 'raw',
			'task'			: 'login'
		},function(){
		});
	}
	return false;
}

function LoadContent() {
$('#target').load('index.php',{
	'option'	 	: 'com_whr', 
	'view' 			: 'user', 
	'format'		: 'raw',
	'task'			: 'login'
}, function(){
});
}

$(document).ready(function(){
  	  	$('#modlgn_username').val( login );	  	
  	  	
  	  	$('.register').click(function(){
  	  		$(this).hide();
  	  	  	$('#form-login').slideDown(200);
  	  	});
  	  	
  	  	$('#modlgn_username').click(function(){
  	  	  if($(this).val() == login) $(this).val(''); 
  	  	});
  	  	$('.topmenu .parent').mouseenter(function(){
  	  		$(this).find('a').css('background-color','#328eb5');
  	  		$(this).find('ul').slideDown(200);
  	  	}).mouseleave(function(){
  	  	  	$(this).find('ul').slideUp(200);
  	  	  	if(!$(this).hasClass('active')) {
	  	  	  	$(this).find('a').css({
	  	  	  	  	'background': 'none',
	  	  	  		'color'		: '#363636 !important'
	  	  		});
  	  	  	}
  	  	});

  	  if ($('#system-message').length > 0) {
  		$('#system-message').slideDown('fast').delay(3000).slideUp('slow');
  	  }
});
