function validMail(val) {
	var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	
	if (val.search(validRegExp) == -1) {
		return false;

	} else {
		return true;
		
	}
}

function doSwitch(newElement) {
		if (!newElement.hasClass("active")) {
			$('.activePhotoWrap').addClass("loading");
			$('.activePhotoWrap img').hide();
			
			$(".thumbBox.active").removeClass("active");
			newElement.addClass("active");
			
			var imgSrc = $("a", newElement).attr("href");
		    var img = new Image();
		    
		    $(img).load(function () {
				$(this).hide();
		        $('.activePhotoWrap').removeClass("loading");
		        $('.bigImgWrap').html(this);

		        $(this).fadeIn();
		    }).error(function () {
		    	console.log("Chyba při načítání");
		    }).attr('src', imgSrc).removeAttr("style");
		}
	}

$(document).ready(function(){
	
	Cufon.replace("#ingredients h2, #clients h2, #whyF13 h2, .quote, #quote h2");	
	Cufon.replace("#subNav a",  {hover: true });
	
	$('#coinSlider').coinslider({ width: 960, height: 520, effect: 'rain', delay: 5000});

	
	/*
	$('#slider').nivoSlider({
		effect:'fold', //Specify sets like: 'fold,fade,sliceDown'
		slices:12,
		animSpeed:1000,
		pauseTime:4000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:false, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:true, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
      	controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:false //Force manual transitions

	});
	*/

	$("#logo").hover(
		function() {
			$(this).stop().animate({
				opacity: 0.4
			});
		}, 
		function() {
			$(this).stop().animate({
				opacity: 1
			});		
		
		}
	);
	
	$(".thumbBox a").click(function(e) {
		e.preventDefault();
		doSwitch($(this).parent());
		
	});
	
	$(".switcher").click(function(e) {
		e.preventDefault();
		
		if ($(this).hasClass("next")) {
			var element = $(".thumbBox", $(".thumbBox.active").parent().next());
			
			if (!element.length) {
				element = $(".thumbBox:first");
			}
		
		} else {
			var element = $(".thumbBox", $(".thumbBox.active").parent().prev());
			
			if (!element.length) {
				element = $(".thumbBox:last");
			}
			
		}
		
		doSwitch(element);
		
	});

  $('a.externalUri').click(function(){
		window.open( $(this).attr('href') );
    return false;
	});


	$(".contactForm").fancybox({
		'titleShow'     : false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'ajax' 			: {
							type: "GET"
						},
		'onComplete'	: function() {
							Cufon.replace(".contactWrap h3");
						  }
		
	});

	$("#send").live("click", function(e) {
		e.preventDefault();
		
		var elements = $("#contactForm .required");
		
		var error = false;
		$.each(elements, function(){
			var element = $(this);
			
			if (!element.val()) {
				element.animate({backgroundColor: "#ffb0b0"}, "slow", function() {
					element.addClass("error");
					element.removeAttr("style");
					error = true;
				});
			}
			
			
		});
		

		if (!validMail($("#mail").val())) {
			var element = $("#mail"); 
			
			if (!element.hasClass("error")) {
				element.animate({backgroundColor: "#ffb0b0"}, "slow", function() {
					element.addClass("error");
					element.removeAttr("style");
					
				});
			}
			
			error = true;
		}
		
		if (error == false) {
			$.post("/mail/send/", $("#contactForm").serialize(), function(returnData) {
				$("#contactForm input, #contactForm textarea").val("");
				
				$("#send").css("position", "absolute");
				
				$("#send").animate({left: 0}, "slow", function() {
					$("#send").animate({left: 500}, "fast", function() {
						$("#send").addClass("sent");
						$("#send").animate({left: 58}, 1000, function() {
							$.fancybox.close();
							
						});
						
					});
					
				});
			});
			
		}
	});
	
	$("#contactForm input, #contactForm textarea").live("focus", function() {
		var element = $(this);
		
			element.animate({backgroundColor: "#ffffb0"}, "slow", function() {
				element.removeClass("error");
				element.removeAttr("style");
			});
	});
		
 });
