$(document).ready (function (){	
	bind_hover ();
	bind_evenodd ();
	bind_mmenu();
	
	$("#et").show().treeview({collapsed: "true", animated: "fast"});
	
	// $("#et").show().treeview({collapsed: "true", animated: "fast"});
	
	// // ajaxove nacitani obsahu webu
	// $('#et a').click(function(){
	// 	if ($('#main .main-content .content').length == 0)
	// 		return true;
	// 		
	// 	var href = $(this).attr('href');
	// 	
	// 	$('#et li').removeClass('active');
	// 	
	// 	$('#et li:has([href='+href+'])').addClass('active');
	// 
	// 	$('#main .main-content .content').block({
	// 		message: '<img src="/img/loader.gif" />',
	// 		css: {
	// 			border:'none',
	// 			backgroundColor:'transparent'
	// 		},
	// 		overlayCSS: {
	// 			backgroundColor: '#eee',
	// 			opacity : 0.6
	// 		},
	// 		focusInput: false
	// 	});
	// 
	// 	// ajaxove nacteni produktu do stranky
	// 	$.get(href+'?layout=center',function(data){		
	// 		var data = data.replace(/layout\=center/ig,'');
	// 		var data = data.replace(/\?\&/ig,'?');
	// 		
	// 		$('#main .main-content .content').unblock();
	// 		$('#main .main-content .content').html(data);
	// 		bind_evenodd ();
	// 	});
	// 	return false;
	// });
	
	bind_switchcontact ();      
	             
	$('input[type=text]').focus(function(){this.select()});
	
	// focus dalsiho produktu po vlozeni do kosiku
	if ($('#product-list') != null) {
		var params = getUrlVars();
		if (params['code']) {
				var items=$("input:text[name*=basket]");
				var index = items.index($('#product-list input[type=text][id=' + params['code'] + ']'));
				if (items[index+1] != null)
		        	items[index+1].focus();
		}
		
	}
	
	change_label_h3();
});

function bind_switchcontact()
{
	$('a[rel=switchcontact]').click(function(){
		
		if (!$(this).attr('inicialized')) {
 			show_switch_dialog();
			$(this).attr('inicialized', true);       
		}
		
		$("#switchcontact").dialog('open');		
	});
	
	// vyber kontaktu hned po prihlaseni   
	if (($("#switchcontact").text()) && (!$.cookie("switched"))) {    
		show_switch_dialog();
	}
	
	// vymazat cookie o prepnuti kontaktu pri odhlaseni
	$("a[href$=logout.html]").click(function(event){             
		$.cookie('switched', null, {path: "/"});      
	})               
}    

function show_switch_dialog()
{
   	$.cookie('switched', "kontakt zmenen", {expires: 1, path: "/"});
	$("#switchcontact").dialog({
		bgiframe: true,
		modal: true,
		buttons: {
			"Zavřít": function() {
				$(this).dialog("close");
			},
			"Vybrat": function() {
				$('form',this).submit();
			}
		}
	});	
}  



function bind_evenodd ()
{
	$('.evenodd tbody tr:even').addClass('even');
	$('.evenodd tbody tr:odd').addClass('odd');
}

function bind_hover ()
{
	$(".mmenu li").hover(
		function() {
			$(this).addClass("sfhover");
		},
		function() {
			$(this).removeClass("sfhover");
		}
	);
}

function bind_tree ()
{
	// inicializace objektu
	$et = $("#et");
	$ets = $("#ets");
	$etsul = $('ul:first', $ets);
	$cs = $("#category-search");
	
	// zajisti otevreni stromu na dane pozici
	$("li.active", $et).addClass("open");
		
	// zduplikuje kategorie shopu
	$ets.hide ();

	// vytvori linearni strom
	var out = '';
	$("ul li a", $et).each(function(){
		out += '<li><a href="'+$(this).attr('href')+'">'+$(this).html()+"</a></li>";
	});
	$ets.html ('<ul>' + out + '</ul>');
	
	// zapne strom
	$et.show().treeview({collapsed: "true", animated: "fast"});

	$cs.attr('rel','0');

	// nabinduje hledani ve stromu
	$cs.click(function(){
		if ($(this).attr('rel') == 0) {
			$(this).val('').attr('rel',1);
		}
	});
	
	$cs.keyup(function(){
		search_text = $(this).val();
		
		if (search_text != "") {
			$et.hide();
			$ets.show();
			$('li', $ets).each(function(){
				var v = $(this).text();
				v = v.toLowerCase();
				var c = new RegExp (search_text);
				if (v.match(c)) {
					$(this).show();
					$('a',this).html(v.replace(c,'<strong>'+search_text+'</strong>'));
				}
				else {
					$('a',this).html($(this).text());
					$(this).hide ();
				}
			});
		}
		else {
			$et.show();
			$ets.hide();
		}
	});
}

// Read a page's GET URL variables and return them as an associative array.
// http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function bind_mmenu() {
    $('.mmenu div ul li a span').each( function () {
        if ($(this).html().length <= 10) $(this).addClass('tworows');
    });
}

function change_label_h3() { 
	$('#accordion h3 a').each( function () {
		if ($(this).text() == "1 Osobně") {
            $(this).text('Osobně v místě dodavatele');
        } else if ($(this).text() == "2 Závoz") {
            $(this).text('Závoz v pravidelné rozvozové dny');
        } else if ($(this).text() == "3 MimoZávoz") {
            $(this).text('Závoz po dohodě s dodavatelem');
        }
        
	});
}

