// studiofabryka.pl
// mc, polcopper, 19 paz 2011

$(document).ready(function(){


// szer. #main zależna od .photos

	if($('.photos').length == 0) {
		$('#main').addClass('fullMain');
	}

//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);}
});


// 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
	$('header nav li:last-child, table tr td: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;
		});
	}




// Gallery

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



// Form validate

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

					$('input, textarea, select, 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
			});
		});
	}



//slideshow

	if($('.slides').length > 0){
		$.getScript('js/jquery.cycle.all.min.js', function(){
			$('.slides').cycle({
				sync:1,
				timeout: 4000,
				speed: 800,
				cleartype:  false
			});
		});
	}





// gdzie kupić

	$('.sklepy').hide();

	$('#polska li').click(function(){
		var maplink = $(this).find('a').attr('href').substring(2);

		//$('#map-pl, .mapinfo').slideUp(300);
		//$('.sklepy').filter(':visible').slideUp(300);
		$('.sklepy').load('/templates/inc_sklep.php?id='+ maplink).delay(300);
		$('.sklepy').fadeIn(300);
	});

	$('.togglemap').live('click', function(){
		//$('#map-pl, .mapinfo').slideDown(300);
		$('.sklepy').filter(':visible').fadeOut(300);
	});



}); // ready end

