$(function(){
	// wysokosc okna
	$(window).load(function(){
		$height = $(window).height() - 340;
		$('#cont,#cont2').css('min-height',($height<370)?370:$height);
	});
	$(window).bind('resize',function(){
		$height = $(window).height() - 340;
		$('#cont,#cont2').css('min-height',($height<370)?370:$height);
	});

	if(window.location.hash){
		var hash = window.location.hash;
		
		$produkt = hash.substr(1);
		
		$('#load').show();
		$('#contRight').css({opacity: '.5'});
		
		$this = $('a[produkt='+$produkt+']');
		$nazwa = $('span:first',$this).text();

		$this.blur();
		$('.on','#lista').removeClass('on');
		$this.addClass('on');
		$location = $('#nav').attr('location');
		if($('a','#nav').length == 4){
			$('a:last','#nav').remove();
			$html = $('#nav').html();
			$length = $html.length;
			$('#nav').html($html.substr(0,$length-6));
		}
		$('#nav').append(' &gt; <a href="'+$location+'#'+$produkt+'">'+$nazwa+'</a>');
		
		$('#buttonInfo').attr('produkt',$produkt);
		$('#buttonInfo').show();
		
		$('#dane').load('produkt.php?action=dane&produkt='+$produkt,function($data){
			$('div[dane=1]','#contRight').show();
			if(!$data){
				$('#dane').html('<div class="danePos" style="padding: 15px 0 5px 20px; color: #666;">Brak danych technicznych.</div>');
			}
			$('#contRight').css({opacity: '1'});
				$('#load').hide();
			$.galeria($produkt);
		});
	}

	//wybor produktu
	$('a','#lista').live('click',function(){
		$('#load').show();
		$('#contRight').css({opacity: '.5'});
		
		$this = $(this);
		$produkt = $this.attr('produkt');
		$nazwa = $('span:first',$this).text();

		$this.blur();
		$('.on','#lista').removeClass('on');
		$this.addClass('on');
		$location = $('#nav').attr('location');
		if($('a','#nav').length == 4){
			$('a:last','#nav').remove();
			$html = $('#nav').html();
			$length = $html.length;
			$('#nav').html($html.substr(0,$length-6));
		}
		$('#nav').append(' &gt; <a href="'+$location+'#'+$produkt+'">'+$nazwa+'</a>');
		
		$('#buttonInfo').attr('produkt',$produkt);
		$('#buttonInfo').show();
		
		$('#dane').load('produkt.php?action=dane&produkt='+$produkt,function($data){
			$('div[dane=1]','#contRight').show();
			if(!$data){
				$('#dane').html('<div class="danePos" style="padding: 15px 0 5px 20px; color: #666;">Brak danych technicznych.</div>');
			}
			$('#contRight').css({opacity: '1'});
				$('#load').hide();
			$.galeria($produkt);
		});
	});

	function email(e){
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(e);
	}
	
	//kontakt form
	$('#wyslij').live('click',function(){
		$this = $(this);
		$odbiorca = $('input[name=odbiorca]');
		$imie = $('input[name=imie]');
		$email = $('input[name=email]');
		$tresc = $('textarea');
		
		if(!$imie.val() || !$email.val() || !email($email.val()) || !$tresc.val()){
			$.each([$imie, $email, $tresc], function(){
				this.stop(true,true).removeClass('err');
				(!this.val())?this.addClass('err').delay(5000).removeClass('err',5000):'';
			});
			(!email($email.val()))?$email.addClass('err').delay(5000).removeClass('err',5000):'';
			//(!$imie.val())? $imie.addClass('new').delay(2000).removeClass('new', 8000);
			alert("Uzupełnij dane.");
			return false;
		}
		
		$this.blur().css({background: 'url(src/load.gif) no-repeat center'}).attr('disabled','disabled');

		$.post('mail.php',{
			odbiorca: $odbiorca.val(),
			imie: $imie.val(),
			email: $email.val(),
			tresc: $tresc.val()
		},function($data){
			if($data=='1'){
				$this.css({background: 'url(src/ok.gif) no-repeat center'});
				$imie.val('');
				$email.val('');
				$tresc.val('');
			}
			else{
				$this.css({background: 'url(src/error.gif) no-repeat center'});
				alert("Błąd! Proszę spróbować później.");
			}
			$this.delay(5000).fadeOut(1000,function(){
				$this.css({background: 'url(src/buttonWyslij.png) no-repeat center'}).removeAttr('disabled').show();
			});
		});
		
		return false;
	});

	//dialog box
	$('#zamow,#zapytaj').live('click',function(){
		$this = $(this);
		$dane = ($this.attr('id')=='zamow')?'Zamow':'Zapytaj';
		
		$this.blur();
		$.popup($dane);
	});

	$.popup = function($dane){
		$width = $(window).width();
		$height = $(window).height();
		$sign = ($dane=='Zamow')?'Zamów':'Zapytaj';

		$('#dialogTitle').html($sign);
		$('#dialog'+$dane).show();
		$('#dialog').show();
		$('#dialogBox').css({
			'left':($width-$('#dialogBox').width())/2,
			'top':($height-$('#dialogBox').height())/2
		}).fadeIn('fast').draggable({
			handle: '#dialogTop'
		});
		$('#dialogWyslij').attr('action',$dane);
	};

	$('#dialogData').live('focus',function(){
		$(this).datepicker({
			dateFormat: 'dd-mm-yy',
			dayNamesMin: ['Nd','Po','Wt','Śr', 'Cz', 'Pt', 'So'],
			monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
			firstDay: 1,
			minDate: 0,
			showAnim: 'slideDown'
		});
	});

	$('#dialogBg, #dialogAnuluj').live('click',function(){
		$('#dialogBox').fadeOut('fast',function(){
			$('div','#dialogCont').hide();
			$('#dialog').hide();
		});
		return false;
	});
	
	//ladowanie img
	$.loadImg = function($src, $f){
		var $i = new Image();
		$($i).load(function(){ $f(); }).attr('src', $src);
		//$($i).load(function(){ $f(); }).attr('src', $src+'?'+Math.random()); //tymczasowo random
	}

	//galeria zdjec
	$.galeria = function($produkt,$f){
		$('#obrazki').show();
		$('img','#obrazkiBig').hide();
		$.get('produkt.php?action=galeria&produkt='+$produkt,function($data){
			if($data != 'Error'){
				$('#thumbs,#obrazkiBig').show();
				$('#thumbs').html($data);
				$('a','#thumbs').hide().each(function(index, $this){
					$this = $(this);
					$src = $('img',$this).attr('src');
					$.loadImg($src,function(){
						$this.show();
					});
					if(index+1 == $('a','#thumbs').size()){ if($f) $f(); }
				});
				$('a:first','#thumbs').addClass('on');
				$src = $('img:first','#thumbs').attr('src');
				$('img','#obrazkiBig').attr('src',$src).load(function(){
					$(this).fadeIn();
				});
			}
			else{
				$('#thumbs,#obrazkiBig').hide();
				if($f) $f();
			}
		});
	};
	
	//podglad zdjecia galeria
	$('a','#thumbs').live('click',function(){
		$this = $(this);
		$src = $('img',$this).attr('src');
		
		$this.blur();
		$('a','#thumbs').removeClass('on');
		$this.addClass('on');
		$('img','#obrazkiBig').hide().attr('src',$src).load(function(){
			$(this).fadeIn();
		});
		return false;
	});
});
