﻿$(document).ready(function(){

	// preload sponsor images
	$('body').append('<div id="preloader"></div>');

	$('#sponsors .content li a img').each(function(){
		var imageAddress = $(this).attr('src');
		imageAddress = imageAddress.replace('_s.gif', '.gif');

		$('#preloader').append('<img src="'+imageAddress+'" />');
	});

	// switch sponsors
	function switchSponsor(current){
		var switchTo = current.attr('src');
		switchTo = switchTo.replace('_s.gif', '.gif');

		$('#sponsors a.selected').prepend('<img src="'+switchTo+'" class="hoveredSelection" />');
		$('.hoveredSelection').fadeIn(1000, function(){
			$('#sponsors img.selected').attr('src', switchTo);
			$(this).remove();
		});
	}

	// show next sponsor to current
	function nextSponsor(){
		var current = 'a';
		current = $('#sponsors img.selected').attr('src');
		current = current.replace('.gif', '_s.gif');
		current = $("#sponsors .content li a img[src$='"+current+"']").parent().parent().next();
		if(current.length == 0){ current = $('#sponsors li:nth-child(1)'); } // if it goes to the last one, go back to first one
		current = current.find('a img');

		switchSponsor(current);
	}

	// set sponsors to scroll automatically
	var sponsorsInterval = setInterval(function(){ nextSponsor(); }, 4000);

	// stop scrolling when mouse hover
	$('#sponsors .content').hover(function(){
		clearInterval(sponsorsInterval);
	}, function(){
		sponsorsInterval = setInterval(function(){ nextSponsor(); }, 4000);
	})

	// scroll through sponsors on mouse hover
	$('#sponsors .content li a img').mouseover(function(){
		switchSponsor($(this));
	});


	// articles controll
	var articleNumber = 1;

	$('#about .sidebar .pages li').fadeIn(100);
	function articleControll(){
		$('#about .sidebar .pages .next').unbind('click');
		$('#about .sidebar .pages .prev').unbind('click');

		if(articleNumber == $('#about .sidebar .articles li').length){
			$('#about .sidebar .pages .next').animate({opacity: 0.3}, 300);
		} else {
			$('#about .sidebar .pages .next').animate({opacity: 1}, 300);
			$('#about .sidebar .pages .next').bind('click', function(){
				$('#about .sidebar .articles').animate({left: '-=386px'}, 300);
				articleNumber++;
				articleControll();
			});
		}

		if(articleNumber == 1){
			$('#about .sidebar .pages .prev').animate({opacity: 0.3}, 300);
		} else {
			$('#about .sidebar .pages .prev').animate({opacity: 1}, 300);
			$('#about .sidebar .pages .prev').bind('click', function(){
				$('#about .sidebar .articles').animate({left: '+=386px'}, 300);
				articleNumber--;
				articleControll();
			});
		}
	}
	articleControll();


	// mail sending
	$('#contact form input[type="submit"]').click(function(){
		$.ajax({
			type: 'post',
			url: 'html/mail.php',
			dataTypeString: 'text',
			data: 'name='+ $('#contact form #name').val() +'&mail='+ $('#contact form #mail').val() +'&subject='+ $('#contact form #subject').val() +'&message='+ $('#contact form textarea').val(),


			beforeSend: function(){
				$('#contact .content').append('<div class="mailSubmit"><img src="style/pictures/gallery_preloader.gif" width="30" height="30" alt="trwa wysyłanie..." /></div>');
				$('#contact .content .mailSubmit').fadeIn(300);
			},

			success: function(data){
				$('#contact .content .mailSubmit img').fadeOut(300, function(){
					$(this).remove();
					$('#contact .content .mailSubmit').append('<span style="display: none">'+data+'</span>');
					$('#contact .content .mailSubmit span').fadeIn(300);
					setTimeout (function(){
						$('#contact form input:not([type="submit"])').val('');
						$('#contact form textarea').val('');
						$('#contact .content .mailSubmit').fadeOut(300, function(){ $(this).remove(); })
					}, 5000);
				});
			}
		});
		return false;
	});
});
