// JavaScript Document

function WyslijMail(DoKogo) {
	if (DoKogo != '') {
	Domena = 'kava.es';
	}
	window.open("mailto:" + [DoKogo] + "%40" + [Domena]);
}


$(function(){ // ma byc!
	var ile_li = $('ul#single_img > li').length; //ilosc elementow w li
	var element_width = $('ul#single_img > li').width();

	var new_margin = 0; // ul#single_img marginLeft

	var counter = 0; // licznik pixeli dla new_margin
	var counter_max = ile_li * element_width - ($('#imageroll').width()); // max przesunięcie new_margin
	
	var lock = 0;

	$('#forward').click(function() { //zwraca ul do poczatku..
		if(lock == 0) {
			lock = 1;
		}
		
		if (counter + element_width < counter_max) {
			new_margin -= element_width;
			counter += element_width;
		} else if (counter < counter_max) {
			counter = counter_max;
			new_margin = counter * -1;
		} else {
			new_margin = 0;
			counter = 0;
		}
		

	 	$('ul#single_img').animate({marginLeft: new_margin}, function() {
	 		lock = 0;
		});

		return false;	//to ma spowodowac ze po kliknieciu w link nie przejdziemy do miejsca gdzie link wskazuje. Ta linia musi być na końcu funkcji.
	});

	$('#back').click(function() { //zwraca ul do poczatku..
		if(lock == 0) {
			lock = 1;
		}

		if (counter == 0) {
			counter = counter_max;
			new_margin = counter * -1;
		} else if (counter < element_width) {
			counter = 0;
			new_margin = 0;
		} else {
			new_margin += element_width;
			counter -= element_width;
		}
		
	 	$('ul#single_img').animate({marginLeft: new_margin}, function() {
	 		lock = 0;
		});

		return false;	//to ma spowodowac ze po kliknieciu w link nie przejdziemy do miejsca gdzie link wskazuje. Ta linia musi być na końcu funkcji.
	});

	var show = 0;
	 
	var contact_show = 0;
	 
	var zaile_show = 0;
	 
	$('#portfolio').click(function(){
	
	 	$('.current').removeClass('current');
	 	$(this).addClass('current');
	 	
	 	contact_show = 0;
	 	zaile_show = 0;

		 $('#zaile').animate({
		 		height: '0'
		 	}, 500, function(){
		 		zaile_show = 0;
		 });
		 
	 	if(show == 0){

		 	$('#black').animate({
		 		bottom: '2.5em'
		 	}, function(){
		 		show = 1;
		 	});
		 	
	 	}else{

		 	$('#black').animate({
		 		bottom: '-500px'
		 	}, function(){
		 		show = 0;
		 	});
	 	
	 	}
	 		
	 	return false;
	 });
	 
	 
	 $('#zaile_link').click(function(){
	 	
	 	$('.current').removeClass('current');
	 	$(this).addClass('current');
	 	
	 	contact_show = 0;
	 	show = 0;


		 $('#black').animate({
		 	bottom: '-500px'
		 });
	 
	 	if(zaile_show == 0){

		 	$('#zaile').animate({
		 		height: '100%'
		 	}, 500, function(){
		 		zaile_show = 1;
		 	});
		 	
	 	}else{

		 	$('#zaile').animate({
		 		height: '0'
		 	}, 500, function(){
		 		zaile_show = 0;
		 	});
	 	
	 	}
	 		
	 	return false;
	 });
	 
	 $('#rolldown').click(function(){ 
	 	$('#black').animate({
			bottom: '-500px'
		});
	 return false;
	 });
	 
	//wysyłanie maili
	 
	 $('#mail_form').submit(function(){ 
	 
			
			var from = $('#from').val();
			var content = $('#content').val();
			
			var from_error = false;
			var content_error = false;

			if(from == ''){
				from_error = true;
				$("#error-from-incorrect").fadeOut(300, function(){
					$("#error-from").fadeIn(300).show();
				});
				
			}else{
				
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				
				if(reg.test(from) == true){
					from_error = false;
					$("#error-from").fadeOut(300);
					$("#error-from-incorrect").fadeOut(300);
				}else{
					from_error = true;
					$("#error-from").fadeOut(300, function(){
						$("#error-from-incorrect").fadeIn(300).show();
					});
				}
			}
		
			if(content == ''){
				content_error = true;
				$("#error-content").fadeIn(300).show();
			}
			else{
				content_error = false;
				$("#error-content").fadeOut(300);
			}
			
			
			
			if(from_error == false && content_error == false){
				
			var data = $('#mail_form').serialize();
			$.ajax({
					type: "POST",
					url: "send.php",
					data: data,
					success: function(){
						$('#form').fadeOut('300', 
								function() {
									$('#sent').fadeIn('300', 
									function() {
									// Mail sent.
									});
								// Animation complete.
								});	
						}
						});

			}
			
		 return false;
		});
		
		 
	 
});
