// studiofabryka.pl
$(document).ready(function(){

//poziome rozwijane menu

$('.lvl2, .lvl3').hide();
$('nav .lvl1 > li').mouseover(function(){
	if($('.lvl2').is(':hidden')){$(this).find('.lvl2').stop(true, true).slideDown(200);}
});
$('nav .lvl1 > li').mouseleave(function(){
	if($('.lvl2').is(':visible')){$(this).dequeue().find('.lvl2').stop(true, true).slideUp(200);}
});

$('nav .lvl2 > li').mouseover(function(){
	if($('.lvl3').is(':hidden')){$(this).find('.lvl3').stop(true, true).fadeIn(200);}
});
$('nav .lvl2 > li').mouseleave(function(){
	if($('.lvl3').is(':visible')){$(this).dequeue().find('.lvl3').stop(true, true).fadeOut(200);}
});

// klasa jeśli ma podmenu

$('nav .lvl1 > li').find('.lvl2').hover(function(){
	$(this).parent().addClass('sub-open');
}, function(){
	$(this).parent().removeClass('sub-open');
});

$('nav .lvl2 > li').find('.lvl3').hover(function(){
	$(this).parent().addClass('sub-open');
}, function(){
	$(this).parent().removeClass('sub-open');
});


// wielopoziomowy akordeon

$('aside ul.lvl1 > li').click(function() {
	var selfClick = $(this).find('ul:first').is(':visible');
	if (selfClick) {
		return;
	}
	$(this)
		.parent()
		.find('> li ul:visible')
		.slideToggle();
	$(this)
		.find('ul:first')
		.stop(true, true)
		.slideToggle();
});

$('aside .current ul:first').show();





// typ pliku wg linku w download

	$('.typPliku').each(function(){
		$(this).text('('+ $(this).parent().attr('href').substring($(this).parent().attr('href').length - 3) +')');
		$(this).addClass($(this).parent().attr('href').substring($(this).parent().attr('href').length - 3));
	});

// last li

	$('header nav li:last-child').addClass('last');

//tabele

	$('table tr:first-child').addClass('firstrow');
	$('table tr:even').addClass('even');

// blank

	if($('.blank').length > 0) {
		$('.blank').click( function() {
			window.open(this.href);
			return false;
		});
	}

// Galeria

	if ($('.fancy').length > 0) {
		$.getScript('fancybox/fancy.js', function(){
			$(".fancy").fancybox({
				titlePosition: 'over',
				overlayOpacity: 0.8,
				overlayColor: '#000'
			});
		});
	}

// Walidacja formularza

	$('#error').hide();


	if($('#contact-form').length > 0) {
		$('#contact-form').bind('submit', function(){
			var valid = true;
			$('.required').click(function(){
				$(this).removeClass('error-input');
			});
			$('.required').not('#subject').each(function(){
				if(($(this).val()=='') || ($('#subject').val()!='') || ($(this).val() == 'imię i nazwisko *') || ($(this).val() == 'email *') || ($(this).val() == 'np. 8:00 *') || ($(this).val() == 'np. 16:00 *')){
					$(this).addClass('error-input');
					 valid = false;
				}else{
					$(this).removeClass('error-input');
				}
			});
			if(!valid){
				$('#error').fadeIn();
				return false;
			}
		});
	}

	// ajax form

		if($('#contact-form').length > 0) {
			$.getScript('js/jquery.form.js', function(){
				$('#contact-form').ajaxForm(function(){
					if($('#error').is(':visible') == true) {
						$('#error').fadeOut(300);
						$('#ok').delay(300).fadeIn(300);
					} else {
						$('#ok').fadeIn(300);
					}

					$('#contact-form input, #contact-form textarea, #contact-form select, #contact-form button').attr('disabled', 'disabled');
				});
			});
		}

//button hovers

	if($('button').length > 0) {
		$('button').hover(function(){
			$(this).css({
				cursor: 'pointer',
				opacity:.8
			});
		}, function(){
			$(this).css({
				cursor: 'pointer',
				opacity:1
			});
		});
	}

if($('table.sort').length > 0){
	$.getScript('js/jquery.tablesorter.min.js', function(){
		$(".sort").tablesorter({
			headers: {
				2: { sorter: false }
			}
		});

		$('.sort th:eq(2)').addClass('notsorted');
		$('.sort thead th').each(function(index){$(this).addClass('item' + index)});

		$('tr').each(function(){
			$(this).find('td:gt(2)').addClass('prices');
		});
	});
}

if($('.slideshow').length > 0) {

	$('.slides').each(function(){
		$(this).find('img:eq(0)').addClass('item0');
		$(this).find('img:eq(1)').addClass('item1');
		$(this).find('img:eq(2)').addClass('item2');
	});

	$.getScript('js/jquery.cycle.all.min.js', function(){
		$('.slideshow .slides').cycle({
			fx: 'scrollHorz',
			timeout: 3000,
			sync: 1,
			speed: 1500
		});
	});
}

// animacja buttona odbitek
	function odbitki(){
		$('.zamow-odbitki').animate({top: 58}, 900);
		$('.zamow-odbitki').delay(100).animate({top: 47}, 900, odbitki);
	}

	$('.zamow-odbitki').mouseover(function(){
		$(this).stop(true, true);
		$('.zamow-odbitki').stop(true, false).animate({top: 58}, 300);
	});
	$('.zamow-odbitki').mouseleave(function(){
		odbitki();
	});

	odbitki();



}); // ready end
