/*
 * plugin for the slide carousel
*/
(function($) {
 
   $.fn.homeCarousel = function(settings) {
		var config = {
			firstItem: 0,
			totalItems : 0,
			easing : "easeOutQuad",
			speed : 800,
			imgHeightMain : 252,
			imgHeightSmall : 225
		};
		
		if (settings) $.extend(config, settings);
		
		config.currentItem = config.firstItem;
		
		// initialise variables that we're going to use
		var num, isForward, numToMove, thisItem, itemMinus3, itemMinus2, itemMinus1, itemPlus1, itemPlus2, itemPlus3, infoSpeed
		
		var imgOffset = Math.floor((config.imgHeightMain - config.imgHeightSmall) / 2);
		
		var imgNormHeight = {"height": config.imgHeightMain, "marginTop": 0};
		var imgSmall = {"height": config.imgHeightSmall, "marginTop": imgOffset};
			
		config.totalItems = $("#carousel li").length;
		
		var liObjects = $("#carousel li");
		
		function init(el) {
			$(el)
				.append('<div class="paginate paginate-prev"></div>')
				.append('<div class="paginate paginate-next"></div>')
				.append('<div class="mask"></div>')
				.append('<div class="infoContainer"></div>');
				
			
			// setup initial view
			liObjects.children(".img").css({"left": 2000});
			liObjects.eq(config.currentItem).children(".img").css({"left": 285});
			liObjects.eq(config.currentItem+1).children(".img").css({"left": 860});
			liObjects.eq(config.currentItem+1).find("img").css(imgSmall);
			
			// setup initial infoblock view
			$("#carousel li .infoblock").css({"display": "none"});
			$("#carousel li:eq("+config.currentItem+") .infoblock").css({"display":"block"});
			
			// set onstate for currently on div
			$("#carousel-nav li").eq(config.currentItem).addClass("on");
			
			// click functions
			$(".paginate-next").click(function() {
				num = (config.currentItem+1)%config.totalItems;
				goTo(num);
			});			
			$(".paginate-prev").click(function() {
				num = (config.currentItem < 1) ? config.totalItems-1 : config.currentItem-1;
				goTo(num);
			});	
			$("#carousel-nav li a span").click(function() {
				num = $(this).parents("li").prevAll().length;
				goTo(num);
			});
			
			$("#carousel-nav li a").click(function() {
				// prevent the anchor hash being displayed in the address bar
				return false;
			});
			
			$(el).addClass("on");
			
		}
		
		
		// jump to specific promo
		function goTo(num) {
			
			// if any elements are still animating then don't do anything
			if ($("#carousel div:animated").length != 0) {
				return false;
			}
			
			thisItem = config.currentItem;
			
			if (num == thisItem) {
				// this promo is currently showing, so don't do anything
				return false;
			} else if (num == (thisItem+1)%5 || num == (thisItem+2)%5) {
				isForward = true;
				numToMove = num > thisItem ? num - thisItem : 5-thisItem+num;
			} else {
				isForward = false;
				numToMove = num < thisItem ? thisItem-num : 5+thisItem-num;
			}			
			
			itemMinus3 = (thisItem < 3) ? thisItem-3 + config.totalItems : thisItem-3;
			itemMinus2 = (thisItem < 2) ? thisItem-2 + config.totalItems : thisItem-2;
			itemMinus1 = (thisItem < 1) ? thisItem-1 + config.totalItems : thisItem-1;
			itemPlus1 = (thisItem+1)%config.totalItems;
			itemPlus2 = (thisItem+2)%config.totalItems;
			itemPlus3 = (thisItem+3)%config.totalItems;
			
			
			infoSpeed = numToMove == 1 ? 600 : 900;
			
			// switch info panel
			liObjects.eq(thisItem).children(".infoblock").animate({"opacity":"hide"},{duration: 100, queue: "fv-features1"});
				setTimeout ( function() {
							liObjects.eq(num).children(".infoblock").animate({"opacity":"show"},{duration: 100, queue: "fv-features1"});
					}, infoSpeed );
			
			
			// move main
			if (!isForward && numToMove == 2) {
							liObjects.eq(thisItem).children(".img")
								.animate({"left": 860},{easing: config.easing, duration: 400, queue: "fv-features2", scope: "scope1"})
								.children("img")
								.animate(imgSmall, {easing: config.easing, duration: 400, queue: "fv-features2", scope: "scope1"})
								.end()
								.animate({"left": 2000},{easing: config.easing, duration: 400, queue: "fv-features2", scope: "scope2"});
			} else {
							var a = isForward ? {"left": -550} : {"left": 860};
							var b = numToMove==1 ? 800 : 400;
							
							liObjects.eq(thisItem).children(".img")
								.animate(a,{easing: config.easing, duration: b, queue: "fv-features2", scope: "scope1"})
								.children("img")
								.animate(imgSmall, {duration: b, easing: config.easing, queue: "fv-features2", scope: "scope1"});
			}
			
			// move +1 item
			if (isForward) {
							if (numToMove == 1) {
								// move 2nd item to center
								liObjects.eq(itemPlus1).children(".img")
									.animate({"left": 285},{duration: 800, easing: config.easing})
									.children("img")
									.animate(imgNormHeight,{duration: 800, easing: config.easing})
							} else {
								// move 2nd item across screen and off to the left
								liObjects.eq(itemPlus1).children(".img")
									.animate({"left": 285},{duration: 400, easing: "linear", queue: "fv-features3", scope: "scope1"})
									.children("img")
									.animate(imgNormHeight,{duration: 400, easing: "linear", queue: "fv-features3", scope: "scope1"})
									.end()
									.animate({"left": -550},{duration: 400, easing: config.easing, queue: "fv-features3", scope: "scope2"})
									.children("img")
									.animate(imgSmall,{duration: 400, easing: config.easing, queue: "fv-features3", scope: "scope2"});	
							}
			} else {
							if (numToMove == 1) {
								// move 2nd item to center
								liObjects.eq(itemPlus1).children(".img")
									.animate({"left": 2000},{duration: config.speed, easing: config.easing, queue: "fv-features3", scope: "scope2"});				
							} else {
								// move 2nd item across screen and off to the left
								liObjects.eq(itemPlus1).children(".img")
									.animate({"left": 2000},{duration: 400, easing: config.easing, queue: "fv-features2", scope: "scope1"});		
							}
			}			
			
			
			// move +2 item
			if (isForward) {				
							if (numToMove == 1) {
								// move item left to sit on right hand side
								liObjects.eq(itemPlus2).children(".img")
									.css({"display": "block", "left": 2000})
									.animate({"left": 860},{duration: 800, easing: config.easing})
									.children("img")
									.css(imgSmall);
							} else {
								// move item left to sit in center
								liObjects.eq(itemPlus2).children(".img")
									.css({"display": "block", "left": 2000})
									.animate({"left": 860},{duration: 400, easing: config.easing, queue: "fv-features3", scope: "scope1"})
									.children("img")
									.animate(imgSmall,{duration: 400, easing: config.easing, queue: "fv-features3", scope: "scope1"})
									.end()
									.animate({"left": 285},{duration: 400, easing:config.easing, queue: "fv-features3", scope: "scope2"})
									.children("img")
									.animate(imgNormHeight,{duration: 400, easing:config.easing, queue: "fv-features3", scope: "scope2"});
							}
			}
			
			// move +3 item
			if (isForward) {
							if (numToMove == 2) {
								liObjects.eq(itemPlus3).children(".img")
									.css({"display": "block", "left": 2000})
									.animate({"left": 860},{duration: 400, easing:"easeInQuad", queue: "fv-features3", scope: "scope2"})
									.children("img")
									.animate(imgSmall,{duration: 400, easing:"easeInQuad", queue: "fv-features3", scope: "scope2"})
							}
			}
			
			// move -1 item
			if (!isForward) {
							if (numToMove == 1) {
								liObjects.eq(itemMinus1).children(".img")
									.css({"display": "block", "left": -550})
									.animate({"left": 285},config.speed, config.easing)
									.children("img")
									.css(imgSmall)
									.animate(imgNormHeight, {duration: config.speed, easing: config.easing});
							} else {
								liObjects.eq(itemMinus1).children(".img")
									.css({"display": "block", "left": -550})
									.animate({"left": 285},{duration: 400, easing: "linear", queue: "fv-features3", scope: "scope1"})
									.children("img")
									.css(imgSmall)
									.animate(imgNormHeight, {duration: 400, easing: "linear", queue: "fv-features3", scope: "scope1"})
									.end()
									.animate({"left": 860},{duration: 400, easing: config.easing, queue: "fv-features3", scope: "scope2"})
									.children("img")
									.animate(imgSmall, {duration: 400, easing: config.easing, queue: "fv-features3", scope: "scope2"});
							}
			}
			
			// move -2 item
			if (!isForward) {
							if (numToMove == 2) {
								liObjects.eq(itemMinus2).children(".img")
									.css({"display": "block", "left": -550})
									.animate({"left": 285}, {duration: 400, easing: config.easing, queue: "fv-features3", scope: "scope2"})
									.children("img")
									.css(imgSmall)
									.animate(imgNormHeight, {duration: 400, easing: config.easing, queue: "fv-features3", scope: "scope2"});
							}
			}
				
				
			config.currentItem = num;
				
			$("#carousel-nav li").removeClass("on").eq(config.currentItem).addClass("on");
				
		}
		
		
		
		
		init(this);
		return this;
		
   };
 
 })(jQuery);