/*  (C) Hradecon | http://hradecon.com   */

$(document).ready(function(){		
	// Menu active handling
	$('header .mainMenu > li > a.active').next().addClass('onTop').show();
	$('header .submenu a.active').parent().parent().addClass('onTop').show().prev().addClass('active');
	$('header .mainMenu li.item1, header .mainMenu li.item2, header .mainMenu li.item3, header .mainMenu li.item4').hover(function(){
		$('header .submenu.onTop').hide();
		$('.submenu', this).show();
		hideBoxUser($('.item5 a'));
	}, function(){
		$('.submenu', this).hide();
		$('header .submenu.onTop').show();
	});
	
	// Users button hover
	$('header .mainMenu li.item5').hover(
		function () {
			showBoxUser($(this).children('a'));
		}, function(){
		});
	$('header .mainMenu li.item5 a').click(function () {
		if ($(this).hasClass('active')) {
			hideBoxUser($(this));
		} else {
			showBoxUser($(this));
		}
	});
	
	// countdown
	var cntD = $('header .countDown .vals');
	setInterval(function(){countdown(cntD);}, 990);
	
	function countdown(holder)
	{
		var sHld = $('a.s', holder),
			secs = parseInt(sHld.text(), 10);
		
		if (0 >= secs) {
			sHld.text(59);
			
			var mHld = $('a.m', holder),
			mins = parseInt(mHld.text(), 10);
			
			if (0 >= mins) {
				mHld.text(59);
				
				var hHld = $('a.h', holder),
				hods = parseInt(hHld.text(), 10);
				
				hHld.text(zeroFill(--hods, 4));
			} else {
				mHld.text(zeroFill(--mins, 2));
			}
		} else {
			sHld.text(zeroFill(--secs, 2));
		}
	}
});

function showBoxUser(a) {
	var boxUser = $('#boxUser');
	
	a.addClass('active');
	boxUser.slideDown();
	boxUser.find('input[type="text"]').focus();
}

function hideBoxUser(a) {
	var boxUser = $('#boxUser');
	
	a.removeClass('active');
	boxUser.slideUp();
}

function zeroFill(number, width)
{
	width -= number.toString().length;
	if ( width > 0 ) {
		return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
	}
	return number;
}
