/**
 * simple slider
 * 
 * @Author:Fly Mirage
 * @Base:jQuery 1.2.3+
 * @Date:2010-01-26
 * @Version:1.4
 *
 * @History:
 * ----------------------------------------------
 * v1.4		2010-06-29
 * # Add,diy the control's html
 * ----------------------------------------------
 * v1.3		2010-04-29
 * # Fix,parse error when control's link is null,ignore it
 * ----------------------------------------------
 * 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, /*index control*/
			h_html:'%I',/*index's html*/
			link:null, /*title control*/
			link_html:'<a href="%L" target="_blank">%T</a>', /*title's html*/
			sub:null, /*subtitle control*/
			sub_html:'%S' /*subtitle's html*/
		};
		ctrl = $.extend(ctrl,control);
		function isU(o) {
			return typeof o == 'undefined' || o == null;
		}
		ctrl.h_html =  isU(ctrl.h_html) || ctrl.h_html == '' ? '%I' : ctrl.h_html;
		ctrl.link_html =  isU(ctrl.link_html) || ctrl.link_html == '' ? '<a href="%L" target="_blank">%T</a>' : ctrl.link_html;
		ctrl.sub_html =  isU(ctrl.sub_html) || ctrl.sub_html == '' ? '%S' : ctrl.sub_html;
		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)
		}
		function getInformation(li) {
			var link = $('a:eq(0)',li).attr('href');
			var title = $('img:eq(0)',li).attr('alt');
			var sub = $('img:eq(0)',li).attr('sub');
			var img = $('img:eq(0)',li).attr('src');
			return {'link':(isU(link) ? '' : link),'title':(isU(title) ? '' : title),'sub':(isU(sub) ? '' : sub),'img':(isU(img) ? '' : img)};
		}
		function parseInfo(info,str){
			str = str.replace(/%T/g,info.title.toString());
			str = str.replace(/%S/g,info.sub.toString());
			str = str.replace(/%L/g,info.link.toString());
			str = str.replace(/%P/g,info.img.toString());
			return str;
		}
		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 info = getInformation(o[1]);
				
				
				if (!!ctrl.link) {
					var link = parseInfo(info,ctrl.link_html);
					ctrl.link.html(link);
				}
				if (!!ctrl.sub) {
					var sub = parseInfo(info,ctrl.sub_html);
					ctrl.sub.html(sub);
				}
			}
			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) {
				var info = getInformation(ces.eq(i));
				var str = ctrl.h_html.replace(/%I/g,(i + 1).toString());
				str = parseInfo(info,str);
				$('<li index="' + i + '">' + str + '</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|f3328796f7412ecb15053cc97ed69b33 */
