var displayIntervalID;

offerCarousel = function(args){
	$carousel = $(this);
	var $width = ($.browser.msie && $.browser.version != "6.0")? $carousel.width():$carousel.width()+1;
	var totalOffers = args.data.length;
	var offers = [];
	$current = 0;
	$carousel.html("<div class='carousel'><div class='offerWrapper'></div></div>")
	
	for(var i=0; i<totalOffers; i++){
		var image = (typeof(args.data[i].image) != "undefined")? "<img src='"+args.data[i].image.src+"' alt='"+args.data[i].image.alt+"' width='185' height='75' />":null;
		var title = (typeof(args.data[i].title) != "undefined")? "<h4 class='black'>"+args.data[i].title+"</h4><div class='separatorInner'></div>":null;
		var desc = (typeof(args.data[i].desc) != "undefined")? "<p class='alt'>"+args.data[i].desc+"</p>":null;
		$carousel.find('.offerWrapper').append("<div id='offer_"+i+"' class='offerpanel'>"+((image!=null)?image:"")+((title!=null)?title:"")+((desc!=null)?desc:"")+"</div>");
	};
	
	$carousel.append("<div class='gradBtnHolder offerControls' style='clear:both; margin:0px;'>"
					+"<a class='offer-prev' href='javascript:void(0);' title='prev'></a>"
					+"<a class='offer-next' href='javascript:void(0);' title='next'></a>"
					+"<a class='circArrow more' href='javascript:void(0);'>Find out more</a></div>");
	
	$carousel.find('.offerWrapper').children('.offerpanel').css({float:"left",
																 width:$width,
																 height:"auto"});
	$carousel.find('.offerWrapper').css({position:"absolute",
										 top:"0px",
										 left:"0px",
										 width:totalOffers*$width,
										 height:"auto"});
	$maxHeight = function(){
		$height = 0;
		$('.offerWrapper').each(function(){
			$height = ($(this).height()>$height)? $(this).height() + 20:$height + 20;
		});
		return $height;
	};
	$carousel.find('.carousel').css({position:"relative", height:$maxHeight(), overflow:"hidden"});
	$(".offerControls").css({width:($width - 5), margin:"0px"});
	$(".offerControls").find(".offer-prev, .offer-next").css({float:"left", marginRight:"3px"})
	
	$(".offer-prev").click(function(){
		$current = ($current==0)? (totalOffers-1)%totalOffers:($current-1)% totalOffers;							
		$carousel.find('.offerWrapper').animate({ left:-1 * $current*$width }, 750, function(){
			$(".offerControls").find(".more").attr({href:args.data[$current].more.href, target:args.data[$current].more.target})});
	});
	$(".offer-next").click(function(){
		$current = ($current+1)% totalOffers;							
		$carousel.find('.offerWrapper').animate({ left:-1 * $current*$width }, 750, function(){
			$(".offerControls").find(".more").attr({href:args.data[$current].more.href, target:args.data[$current].more.target})});
	});
	
	function triggerNext(){
		$(".offer-next").click();
	};
	
	if(args.data.length > 1){
		$carousel.mouseout(function(){
			displayIntervalID = setInterval(triggerNext, args.delay*1000)
		});
	
		$carousel.mouseover(function(){
			clearInterval(displayIntervalID);
		});
	
		displayIntervalID = setInterval(triggerNext, args.delay*1000);
	}else{
		$(".offerControls").find(".offer-prev, .offer-next").remove();
	}
	$(".offerControls").find(".more").attr({href:args.data[$current].more.href, target:args.data[$current].more.target});
	return $carousel;
}

