(function($) {
$.widget("ui.aktionsslider",{
	options: {
		items: [],
		itemStyles: {},
		index: 0,
		itemCount: 4,
		classPrefix: "aktionsslider",
		sliderHeight: 150,
		sliderWidth:  720,
		autoslide: false,
		autoslideDelay: 3000, 
		autoslideAnimationTime: "slow"
	},
	privates: {
		index: 0,
		itemWidth: undefined		/// Absichtlicher IE-BUG!!!
	},
	_init: function(){
		var widget = this;
		
		
		if (widget.options.itemCount > widget.options.items.length) {
			widget.options.itemCount = widget.options.items.length;
		}
		widget._setupSlider();
		
		
		
		if (widget.options.index > widget.options.items.length - widget.options.itemCount){
			widget.options.index = widget.options.items.length - widget.options.itemCount;
		} else if (widget.options.index < 0){
			widget.options.index = 0;
		}
		widget._scrollTo(widget.options.index, false);
		
		
		if(this.options.autoslide){
			this._setupAutoslideTimer();
		}
	},
	_setupSlider: function(){
		var widget = this, $element = $(widget.element), options = widget.options, items = options.items, $container, $list, $item, sliderWidth;
		
		$element.html("<table border='0' cellspacing='0' cellpadding='0'><tr><td class='"+options.classPrefix+"-controls "+options.classPrefix+"-controls-prev'></td><td class='"+options.classPrefix+"-container'></td><td class='"+options.classPrefix+"-controls "+options.classPrefix+"-controls-next'></td></tr></table>")
		
		$container = $element.find("."+options.classPrefix+"-container");
		$prev = $element.find("."+options.classPrefix+"-controls-prev");
		$next = $element.find("."+options.classPrefix+"-controls-next");
				
		$container.append("<div class='"+options.classPrefix+"-body'><div class='"+options.classPrefix+"-slider'><table cellpadding='0' cellspacing='0' class='"+options.classPrefix+"-item-container'><tr></tr></table></div></div>");
		$prev.append("<a href='#' class='prev'><img src='images/aktionsslider/prev.png'></a>");
		$next.append("<a href='#' class='next'><img src='images/aktionsslider/next.png'></a>");
				
		// SETUP List		
		$list = $container.find("."+options.classPrefix+"-item-container tr");
				
		$element.children("table").css({width: options.sliderWidth, height: options.sliderHeight});
		$element.find("."+options.classPrefix+"-body").width(sliderWidth);
		
		itemHeight = widget.options.sliderHeight;
		itemWidth = (options.sliderWidth-60)/widget.options.itemCount;

		widget.privates.itemWidth = itemWidth; 
		
		for(var i=0; i<items.length;i++){
			$item = $("<td class='"+options.classPrefix+"-list-item"+((i==0) ? " "+options.classPrefix+"-list-item-first" : "")+"'></td>")
						.css({width: itemWidth, height: itemHeight});
			$item.wrapInner("<div class='"+options.classPrefix+"-listitem-wrapper'>")
				.find("."+options.classPrefix+"-listitem-wrapper")
				.css({width: itemWidth, height: itemHeight, overflow: "hidden"})
				.append("<div class='"+options.classPrefix+"-listitem-content'>"+this._itemContent(i)+"</div>");
			$item.find("."+options.classPrefix+"-listitem-content").css(widget.options.itemStyles);
			$item.appendTo($list)
		}
		
		
		$container.find("."+options.classPrefix+"-body").css({height:options.sliderHeight});
		$container.find("."+options.classPrefix+"-item-container").css({width: itemWidth * options.items.length});
		
		
		
		$element.find("."+widget.options.classPrefix+"-controls a").click(function(){
			if ($(this).hasClass("next")){
				widget._scrollTo(widget.privates.index+1);
			} else {
				widget._scrollTo(widget.privates.index-1);
			}
			
			if (widget.options.autoslide){
				widget._resetSliderTimer();
			}
			
			return false;
		}).hide();
		

		if(items.length > widget.options.itemCount){
			$element.find("."+widget.options.classPrefix+"-controls a").show();
		}
	},
	_setupAutoslideTimer: function() {
		var widget = this;
		timer = $.timer(widget.options.autoslideDelay,function(){
			widget._scrollTo(widget.privates.index+1); // next img
		});
		
		widget.privates.autoslideTimer = timer;
	},
	_itemContent: function(index){
		var widget = this, item = widget.options.items[index];
		
		// additional Processing
				
		return item;
	},
	_scrollTo: function(indx, animate){
		var widget = this, $element = $(widget.element), items = widget.options.items,  minscroll = 0, maxscroll, $slider = $element.find("."+widget.options.classPrefix+"-slider");
		
		
		animate = (animate === undefined) ? true : animate ;
		
		if (!$slider.is(":animated")){
			maxscroll = (items.length - widget.options.itemCount ) ? items.length - widget.options.itemCount : 0;
			if (indx<minscroll){
				indx=maxscroll
			} else if (indx>maxscroll) {
				indx = 0;
			}
			if (animate){
				$slider.animate({"left": -(indx * widget.privates.itemWidth) }, widget.options.autoslideAnimationTime);
			} else {
				$slider.css({"left": -(indx * widget.privates.itemWidth) });
			}
			widget.privates.index = indx;
		}
	},
	_stopSliderTimer: function(){
		var widget = this;
		if (widget.options.autoslide && widget.privates.autoslideTimer)
			widget.privates.autoslideTimer.stop();
	},
	_resetSliderTimer: function(){
		var widget = this;
		if (widget.options.autoslide && widget.privates.autoslideTimer){
			widget.privates.autoslideTimer.reset(widget.options.autoslideDelay);
		}
	},
	getIndex: function() {
		return this.privates.index;
	}
});
})(jQuery);

