$(document).ready(function(){	
	bind_hover ();
	bind_treeview ();
	bind_cycle_code ();
	bind_cycle_code2 ();
	bind_path ();
	
	$('.ifixpng').ifixpng();
	$('.treeview li').ifixpng();
    $('.main-panel div.cont').ifixpng();
//     $('.mmenu div ul li.active').ifixpng();
//     $('.mmenu div ul li.active a').ifixpng();
//     $('.mmenu div ul li:hover').ifixpng();
//     $('.mmenu div ul li:hover a').ifixpng();
});


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


/* submenu tree */
function bind_treeview ()
{
	$(".left-panel ul li").each(function(){
		if($(this).hasClass("active")) {
			$(this).addClass("open");
		}
	});

	$(".left-panel ul").treeview({
		collapsed: "true",
		animated: "fast"
	});
}


/* cycle banner */
function bind_cycle ()
{
	$('#cycle').cycle({ 
	    fx:     'fade', 
	    speed:  1000, 
	    timeout: 6000, 
	    next:   '#button3', 
	    prev:   '#button1' 
	});
	
	$('#button2').click(function() {
		if( $('#button2').hasClass('paused') )
		{
			$('#cycle').cycle('resume');
			$('#button2').removeClass('paused')
		}
		else
		{			
			$('#cycle').cycle('pause');
			$('#button2').addClass('paused')
		}
	});
}

/* cycle2 banner */
function bind_cycle2 ()
{
	$('#cycle2').cycle({
	    fx:     'fade',
	    speed:  1000,
	    timeout: 6000,
	    next:   '#topprodukt-button3',
	    prev:   '#topprodukt-button1'
	});

	$('#topprodukt-button2').click(function() {
		if( $('#topprodukt-button2').hasClass('paused') )
		{
			$('#cycle2').cycle('resume');
			$('#topprodukt-button2').removeClass('paused')
		}
		else
		{
			$('#cycle2').cycle('pause');
			$('#topprodukt-button2').addClass('paused')
		}
	});
}

/* 
prepis HTML kodu pro cycle 
vstup: ul li seznam s obrazky a texty
vystup: layout v divech
*/
function bind_cycle_code ()
{
	var images = new Array();
	var headers = new Array();
	var texts = new Array();
	var links = new Array();

	if ($('#cycle ul li').length > 0)
	{
        $('#path').css('top', '450px');
        
		// pole s obrazky
		$("#cycle ul li img").each(
			function()
			{
				images.push($(this).attr("src"));
			}
		);
		
		// pole s nadpisy
		$("#cycle ul li h2").each(
			function()
			{
				var str = $(this).text();
				if(str != "")
					headers.push(str);
			}
		);
	
		// pole s texty
		$("#cycle ul li p").each(
			function()
			{
				var str = $(this).text();
				if(str != "")
					texts.push(str);
			}
		);
		
		// pole s odkazy
		$("#cycle ul li a").each(
			function()
			{
                links.push($(this).attr("href"));
			}
		);
	
		// smazani ul li seznamu
		$("#cycle ul").remove();
	
		// novy layout v divech
		if(images.length == texts.length)
		{
			for (var i = 0; i < images.length; i++) 
			{
				$("#cycle").append("<div><div class='abs cont'></div><div class='abs cont-content excerpt wysiwyg'><h2>" + headers[i] + "</h2>" + texts[i] + "<a class='tp-vice' href='" + links[i] + "'>více</a></div><img src='" + images[i] + "' alt='' /></div>");
			}	
		}
		bind_cycle ();
	}
	else {
		$('#main-panel').hide();
		$('#main').css('padding-top','30px');
	}
}

/*
prepis HTML kodu pro cycle2
vstup: ul li seznam s obrazky a texty
vystup: layout v divech
*/
function bind_cycle_code2 ()
{
	var images = new Array();
	var headers = new Array();
	var texts = new Array();
	var links = new Array();

	if ($('#cycle2 ul li').length > 0)
	{
		// pole s obrazky
		$("#cycle2 ul li img").each(
			function()
			{
				images.push($(this).attr("src"));
			}
		);

		// pole s nadpisy
		$("#cycle2 ul li h2").each(
			function()
			{
				var str = $(this).text();
				if(str != "")
					headers.push(str);
			}
		);

		// pole s texty
		$("#cycle2 ul li p").each(
			function()
			{
				var str = $(this).text();
				if(str != "")
					texts.push(str);
			}
		);

		// pole s odkazy
		$("#cycle2 ul li a").each(
			function()
			{
                links.push($(this).attr("href"));
			}
		);

		// smazani ul li seznamu
		$("#cycle2 ul").remove();

		// novy layout v divech
		if(images.length == texts.length)
		{
			for (var i = 0; i < images.length; i++)
			{
				$("#cycle2").append("<div><div class='abs cont2'></div><div class='abs cont-content2 excerpt wysiwyg'><h2>" + headers[i] + "</h2>" + texts[i] + "<a class='tp-vice' href='" + links[i] + "'>více</a></div><img src='" + images[i] + "' alt='' /></div>");
			}
		}
		bind_cycle2 ();
		$("#main-panel").hide();
		$('#main').css('padding-top','30px');
	}
	else {
		$('#topprodukt-panel').hide();
	}
}

function bind_path () {
    $('<span class="separator">/</span>').insertBefore('#path ul li:not(.first, .last, .depth2)');
}

