var tx_kiwislider_pi1 = { 
	/*
	 *	Default Configuration
	 */
	opts: {
		//for testing
		message: 'Hallo Slider!',
		
		//duration of scrolling
		duration: 300,
		
		//content for arrows
		//will be wrapped by div for trigger the click 
		arrows: {
			left: '',
			right: ''
		},
		cycle: 'true',
		
		//slide horizontal or vertical
		horizontal: 'true',
		
		//automatic movement
		autoscroll: 'false',
		delay: 2000,
		autostop: 'true',
		
		//Extras (not implemented yet)
		easing: 'swing',
		gesture: 'false'
	},
	/*
	 *	Set new Options and merge with defaults
	 */
	set_options: function(object) {
		this.opts = $.extend(this.opts, object);
	},
	/*
	 *	the heart of the slider
	 */	 
	make: function($slider) {
		var _this = this,
			arrows = false,
			$panels = $('.scrollContainer > div', $slider),
			$container = $('.scrollContainer', $slider),
			$nav = $('ul.navigation', $slider),
			random = $slider.attr('rel'),
			cycle = _this.opts.cycle == 'true' ? true : false,
			$scroll = $('.scroll', $slider).css('overflow', 'hidden');
		var hasNav = $nav.length == 1 ? true : false;
		if(_this.opts.arrows.left.length > 0) {
			$scroll
				.before($("<div>").html(_this.opts.arrows.left).attr("id", "tx_kiwi_left_"+random).attr("class", "scrollButtons left"))
			  	.after($("<div>").html(_this.opts.arrows.right).attr("id", "tx_kiwi_right_"+random).attr("class", "scrollButtons right"));
			$arrows = {left: $slider.find('.scrollButtons.left'), right: $slider.find('.scrollButtons.right')};
			arrows = true;
		}
		$container.carouFredSel({
			items: parseInt(_this.opts.visible_elements),
			auto: {
				play: _this.opts.autoscroll == 'true' ? true : false,
				pauseDuration: parseInt(_this.opts.delay),
				pauseOnHover: _this.opts.autostop == 'true' ? false : true,
				scroll: {
					duration: _this.opts.duration
				}
			},
			infinite: _this.opts.autoscroll == 'true' || cycle ? true : false,
			circular: cycle,
			direction: _this.opts.horizontal == 'false' ? 'up' : 'left',
			scroll: {
				items: parseInt(_this.opts.scroll_elements),
				duration: parseInt(_this.opts.duration),
				onAfter: function(odd, cur) {
					if($nav.length > 0) {
						$nav
							.find('.activeSlide').removeClass('activeSlide').end()
							.find('a[href$='+$(cur).attr('id')+']').closest('li').addClass('activeSlide');
					}
				}
			},
			prev: {
				button: arrows ? $arrows.left : false
			},
			next: {
				button: arrows ? $arrows.right : false
			}
		});
		if($nav.length > 0) {
			$('a:first', $nav).closest('li').addClass('activeSlide');
			$('a', $nav).click(function(e) {
				e.preventDefault();
				tar = $(this).closest('.tx-kiwislider-pi1').find('#'+$(this).attr('hash').substring(1));
				$container.trigger('slideTo', tar);
				$(this)
					.closest('li')
					.siblings('.activeSlide')
						.removeClass('activeSlide')
						.end()
					.addClass('activeSlide');
			});
		}
	},
	/*
	 *	create a slider from selector with options
	 */
	init: function(selector, options) {
		this.set_options(options);
	  	this.make($(selector));
	}
}
