$(document).ready(function () {
	$("#banner .message").redwebSlider({'autoRotate' : true});
	
	$("#finder").uberNav();
});

/*
 * UberNav v1.0 - jQuery plugin to reveal div with horizontal scrolling.
 *
 *	I tried to make this as 'plugin' like as possible but the usage is so specific
 *	that there is hard coded stuff everywhere so best to understand the plugin and
 *	modify it to do what you want etc.
 *
 * Copyright (c) 2008 Redweb Limited
 *
 * Built by Gavin Cooper gavincooper@redweb.com
 *
 */
(function($) {  
	$.fn.uberNav = function(options) {  

		var defaults = {  
			'speed' 	: 500
		}, options = $.extend(defaults, options);  

		this.each(function() { 
			var $super = $(this);
						   
			$(".products", this).each(function () {												
				var numProducts = parseInt($("li", $(this)).length);
				
				if (numProducts > 6) {
					var listWidth = Math.ceil((numProducts / 3)) * 320; // 320 = li width
					$("ul", this).width(listWidth);
					
					// Sort cols...
					switch (numProducts) {
						case 7:
							$("ul li:nth-child(6)", this).css("clear", "left");
							break;
						case 10:
							$("ul li:nth-child(4n)", this).css("clear", "left");
							break;
					}
					
					$(this).append('<div class="slide-wrapper"><div class="slider"></div></div>');
					//$(".slider", this).slider();
				}
				
				// Set the height of the UL - so we can use it later
				// each li is 91px tall.
				var $ul = $(this).find("ul");
				var liHeight = 91;
				var o = liHeight;
				if (numProducts == 3 || numProducts == 4) {
					o = liHeight * 2;
				} else if (numProducts >= 5) {
					o = liHeight * 3;
				}
				
				$("ul", this).css("height", o);
				// container/div has slider thus add extra height to container to accomodate this.
				if ($(".slider", this).length) {
					$("ul", this).css("height", parseInt($("ul", this).css("height")) + 20); // 20 = slider height.
					// ------------------------
					// Setup slider controls
					// ------------------------
					var itemsWidth = parseInt($(this).innerWidth()) - 740; // content width!
					
					$(".slider", this).slider({
						minValue 	: 0,
						maxValue	: itemsWidth
					});
					
					$(".slider", this).bind("slide", function (event, ui) {
						// Current left value of slider.
						var position = parseInt($(".ui-slider-handle", this).css("left"));
						// Calc the location of the slider in percentage value of the maximum value.
						var percentage = (position / 328) * 100;
						// How much of the view is out of view.
						var offset = parseInt($(this).parent().parent().find("ul").width()) - parseInt($(this).parent().parent().parent().width());
						// Percentage of the the out of view space.
						var moveX = (percentage / 100) * offset;
						// Move it.
						$(this).parent().parent().find("ul").css("left", parseInt('-'+moveX));
					});
					// Insert right and left step controls.
					$(".slide-wrapper", this).prepend('<a class="left" href="#" title="Left">&lt;</a><a class="right" href="#" title="Right">&gt;</a>');
					var timerLeft; // hold timer.
					$(".slide-wrapper a.left", this).mousedown(function () {
						$container = $(this).parent();
						timerLeft = setInterval(function () {
							if ((parseInt($(".ui-slider-handle", $container).css("left")) - 5) >= 0) {
								$(".ui-slider-handle", $container).css("left", parseInt($(".ui-slider-handle", $container).css("left")) - 5);
							} else {
								$(".ui-slider-handle", $container).css("left", 0);
							}
							
							// Move view in the same way as above - should really be abstracted.
							var percentage = (parseInt($(".ui-slider-handle", $super).css("left")) / 328) * 100;
							// How much of the view is out of view.
							var offset = parseInt($container.parent().find("ul").width()) - parseInt($container.parent().width());
							// Percentage of the the out of view space.
							var moveX = (percentage / 100) * offset;
							// Move it.
							$container.parent().find("ul").css("left", parseInt('-'+moveX));
						}, 50);
					}).mouseup(function () {
						clearInterval(timerLeft);
					});
					var timerRight;
					$(".slide-wrapper a.right", this).mousedown(function () {
						$container = $(this).parent();
						timerRight = setInterval(function () {
							if ((parseInt($(".ui-slider-handle", $container).css("left")) + 5) <= 328) {
								$(".ui-slider-handle", $container).css("left", parseInt($(".ui-slider-handle", $container).css("left")) + 5);
							} else {
								$(".ui-slider-handle", $container).css("left", 328);
							}
							
							// Move view in the same way as above - should really be abstracted.
							var percentage = (parseInt($(".ui-slider-handle", $super).css("left")) / 328) * 100;
							// How much of the view is out of view.
							var offset = parseInt($container.parent().find("ul").width()) - parseInt($container.parent().width());
							// Percentage of the the out of view space.
							var moveX = (percentage / 100) * offset;
							// Move it.
							$container.parent().find("ul").css("left", parseInt('-'+moveX));
						}, 50);
					}).mouseup(function () {
						clearInterval(timerRight);
					});
				}
				
				if ($(".controls a[rel='"+$(this).attr("id")+"']").length) {
				var id = $(this).attr("id");
				//var btnCenter = parseInt($(".controls a[rel='"+id+"']", $super).offset().top) + parseInt($(".controls a[rel='"+id+"']", $super).height() / 2);
				var btnCenter = parseInt($(".controls a[rel='"+id+"']", $super).offset().top) - parseInt($super.offset().top);
				btnCenter = btnCenter + (parseInt($(".controls a[rel='"+id+"']", $super).outerHeight() / 2));
				var t = (btnCenter - (parseInt($(this).find("ul").css("height")) / 2));
				
				$(this).css("top", t);
				// Sort out - or to much +
				if (t < 0) {
					t = 0;
					$(this).css("top", t);
				} else if ((parseInt($("ul", this).css("height")) + parseInt($(this).css("top"))) > 320) {
					var top = 320 - parseInt($("ul", this).css("height"));
					$(this).css("top", top);
				} else {
					$(this).css("top", t);
				}
				}
				
				$(this).hide();
			});
			
			// TEMPORARY
			//$("#pump").show();
			
			// Setup controls...
			$(".controls li a", $super).click(function() {
				var current = $(".products:visible", $super).attr("id");
				// Hide All...
				$(".products:visible", $super).animate({"width" : 0}, options.speed, null, function () { 
				 	$(this).hide(); 
				 });
				$(".controls li", $super).removeClass("active");
														
				// Only show if we haven't just hidden it...
				if (current != $(this).attr("rel")) {
					if ($("#"+$(this).attr("rel"), $super).length) {
						var e = $("#"+$(this).attr("rel"), $super);
						// Silly storing of width horizontal slideout solution = no time :(
						var width = $("ul", $(e)).css("width");
						
						e.css({"width" : 0, "overflow" : "hidden"});
					
						e.show();
						
						e.animate({"width" : parseInt(width)}, options.speed);
						
						$(this).parent().addClass("active");
					}
				}
				
				return false;
			});
						
		}); // end each  
		
		$.fn.extend({
		fade : function() {
				
		}
		})
	}
})(jQuery); 

/*
 * redwebSlider v1.1 - jQuery image banner slider
 * Copyright (c) 2008 Redweb Limited
 *
 * Built by Gavin Cooper gavincooper@redweb.com
 *
 */
(function($) {  
	$.fn.redwebSlider = function(options) {
	    var autoRotate = true;
	    
	   
		var defaults = {  
			'speed' 	: 1500,
			'delay'		: 10000,
			'autoRotate': autoRotate		
		},options = $.extend(defaults, options);  
        
	    
			
		var timer;
		var currentIndex = 0;
		var totalItems = 0;

		this.each(function() { 
			// Chache "this"
			var $this = $(this);
			totalItems = parseInt($this.children().length);
						   
			$this.parent().css('overflow', 'hidden');
						   
			// Sort with of UL.
			$this.width(totalItems * parseInt($("li:first", this).width()));
						
			if (totalItems > 1) {
				$this.parent().append('<ul id="controls"></ul>');
				var controls = $this.parent().find("ul#controls");
				
				controls.append('<li class="prev"><a href="#" title="Previous">Previous</a></li>');
				
				for (var i = 0; i < totalItems; i++) {
					var text = $("li:nth-child("+(i+1)+") .title", $this).text()+' - '+$("li:nth-child("+(i+1)+") .tagline", $this).text();

					controls.append('<li><a href="#" title="'+text+'">'+text+'</a></li>');
				}
				$("li:nth-child(2)", controls).addClass("selected");
				
				controls.append('<li class="next"><a href="#" title="Next">Next</a></li>');
				
				$("li a", controls).click(function() {
					if (!$(this).parent().hasClass('prev') && !$(this).parent().hasClass('next')) {
						var index = ($("ul#controls li").index($(this).parent())) - 1;
						$this.slideToIndex(index);
					} else if ($(this).parent().hasClass('prev')) {
						$this.transition('prev');
					} else if ($(this).parent().hasClass('next')) {
						$this.transition('next');
					}
					
					if (options.autoRotate)
						clearInterval(timer);
						
					return false;
				});
			}
			
			if (options.autoRotate)
				timer = setInterval(function() { 
				    if(!$('.active').length){
				        $this.transition('next'); 
				    }				
				}, options.delay);
				
			
			return this;  
		
		}); // end each  
		
		$.fn.extend({
		slideToIndex : function(i) {
			if (i != currentIndex) {
				this.slide(i);
				currentIndex = i;
			}				
			return this;
		}, 
		transition : function (direction) {
			var newIndex;
			if (direction == 'next') {
				newIndex = currentIndex == (totalItems - 1) ? 0 : currentIndex + 1;
			} else {
				newIndex = currentIndex - 1;
				newIndex = newIndex < 0 ? (totalItems - 1) : newIndex;
			}
			
			this.slide(newIndex);
			
			currentIndex = newIndex;
		},
		slide : function(i) {
			this.animate({
							'left' : '-' + i * parseInt($("li:first", this).width())
						  }, options.speed);
			
			$("ul#controls li.selected").removeClass("selected");
			$("ul#controls li:nth-child("+(i+2)+")").addClass("selected");
		},
		fade : function(i) {
				
		}
		})
	}
})(jQuery); 