(function($) {
	/*  jquery.scCarousel.js v0.1
		Last updated: 14 May 2010

		Created by Phil Middlemass */
	
	$.fn.scCarousel = function (options) {
	
		$.fn.scCarousel.defaults = {
			easing: 'swing',
			duration: 200,
			interval: 2000,
			hoverPause: false
		};
		var o = $.extend({}, $.fn.scCarousel.defaults, options);
		
		// DOM references
		var $theWrap = $('div.wrapper', this);
		var $theUL = $('div.wrapper > ul', this);
		var $LInum = $('> li', $theUL).length;
		var $theIndicator = $('div#indicators', this);
		if ($LInum > 2 ) {
			// Timer object
			var timer;
			var count = 0;
					
			// build indicator list
			for (i=1; i<$LInum; 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.click(function() {
					clearInterval(timer);
					$theUL.stop(true,false);
					var newCount = parseInt($(this).attr('rel'));
					count = newCount-1;
					slideAnimate(true);
				}).attr('rel',i-1).children('span').html('Slide '+i);
				$theIndicator.append($indicatorEl);
			}
			
			// Hover pause, if user mouseover's the slider area
			if (o.hoverPause) {
				$theWrap.mouseover(function () {
						clearInterval(timer);
					}).mouseout(function () {
						intervalSlide();
				});
			}
			
			// clone first LI to create a loop point - refresh Cufon on elements
			//var $clonedLI = $($theUL).children('li:first').clone().addClass('loopClone').appendTo($theUL);
			//$($theUL).children('li.loopClone').removeCufon();
			//Cufon('.infiniteCarousel ul li.loopClone h1, .infiniteCarousel ul li.loopClone p');
			
			// Animate functions
			itemWidth = parseInt($theWrap.width());
			
			intervalSlide();
			
			// Called to initate the interval period for sliding
			function intervalSlide() {
				timer = setInterval(function () {	
					slideAnimate();
				}, o.interval);
			}
			
			// Called to do the actual slide animation
			function slideAnimate(doInt) {
				count = count + 1;
				lPos = parseInt(count*-itemWidth);
				$theUL.animate({left:lPos}, o.duration, function() {					
					if (count == ($LInum-1)) {
						$theUL.css({left: 0});
						count =0;
					}
					// null classes on indicators
					$theIndicator.children('a').removeClass('on').eq(count).addClass('on');
				});
				if (doInt) {
					intervalSlide();
				}
			}
		}
		
	};
	
	
	/*  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);
