$(document).ready(function(){

	//------------------------------------------------------------------------
	// Hover menu
	//------------------------------------------------------------------------
	$($('#hoverTopMenu').html()).insertAfter('#nav-main a[href*=/produkty/]');

	//------------------------------------------------------------------------
	// Tooltip v hlavicke na uvode
	//------------------------------------------------------------------------
	$('.ttp').css('opacity',0);
	$('.ttp-pt').each(function(){
		$(this).css('left', parseInt($(this).attr('name').substr(1,$(this).attr('name').length-1)) + 'px');
		$(this).css('top', parseInt($(this).attr('rel').substr(1,$(this).attr('rel').length-1)) + 'px');
	});
	setInterval(function(){
		$('.ttp-pt').each(function(){
			$(this).children('i').fadeOut(700, function(){$(this).fadeIn(700);});
		});
	}, 1500);
	$('.ttp-pt').hover(function(){
		$tooltip = $(this).next('.ttp');
		$tooltip.css('top', parseInt($(this).attr('rel').substr(1,$(this).attr('rel').length-1)) - 10 + 'px');
		if(parseInt($(this).attr('name').substr(1,$(this).attr('name').length-1))>540) {
			$tooltip.addClass('lft');
			$tooltip.css('left', parseInt($(this).attr('name').substr(1,$(this).attr('name').length-1)) - 415 + 'px');
		}
		else {
			$tooltip.css('left', parseInt($(this).attr('name').substr(1,$(this).attr('name').length-1)) + 25 + 'px');
		}
		$tooltip.css('display','block').stop('true','true').animate({opacity: 1},125);
	}, function(){
		$tooltip = $(this).next('.ttp');
		$tooltip.stop('true','true').animate({opacity: 0}, 125, function(){$(this).css('display','none')});
	});

	//------------------------------------------------------------------------
	// Tooltip v Kde kupit
	//------------------------------------------------------------------------
	$('.vmape li').hover(function(){
		$tooltip = $(this).children('.ttp');
		$tooltip.css('display','block').stop('true','true').animate({opacity: 1},125);
	}, function(){
		$tooltip = $(this).children('.ttp');
		$tooltip.stop('true','true').animate({opacity: 0}, 125, function(){$(this).css('display','none')});
	});

	//------------------------------------------------------------------------
	// Select produktu
	//------------------------------------------------------------------------
	$('.slct').click(function(){
		if(!$(this).hasClass('active')){
			$(this).addClass('active');
			$('.slc b, .slc ul').css('display','block');
		}
		else {
			$(this).removeClass('active');
			$('.slc b, .slc ul').css('display','none');
		}
		return false;
	});

	//------------------------------------------------------------------------
	// Posuvny slide produktov
	//------------------------------------------------------------------------
	//arrowslide #1
	
	var setup = 0;
	var clonsCount2 = 6; // pocet zobrazenych prvkov
	var boxCount_origin2 = $(".arrowslide ul li").size(); 
	var minus2 = boxCount_origin2 - 1;
	var boxwidth = 130;
	if($(".arrowslide_out").hasClass('wide')){
		var boxwidth = 199;
	}

	$(".arrowslide ul").css('marginLeft', -boxCount_origin2 * boxwidth  + 'px');
	var marg;
	for (i=0; i<clonsCount2; i++) {
		$('.arrowslide ul li:eq('+i+')').clone().insertAfter($('.arrowslide ul li:eq(' + minus2 + ')'));
		minus2++;
	}
	var minus2 = boxCount_origin2 - 1;
	for (i=0; i<boxCount_origin2; i++) {
		$('.arrowslide ul li:eq(' + minus2 + ')').clone().insertBefore($('.arrowslide ul li:eq(0)'));
	}

	$('.arrowleft').click(function(){
		setup++;
		marg = setup * ($('.arrowslide ul li').width() + 10) - (boxCount_origin2 * boxwidth);
		//$(".slide ul").css('marginLeft', marg + 'px');
		$(".arrowslide ul").stop(true,true).animate({marginLeft: marg + 'px'}, 300);
		if(setup==boxCount_origin2){
			$(".arrowslide ul").animate({marginLeft: marg + 'px'}, 300, function(){$(".arrowslide ul").css('marginLeft', -boxCount_origin2 * boxwidth  + 'px');return false;});
			setup = 0;
		}
		return false;
	});

	$('.arrowright').click(function(){
		setup--;
		marg = setup * ($('.arrowslide ul li').width() + 10) - (boxCount_origin2 * boxwidth);
		//$(".slide ul").css('marginLeft', marg + 'px');
		$(".arrowslide ul").stop(true,true).animate({marginLeft: marg + 'px'}, 300);
		if(setup==-boxCount_origin2){
			//$(".slide ul").css('marginLeft', -boxCount_origin2 * 160  + 'px');
			$(".arrowslide ul").animate({marginLeft: marg + 'px'}, 300, function(){$(".arrowslide ul").css('marginLeft', -boxCount_origin2 * boxwidth  + 'px');return false;});
			setup = 0;
		}
		return false;
	});
	
	//arrowslide #2
	
	/*

	var setup_2 = 0;
	var clonsCount2_2 = 6; // pocet zobrazenych prvkov
	var boxCount_origin2_2 = $(".arrowslide2 ul li").size(); 
	var minus2_2 = boxCount_origin2_2 - 1;
	var boxwidth_2 = 130;
	if($(".arrowslide_out").hasClass('wide')){
		var boxwidth_2 = 199;
	}	
	
	$(".arrowslide2 ul").css('marginLeft', -boxCount_origin2_2 * boxwidth_2  + 'px');
	var marg_2;
	for (i=0; i<clonsCount2_2; i++) {
		$('.arrowslide2 ul li:eq('+i+')').clone().insertAfter($('.arrowslide2 ul li:eq(' + minus2_2 + ')'));
		minus2++;
	}
	var minus2_2 = boxCount_origin2_2 - 1;
	for (i=0; i<boxCount_origin2_2; i++) {
		$('.arrowslide2 ul li:eq(' + minus2_2 + ')').clone().insertBefore($('.arrowslide2 ul li:eq(0)'));
	}

	$('.arrowleft2').click(function(){
		setup_2++;
		marg_2 = setup_2 * ($('.arrowslide2 ul li').width() + 10) - (boxCount_origin2_2 * boxwidth_2);
		$(".arrowslide2 ul").stop(true,true).animate({marginLeft: marg_2 + 'px'}, 300);
		if(setup_2==boxCount_origin2_2){
			$(".arrowslide2 ul").animate({marginLeft: marg_2 + 'px'}, 300, function(){$(".arrowslide2 ul").css('marginLeft', -boxCount_origin2_2 * boxwidth_2  + 'px');return false;});
			setup_2 = 0;
		}
		return false;
	});

	$('.arrowright2').click(function(){
		setup_2--;
		marg_2 = setup_2 * ($('.arrowslide2 ul li').width() + 10) - (boxCount_origin2_2 * boxwidth_2);
		//$(".slide ul").css('marginLeft', marg + 'px');
		$(".arrowslide2 ul").stop(true,true).animate({marginLeft: marg_2 + 'px'}, 300);
		if(setup==-boxCount_origin2){
			//$(".slide ul").css('marginLeft', -boxCount_origin2 * 160  + 'px');
			$(".arrowslide2 ul").animate({marginLeft: marg_2 + 'px'}, 300, function(){$(".arrowslide2 ul").css('marginLeft', -boxCount_origin2_2 * boxwidth_2  + 'px');return false;});
			setup_2 = 0;
		}
		return false;
	});		

	*/

	//------------------------------------------------------------------------
	// IE6
	//------------------------------------------------------------------------
	if(typeof document.body.style.maxHeight === "undefined") {
		$('body').append('<div class="warning"><p><strong>Váš internetový prehliadač je zastaralý. </strong>Nainštalujte si <a href="http://windows.microsoft.com/en-US/internet-explorer/products/ie/home">najnovšiu verziu</a></p></div>');
	}

	//------------------------------------------------------------------------
	// Accordion
	//------------------------------------------------------------------------
	$('.accordion li div').css('display','none');
	$('.accordion li.active div').css('display','block');
	$('.accordion li.active').prev().addClass('prev');
	$('.accordion li').first().addClass('first');

	$('.accordion li a').not('.accordion li div a').click(function(){
		if($(this).parent('li').hasClass('active')){
			$(this).siblings('div').slideUp(200 , function(){
				$(this).parent('li').removeClass('active');
				$('#scroll-box').jScrollPane({scrollbarWidth:6, showArrows:false});
			});
			$(this).parent('li').prev().removeClass('prev');
		}
		else {
			$(this).siblings('div').slideDown(200 , function(){
				$(this).parent('li').addClass('active');
				$('#scroll-box').jScrollPane({scrollbarWidth:6, showArrows:false});
			});
			$(this).parent('li').prev().addClass('prev');
		}
		return false;
	});

	//------------------------------------------------------------------------
	// Oble rozky na obrazkoch
	//------------------------------------------------------------------------
	$('img[align="left"]').addClass('left');
	$('img[align="right"]').addClass('right');

	//------------------------------------------------------------------------
	// Zalozky
	//------------------------------------------------------------------------
	$('.tabContent').css('display','none');
	$($('.tabMenu a.active').attr('href')).show();
	$('.tabContent').wrapInner('<div class="tab-in" />');
	$('.tabContent .tab-in').not($('.tabContent:visible .tab-in')).animate({opacity: 0}, 0);

	$('.tabMenu a').click(function(){
		$('.tabMenu a').removeClass('active');
		$(this).addClass('active');
		$active = $($(this).attr('href'));
		$active_content = $($(this).attr('href') + ' .tab-in');
		$('.tabContent:visible .tab-in').stop(true, true).animate({opacity: 0}, 500, function() {
			$('.tabContent:visible').css('display', 'none');
			$active.css('display', 'block');
			$active_content.animate({opacity: 1}, 500);
		});
		return false;
	});

	//------------------------------------------------------------------------
	// Mapa
	//------------------------------------------------------------------------
	$('.head ul li ul li:eq(0) a, .head ul li ul li:eq(1) a').addClass('notop');
	
	$('.kde-kupit').hover(
	function(){
		$('.kraj.' + this.rel).css('display','block');
	},
	function(){
		$('.kraj.' + this.rel).css('display','none');
	});

	//------------------------------------------------------------------------
	// Hover menu
	//------------------------------------------------------------------------
	$('.head ul li').hover(
	function(){
		if($(this).children('div').length!=0){
			$(this).addClass('hover');
		}
	},
	function(){
		if($(this).hasClass('hover')){
			$(this).removeClass('hover');
		}
	});

	//------------------------------------------------------------------------
	// Slide s bulletmi
	//------------------------------------------------------------------------
	// SLIDER
	var i = 0;
	$(".opts").empty();
	$(".uls li").each(function(){
		i++;
		$(".opts").append('<li><a href="#" rel="' + i + '"></a></li>');
	});

	$(".opts li a:first").addClass("on");
	var boxWidth = 940;
	var boxCount = $(".uls li").size();

	rotate = function(){
		i = $active.attr("rel") - 1;
		var slider_inPosition = i * boxWidth;
		$(".opts li a").removeClass('on');
		$active.addClass('on');
		$(".uls").animate({
			marginLeft: -slider_inPosition
		}, 800 );
	};

	rotateSwitch = function(){
		play = setInterval(function(){
			$active = $('.opts li a.on').parent('li').next().children('a');
			if ( $active.length == 0) {
				$active = $('.opts li a:first');
			}
			rotate();
		}, 5000);
	};

	rotateSwitch();

	$(".slide").hover(function() {
		clearInterval(play);
	},function() {
		rotateSwitch();
	});

	$(".opts li a").click(function() {
		if(!$(this).hasClass('on'))	{
			clearInterval(play);
			$active = $(this);
			rotate();
		}
		return false;
	});

	//------------------------------------------------------------------------
	// Pohlavie
	//------------------------------------------------------------------------
	
	/*
	$('.man').click(function(){
		$('.ovl, .pop').css('display','none');
		return false;
	});
	*/

	//------------------------------------------------------------------------
	// Zvysok
	//------------------------------------------------------------------------
	//$('a[target="_blank"]').addClass('blank');
	$('a.blank').attr('target', '_blank');
	$('a.nolink').click(function(){return false;});

	$('img[align="left"]').addClass('left');
	$('img[align="right"]').addClass('right');

	$(".fancy").fancybox({
		'titlePosition'  : 'over',
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'scrollBar'	: true
	});

	if(screen.width>500) {
		$(".kde-kupit").fancybox({
			'width'				: 500,
			'height'			: '60%',
			'autoScale'			: false,
			'transitionIn'			: 'none',
			'transitionOut'			: 'none',
			'type'				: 'iframe',
			'scrollBar'	: true
		});
	}
	
	$(".poslat-znamemu").fancybox({
		'height'			: '60%',
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'titleShow' : false,
		'scrollBar'	: true
	});	
	
	$(".fancy-video").fancybox({
		'height'			: '60%',
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'titleShow' : false,
		'scrollBar'	: true
	});	

	valideForm("form.validateForm");
	valideForm("form.validateForm2");
	
	$( "#predaj" ).datepicker($.datepicker.regional[ "sk" ]);

	$('.sidebar .box .input').each(function(){
		$(this).addClass("default");
		$(this).attr("value", this.title);
	});
	$('.sidebar .box .input').focus(function() {
		$(this).removeClass("default").addClass("active");
		if (this.value == this.title){
			this.value = '';
		}
		if(this.value != this.title){
			this.select();
		}
	});
	$('.sidebar .box .input').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");
		}
	});

	$('#scroll-box').jScrollPane({scrollbarWidth:6, showArrows:false});
	$('#scroll-box2').jScrollPane({scrollbarWidth:6, showArrows:false});
	
	if ($('.colk2').height() < $('.colk1').height()) $('.colk2').height($('.colk1').height());
	
	$('.validateForm').submit(function() {
	
		validateProcess = true;
		relocated = false;

		if ($('input[name^=answer]').size())
		{

			$('input[type=radio]').each(
				function (k, v)
				{
					radioName = ($(v).attr("name"));
					isValueSet = ($(v).parent().find('input[type=radio]:checked').size())
					
					if (isValueSet == 0)
					{	
						validateProcess = false;
						
						radioLabel = $(v).parent().find('label').not('.radio_label');
						radioLabelId = radioLabel.attr('id');
						
						if (relocated == false)
						{
							location.href = '#' + radioLabelId;
							relocated = true;
						}
						
						$(radioLabel).css('color', '#940000');
					}
					else
					{
						$(v).parent().find('label').not('.radio_label').css('color', 'black');
					}
				}
			);
			
			$('input[type=checkbox]').each(
				function (k, v)
				{
					radioName = ($(v).attr("name"));
					isValueSet = ($(v).parent().find('input[type=checkbox]:checked').size())
					
					if (isValueSet == 0)
					{	
						validateProcess = false;
						
						radioLabel = $(v).parent().find('label').not('.radio_label');
						radioLabelId = radioLabel.attr('id');
						
						if (relocated == false)
						{
							location.href = '#' + radioLabelId;
							relocated = true;
						}
						
						$(radioLabel).css('color', '#940000');
					}
					else
					{
						$(v).parent().find('label').not('.radio_label').css('color', 'black');
					}
				}
			);			
		}
		
		return validateProcess;
	});
	
	$('input[name^=other]').hide();
	
	$('input[name^=answer]').click(function(){
		
		$('input[type=radio]').each(
			function(k,v)
			{
				othersField = ($(v).attr("rel"));
				if (othersField && $(v).is(':checked'))
				{
					$('#' + othersField).addClass('defaultInvalid');
					$('#' + othersField).addClass('required');					
					$('#' + othersField).show();
				}
				else
				{
					$('#' + othersField).removeClass('defaultInvalid');
					$('#' + othersField).removeClass('required');						
					$('#' + othersField).hide();
				}
			}
		);

	});
});

//------------------------------------------------------------------------
// Oble rozky na obrazkoch
//------------------------------------------------------------------------
$(window).load(function(){
	$('.content img.left, .content img.right, img.rnd').each(function(){
		var sirka = $(this).width();
		var vyska = $(this).height();
		if($(this).hasClass('left')) {
			$(this).replaceWith('<span class="img left" style="background:url(' + this.src + ') left top no-repeat; width:' + sirka + 'px; height:' + vyska + 'px;">' + this.alt + '</span>');
		}
		else if($(this).hasClass('right')) {
			$(this).replaceWith('<span class="img right" style="background:url(' + this.src + ') left top no-repeat; width:' + sirka + 'px; height:' + vyska + 'px;">' + this.alt + '</span>');
		}
		else {
			$(this).replaceWith('<span class="img" style="background:url(' + this.src + ') left top no-repeat; width:' + sirka + 'px; height:' + vyska + 'px;">' + this.alt + '</span>');
		}
	});
});
