$(document).ready( function() {
	// SLIDESHOW
	if($('#carousel-slide').length > 0)
		$('#carousel-slide').jcarousel();

	// 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-tab .content-item-visible').removeClass("content-item-visible");
		var ind = $("#content-tab .bookmarks li").index(this);
		tabIndex = ind;
		$("#content-tab .content-item:eq("+ind+")").addClass("content-item-visible");
	});

	// BOOKMARKS SUBMENU
	$('#box-tab .bookmarks li').click(function() {
		$('#box-tab .bookmarks li.active').removeClass("active");
		$(this).addClass("active");

		$('#box-tab .content-item-visible').removeClass("content-item-visible");
		var ind = $("#box-tab .bookmarks li").index(this);
		tabIndex = ind;
		$("#box-tab .content-item:eq("+ind+")").addClass("content-item-visible");
	}); 

	// PRODUCT IMAGE 
	$('.image-product .thumbs li').click(function() {
		$('.image-product .thumbs a').removeClass("active");
		$(this).addClass("active");

		$('.image-product .image-big li').fadeOut();
		var img = $(".image-product .thumbs li").index(this);
		$(".image-product .image-big li:eq("+img+")").fadeIn('slow');
	});

	// FOCUS
	$('.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');
	}
	);

	// select size
	$(function(){
		$(".size select").uniform();
	});

	// registrace, editace
	if ($("#users_register_form").length || $("#edit_user").length) {

		// skryti firemnich udaju, pokud nejsou potreba
		if ( ! $("#companyShow").attr("checked"))
			$("#companyForm").hide();

		$('#companyShow').click(function() {
			if($(this).is(':checked')) {
				$('#companyForm').show('normal');
			} else {
				$('#companyForm').hide('normal');
			}
		});

		// skryti dorucovacich udaju, pokud nejsou potreba
		if ( ! $("#adressShow").attr("checked"))
			$("#adressForm").hide();

		$('#adressShow').click(function() {
			if($(this).is(':checked')) {
				$('#adressForm').show('normal');
			} else {
				$('#adressForm').hide('normal');
			}
		});
	}

	$("#products_size_id").change(function(){
		var product_id = 
		$.ajax({
			url: BreezyCMS_URL + "produkt/availability/" + $("#product_id").val()+ '/' + $("#products_size_id").val() + '/' ,
			success : function (result) {
				$('#product-availability').html(result);
				return;
			}
		});

		return false;
	});

	$("#products_size_id").trigger('change');
});

// 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;
			}
			//self._renderItem( ul, item );

			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;
		}
	});
}
