function Carousel (id, margin, columns, rows, visible, fadeImages)
{
	this.id = id;
	this.div = $("#" + id);
	this.margin = margin;
	this.columns = columns;
	this.rows = rows;
	this.visible = visible;
	this.fadeImages = fadeImages;
	this.currentPos = 0;
	this.positions;
	this.alphaAvailable = 1;
	this.alphaUnavailable = 0.3;
	this.tooltip = null;
	this.PNGSupport = true; // not for ie7 and below - if you fade a transparent PNG it won't render
	this.ie6 = false;
	this.playerCounter = 0;
	this.currentTitle;
	this.currentProgrammeName;
	this.currentProgrammeURL;
	this.thumbnailURL;
			
	if (navigator.userAgent.match(/msie/i))
	{
		var version = navigator.userAgent.match(/msie [^\r\n][^\r\n][^\r\n]/i);
		version = Number(version[0].substring(5, 6));
		if (version <= 7) this.PNGSupport = false;
		if (version < 7) this.ie6 = true;
	}
	
	var self = this;
	
	this.init = function()
	{
		var counter = 0;
		var margin = this.margin;
		var tooltipRequired = false;
		
		$("#" + this.id).find(".CarouselContent ul li div").width(this.margin - 6);
		
		if (self.columns > 1 || self.rows > 1)
		{
			$(this.div).find('.CarouselContent > a').wrapInChunks('<li />', this.rows * this.columns).appendTo("#" + this.id  + " .CarouselContent ul");
		}
		
		counter = 0;
		$(this.div).find('ul li a').each(function()
		{
			if (counter == self.columns - 1)
			{
				$(this).css({'margin-right':0});
			}
			counter ++;
			if (counter > self.columns - 1) counter = 0;
		});
		
		counter = 0;
		$("#" + this.id).find("ul li").each(function()
		{
			$(this).css({left:counter * margin});
			if (self.fadeImages) $(this).children("a").children("img").css({opacity:0.9});
			$(this).show();

			if ($(this).attr('tooltip') != '' && $(this).attr('tooltip') != undefined)
			{
				tooltipRequired = true;
				$(this).mouseenter(self.ShowTooltip);
				$(this).mouseleave(self.HideTooltip);
			}
			
			counter ++;
		});
		
		$("#" + this.id).find("a").click(this.Click);
		
		if (tooltipRequired) this.addTooltip();
			
		this.positions = Math.ceil($("#" + this.id).find("ul li").size() / this.visible);
		
		if (self.fadeImages) $("#" + this.id).find("ul li a").mouseover(this.Over);
		if (self.fadeImages) $("#" + this.id).find("ul li a").mouseout(this.Out);	
		
		if (this.positions > 1)
		{
			$("#" + this.id).find(".CarouselButton").click(this.ArrowClick);
			this.moveIt(this.currentPos);
		}
		else
		{
			$("#" + this.id).find(".CarouselContent").css({left:0});
			$("#" + this.id).find(".CarouselButton").hide();
		}
	}
	
	this.Click = function()
	{
		if ($(this).attr('videoURL') != '' && $(this).attr('videoURL') != undefined)
		{
			var divID = "VideoPlayer" + self.playerCounter;
			$("#VideoPlayerInner").html('<div id="' + divID + '"></div>');
			
			self.currentTitle = $(this).attr('title');
			self.currentProgrammeName = $(this).attr('programmeName');
			self.currentProgrammeURL = $(this).attr('programmeURL');
			self.thumbnailURL = $(this).attr('thumbnailURL');
			$("#VideoPlayer > img").remove();
			$("#VideoPlayer").append('<img src="' + self.thumbnailURL + '" width="650" height="360" />');
			
			var flashvars = {videoURL:$(this).attr("videoURL"), /*thumbnailURL:$(this).attr("thumbnailURL"), */colour:"1ca08e", autoPlay:"true", width:650, height:360};
			var params = {menu:"false", wmode:"transparent", bgcolor:"#1ca08e", allowfullscreen:"true"};
			var attributes = {id:"FlashVideoPlayer"};
			swfobject.embedSWF("assets/flash/dynamic.player.v1.swf", divID, "650", "360", "10.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
			self.playerCounter ++;
		}
	}
	
	this.show = function()
	{
		$("#VideoPlayerInner").show();
	}
	
	this.closer = function()
	{
		$("#VideoPlayerInner").hide();
	}
	
	this.setVideoTitle = function(title, currentArray, currentIndex, currentOpts)
	{
		return '<span class="Upper">' + self.currentTitle + '</span> in ' + '<span class="Upper"><a href="' + self.currentProgrammeURL + '">' + self.currentProgrammeName + '</a>';
	}
	
	this.Over = function()
	{
		$(this).children("img").stop().animate({opacity:1}, 500, "easeOutExpo");
	}
	
	this.Out = function()
	{
		$(this).children("img").stop().animate({opacity:0.9}, 500, "easeOutExpo");
	}
	
	this.ArrowClick = function(event)
	{
		var direction = $(this).hasClass('CarouselRight') ? 1 : -1;
			
		var newPos = self.currentPos + direction;
		
		if (newPos > -1 && newPos < self.positions)
		{
			self.moveIt(newPos);
		}
	}
	
	this.moveIt = function(pos)
	{
		$("#" + this.id).find("ul").stop().animate({left:-pos * this.visible * this.margin}, 750, "easeInOutExpo");
		this.currentPos = pos;
		
		if (this.currentPos == 0)
		{
			$("#" + this.id).find(".CarouselLeft").stop().animate({opacity:this.alphaUnavailable}, 500, "easeOutExpo");
			$("#" + this.id).find(".CarouselLeft").removeClass("Active");
		}
		else
		{
			$("#" + this.id).find(".CarouselLeft").stop().animate({opacity:this.alphaAvailable}, 500, "easeOutExpo");
			$("#" + this.id).find(".CarouselLeft").addClass("Active");
		}
		
		if (this.currentPos == this.positions - 1)
		{
			$("#" + this.id).find(".CarouselRight").stop().animate({opacity:this.alphaUnavailable}, 500, "easeOutExpo");
			$("#" + this.id).find(".CarouselRight").removeClass("Active");
		}
		else
		{
			$("#" + this.id).find(".CarouselRight").stop().animate({opacity:this.alphaAvailable}, 500, "easeOutExpo");
			$("#" + this.id).find(".CarouselRight").addClass("Active");
		}
	}
	
	this.ShowTooltip = function()
	{
		var html = '';
		html += '<h2>' + $(this).find('.Title').html() + '</h2>';
		html += '<h3>' + $(this).find('.Subtitle').html() + '</h3>';
		html += $(this).attr('tooltip');
		$(self.tooltip).find(".TooltipContent").html(html);
		
		var left = $(this).offset().left + $(this).width() - 25;
		var top = $(this).offset().top + $(this).height() / 2 - Math.round($(self.tooltip).height() / 2 + 15);
		$(self.tooltip).find(".TooltipArrow").css({top:$(self.tooltip).height() / 2 - 30});
		
		if (self.PNGSupport) $(self.tooltip).css({opacity:0});
		$(self.tooltip).css({top:top});
		if (!self.PNGSupport) $(self.tooltip).css({left:left});
		if (self.PNGSupport) $(self.tooltip).css({left:left + 50});
		$(self.tooltip).show();
		if (self.PNGSupport) $(self.tooltip).stop().animate({opacity:1, left:left}, 500, "easeOutExpo");
	}
	
	this.HideTooltip = function()
	{
		$(self.tooltip).hide();
	}
	
	this.addTooltip = function()
	{
		var tooltipID = this.id + "Tooltip";
		
		// Store the tooltip object
		$("body").append('<div id="' + tooltipID + '" class="Tooltip"><span class="TooltipArrow"></span><span class="TooltipTop"></span><span class="TooltipMiddle"><span class="TooltipContent"></span></span><span class="TooltipBottom"></span></div>');
		this.tooltip = $("#" + tooltipID);
	}
	
	this.init();
}
