function carousel_control(carousel_var,id,per_page) {

	// Po overu se zastavi skrolovani	
	carousel_var.clip.hover(function() {
      	carousel_var.stopAuto();
	}, function() {
      	carousel_var.startAuto();
  	});

	jQuery(id).bind('click', function() {
		jQuery(this).parent().find('a').removeClass('active');
		jQuery(this).addClass('active');
		var index = parseInt(jQuery(this).find('span').html());
		var moveto = (index-1)*per_page+1;
		carousel_var.scroll(jQuery.jcarousel.intval(moveto));
		carousel_var.startAuto(0);		
		return false;
	});
}

function mycarousel_initCallback_gallery(carousel) {carousel_control(carousel,'.product-images .jcarousel-control a',1)};
function mycarousel_initCallback_all_cat(carousel) {carousel_control(carousel,'#all_cat .jcarousel-control a',3)};
function mycarousel_initCallback_pneus(carousel) {carousel_control(carousel,'#pneus .jcarousel-control a',3)};
function mycarousel_initCallback_alu_kola(carousel) {carousel_control(carousel,'#alu_kola .jcarousel-control a',3)};
function mycarousel_initCallback_plechy(carousel) {carousel_control(carousel,'#plechy .jcarousel-control a',3)};
function mycarousel_initCallback_komplety(carousel) {carousel_control(carousel,'#komplety .jcarousel-control a',3)};
function mycarousel_initCallback_prislusenstvi(carousel) {carousel_control(carousel,'#prislusenstvi .jcarousel-control a',3)};

/*******************************************************************************************************************
*  If element exist return true                                                                                    *
*******************************************************************************************************************/
function isElement(element){
     
    var result;
    if($(element).length > 0){ result = true; }else{ result = false }  
    return result;
   
}

jQuery(document).ready(function() {

	if (Cufon && Cufon.replace) {
		Cufon.replace('h1', { fontFamily: 'Helvetica LT Std Light' });
		Cufon.replace('.cart h2.title,#content .article h2', { fontFamily: 'Helvetica LT Std Light' });
		Cufon.replace('.offer-title', { fontFamily: 'Helvetica LT Std Light' });
		Cufon.replace('p.anotation', { fontFamily: 'Helvetica LT Std Roman' });
		Cufon.replace('.block h4', { fontFamily: 'Helvetica LT Std Roman' });
		Cufon.replace('strong.title', { fontFamily: 'Helvetica LT Std Roman' });
		Cufon.replace('.forms h2', { fontFamily: 'Helvetica LT Std Light' });
		Cufon.replace('.complete-info h2', { fontFamily: 'Helvetica LT Std Roman' });
		
	}
	
	if (isElement(jQuery(".tab-ord-summary"))) {
	 	jQuery(".tab-ord-summary tr :last-child").addClass("last-child");
	}
	
	if (isElement(jQuery(".two-cols table .relative.fix-buttons"))) {
		jQuery(".two-cols table .relative").each(function(){
			var td_height = jQuery(this).parent().height();
			jQuery(this).height(parseInt(td_height-75));
		});
	}
	
	if (isElement(jQuery(".roll-tabs"))) {
		jQuery('.roll-tabs .minus').parent().hide();
		jQuery('.roll-tabs .plus').bind('click',function(){
//			jQuery(jQuery(this).attr('href')).slideDown(700);
			jQuery(jQuery(this).attr('href')).show(700);
			jQuery('.roll-tabs .plus').parent().hide();
			jQuery('.roll-tabs .minus').parent().show();
			return false;
		});

		jQuery('.roll-tabs .minus').bind('click',function(){
//			jQuery(jQuery(this).attr('href')).slideUp(700);
			jQuery(jQuery(this).attr('href')).hide(700);
			jQuery('.roll-tabs .minus').parent().hide();
			jQuery('.roll-tabs .plus').parent().show();
			return false;
		});
	}
	
	// Zaskrtne / odskrtne vsechny checkboxy
	if (isElement(jQuery("#check_all"))) {
		jQuery("#check_all").click(function() {
			jQuery(this).parents('.producers-inner').find(':checkbox').attr('checked', this.checked);
		});
	}
	// Fancybox - 3D zobrazeni náhledu pneumatik
	if (isElement(jQuery(".link3d"))) {
		$("a.link3d").fancybox({
		    'padding'           : 0,
		    'width'				: 340,
	        'autoScale'     	: false,
	        'transitionIn'		: 'none',
			'transitionOut'		: 'none'
		});
	}
	
// Fancybox - náhledy barev na vozidle
if (isElement(jQuery(".link-color"))) {
	$("a.link-color").fancybox({
        'padding' : 0,
        'width' : 340,
        'autoScale' : false,
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'ajax': { cache: false }
    });
}

    /************************************** j(Query)carousel ***********************************/
/* verze se skrolovanim
    var autoscroll_delay = 1;
    var animation_speed = 1500;
	jQuery("#all_cat .slider").jcarousel({animation: animation_speed,auto: autoscroll_delay,wrap: "circular",scroll:3,dispItems:3,initCallback: mycarousel_initCallback_all_cat,buttonNextHTML: null,buttonPrevHTML: null});
    jQuery("#pneus .slider").jcarousel({animation: animation_speed,auto: autoscroll_delay,wrap: "circular",scroll:3,dispItems:3,initCallback: mycarousel_initCallback_pneus,buttonNextHTML: null,buttonPrevHTML: null});
	jQuery("#alu_kola .slider").jcarousel({animation: animation_speed,auto: autoscroll_delay,wrap: "circular",scroll:3,dispItems:3,initCallback: mycarousel_initCallback_alu_kola,buttonNextHTML: null,buttonPrevHTML: null});
	jQuery("#plechy .slider").jcarousel({animation: animation_speed,auto: autoscroll_delay,wrap: "circular",scroll:3,dispItems:3,initCallback: mycarousel_initCallback_plechy,buttonNextHTML: null,buttonPrevHTML: null});
	jQuery("#komplety .slider").jcarousel({animation: animation_speed,auto: autoscroll_delay,wrap: "circular",scroll:3,dispItems:3,initCallback: mycarousel_initCallback_komplety,buttonNextHTML: null,buttonPrevHTML: null});
	jQuery("#prislusenstvi .slider").jcarousel({animation: animation_speed,auto: autoscroll_delay,wrap: "circular",scroll:3,dispItems:3,initCallback: mycarousel_initCallback_prislusenstvi,buttonNextHTML: null,buttonPrevHTML: null});

*/
	jQuery("#all_cat .slider").jcarousel({scroll:3,dispItems:3,initCallback: mycarousel_initCallback_all_cat,buttonNextHTML: null,buttonPrevHTML: null});
    jQuery("#pneus .slider").jcarousel({scroll:3,dispItems:3,initCallback: mycarousel_initCallback_pneus,buttonNextHTML: null,buttonPrevHTML: null});
	jQuery("#alu_kola .slider").jcarousel({scroll:3,dispItems:3,initCallback: mycarousel_initCallback_alu_kola,buttonNextHTML: null,buttonPrevHTML: null});
	jQuery("#plechy .slider").jcarousel({scroll:3,dispItems:3,initCallback: mycarousel_initCallback_plechy,buttonNextHTML: null,buttonPrevHTML: null});
	jQuery("#komplety .slider").jcarousel({scroll:3,dispItems:3,initCallback: mycarousel_initCallback_komplety,buttonNextHTML: null,buttonPrevHTML: null});
	jQuery("#prislusenstvi .slider").jcarousel({scroll:3,dispItems:3,initCallback: mycarousel_initCallback_prislusenstvi,buttonNextHTML: null,buttonPrevHTML: null});


    /************************************** j(Query) select(menu) ***********************************/

	// Inicializuji selectboxy, pokud nejsem na strance kde je selectbox v shadowboxu
	if (!isElement(jQuery(".link_change"))) {
		jQuery('select').selectmenu({style:'dropdown',maxHeight:220});	
	} else {
		var link_change_clicked = false;
		jQuery(".link_change").click(function(){
			var cont_var = jQuery('#box').html();
			Shadowbox.init({handleOversize:"resize"});
			Shadowbox.open({content:cont_var,player: "html",options:
				{onFinish: function () {jQuery('select').selectmenu({style:'dropdown',maxHeight:220});}
				,onClose : function() { jQuery('select').selectmenu('destroy'); }
			}});
		});
	}

	// Přizpůsobíme výšky bloků s akčním zbožím
	
	if (!isElement(jQuery(".product-detail"))) {

		// Přizpůsobíhne výšku položek javascriptem
		if (isElement(jQuery(".left .shop-offer"))) {
		
			var max_desc_height;
	
			function check_max_height(object) {
				desc_height = jQuery(object).find('span.size').height(); 
				if (desc_height != null && desc_height > max_desc_height) {
					max_desc_height = desc_height;
				}
			}
			
			function set_item_height(object) {
				var height = parseInt(219 + max_desc_height);
				jQuery(object).css('height',height);
			}
	
			jQuery(".left .shop-offer li:nth-child(3n - 2)").each(function(){
	
				max_desc_height = 0;
				
				check_max_height(jQuery(this));
				check_max_height(jQuery(this).next());
				check_max_height(jQuery(this).next().next());
				
				set_item_height(jQuery(this));
				set_item_height(jQuery(this).next());
				set_item_height(jQuery(this).next().next());
				
			});
		}
	
		if (isElement(jQuery(".article-tabs .tab-content .shop-offer"))) {
			jQuery(".article-tabs .tab-content .shop-offer").each(function(){
				var max_item_height = 0;
				jQuery(this).find('li').each(function() {
					var item_height = jQuery(this).height();
	
					if (item_height != null && item_height > max_item_height) {
						max_item_height = item_height;
					}
				});
				jQuery(this).parent().parent().parent().css('height',max_item_height-7);
				jQuery(this).parent().parent().parent().parent().css('height',max_item_height+3);
				jQuery(this).parent().parent().parent().parent().parent().css('height',max_item_height+3);
			});
		}
	}	

	jQuery("a.targetblank").attr('target','_blank');
	
	// Vygenerování HTML pro kroků číslovaný seznam
	var krok = 1;
	jQuery(".steps").each(function(){
		jQuery(this)
		.css("listStyleType", "none")
		.find("> li").each(function(i){
			jQuery(this).prepend('<span class="step">'+ (krok++) +'</span>');
		});
	});
	
	jQuery("#search, .search").focus(function(event){
		if (jQuery(this).attr('title') == (jQuery(this).attr('value'))) {
			jQuery(this).attr('value','');
		}
	});
		
	jQuery("#search, .search").blur(function(event){
		if (jQuery(this).attr('value') == '') {
			jQuery(this).attr('value',jQuery(this).attr('title'));
		}
	});

	jQuery("#categories > li:not('.simple')").mouseover(function(){jQuery(this).addClass("hover");});
	jQuery("#categories > li:not('.simple')").mouseout(function(){jQuery(this).removeClass("hover");});
	jQuery(".article-tabs").tabs({selected:0})
	jQuery(".link-help").mouseover(function(){jQuery(this).next().show();});
	jQuery(".link-help").mouseout(function(){jQuery(this).next().hide();});

	jQuery(".shop-offer.noscroll li:nth-child(3n)").addClass('mod3');
	
	jQuery(".link-login").click(function(){
		if (jQuery(this).hasClass('active')) {
			jQuery("#reg-info").slideUp('fast');
			jQuery(this).removeClass('active');
		} else {
			jQuery("#reg-info").slideDown('medium');
			jQuery(this).addClass('active');
		
		}
		return false;
	});
		
	if (isElement(jQuery("#menu-sub ul"))) {
		var max_height = 0;
		jQuery("#menu-sub > li").each( function() {
			var height = jQuery(this).height();
			if (height > max_height) {
				max_height = height;
			}
		});

		jQuery("#menu-sub > li").each( function() {
			jQuery(this).height(max_height);
		});
	}	
	
	/******************** Mapa prodejny v registraci ************************/
	if (isElement(jQuery(".link-map"))) {
		jQuery(".link-map").click(function(){
			jQuery(".link-map").removeClass('hidden');
			jQuery(this).addClass('hidden');
			if (jQuery(this).hasClass('show')) {
				jQuery(".map").show();			
			} else {
				jQuery(".map").hide();			
			}
			return false;
		});
	}


	/******************** Alu kola detail ************************/
	if (isElement(jQuery(".list-colors"))) {
		var list_colors_used = false
		jQuery(".list-colors li a").click( function() {
		
			var ths = jQuery(this);
			var list_cars = ths.parent().parent().next();
			
			var index = parseInt(ths.parent().index());
			
			if(list_colors_used == false) {
				if (!ths.hasClass('active')) {
					list_colors_used = true;
					$(list_cars).find('ul li.active').fadeOut(450, function() {
						$(this).removeClass('active');

						var li_activate = jQuery(list_cars).find("li:nth-child("+(index+1)+")");
						
						jQuery(ths).parent().parent().find('a').removeClass('active');
						jQuery(ths).addClass('active');
						
						$(li_activate).fadeIn(400, function() {
							$(li_activate).addClass('active');
							list_colors_used = false;
						});
					});
				}
			}
			return false;
		});
	}

	if (isElement(jQuery(".bl-blue.form input[type=text], .bl-blue.form input[type=password]"))) {
		jQuery(".bl-blue.form input[type=text], .bl-blue.form input[type=password]").wrap('<div class="inputFolder" />');
		jQuery(".bl-blue.form .inputFolder").each(function(){
			$(this).css('background-image',$(this).children("input").css("background-image"));
			$(this).children("input[type=text], input[type=password]").css("background-image","none")
		})
	}

	if (isElement(jQuery(".reg.form-filter input[type=password],.reg.form-filter input[type=text]"))) {
		jQuery(".reg.form-filter input[type=password],.reg.form-filter input[type=text]").wrap('<div class="inputFolder" />');
		jQuery(".reg.form-filter .inputFolder").each(function(){
			$(this).css('background-image',$(this).children("input").css("background-image"));
			$(this).children("input[type=text], input[type=password]").css("background-image","none")
		})
	}

	if (isElement(jQuery("#user-account-info input[type=password],#user-account-info input[type=text]"))) {
		jQuery("#user-account-info input[type=password],#user-account-info input[type=text]").wrap('<div class="inputFolder" />');
		jQuery("#user-account-info .inputFolder").each(function(){
			$(this).css('background-image',$(this).children("input").css("background-image"));
			$(this).children("input[type=text], input[type=password]").css("background-image","none")
		})
	}

	if (isElement(jQuery("#reg-info input[type=password], #reg-info input[type=text]"))) {
		jQuery("#reg-info input[type=password], #reg-info input[type=text]").wrap('<div class="inputFolder" />');
		jQuery("#reg-info .inputFolder").each(function(){
			$(this).css('background-image',$(this).children("input").css("background-image"));
			$(this).children("input[type=text], input[type=password]").css("background-image","none")
		})
	}

	if (isElement(jQuery("#form-pass input[type=password], #form-pass input[type=text]"))) {
		jQuery("#form-pass input[type=password], #form-pass input[type=text]").wrap('<div class="inputFolder" />');
		jQuery("#form-pass .inputFolder").each(function(){
			$(this).css('background-image',$(this).children("input").css("background-image"));
			$(this).children("input[type=text], input[type=password]").css("background-image","none")
		})
	}
	if (isElement(jQuery("#packages input[type=text]"))) {
		jQuery("#packages input[type=text]").wrap('<div class="inputFolder" />');
		jQuery("#packages .inputFolder").each(function(){
			$(this).css('background-image',$(this).children("input").css("background-image"));
			$(this).children("input[type=text], input[type=password]").css("background-image","none")
		})
	}

	if (isElement(jQuery(".type-list"))) {
		jQuery(".type-list input[type=checkbox]").change(function(){
			var state = jQuery(this).attr("checked");
			if (state == "checked") {
				jQuery("#type_list_form").attr("value","1");
			} else {
				jQuery("#type_list_form").attr("value","0");
			}
		});
	}

/*
	if (isElement(jQuery(".list-colors"))) {
		jQuery(".list-colors a").click( function() {
			var ths = $(this);
			if (!ths.hasClass('active')) {
				$('.list-colors a').removeClass('active');
				$('.list-cars li.active').fadeOut(600, function() {
					$(this).removeClass('active');
					var index = $(ths).attr("title");									
					var li_class = ".list-cars .item-"+index;
					$(ths).addClass('active');
					$(li_class).fadeIn(400, function() {
						$(li_class).addClass('active');
					});
				});
			}				
			return false;
		});
	}
*/
	
	if (isElement(jQuery(".listInfo"))) {
		$(".listInfo li:nth-child(4n+1)").addClass("it-left");
	}

});
