$(document).ready(function () {

  	// Fancybox  ---------------------------------------------------------------------------------------------------
		$("a.fancybox").fancybox({
			'zoomSpeedIn': 300,
			'zoomSpeedOut': 300,
      'titlePosition'	: 'over',
			'overlayShow' :true
		});

		$("a.fancybox-ajax").fancybox({
			'type': 'iframe',
			'zoomSpeedIn': 300,
			'zoomSpeedOut': 300,
			'width': 550,
			'height': 400,
			'overlayShow' :true,
			'hideOnContentClick': false
		});

   	// Fancybox  ---------------------------------------------------------------------------------------------------
    $('.input, .textarea').focus(function () {
      $(this).parent().addClass('focus');
      $(this).parent().parent().addClass('show-help');
    }).blur(function () {
      $(this).parent().removeClass("focus");
      $(this).parent().parent().removeClass('show-help');
      }
    );

  	// BOOKMARKS  ---------------------------------------------------------------------------------------------------
		$('#content-tab .bookmarks li').click(function() {
		    $('#content-tab .bookmarks li.active-before').removeClass("active-before");
		    $('#content-tab .bookmarks li.active-before-first').removeClass("active-before-first");
		    $('#content-tab .bookmarks li.active').removeClass("active");
		    $('#content-tab .bookmarks li.active-after').removeClass("active-after");
		    $(this).addClass("active");
		    $(this).next().addClass("active-after");
		    $(this).prev().addClass("active-before");
		    
		    $('.content-item-visible').removeClass("content-item-visible");
		    var ind = $("#content-tab .bookmarks li").index(this);
		    tabIndex = ind;
		    $(".content-item:eq("+ind+")").addClass("content-item-visible");
		});

    // Featured Rotation  ---------------------------------------------------------------------------------------------------
    var featuredContainer = $('.imageProduct div.imageBig > div');
    featuredContainer.hide().filter(':first').show();
    
    $('.imageProduct div.thumb a').click(function () {
      featuredContainer.hide();
      featuredContainer.filter(this.hash).show();
      $('.imageProduct div.thumb a').removeClass('selected');
      $(this).addClass('selected');
      return false;
    }).filter(':first').click();
    
    $(".imageProduct div.imageBig .ico-thumb").click(function(){
      href = $(this).attr('href');      
      $(href).children("a:first").click();
      return false;
    })


   // Otevreni v odkazu v novem okne ---------------------------------------------------------------------------------------
   $("a.ext").click(function(){
     return ! window.open(this.href);
   });
});

 // Cufon replace ---------------------------------------------------------------------------------------------------
 Cufon.replace('h1, .box h2, .box h3, .titleBox h2, .order-step a'); // Works without a selector engine
 
// prekresli texty cufonem po nacteni obsahu pres ajax
$(document).ajaxSuccess(function() {
  Cufon.refresh();
});

// products - fulltext search
$.widget( "custom.catcomplete", $.ui.autocomplete, {
	_renderMenu: function( ul, items ) {
		var self = this,
		currentCategory = "";
		$.each( items, function( index, item ) {
			if ( item.category != currentCategory ) {
				ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
				currentCategory = item.category;
			}
			return $( "<li" + ((item.last) ? " class=\"last\"" : "") +"></li>" )
			.data( "item.autocomplete", item )
			.append( $( "<a></a>" ).text( item.label ) )
			.appendTo( ul );
		});
	}
});

// products - init fulltext search
function initProductsFulltextSearchAutocomplete()
{    
	$( "#products_fulltext_search_form #q" ).catcomplete({
		delay: 0,
		source: BreezyCMS_URL + "produkt/suggest/",
		select: function(event, ui) {
			if (ui.item.type == 'product')
				window.location.href = BreezyCMS_URL + "produkt/" + ui.item.id;
			else if (ui.item.type == 'category')
				window.location.href = BreezyCMS_URL + "kategorie/" + ui.item.id;
		}
	});
}

// nastavi swip predanemu elementu
function setSwip($element, message) {
  $element.focus(function(){
    swip($element, message, "");
  });
      
  $element.blur(function(){
    swip($element, "", message);
  });
}

// swipnuti hodnoty
function swip($element, swipFrom, swipTo) {
  if ($element.val() == swipFrom) 
    $element.val(swipTo);
}
