$(document).ready( function() {
	
	$('#content-tab .bookmarks').tabify();

	/* slide colors product */
	if($('.colors-jcarousel #carousel-slide').length > 0)
		 $('.colors-jcarousel #carousel-slide').jcarousel();

	/* slide similar product */
	if($('.product-list #carousel-slide').length > 0)
		 $('.product-list #carousel-slide').jcarousel();

	$("a.fancybox").fancybox({
		'zoomSpeedIn'   : 300,
		'zoomSpeedOut'  : 300,
		'titlePosition'	: 'inside',
		'overlayShow'   : true
	});

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

  $("a.fancybox-html").fancybox({
    'padding'        : 10,
    'autoScale'      : false,
    'width'          : 500,    
    'scrolling'   	 : 'no',
    'type'           : 'iframe'
  });
	
	$("a.form-href-submit").click( function (){
	  $(this).parents('form').submit();
		return false;
	});

	$('#slideshow').cycle({
		fx: 'fade',
		speed: 700,
		timeout:  0,
		pager: '#pager'
	});

	$('#banner').cycle({
		fx: 'fade', // transition fx
		speed: 700, // transitions speed
		pause: 1000 // pause between slides
	});

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

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

	$("a.product-changer").click(function(){
		var o = $(this);
		var img = o.find("img").attr('src').replace("_s.",".");
		var img_b = o.find("img").attr("src").replace("_s.","_big.");
		var product_id = o.attr("href").replace("#product-","");
		o.parent().parent().find('li').removeClass('active');
		o.parent().addClass('active');

		$.ajax({
			dataType: 'html',
			url: BreezyCMS_URL + "produkt/shapes/" + product_id + '/',
			success : function (result) {
				$('#product-shapes').html(result).ready(function(){
					$('#product-shapes select').uniform();
 					$('#basket-shape').attr('value', $("#vase_shape_id").val());					
					update_product_detail(product_id, $("#vase_shape_id").val());
				});

				$("#vase_shape_id").change(function(){
					update_product_detail(product_id, $("#vase_shape_id").val());
					$('#basket-shape').attr('value', $(this).val());					
					return false;
				});
				return;
			}
		});


		$.ajax({
			dataType: 'html',
			url: BreezyCMS_URL + "produkt/slideshow/" + product_id+ '/',
			success : function (result) {
				$('#slideshow-product').html(result);
				jQuery('#slideshow').cycle({
					fx: 'fade',
					speed: 700,
					timeout:  0,
					pager: '#pager'
				});
				$("a.fancybox").fancybox({
					'zoomSpeedIn': 300,
					'zoomSpeedOut': 300,
					'titlePosition'	: 'inside',
					'overlayShow' : true
				});
				return;
			}
		});

		$('#basket-product').attr("value", product_id);
		$('#basket-shape').attr('value', $("#vase_shape_id").val());

		return false;
	});

	$("a.product-changer").first().trigger('click');

});

function update_product_detail(product_id, vase_shape_id){
	$.ajax({
		url: BreezyCMS_URL + "produkt/availability/" + product_id+ '/' + vase_shape_id + '/' ,
		success : function (result) {
			$('#product-availability').html(result);
			return;
		}
	});

	$.ajax({
		url: BreezyCMS_URL + "produkt/availability_class/" + product_id+ '/' + vase_shape_id + '/',
		success : function (result) {
			$('#product-availability').attr('class', result);
			return;
		}
	});
	$.ajax({
		url: BreezyCMS_URL + "produkt/delivery/" + product_id+ '/' + vase_shape_id + '/' ,
		success : function (result) {
			$('#product-delivery').html(result);
			return;
		}
	});
}
