

;(function($) {
	
	var z = 1000;
	
	$.fn.homeSlides = function(delay) {
		
		var slides = $(this);
		
		slides.css("position", "relative");
		
		setTimeout(function() {
		
			setInterval(function() {
				
				var current = $("img.current", slides),
					next = current.next();
				
				if(!next.length) next = $("img:first-child", slides);
					
				current.removeClass("current");
				next
					.addClass("current")
					.css("display", "none")
					.css("position", "absolute")
					.css("top", "0px")
					.css("left", "0px")
					.css("z-index", z++)
					.fadeIn(2000);
				
				$(".logo").css("z-index", z++);
				
			}, 5000);
		
		}, delay);
		
	};
	
	
	
	function draw() {
		var w, h;
		
		// if we're in landscape
		if($(window).width() / $(window).height() > 1.5) {
			h = $(window).height() - 40;
			if(h > 900) h=900;
			w = h*1.5;
		}
		
		// or in portrait
		else {
			w = $(".fluid").width();
			if(w > 1440) w=1440;
			h = w / 1.5;
		}
		
		// and scale
		$(".home")
			.width(w)
			.height(h);
		
		$(".panel, .panel img")
			.width(parseInt(w/5))
			.height(parseInt(w/5*3.3));
			
		$(".logo")
			.css("top", h/2-150);
		
	}


	$(function() {
		
		var isiPad = navigator.userAgent.match(/iPad/i) != null;
		
		$(".panel img").hide();
		
		$(".panel").each(function() {
			$(this).find("img").first().load(function() {
				$(this).fadeIn(2000);
			});
				
		});	
		
		if(!isiPad) {
			$(".panel").eq(0).homeSlides(0);
			$(".panel").eq(1).homeSlides(500);
			$(".panel").eq(2).homeSlides(1000);
			$(".panel").eq(3).homeSlides(1500);
			$(".panel").eq(4).homeSlides(2000);
		}
				
		$("img").load(draw);
		
		$(window).resize(draw);
		draw();
	});
	
})(jQuery);
