var m = document.uniqueID && document.compatMode && !window.XMLHttpRequest && document.execCommand; 
try {if(!!m) {m("BackgroundImageCache", false, true);}}catch(e) {};

(function($){

$.fn.carousel = function(opts) {
	
	opts = $.extend({
		buttons:['div.left a','div.right a','div.pause a','div.play a'],
		content:'div.control ul',
		node:'li',
		speed:'slow',
		easing:'swing',
		direction:'hor',
		cycle:0
	},opts);
	
	return $(this).each(function(){
		
		var $this = $(this),
			$content = $this.find(opts.content),
			$nodes = $content.find(opts.node),
			$highlight = $this.find('div.selected'),
			$features = $this.find('div.features'),
			num = $nodes.length,
			nDimension = $nodes.outerWidth(),
			cDimension = nDimension*num,
			position = 0,
			high_position = 0,
			offset = 0,
			active = true,
			playing = true,
			visible,
			timer;
			
		$this.find('div.buttons, div.selected').show();
		$this.find('ul').css('position', 'absolute').clone().attr('id', 'accessibility').css('left', '-10000px').appendTo($this);
		
		if($(this).hasClass('vertical')) {
			opts.direction = 'vert';
			nDimension = $nodes.outerHeight();
			cDimension = nDimension*num;
		}
		
		if(isNaN(opts.size)) {throw new Error('CAROUSEL: size has not been defined or is not a number');}

		visible = Math.min(Math.floor(opts.size/nDimension),num);
		
		$content.css(opts.direction=='hor'?'width':'height',cDimension+'px');
		
		function setActive(e){
			$(this).closest('div').addClass('active');
		}
		
		if(num == 1) {
			$this.find(opts.buttons.join(',')).hide();
		}
		else {
			var $l = $this.find(opts.buttons[0])
			.mousedown(setActive)
			.mouseup(function(){
				move(-1);
				if(!playing){
					clearTimeout(timer);
				}
			})
			.click(function(){
				move(-1);
				if(!playing){ 
					clearTimeout(timer);
				}
				return false;
			})
			.keypress(function(event){
				if (event.keyCode == '0') {
					$l.click();
					 event.preventDefault();
			   	}
			});
			
			var $r = $this.find(opts.buttons[1])
			.mouseup(function(){
				move(1);
				if(!playing){
					clearTimeout(timer);
				} 
			})
			.mousedown(setActive)
			.click(function(){
				move(1);
				if(!playing){
					clearTimeout(timer);
				}
				return false;
			})
			.keypress(function(event){
				if (event.keyCode == '0') {
					$r.click();
					event.preventDefault();
			   	}
			});
			
			$(document).mouseup(function(){
				$l.closest('div').removeClass('active');
				$r.closest('div').removeClass('active');
			})
			
			var $play = $this.find(opts.buttons[3]).click(function(){
				playing = true;
				clearTimeout(timer);
				timer = setTimeout(function(){
					move(1);
				}, opts.cycle/5); 
				$(this).closest('div').addClass('active');
				$pause.closest('div').removeClass('active');
				return false;
			})			
			.keypress(function(event){
				if (event.keyCode == '0') {
					$play.click();
					event.preventDefault();
			   	}
			});
			var $pause = $this.find(opts.buttons[2]).click(function(){
				playing = false;
				clearTimeout(timer);
				$(this).closest('div').addClass('active');
				$play.closest('div').removeClass('active');
				return false;
			})		
			.keypress(function(event){
				if (event.keyCode == '0') {
					$pause.click();
					event.preventDefault();
			   	}
			});
		}
		
		$nodes.each(function(n){
			$(this).click(function(){
				jump(n);
				return false;
			})
			.find('img').attr('alt', titles[n].replace(/&amp;/g, "&") + ": " + copy[n]).attr('title', '');
		});
		
		move(0);
		
		if(opts.cycle > 0) {timer = setTimeout(function(){move(1);},opts.cycle);}
		
		function move(direction) {
			if(active && direction !== 0) {
				active = false;
				clearTimeout(timer);
				var p = opts.direction=='hor'?$content.position().left:$content.position().top,
					d = opts.direction=='hor'?$content.outerWidth():$content.outerHeight(),
					prop = opts.direction=='hor'?'left':'top';
	
				if(high_position + direction < 0 || high_position + direction >= visible) {
					position += direction;
					if(position < 0) {
						position = num - visible;
						high_position = visible - 1;
					}
					else if(position + high_position >= num) {
						position = 0;
						high_position = 0;
					}
				}
				else {
					high_position += direction;
				}
				var cTo = {},
					hTo = {};

				cTo[prop] = -position*nDimension; 
				hTo[prop] = high_position*nDimension;
				$content.animate(cTo,opts.speed,opts.easing);
				$highlight.animate(hTo,opts.speed,opts.easing);
				
				update(position + high_position);
				if(opts.cycle > 0) {timer = setTimeout(function(){move(1);},opts.cycle);}
			}
		}
		
		function jump(index) {
			if(active) {
				var to = -1*(high_position + position - index);
				move(to);
			}
		}
		
		function update(position) {
			var $oldpanel = $this.find('div.feature'),
				$panel = $.build('.feature.feat0'+position+' .carouselOverlay').css({position:'absolute'});
				$('<div class="carouselImageWrapper"/>').prependTo($panel).append('<img title="" alt="'+titles[position]+'" src="'+images[position]+'"/>');
				$panel.find('.carouselOverlay')
					.append($('<a href="'+links[position]+'" class="buttonStyle03"><span>Find out more</span></a>'))
					.append($('<h2/>').html('<span>'+titles[position]+'</span>'))
					.append($('<p/>').html(copy[position]));
					
			$features.prepend($panel/*.attr('aria-live', 'polite')*/);
			$oldpanel.fadeOut(opts.speed,function(){
				$(this).remove();
				$panel.css({
					position: ''
				});
				active = true;
			});
			$panel.fadeIn(opts.speed);
		}
		
	});
	
}

})(jQuery);
