$(document).ready(function(){

	var counter = 0;
	var width = 0;
	var slider_inPosition;
	var boxWidth = $(".manufacturers ul").width(); 
	var clonsCount = 5; // pocet zobrazenych prvkov
	var boxCount_origin = $(".manufacturers ul li").size(); 
	var minus = boxCount_origin - 1;
	
	for (i=0; i<clonsCount; i++) {
		$('.manufacturers ul li:eq('+i+')').clone().insertAfter($('.manufacturers ul li:eq(' + minus + ')'));
		minus++;
	}
	
	for (i=0; i<boxCount_origin; i++) {
		width = width + $('.manufacturers ul li:eq('+i+')').width() + 7;
		minus++;
	}

	rotate = function(){
		counter++;
		slider_inPosition = counter * 3;
		$(".manufacturers ul").animate({
			marginLeft: - slider_inPosition
		}, 35, function(){
			if(counter*3 >= width){
				$(".manufacturers ul").css('marginLeft','0');
				counter = 0;
			}
			rotateManufacturers();
		});
	}; 

	rotateManufacturers = function(){
		play = setTimeout(function(){
			rotate();
		}, 35); // casovanie
	};

	rotateManufacturers();
	
	$('.popisok').each(function(){
		$(this).addClass("default");
		$(this).attr("value", this.title);
	});
	
	$('.popisok').focus(function() {
		$(this).removeClass("default").addClass("active");
		if (this.value == this.title){
			this.value = '';
		}
		if(this.value != this.title){
			this.select();
		}
	});
	
	$('.popisok').blur(function() {
		if (this.value == ''){
			$(this).removeClass("active").addClass("default");
			this.value = this.title;
		} else if (this.value == this.title){
			$(this).removeClass("active").addClass("default");
		}
	});
	
	$("a.fancy").fancybox({
		'titleShow' : true,
		'titlePosition' : 'over',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic'
	});
	
	$('a.blank').click(function(){
		window.open(this.href);
		return false;
	});

	
	$("#iframe").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
	});


	$(".topmenu li:not('.topmenu li ul li')").each(function(){
		
		if ($(this).children('div').children('ul').children('li').length != 0) {
			$(this).addClass('dropdown');
		}
	});
	
	$('.dropdown').hover(function() {
		$(this).addClass("hover");
	},function() {
		$(this).removeClass("hover");
	});	
	
	/*POSUVNY SLIDE PRODUKTOV*/
	var setup = 0;
	var clonsCount2 = 6; // pocet zobrazenych prvkov
	var boxCount_origin2 = $(".slide ul li").size(); 
	var minus2 = boxCount_origin2 - 1;
	var minus3 = minus2;
	$(".slide ul").css('marginLeft', -boxCount_origin2 * 181  + 'px');
	var marg;
	for (i=0; i<clonsCount2; i++) {
		$('.slide ul li:eq('+i+')').clone().insertAfter($('.slide ul li:eq(' + minus2 + ')'));
		minus2++;
	}
	var minus2 = boxCount_origin2 - 1;
	for (i=0; i<boxCount_origin2; i++) {
		$('.slide ul li:eq(' + minus2 + ')').clone().insertBefore($('.slide ul li:eq(0)'));
	}

	$('.arrowleft').click(function(){
		setup++;
		marg = setup * ($('.slide ul li').width() + 10) - (boxCount_origin2 * 181);
		//$(".slide ul").css('marginLeft', marg + 'px');
		$(".slide ul").animate({marginLeft: marg + 'px'}, 200);
		if(setup==boxCount_origin2){
			$(".slide ul").animate({marginLeft: marg + 'px'}, 200, function(){$(".slide ul").css('marginLeft', -boxCount_origin2 * 181  + 'px');return false;});
			setup = 0;
		}
		return false;
	});

	$('.arrowright').click(function(){
		setup--;
		marg = setup * ($('.slide ul li').width() + 10) - (boxCount_origin2 * 181);
		//$(".slide ul").css('marginLeft', marg + 'px');
		$(".slide ul").animate({marginLeft: marg + 'px'}, 200);
		if(setup==-boxCount_origin2){
			//$(".slide ul").css('marginLeft', -boxCount_origin2 * 160  + 'px');
			$(".slide ul").animate({marginLeft: marg + 'px'}, 200, function(){$(".slide ul").css('marginLeft', -boxCount_origin2 * 181  + 'px');return false;});
			setup = 0;
		}
		return false;
	});	/*end slide*/
	
	$('#category-menu a.active').parent('li').children('ul').css('display', 'block');
	
	$("#category-menu a.active").each(function(){
		if($(this).parent("li").children("ul").length) {
		    $(this).addClass("parent");
		} 
	});

	$('#get-price').click(function(){$('html').animate({scrollTop: $("#tab-box2-05").offset().top}, 1000)});

    $('.filter-form').submit(function() {
		if ($('#keywords').val() ==  $('#keywords').attr('title')) {$('#keywords').val('');}
    });
	
});

function valideForm(handler) {
	
	// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		return value != element.defaultValue;
	}, "");
	
	jQuery.validator.messages.required = "";
	$(handler).validate({
		invalidHandler: function(e, validator) {
		},
		onkeyup: false,
		submitHandler: function() {
			form.submit();
		}
	});
}

