/*
	Sport Runner - Believe in the Run
	Author: Scott Kyle
*/

// flash accessible namespace
var SR = {
	open : function(url) {
		return !!window.open(url);
	}
};

// crazy fix to once and for all plug a horrible firefox 3 error
// where at one point a DOM element returns a huge number as its
// length, when it should be undefined
jQuery.makeArray = function( array ) {
	var ret = [];

	if( array != null ){
		var i = array.length;
		//the window, strings and functions also have 'length'
		if( i == null || array.nodeType || array.split || array.setInterval || array.call )
			ret[0] = array;
		else
			while( i )
				ret[--i] = array[i];
	}

	return ret;
};

(function($) {
	$(function() {
		
		// setup quote form
		var $form = $('#quotes form');
		$('#quotes .button').appendTo($form).addRules('submit');
		var form = new $.FormCheck($form, {
			ajax : {
				success : function(response) {
					if (/failed/i.test(response)) form.submitError();
					else $('#quotes').addClass('sent');
				}
			}
		});
		
		// auto rotate through the quotes
		var timer, $arrow = $('#quotes .arrow-right');
		
		var quotes = new Carousel('#quotes .carousel', {
			left : '#quotes .arrow-left',
			right : '#quotes .arrow-right',
			size : 1,
			onChange : function() {
				clearTimeout(timer);
				timer = setTimeout(function() {
					quotes.change(1);
				}, 10000);
			}
		});
		
		// start the timer
		quotes.onChange();
		
		// create the download bubble
		var $download = $('#wallpapers .arrow-down').bubble('.bubble');
		var $carousel = $('#wallpapers .carousel');
		
		new Carousel($carousel, {
			left : '#wallpapers .arrow-left',
			right : '#wallpapers .arrow-right',
			size : 1,
			speed : 'fast',
			onChange : wallpaper
		});
		
		wallpaper();
		function wallpaper() {
			$download.attr('href', $carousel.find('li a').attr('href'));
		}
		
		// play video
		$('#nikeplus .button, #video').click(function() {
			$('html, body').animate({scrollTop : 0});
			var id = $('#flash').find('embed, object')[0].id;
			var swf = $.browser.msie ? window[id] : document[id];
			swf.playNikePlusVideo();
			return false;
		});
		
		// fix me some pngs in IE6
		if (window.supersleight && $.browser.msie && $.browser.version < 7) {
			
			// supersleight will set A tags to relative
			$('#modules .button').css('position', 'absolute');
			$('#wallpapers .arrow-down').css('position', 'static');
		}
	});
	
	// floating "Coda" bubble
	$.fn.bubble = function(bubble, options) {
		options = $.extend({
			distance : 10,
			speed : 200,
			delay : 300,
			easing : 'swing'
		}, options);
		
		var $bubble = typeof bubble == 'string' ? this.find(bubble) : $(bubble);
		var top = $bubble.show().position().top;
		var timer, shown = false;
		reset();
		
		function reset() {
			if (!shown) $bubble.hide().css('top', top + options.distance);
		}
		
		function animate(show, callback) {
			var delta = {
				top : top - (show ? 0 : options.distance)
			};
			
			if ($.browser.msie) $bubble[show ? 'show' : 'hide']();
			else delta.opacity =  show ? 'show' : 'hide';

			if ($.browser.msie && !show) callback && callback();
			else $bubble.animate(delta, options.speed, options.easing, callback);
		}
		
		function show() {
			if (shown)
				clearTimeout(timer);
			else {
				shown = true;
				animate(true);
			}
		}
		
		function hide() {
			timer = setTimeout(function() {
				shown = false;
				animate(false, reset);
			}, options.delay);
		}
		
		this.hover(show, hide);
		return this;
	};
	
	// Carousel that can be infinite
	function Carousel(container, options) {
		options = $.extend({
			left : '#arrow-left',
			right : '#arrow-right',
			infinite : true,
			size : 4,
			speed : 'slow',
			easing : 'swing',
			onChange : function() {}
		}, options);
		
		var $el = $(container);
		var $arrows = [$(options.left), $(options.right)];
		
		var width = $el.width();
		$el.css({
			width : width,	// bugfix for ie6
			overflow : 'hidden'
		});
		var $ul = $el.find('ul').css({
			position : 'relative',			// set sane CSS values
			width : 9999
		});									// li elements need to have
		var $li = $ul.children('li');		// CSS dimensions for spacers
		
		// fix pngs now, before stuff is taken off the DOM
		if (window.supersleight && $.browser.msie && $.browser.version < 7) {
			supersleight.limitTo($el.attr('id') || $el.parents('*[id]').attr('id'));
			supersleight.run();
			supersleight.limitTo(false);
		}
		
		// no carousel if only options.size items
		if ($li.size() <= options.size) return;
		
		if (options.infinite) {
			$li.remove();
		
			// place spacers into last page if uneven
			for (var i = ($li.size() % options.size) || options.size; i < options.size; i++)
				$li = $li.add('<li />');
			
			var $pages = [];	// split items into page chunks
			for (var i = 0, l = $li.size(); i < l; i += options.size)
				$pages.push($li.slice(i, i + options.size));
			
			var page = 0, last = $pages.length - 1, step = width;
			
			// put 1st page back into DOM
			$pages[page].appendTo($ul);
		}
		else {
			rollover(0, 'off');
			var page = 0, last = $li.size() - options.size, step = width / options.size;
		}
		
		$.each($arrows, function(i, $arrow) {
			$arrow.show().hover(
				function() {
					rollover(i, 'hover');
				},
				function() {
					rollover(i);
				}
			).click(function() {
				change(i);
			});
		});
		
		var to, next, lock = false;
		
		// change pages
		function change(i) {
			if (lock) return;	// locks until page turn is complete
			else lock = true;
			
			if (i > 0) i = 1;
			else i = 0;
			
			if (options.infinite) {
				next = page + 2 * i - 1;
				next = next > last ? 0 : (next < 0 ? last : next); 	// loop pages
				$ul[i ? 'append' : 'prepend']($pages[next]).css('left', (i - 1) * step);
			
				to = -i * step;
			}
			else {
				next = page + (2 * i - 1) * options.size;
				next = (next < 0) ? 0 : ((next > last) ? last : next);
				
				if (next == page) {
					lock = false;
					return;
				}
				
				to = (i ? '-=' : '+=') + step * Math.abs(next - page);
			}
			
			// smooths animation in IE
			setTimeout(animate, 10);
		}
		
		// animate page change
		function animate() {
			$ul.animate({
				left : to
			}, options.speed, options.easing, function() {
				if (options.infinite) {
					$pages[page].remove();	// remove previous page from DOM
					$ul.css('left', 0);
				}
				else {
					rollover(1 - i, 'on');
					if (next == 0 || next == last)
						rollover(i, 'off');
				}
				page = next;
				options.onChange.call($el, page);
				lock = false;
			});
		}
		
		// change state of arrow
		function rollover(i, state) {
			var suffix = '', $arrow = $arrows[i];
			
			if ((!state || state == 'hover') && $arrow.hasClass('off')) return;
			
			switch(state) {
				case 'off':
					$arrow.removeClass('hover').addClass('off');
					suffix = '_off';
					break;
				case 'on':
					$arrow.removeClass('off');
					break;
				case 'hover':
					$arrow.addClass('hover');
					suffix = '_on';
					break;
				default:
					$arrow.removeClass('hover');
			}
			
			if ($arrow[0].tagName == 'IMG') $arrow.attr('src', swap($arrow.attr('src')));
			else $arrow.css('background-image', swap($arrow.css('background-image')));
			
			function swap(src) {
				return src.replace(/(_on|_off)?\.(gif|png|jpe?g)/i, suffix + '.$2');
			}
		}
		
		// public methods
		return $.extend(this, {
			left : function() { change(0); return this; },
			right : function() { change(1); return this; },
			onChange : function() { options.onChange.call($el, page); return this; },
			change : function(i) { change(i); return this; }
		});
	}
})(jQuery);