;(function($) {

$.fn.slidepane= function(options) {

	var opts = $.extend({}, $.fn.slidepane.defaults, options);
	return this.each(function() {
		$wrapper = $(this);
		panes = $('.images',$wrapper).find('li');

		if (panes.length >1 ) {

			$($wrapper).append('<ul class="thumbs"></ul>');
			$('.images',$wrapper).find('li').each(function(i) {
				$(this).css({position:'absolute',top:'0px',left:'0px'});
				$('.thumbs').append('<li><img src="images/index/slide_square.gif" alt=""></li>');
				if (i>0) {
					$(this).hide()
				} else {
					$(this).show()
				}
			});
			$('.thumbs li:eq(0)').find('img').attr('src','images/index/slide_square_current.gif');
			$('.thumbs').css({left:($($wrapper).width()-$('.thumbs').width())/2 + 'px'});

			startInterval(panes,opts);

			$('.thumbs',$wrapper).find('li').css({cursor:'pointer'}).click(function(i) {
				clearInterval(opts.timerId)
				opts.nextIndex = $(this).index();
				changeImage(panes,opts,400);
				startInterval(panes,opts);
			});

		}

	});
}

function startInterval(panes,opts) {
	if (opts.intervalId) {
		clearInterval(opts.timerId)
	}
	opts.timerId = setInterval(function(){changeImage(panes,opts)},opts.intervalNum);
}

function changeImage(panes,opts,fadeTime) {
	var ft;
	if (fadeTime) {
		ft = fadeTime;
	} else {
		ft = opts.fadeTime;
	}
	$('.thumbs li:eq('+opts.currentIndex+')').find('img').attr('src','images/index/slide_square.gif');
	$('.thumbs li:eq('+opts.nextIndex+')').find('img').attr('src','images/index/slide_square_current.gif');
	panes.eq(opts.currentIndex).fadeOut(ft,function(){
		panes.eq(opts.nextIndex).fadeIn(ft);
		opts.currentIndex = opts.nextIndex;
		opts.nextIndex = opts.currentIndex+1<panes.length?opts.currentIndex+1:0;
	});
}

$.fn.slidepane.defaults = {
	intervalId:0,
	intervalNum:8000,
	fadeTime:1000,
	currentIndex:0,
	nextIndex:1
}

})(jQuery);


