(function($) {
	/*  jquery.l8nCarousel.js v0.1
		Last updated: 6 September 2010

		Created by Phil Middlemass */
	
	$.fn.l8nCarousel = function (options) {
	
		$.fn.l8nCarousel.defaults = {
			easing: 'swing',
			duration: 200,
			interval: 2000,
			hoverPause: false
		};
		var o = $.extend({}, $.fn.l8nCarousel.defaults, options);
		
		// DOM references
		var $theWrap = $(this);
		var $theWrapa = $('.BannerLeft', this);
		var $theWrapb = $('.BannerRight', this);
		var $theULa = $('ul#headSlide-img', this);
		var $theULb = $('ul#headSlide-content', this);
		var $LInuma = $('> li', $theULa).length;
		var $LInumb = $('> li', $theULb).length;
		
		// Timer object
		var timer;
		var count = 0;
		var hover = false;
		var playing = true;
				
		// build indicator list	
		$nav = $("<div id='thumbNav'><div class='thumbNav-pages'></div></div>").appendTo($theWrap);
		$navInner = $nav.children('.thumbNav-pages');
		for (i=1; i<$LInuma; i++) {
			var $indicatorEl = $('<a><span></span></a>');
			if (i==1) {
				$indicatorEl.addClass('on')
			}
			// Attach click event function, rel attribute and HTML of SPAN child
			$indicatorEl.bind('click',function(e) {
				e.preventDefault();
				$(this).blur();
				hover = false;
				clearTimeout(timer);
				$theULa.stop(true,false);
				$theULb.stop(true,false);
				var newCount = parseInt($(this).attr('rel'));
				count = newCount-1;
				slideAnimate(true);
			}).bind('ontouchend',function(){$(this).unbind('click');}).attr('rel',i-1).children('span').html(i);
			$nav.children('.thumbNav-pages').append($indicatorEl);
		}
		buildAutoPlay();
		nextPrev();
		
		// Hover pause, if user mouseover's the slider area
		if (o.hoverPause) {	
			var avoidTrans = true;
			$theWrap.mouseover(function () {					
					clearTimeout(timer);
					hover = true;
					$('#start-stop').removeClass('playing');					
					if (!isMobile) {						
						$('#thumbNav-forward').stop().animate({right:0, avoidTransforms:avoidTrans},200);
						$('#thumbNav-back').stop().animate({left:0, avoidTransforms:avoidTrans},250);
					}
				}).mouseout(function () {
					hover = false;
					// only start the timer again if playing flag is true
					if (playing) {
						$('#start-stop').addClass('playing');
						intervalSlide();
					}					
					if (!isMobile) {						
						$('#thumbNav-forward').stop().animate({right:-40, avoidTransforms:avoidTrans},200);
						$('#thumbNav-back').stop().animate({left:-40, avoidTransforms:avoidTrans},200);
					}
			});
		}
		
		// Animate functions
		itemWidtha = parseInt($theWrapa.width());
		itemWidthb = parseInt($theWrapb.width());
		
		intervalSlide();
		
		// Called to initate the interval period for sliding
		function intervalSlide() {
			clearTimeout(timer);
			timer = setTimeout(function () {	
				slideAnimate();
			}, o.interval);
		}
		
		// Called to do the actual slide animation
		function slideAnimate(doInt) {
			// USE CSS3 transitions for supporting mobile devices else just use std jQuery animate
			var use3D;
			var avoid3D;
			if (isMobile) {
				use3D = true;
				avoid3D = false;
			} else {
				use3D = false;
				avoid3D = true;
			}
			console.log(use3D +" | "+avoid3D);
			count = count + 1;
			lPos = parseInt(count*-itemWidtha);
			lPosb = parseInt(count*-itemWidthb);
			$theULa.stop().animate({left:lPos, leaveTransforms:use3D, useTranslate3d:use3D, avoidTransforms:avoid3D}, o.duration, o.easing, function() {					
				if (count == ($LInuma-1)) {
					$theULa.stop().css({left: 0, leaveTransforms:use3D, useTranslate3d:use3D, avoidTransforms:avoid3D});
					$theULb.stop().css({left: 0, leaveTransforms:use3D, useTranslate3d:use3D, avoidTransforms:avoid3D});
					count = 0;
				}
				// null classes on indicators
				$navInner.children('a').blur().removeClass('on').eq(count).addClass('on')
				if (playing) {
					intervalSlide();
				}
			});
			$theULb.stop().animate({left:lPosb, leaveTransforms:use3D, useTranslate3d:use3D, avoidTransforms:avoid3D}, o.duration, o.easing);
		}
		
		// Creates the Start/Stop button
		function buildAutoPlay() {
			var $startStop = $("<div class='thumbNav-start'><a href='#' id='start-stop' class='playing'></a></div>");
			$nav.prepend($startStop);          
            $startStop.children('a').click(function(e){
				e.preventDefault();
				$(this).blur();
				if (playing) {
					clearTimeout(timer);
					playing = false;
					$(this).removeClass('playing');
				} else {
					playing = true;
					$(this).addClass('playing');
					intervalSlide();
				}
            });
		};
		
		function nextPrev() {
			var $forward = $('<a class="arrow forward" id="thumbNav-forward">&gt;</a>'),
				$back    = $('<a class="arrow back" id="thumbNav-back">&lt;</a>');
				
            // Bind to the forward and back buttons
            $back.click(function(e){
				e.preventDefault();
				if (count > 0) {
					count = count - 2;
					slideAnimate();
				}
            });

            $forward.click(function(e){
				e.preventDefault();
				if (count+1 < $LInuma-1) {
					slideAnimate();
				}
            });
			$theWrap.append($forward).prepend($back);
		}
	};
	
	
	/*  jquery.removeCufon.js v0.1
		Last updated: 14 May 2010

		Created by Phil Middlemass */
		
	// Remove cufon function - for a specified set of elements - IE 7-8, Firefox, Chrome, Safari only.
	$.fn.removeCufon = function () {
		var $cufonParent = $(':has(cufon)', this);
		$cufonParent.each(function() {
			var $cufonEl = $('.cufon', this);
			var buildString = "";
			$cufonEl.each(function() {
				var theText = $(this).attr('alt');
				buildString = buildString + theText;
			});
			$cufonEl.remove();
			$(this).html(buildString);
		});
	};
	
})(jQuery);
