// JavaScript Document
// by fanks@21mmo.com

function Slider(ctrls, pics, config){
	this.ctrls = ctrls;
	this.pics = pics;
	this.config = {
		speed:1500,//轮播速度
		fadeSpeed:1500,//渐显速度
		auto:true,//是否启用自动播放
		xmlurl:null,//配置图片地址、链接的xml文件地址
		effect:true,//是否开启效果（未实现）
		ctrlClass:'',//当前按钮的样式名
		index:0,//初始化显示第n张图片
		wrap:'',//图片容器
		ctrlClass:''//图片容器的class 
	}
	for(var i in config){
		this.config[i] = config[i];
	}
	
	var _this = this;
	if(_this.config.xmlurl != null){this.loadxml(_this)}
	this.init(_this);
}

Slider.prototype = {
	loadxml:function(_this){
		$.ajax({
			type:'GET',
			url:_this.config.xmlurl,
			async:false,
			cache:false,
			timeout:2000,
			error:function(XMLHttpRequest, textStatus, errorThrown){
				_this.ctrls.hide();
				return false;
			},
			success:function(resp){
				var respics = $(resp).find('items pic');
				_this.pics.find('a').each(function(i){
					$(this).html('<img src="' + $(respics).eq(i).attr('href') + '" />');
				});
			}
		});
	},
	
	init:function(_this){
		if(this.config.index != 0){
			this.pics.hide();
			this.ctrls.removeClass(this.config.ctrlClass);
			this.pics.eq(this.config.index).show();
			this.ctrls.eq(this.config.index).addClass(this.config.ctrlClass);
		}
		function temp(){
			_this.autoSlide();
		}
		
		if(this.config.auto){
			var timer = setInterval(temp,_this.config.speed);
		}
		
		this.timer2 = null;
		
		this.ctrls.each(function(i){
			$(this).mouseover(function(){
				if(timer){
					clearInterval(timer);
				}
				if($(this).hasClass(_this.config.ctrlClass)){
					return false;
				}
				__this = $(this);
				_this.timer2 = setTimeout(function(){
						_this.ctrls.removeClass(_this.config.ctrlClass);
						$(__this).addClass(_this.config.ctrlClass)
						_this.pics.hide();
						_this.pics.eq(i).fadeIn(_this.config.fadeSpeed);
						if(_this.config.wrap != ''){
							var rg = new RegExp(_this.config.wrapClass + '\\d');
							_this.config.wrap.get(0).className = _this.config.wrap.get(0).className.replace(rg, '');
							_this.config.wrap.addClass(_this.config.wrapClass + i);
						}
					},200)
				
			})
			.mouseout(function(){
				if(_this.config.auto){
					timer = setInterval(temp,_this.config.speed);
				}
				clearTimeout(_this.timer2);
			});
		});
	},
	
	autoSlide:function(){
		var rg = new RegExp(this.config.wrapClass + '\\d');
		if(this.config.index == this.pics.length - 1){
			this.pics.eq(this.config.index).hide();
			this.pics.eq(0).fadeIn(this.config.fadeSpeed);
			this.ctrls.eq(this.config.index).removeClass(this.config.ctrlClass);
			this.ctrls.eq(0).addClass(this.config.ctrlClass);
			if(this.config.wrap != ''){
				this.config.wrap.get(0).className = this.config.wrap.get(0).className.replace(rg, '');
				this.config.wrap.removeClass(this.config.wrapClass + this.config.index);
			}
			this.config.index = 0;
			if(this.config.wrap != ''){
				this.config.wrap.get(0).className = this.config.wrap.get(0).className.replace(rg, '');
				this.config.wrap.addClass(this.config.wrapClass + this.config.index);
			}
		}
		else{
			this.ctrls.eq(this.config.index).removeClass(this.config.ctrlClass);
			this.ctrls.eq(this.config.index + 1).addClass(this.config.ctrlClass);
			this.pics.eq(this.config.index).hide();
			this.pics.eq(this.config.index + 1).fadeIn(this.config.fadeSpeed);
			if(this.config.wrap != ''){
				this.config.wrap.get(0).className = this.config.wrap.get(0).className.replace(rg, '');
				this.config.wrap.removeClass(this.config.wrapClass + this.config.index);
			}
			
			this.config.index++;
			if(this.config.wrap != ''){
				this.config.wrap.get(0).className = this.config.wrap.get(0).className.replace(rg, '');
				this.config.wrap.addClass(this.config.wrapClass + this.config.index);
			}
			
		}
		return this.config.index;
	}
}