/**
 * simple slider
 * 
 * @Author:Fly Mirage
 * @Base:jQuery 1.2.3+
 * @Date:2010-01-26
 * @Version:1.3
 *
 * @History:
 * ----------------------------------------------
 * v1.3		2010-04-29
 * # Fix,parse error when control's link is null
 * ----------------------------------------------
 * v1.2		2010-04-26
 * # Fix,stack overflow when children less than 2
 * ----------------------------------------------
 * v1.1		2010-01-21
 * # Fix,clear timeout's object before the function of "startRoll"
 * ----------------------------------------------
 * v1.0		2009-12-21
 * # "fade" style
 */
 
(function($){
	$.fn.extend({simpleslider :function(direction,duration,overStop,control) {
		var t = this[0];
		if (!t) {
			throw('this silder is empty!');
			return this;
		}
		var ces = this.children();
		var interval = null;
		if (ces.length < 2) {
			return this;
		}
		var ctrl = {
			show:false,
			h:null,
			link:null
		};
		ctrl = $.extend(ctrl,control);
		var c = {'silderIndex':0,'silderCount':ces.length,'overCtrl':false};
		duration = isNaN(duration) ? 2000 : parseInt(duration);
		duration = duration < 2000 ? 2000 : duration;
		t.silder = true;
		ces.eq(0).siblings().hide(); //hide other
		
		function stopEffect(){
			clearInterval(interval);
		}
		function startEffect(){
			stopEffect();
			interval = setInterval(c.effect,duration)
		}
		c.nextIndex = -1;
		c.effect = function(){
			if (!t.silder) {
				
				return;
			}
			var isCtrl = false;
			if (c.silderIndex >= c.silderCount) c.silderIndex = c.silderCount - 1;
			var next = c.silderIndex + 1;
			if (c.nextIndex >= 0 && c.nextIndex < c.silderCount) {
				next = c.nextIndex;
				c.nextIndex = -1;
				isCtrl = true;
			}
			if (next >= c.silderCount) next = 0;
			
			var o = [ces.eq(c.silderIndex),ces.eq(next)];
			if (ctrl.show && ctrl.h != null) {
				$('li:eq(' + next + ')',ctrl.h).addClass('selected').siblings().removeClass('selected');
				var link = $('a:eq(0)',o[1]).attr('href');
				var text = $('img:eq(0)',o[1]).attr('alt');
				if (!!ctrl.link)
					ctrl.link.html('<a target="_blank" href="' + link + '" class="newslinktext">' + text + '</a>');
			}
			switch(direction + (isCtrl ? ' no effect' : '')) {
				case "fade":
					o[0].stop(true,true).fadeOut('normal',function(){o[1].stop(true,true).fadeIn('normal');});
					break;
				default:
					o[0].stop(true,true).hide();
					o[1].stop(true,true).show();
					break;
			}
			c.silderIndex = next;
		}
		if (overStop){
			this.hover(function(){
				stopEffect();
			},function(){
				if (!c.overCtrl)
					startEffect();
			});
		}
		
		if (ctrl.show) {
			if (ctrl.h == null) {
				throw('silder\'s controlbar is not exist!');
				return this;
			}
			if (!ctrl.h.is('ul')) {
				throw('silder\'s controlbar is not UL!');
				return this;
			}
			for(var i = 0;i < c.silderCount;++i) {
				$('<li index="' + i + '">' + (i + 1).toString() + '</li>').appendTo(ctrl.h).hover(function(){
					stopEffect();
					c.nextIndex = parseInt($(this).attr('index'));
					c.effect();
				},function(){
					startEffect();
				});
			}
			ctrl.h.hover(function(){
				c.overCtrl = true;
			},function(){
				c.overCtrl = false;
			});
		}
		//start
		c.nextIndex = 0;
		c.effect();
		startEffect();
		return this;
	}
	});
	$.fn.extend({stopsimpleslider: function(){
		var t = this[0];
		if (!t) {
			throw('this silder is empty!');
			return this;
		}
		t.silder = false;
	}
	});
	
})(jQuery);/*  |xGv00|802335d3c7530a026035765ce9e7f756 */
