var sliderInterval = 6000;
var sliderAnimationSpeed = 1500;
var videoBaseDir = "/aton/wp/Videos/";

$(document).ready(function(){
	/* MENU CENTERING */
	var fullWidth = 0;
	$("#header .menu li:last").addClass("last");
	$("#header .menu li").each(function(){
		fullWidth += $(this).width() + parseInt($(this).css("borderRightWidth"));
	});
	$("#header .menu ul").css("width", fullWidth + "px");
	
	$("#sidebarRotator").slider();
	
	$("#contactForm").sendForm();
	
	$("#videoGallery").tryLoadNeededVideo().videoGallery();
});


jQuery.fn.extend({
	
	slider: function(){
		return this.each(function(){
			var slider = $(this);
			var imgs = new Array();
			var currentImg = 0;
			var animationNow = false;
			var sliderPaused = false;
			var skipNextTimerTick = false;
			
			slider.find("img").each(function(){
				imgs[imgs.length] = $(this).attr("src");
				$(this).remove();
			});
			
			slider.append('<div class="display"><div class="image" style="background-image:url(' + imgs[0] + ')"></div></div>');
			
			setInterval(function(){
				flipImages(currentImg + 1);
				skipNextTimerTick = false;
			}, sliderInterval);
			
			function flipImages(index)
			{
				
				if (!animationNow) {
					var old = slider.contents(".display").find(".image");
					
					if (index < 0) {
						index = imgs.length - 1;
					}
					else if (index >= imgs.length) {
						index = 0;
					}
					
					slider.contents(".display").append('<div class="new image" style="background-image:url(' + imgs[index] + ')"></div>');
					currentImg = index;
					animationNow = true;
					slider.contents(".display").find(".new.image").css("opacity", "0").
						animate({
							opacity:"1"
						}, sliderAnimationSpeed, "", function(){
							$(this).removeClass("new");
							old.remove();
							animationNow = false;
						});
				}
				
			}
		})
	},

	sendForm: function(){
		return this.each(function(){
			var form = $(this);
			
			form.contents().find("br").remove();
			
			form.children("input.submit").click(function(){
				var name = form.contents().find("input[name='name']");
				var email = form.contents().find("input[name='email']");
				var phone = form.contents().find("input[name='phone']");
				var message = form.contents().find("textarea[name='message']");
				var cancel = false;
				
				form.contents().find("label").css("color", "inherit");
				
				if (name.val().length < 3) {
					cancel = true;
					name.prevAll("label").css("color", "red");
				}
				if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
					cancel = true;
					email.prevAll("label").css("color", "red");
				}
				if (phone.val().length < 5) {
					cancel = true;
					phone.prevAll("label").css("color", "red");
				}
				if (message.val().length < 5) {
					cancel = true;
					message.prevAll("label").css("color", "red");
				}
				
				if (!cancel) {
					form.css("height", form.height() + "px");
					
					form.children("*").animate({
						opacity:0
					}, 500, "", function(){
						form.html("<p>Sending...</p>");
						form.children("*").css("lineHeight", form.height() + "px").
						css("textAlign", "center").css("opacity", "0").
						animate({
							opacity:1
						}, 500);
					});
					
					jQuery.ajax({
						data: {
							name: name.val(),
							email: email.val(),
							phone: phone.val(),
							message: message.val()
						},
						type: "POST",
						url: "/aton/wp/sendcontact.php",
						success: function(){							
							form.children("*").animate({
								opacity:0
							}, 500, "", function(){
								form.html("<p>Message sent. Thank you!</p>");
								form.children("*").css("lineHeight", form.height() + "px").
								css("textAlign", "center").css("opacity", "0").
								animate({
									opacity:1
								}, 500);
							});
						}
					});
				}
			});
		});
	},
	
	tryLoadNeededVideo: function(){
		return this.each(function(){
			var params = window.location.href.split("#");
			
			if (params[1].length > 1) {
				$(this).contents().find("embed").attr("src", videoBaseDir + params[1]);
			}
		});
	},
	
	videoGallery: function(){
		return this.each(function(){
			var gallery = $(this);

			gallery.contents().find(".vid a").click(function(){
				
				gallery.contents().find("embed").attr("src", videoBaseDir + 
					$(this).attr("href").replace("#", ""));
			});
		});
	}
	
})