// We define a function that takes one parameter named $.
(function ($) {
	
	curFeature = 2;
	points = new Array();
	curAni = false;
	switchInterval = null;
	
	function calculatePoints() {
		
		winWidth = $(window).width();
		featureWidth = 660;
		htmlWidth = 960;
		
		htmlGap = (htmlWidth-featureWidth)/2;
		windowGap = (winWidth-htmlWidth)/2;
		
		points[3] = 0 - (2*featureWidth) - (featureWidth - (windowGap + htmlGap)); // start
		
		points[2] = points[3]+featureWidth;
		points[1] = points[2]+featureWidth;
		points[0] = points[1]+featureWidth;
		
		points[4] = points[3]-featureWidth;
		points[5] = points[4]-featureWidth;
		points[6] = points[5]-featureWidth;
		points[7] = points[6]-featureWidth;
		points[8] = points[7]-featureWidth;
		
		$("#feature-holder").css("left",points[curFeature+1] + "px");
		$(".feature").each(function() { $(this).css("opacity", 0.4); });
		$(".f" + curFeature).each(function() { $(this).animate({ opacity: 1 },500); });
	}
	
	function play(id) { 
		$("#youtube-wrapper").css("display","block");
		$("#youtube-container").css("display","block");
		$("#youtube-embed").html(
			"<iframe width='560' height='349' src='http://www.youtube.com/embed/" + id + "' frameborder='0' allowfullscreen></iframe>");
	}

	$(document).ready(function() {
		calculatePoints();
		
		function animateFeatures(cur,p,nF,shift) {
			curAni = true;
			
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				$('#feature-title').html($("#feature-image-" + nF).attr("title"));
				$('#feature-alt').html($("#feature-image-" + nF).attr("alt"));
				$('#feature-explain').attr('class', "f-explain-cat" + $("#feature-link-" + nF).attr("class"));
			} else {
				$('#feature-box').animate({ opacity: 0 }, 500, function() {
					$(".f" + cur).each(function() { $(this).animate({ opacity: 0.4 },500); });
					$(".f" + nF).each(function() { $(this).animate({ opacity: 1 },500); });
					$('#feature-title').html($("#feature-image-" + nF).attr("title"));
					$('#feature-alt').html($("#feature-image-" + nF).attr("alt"));
					$('#feature-link').attr('href',$("#feature-link-" + nF).attr("href"));
					$('#feature-explain').attr('class', "f-explain-cat" + $("#feature-link-" + nF).attr("class"));
					$('#feature-box').animate({ opacity: 100 }, 500, function() {});
				});
			}
			
			$('#feature-holder').animate({ left: points[p] }, 1000, 'linear', function() {
				
				if(!(shift == undefined)) {
					$('#feature-holder').css('left',points[shift] + "px");
				}
				curFeature = nF;
				curAni = false;
			});
			
			
		}
		
		$("#control-left").click(function goLeft() {
			clearInterval(switchInterval);
			if(!curAni) {
				if(curFeature==2) { 
					animateFeatures(2,2,1);
					return;
				}
				
				if(curFeature==1) { 
					animateFeatures(1,1,4,5);
					return;
				}
				
				if(curFeature==3) { 
					animateFeatures(3,3,2);
					return;
				}
				
				if(curFeature==4) { 
					animateFeatures(4,4,3);
					return;
				}
			}
		});
		
		$("#control-right").click(function goLeft() {
			clearInterval(switchInterval);

			if(!curAni) {
				if(curFeature==2) { 
					animateFeatures(2,4,3);
					return;
				}
				
				if(curFeature==1) { 
					animateFeatures(1,3,2);
					return;
				}
				
				if(curFeature==3) { 
					animateFeatures(3,5,4);
					return;
				}
				
				if(curFeature==4) { 
					animateFeatures(4,6,1,2);
					return;
				}
			}
		});
		
		switchInterval = setInterval(function() {
			if(!curAni) {
				if(curFeature==2) { 
					animateFeatures(2,4,3);
					return;
				}
				
				if(curFeature==1) { 
					animateFeatures(1,3,2);
					return;
				}
				
				if(curFeature==3) { 
					animateFeatures(3,5,4);
					return;
				}
				
				if(curFeature==4) { 
					animateFeatures(4,6,1,2);
					return;
				}
			}
		},5000);
		
		$("#youtube-close").click(function youtubeClose() {
			$("#youtube-wrapper").css("display","none");
			$("#youtube-container").css("display","none");
			$("#youtube-embed").html("");
		});
		
		$(".play").each(function (i) {
			$(this).click(function () { play($(this).attr("title")); });
			$(this).attr("href", "#");
		});
		
	});
	
	var resizeTimer = null;
	$(window).bind('resize', function() {
	    if (resizeTimer) clearTimeout(resizeTimer);
	    resizeTimer = setTimeout(calculatePoints, 100);
	});
	
}(jQuery));;

