﻿function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str))
}

var cross_active=0;

function overlay()
{
	if(!$('.overlay').width())
	{
		$('body').prepend('<div class="overlay"></div>');
		$('.overlay').height($(document).height()).css('opacity', '0.8');
	}
	$('.overlay').fadeIn();
}


function cycle_change()
{
	if(cross_active==2)
	cross_active=0
	else
	cross_active++;
	$('#cross img').hide();
	$('#cross img').eq(cross_active).show();
}
function sendAjax()
{
	var resultList = '';
	
	$('input, textarea').each(function() {
		resultList += $(this).attr('name')+'='+$(this).val()+'&';
	});

	$.ajax({   
		type: "POST",   
		url: "/extras/send.php",  
		data: resultList,   
		success: function(html)
		{  
			$('.loader').fadeOut('normal', function(){ $('form').html('<p>'+html+'</p>'); });
			
		}	 
	});
}

$.fn.blink = function() {
    $(this).stop().show().animate({ dummy: 1 }, 3000, function() {$(this).fadeOut()});
    return this;
};


$(document).ready(function() {

	$('form').submit(function() {
		var text = 'Pole oznaczone na czerwono nie może być puste.';
		error = false;


		
		$('input[name=email]').parent().removeClass('error');

		$('input.req, textarea.req').each(function() {

			if($(this).val()=='')
			{
				$(this).parent().addClass('error');
				error = true;
			}
			else
			$(this).parent().removeClass('error');	
		});

		if($('input[name=email]').length>0 && $('input[name=email]').val()!=='')
		{
			if(!checkemail($('input[name=email]').val()))
			{
				$('input[name=email]').parent().addClass('error');
				error = true;
				var text = 'Adres e-mail jest niepoprawny.';
			}
		}

		if($('#checkbox').length>0 && $('#checkbox').attr('checked')==false)
		{
			error=true
			var text = 'Musisz zaakceptować regulamin.';
		}
	

		if(error==true)
		$('form .message').html(text).blink();
		else
		{
			$('form table').hide().parent().append('<div class="loader"><img src="images/load.gif" alt="ładowanie" /></div>').find('.loader').fadeIn();
			sendAjax();
		}

		return false

	});


	if($('.arrow_l').length>0)
	{
		var quote_init = 0;
		var quote_max = $('.container').length-1;
		
		$('.arrow_l').click(function() {

			$('.container').eq(quote_init).fadeOut('slow', function() {$('.container').eq(quote_init).fadeIn()});
			quote_init--;
			if(quote_init==-1) quote_init=quote_max
			return false;
		});

		$('.arrow_r').click(function() {

			$('.container').eq(quote_init).fadeOut('slow', function() {$('.container').eq(quote_init).fadeIn()});
			quote_init++;
			if(quote_init==quote_max+1) quote_init=0
			return false;
		});
	}
	if($('#cross').length>0)
	{

		$('.link1').click(function() {
			$('#cross img').hide();
			$('#cross img').eq(0).show();
			cross_active=0;
		});

		$('.link2').click(function() {
			$('#cross img').hide();
			$('#cross img').eq(1).show();
			cross_active=1;
		});

		$('.link3').click(function() {
			$('#cross img').hide();
			$('#cross img').eq(2).show();
			cross_active=2;
		});
		setInterval(cycle_change, 10000);
	}


	$('.map').click(function() {
		overlay();

		if($.browser.msie)
		$('.map_handler').css({'left': '50%'});
		else
		$('.map_handler').animate({'left': '50%'});
		
	
		return false;
	});

	$('.map_close').click(function() {
		$('.overlay').fadeOut();
		$('.map_handler').css('left', '-50%');
		return false;
	});

$('#map').jmap('init', {'mapType':'map','mapCenter':[50.209034,18.974504], 'mapZoom':14});
	$('#map').jmap('AddMarker', {'pointLatLng': [50.209034,18.974504]});

});
