/*
 *		幻灯片对象
 *		功能:	跨浏览器兼容性，单一页面可多实例；实现IE滤镜效果；自动切换图片；标签切换图片
 *			
 *      ouxingzhi_2006@126.com		QQ:157890044
 *		使用示例:	var slide = new Slide("box",{"width":"300px","height":"300px"}) //  第一个参数为放置幻灯片的容器,第二个是可以选参数，目前支持width,height两个参数
 *							slide.add("/images/1.jpg","http://www.shoushow.com","廋秀网");  //添加图片，第一个参数为图片地址,第二个参数为链接地址，第三个参数为标题
 *							slide.add("/images/1.jpg","http://www.shoushow.com","廋秀网");
 *							slide.add("/images/1.jpg","http://www.shoushow.com","廋秀网");
 *							slide.play(3000);		//开始播放;  参数为切换图片的速度
 *							
 */
 
 function Slide(box,option){
	//由ID来获得Element对象
	this.$ = function(obj){
		return document.getElementById(obj);
	}
	//新建元素
	this.C = function(tag,option){
		var mElement = document.createElement(tag);
		if(typeof option == "object"){
			for(var i in option){
				mElement.setAttribute(i,option[i]);
			}
		}
		return mElement;
	}
	//创建文本节点
	this.CTE = function(data){
		return document.createTextNode(data);
	}
	//为制定元素添加子元素
	this.A = function(El,subEl){
		El.appendChild(subEl);
	}
	//返回元素指定的tagname的元素
	this.Tags = function(El,tag){
		return El.getElementsByTagName(tag);
	}
	//给集合统一添加样式
	this.TCss = function(Arr,Style){
		if(Arr.length > 0){
			for(var i=0; i<Arr.length;i++){
				this.Css(Arr[i],Style);
			}
		}
	}
	//判断是否IE
	this.isIE = function(){
		return ( window.navigator.userAgent.indexOf("MSIE") === -1) ? false : true;
	}
	//判断是否为IE6
	this.isIE6 = function(){
		return ( window.navigator.userAgent.indexOf("MSIE 6") === -1) ? false : true;
	}
	//添加样式
	this.Css = function(El,S){
			for(var i in S){
				eval("El.style."+i+" = \""+S[i]+"\";");
			}
	}
	//随机数
	this.rand = function(num){
		num = (num === undefined) ? 22 : num;
		return Math.floor(Math.random() * num);
	}
	/////////////////////////////////
	//公有方法
	//下一张
	this.next = function(){
		
		if(this.now < (this.ilist.length-1)){
			this.now++;
		}else{
			this.now = 0;
		}
		this.go(this.now);
	}
	//上一张
	this.prer = function(){
		if(this.now > 0){
			this.now--;
		}else{
			this.now = this.ilist.length-1;
		}
		this.go(this.now);
	}
	//按键复位
	this.reset = function(style){
		var mlist = this.Tags(this.$(this.Img1boxul),"li");
		this.TCss(mlist,style);
	}
	//标签切换动作样式
	this.swit = function(num){
		//将所有标签样式设为默认
		//指定元素添加样式样式
		var list = this.Tags(this.$(this.Img1boxul),"li");
		this.TCss(list,{"backgroundColor":"#000","color":"#fff","borderColor":"#ffffff"});
		this.Css(list[num],{"backgroundColor":"#fff","color":"#000","borderColor":"#f00"});

	}
	//到指定页数
	this.go = function(num){		
		if((num <= this.ilist.length-1) && (num >= 0)){
			this.now = num;
			if(this.isIE()){
				if(this.isIE6()){
					this.Img1img.src = this.ilist[num].img;
					this.Img1img.title = this.ilist[num].title;
					this.Img1A.title = this.ilist[num].title;
					this.Img1A.href =  this.ilist[num].url;
					this.swit(num);
				}else{
					 var rand = this.rand();
					 this.swit(num);
					 this.Img1box.filters[rand].Apply();
					 this.Img1img.src = this.ilist[num].img;
					 this.Img1box.filters[rand].Play();
					 this.Img1img.title = this.ilist[num].title;
					 this.Img1A.title = this.ilist[num].title;
					 this.Img1A.href =  this.ilist[num].url;
				}
			}else{
				this.Img1img.src = this.ilist[num].img;
				this.Img1img.title = this.ilist[num].title;
				this.Img1A.title = this.ilist[num].title;
				this.Img1A.href =  this.ilist[num].url;
				this.swit(num);
			}
		}
	}
	//添加图片
	this.add = function(img,url,title){
		this.ilist.push({"img":img,"url":url,"title":title});
	}
	//自动运行
	this.play = function(time){
		if(this.ilist.length < 1) return;
		this.init();
		time = (time === undefined) ? 200 : time;
		var mthis = this;
		mthis.resou = window.setInterval(function(){mthis.next()},time);		
	}
	//停止
	this.stop = function(){
		clearInterval(this.resou);
	}
	 //初始化
	 this.init = function(){
		////////////////////////////////////
		//新建盒子对象
		this.boxtwo = this.C("div");
		//设置样式
		this.Css(this.boxtwo,{"overflow":"hidden","position":"relative"});
		this.Css(this.boxtwo,{"width":this.widths,"height":this.heights});
		////////////////////////////////////
		//图片显示区
		//图片1
		this.Img1boxul = "sildeul"+this.rand();
		this.Img1box = this.C("div");
		this.Css(this.Img1box,{"width":"100%","height":"100%","position":"absolute","left":"0px","top":"0px","zIndex":"100","overflow":"hidden"});
		//增加滤镜样式
		if(this.isIE()){
			this.Css(this.Img1box,{"filter":this.filter});
		}
		//A元素
		this.Img1A = this.C("a",{"href":this.ilist[this.now].url,"title":this.ilist[this.now].title,"target":"_blank"});
		//img元素
		this.Img1img = this.C("img",{"src":this.ilist[this.now].img,"title":this.ilist[this.now].title});
		this.Css(this.Img1img,{"border":"0px","width":"100%"});
		this.A(this.Img1A,this.Img1img);
		this.A(this.Img1box,this.Img1A);
		//将两图片添加到盒子
		this.A(this.boxtwo,this.Img1box);
		this.A(this.box,this.boxtwo);
		///////////////////////////////////////
		//图片标签
		this.tagbox = this.C("div");
		this.Css(this.tagbox,{"width":"100%","height":"26px","overflow":"hidden","position":"absolute","left":"0px","bottom":"5px","zIndex":"200","textAlign":"center"})
		this.tagul = this.C("ul",{"id":this.Img1boxul});
		this.Css(this.tagul,{"overflow":"hidden","backgroundColor":"#000","textAlign":"right","padding":"2px 0px"});
		//if(this.isIE()){
			$(this.tagul).css({opacity:0.5});
		//}else{
			//this.Css(this.tagul,{"MozOpacity":"0.5"});
		//}
		//小图片
		//计算小图片尺寸
		//宽度
		var minheight = "18px"
		var minwidth = "18px";
		for(var t=0; t<this.ilist.length; t++){
			var tmpeimg = this.C("li",{"id":t});
			tmpeimg.innerHTML = t+1;
			var Mthis = this;
			this.Css(tmpeimg,{"border":"2px solid #ffffff","textAlign":"center","lineHeight":minheight});
			//标签点击事件处理函数
			tmpeimg.onclick = function(){
				var sthis = Mthis;
				sthis.go(this.getAttribute("id"));
			}
			this.Css(tmpeimg,{"height":minheight,"padding":"0px 5px","cursor":"pointer"});
			this.Css(tmpeimg,{"display":"inline","margin":"2px","color":"#ffffff"});
			this.A(this.tagul,tmpeimg);
			//鼠标移出时
			/*tmpeimg.onmouseout = function(){
				var sthis = Mthis;
				var tthis =this;
				if(tthis.filters == undefined){
					sthis.Css(tthis,{"MozOpacity":"0.75"});
				}else{
					sthis.Css(tthis,{"filter":"progid:DXImageTransform.Microsoft.Alpha(opacity=75)"});
				}
			}
			//鼠标悬停时
			tmpeimg.onmouseover = function(){
				var sthis = Mthis;
				var tthis =this;
				if(tthis.filters == undefined){
					sthis.Css(tthis,{"MozOpacity":"1"});
				}else{
					sthis.Css(tthis,{"filter":"progid:DXImageTransform.Microsoft.Alpha(opacity=100)"});
				}
			}*/
			
		}
		
		this.A(this.tagbox,this.tagul);
		this.A(this.boxtwo,this.tagbox);
		this.Css((this.$(this.Img1boxul).getElementsByTagName("li"))[0],{"backgroundColor":"#fff","color":"#000","borderColor":"#f00"});
	 }
	 ///////////////////////////////
	 //属性
	 //幻灯片容器元素
	 this.box = this.$(box);
	 //图片集
	 this.ilist = new Array();
	 //当前位置
	 this.now = 0;
	 //读取配置
		//宽度
	if(typeof option == "object"){
		if(option.width === undefined){
			this.widths = "300px";
		}else{
			this.widths = option.width;
		}
		//高度
		if(option.height === undefined){
			this.heights = "200px";
		}else{
			this.heights = option.height;
		}
	}else{
		this.widths = "300px";
		this.heights = "200px";
	}
	//IE下滤镜代码
	this.filter =  "";   
	this.filter += "progid:DXImageTransform.Microsoft.Barn(duration=0.5, motion='out', orientation='vertical') ";   
	this.filter += "progid:DXImageTransform.Microsoft.Barn ( duration=0.5,motion='out',orientation='horizontal') ";   
	this.filter += "progid:DXImageTransform.Microsoft.Blinds ( duration=0.5,bands=10,Direction='down' )";   
	this.filter += "progid:DXImageTransform.Microsoft.CheckerBoard()";   
	this.filter += "progid:DXImageTransform.Microsoft.Fade(duration=0.5,overlap=0)";   
	this.filter += "progid:DXImageTransform.Microsoft.GradientWipe ( duration=1,gradientSize=1.0,motion='reverse' )";   
	this.filter += "progid:DXImageTransform.Microsoft.Inset ()";   
	this.filter += "progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=PLUS,motion=out )";   
	this.filter += "progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=PLUS,motion=in )";   
	this.filter += "progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=DIAMOND,motion=in )";   
	this.filter += "progid:DXImageTransform.Microsoft.Iris ( duration=1,irisStyle=SQUARE,motion=in )";   
	this.filter += "progid:DXImageTransform.Microsoft.Iris ( duration=0.5,irisStyle=STAR,motion=in )";   
	this.filter += "progid:DXImageTransform.Microsoft.RadialWipe ( duration=0.5,wipeStyle=CLOCK )";   
	this.filter += "progid:DXImageTransform.Microsoft.RadialWipe ( duration=0.5,wipeStyle=WEDGE )";   
	this.filter += "progid:DXImageTransform.Microsoft.RandomBars ( duration=0.5,orientation=horizontal )";   
	this.filter += "progid:DXImageTransform.Microsoft.RandomBars ( duration=0.5,orientation=vertical )";   
	this.filter += "progid:DXImageTransform.Microsoft.RandomDissolve ()";   
	this.filter += "progid:DXImageTransform.Microsoft.Spiral ( duration=0.5,gridSizeX=16,gridSizeY=16 )";   
	this.filter += "progid:DXImageTransform.Microsoft.Stretch ( duration=0.5,stretchStyle=PUSH )";   
	this.filter += "progid:DXImageTransform.Microsoft.Strips ( duration=0.5,motion=rightdown )";   
	this.filter += "progid:DXImageTransform.Microsoft.Wheel ( duration=0.5,spokes=8 )";   
	this.filter += "progid:DXImageTransform.Microsoft.Zigzag ( duration=0.5,gridSizeX=4,gridSizeY=40 ); width: 100%; height: 100%";   

 }
