$(document).ready(function(){	
	bind_hover ();
	bind_search ();
	bind_menu ();
	/*replacePhotos();*/
	//showArticle();
	showThumbs();
	bind_cycle('akce', 5000);	
	bind_cycle('prace', 8000);
});

/**
 * hover efekt na menu
 **/
function bind_hover ()
{
	$(".menu li, .path li, .submenu 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('');
	});
}


/**
 * menu
 **/
function bind_menu ()
{
	$("#treemenu ul li").each(function()
	{
		if($(this).hasClass("sub"))
		{
			$(this).addClass("parent");
		}
		else
		{
			$(this).addClass("child");
		}
	});

	$('#treemenu').menuTree({ 
		expandSpeed: 300,
		collapseSpeed: 300,
		multiOpenedSubMenu: false		
	});
}


/**
 * fotogalerie produktu
 **/ 
function replacePhotos()
{ 
	$('.thumbs a').each(function () {
		
		
		// po kliknuti prohodi src se zvetsenym obrazkem a nastavi mu spravnou koncovku za ?
		// to se provede jednoduse regexpem
		$(this).click(function () {
			var large = $('#large img').attr('src');
			var thumb = $('img', this).attr('src');

			//bez animace
			//$('#large img').attr('src', thumb.replace('refright', 'reflarge'));
			//$('img', this).attr('src', large.replace('reflarge', 'refright'));
			
			//nahradi miniaturu
			$('img', this).fadeOut("normal", function() {
				$(this).attr('src', large.replace('reflarge', 'refright'));
			});
			$('img', this).fadeIn("normal");

			
			//nahradi velkou
			$('#large img').fadeOut("normal", function() {
				$('#large img').attr('src', thumb.replace('refright', 'reflarge'));	
			});
			$('#large img').fadeIn("normal");
			
			return false; 
		});
	});
	
	var i = 1;
	$('.article5').each(function () {
		
		if(i%4 != 0) 
			$(this).css('margin-right', '1px');
			
		i++;
	}); 
} 

/**
 * dle ID kliknuteho nahledu zviditelni cely clanek
 **/ 
function showArticle()
{
	$('.thumbnail a').click(function ()
	{
		var id_thumb = $(this).parent().attr('id');
		var id_detail = id_thumb.replace('thumb', 'detail');
		$('.content .detail').not('.dn').not('#' + id_detail).toggleClass('dn'); //zabranuje rozbaleni vice detailu
		
		
		
		$('#' + id_detail).toggleClass('dn');
		return false;	//aby se po kliknuti neprechazelo na jinou stranku
	});
}

/**
 * pred prvni clanek vlozi novy DIV, do ktereho premisti prvni obrazky ze vsech clanku na strance (vznikne pruh s nahledy)
 **/ 
function showThumbs()
{
	if($('.article5').length > 0)
	{
		$('.content .article5:first').before('<div id="thumbs" class="gallery"><ul class="thumbs noscript"></ul></div><div class="cb"></div><div id="controls"></div><div id="loading"></div><div id="slideshow"></div><div id="caption"></div>');
		$('.thumbnail a').each(function() 
		{	
			var id_thumb = $(this).parent().attr('id');
			var id_detail = id_thumb.replace('thumb', 'detail');
			
			$(this).parent().replaceWith(this);		//odstranim wrapujici DIV
			$(this).appendTo('#thumbs ul').wrap('<li />');		//vlozim do seznamu a obalim <li>
			$(this).parent().append('<div class="caption"></div>');
			$('#' + id_detail).appendTo($(this).parent().find('.caption'));
			
		});	
		
		$('#thumbs').galleriffic({
	        delay:                     5000, // in milliseconds
	        numThumbs:                 6, // The number of thumbnails to show page
	        preloadAhead:              40, // Set to -1 to preload all images
	        enableTopPager:            true,
	        enableBottomPager:         false,
	        maxPagesToShow:            1,  // The maximum number of pages to display in either the top or bottom pager
	        imageContainerSel:         '#slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
	        controlsContainerSel:      '#controls', // The CSS selector for the element within which the slideshow controls should be rendered
	        captionContainerSel:       '#caption', // The CSS selector for the element within which the captions should be rendered
	        loadingContainerSel:       '#loading', // The CSS selector for the element within which should be shown when an image is loading
	        renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
	        renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
	        playLinkText:              'Spustit přehrávání',
	        pauseLinkText:             'Zastavit přehrávání',
	        prevLinkText:              'Předchozí',
	        nextLinkText:              'Další',
	        nextPageLinkText:          'Další &rsaquo;',
	        prevPageLinkText:          '&lsaquo; Předchozí',
	        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
	        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
	        autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
	        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
	        defaultTransitionDuration: 2000 // If using the default transitions, specifies the duration of the transitions
	        
		});	
	}	    
}

function bind_cycle(suffix, timeout)
{
	var id_rotace = '#rotujici-' + suffix;
	var id_buttons = 'buttons-' + suffix;
	
	if ($(id_rotace + ' dl dd img').length > 1)
	{
		$(id_rotace + ' dd div').after('<ul id="' + id_buttons + '"  class="buttons"></ul>').cycle({ 
			fx:     'fade', 
			speed:  1000,
			pause: 1,  
			timeout: timeout, 
			startingSlide: 0,
			pager:  '#' + id_buttons, 
		 
			// callback fn that creates a thumbnail to use as pager anchor 
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li><a href="#">'+(idx+1)+'</a></li>'; 
			}
		});
	}
}
