$(document).ready(function(){	
	bind_hover ();
	bind_search ();
	bind_treeview ();
	bind_pair ();
	
	menu_widths();

	bind_required();	
});

/**
 * hover efekt na menu
 **/
function bind_hover ()
{
	$(".menu li, .path li").hover(
		function() {
			$(this).addClass("sfhover");
		},
		function() {
			$(this).removeClass("sfhover");
		}
	);
}

/**
 * smazani textu v search input
 **/
function bind_search ()
{
	// vynulovani pole pri focusu
	$(".search-input").focus(function() {
		$(this).val('');
	});
}

/* submenu treeview */
function bind_treeview ()
{
	$("#eshop-menu ul li").each(function(){
		if($(this).hasClass("active")) {
			$(this).addClass("open");
		}
	});

	$("#eshop-menu ul").treeview({
		collapsed: "true",
		animated: "fast"
	});
}

/**
 * pridani tridy sudemu prvku
 **/
function bind_pair ()
{
	var i=0;
	$("dd>.treeview>li").each(function(){
		if(i==1) {
			$(this).addClass("even");
			i=0;
		}
		else
		{
			$(this).addClass("odd");
			i++;
		}	
	});
}

/**
 *	Nastavi spravne sirky pro submenu
 */
function menu_widths() {

	var menu_w = $('#smenu').width()-15;	// pravy margin je 15 px
	var items_cnt = $('#smenu>ul>li').length;
	var items_w = 0;
	
	$('#smenu>ul>li a').each(function () {
		items_w = items_w + $(this).width();
	});

	var padding = ((menu_w-items_w+2)/items_cnt-2)/2;	// -2 je border, +2 je pridany border nejpravejsiho prvku, ktery tam neni

//	console.log(menu_w);
//	console.log(items_w);
//	console.log(padding);

	$('#smenu>ul>li a').each(function () {
		$(this).css('paddingLeft', padding);
		$(this).css('paddingRight', padding);
	});

}

/**
 *	Zajisti, aby pri odeslani byla vsechna povinna pole vyplnena
 */
function bind_required() {

	$('.contact').submit(function () {
		var succ = true;
		if($('.required').length > 0) {
			$('.required').each(function () {
				if(!$(this).val())
					succ = false;
			});
		}
		
		if(!succ) {
			alert('Nejsou vyplnena vsechna povinna pole!');
			return false;
		}
	});
}


