(function(){
var D={
	is_Array:function(obj){return (obj instanceof Array);},
	is_dArray:function(obj){return (obj instanceof dArray);},
	is_file:function(str){var reg=/^(\w|\.|\/|:)+\.\w$/i,bg;return reg.test(str);},
	preload:function(picsArray) {
		pics = new Array();
		for(var i=0;i<picsArray.length;i++){
			pics[i] = new Image();
			pics[i].src = picsArray[i];
		}
	},
	dArray:function(){
		this.elems=new Array();
		this.index=0;
		this.length;
		if(arguments.length==1 && D.is_Array(arguments[0])){
			for(var i=0;i<arguments[0].length;i++)this.elems.push(arguments[0][i]);
			this.length=arguments[0].length;
		}
		else{
			for (var i=0;i<arguments.length;i++){
				this.elems.push(arguments[i]);
			}
			this.length=arguments.length;
		}
	},
	setOpacity:function(obj,lvl){
		if(lvl>=0 && lvl<=100){
			if(obj.filters){
				obj.style.filter='alpha(opacity='+lvl+')';
			}
			else {
				obj.style.opacity=lvl/100;
			}
			return lvl;
		}
		else return null;
	},
	getOpacity:function(obj){
		var op;
		if(obj.filters)op=obj.style.filter.substring(14,obj.style.filter.length-1);
		else op=obj.style.opacity*100;
		return op;
	},
	fadeOut:function(obj,time,callback){
		var lvl=D.getOpacity(obj);
		var interval=setInterval(function(){
			D.setOpacity(obj,lvl--);
			if(lvl==0){
				clearInterval(interval);
				if(typeof(callback)=="function")callback();
			}
		},time/lvl);
	},
	fadeIn:function(obj,time,callback){
		var lvl=getOpacity(obj);
		var interval=setInterval(function(){
			D.setOpacity(obj,lvl++);
			if(lvl==100){
				clearInterval(interval);
				if(typeof(callback)=="function")callback();
			}
		},time/(100-lvl));
	},
	layer:function(opt){ // opt{src=url, layerbg:colorORpic, text:layer_text, textcolor:color, textbg:colorORpic, link:layer_link, zindex:layer_zIndex}
		this.obj=document.createElement('img');
		this.text=document.createElement('p');
		this.link=document.createElement('a');
		this.aux=document.createElement('img');
		this.text.style.position=this.link.style.position=this.aux.style.position="absolute";
		this.obj.style.padding=this.obj.style.margin=this.obj.style.border=this.text.style.padding=this.text.style.margin=this.text.style.border=this.link.style.padding=this.link.style.margin=0;
		this.link.style.width=this.link.style.height=this.text.style.width="100%";
		this.text.style.bottom=0;
		this.text.style.left=0;
//		this.text.style.zIndex=2;
//		this.obj.style.zIndex=1;
//		this.aux.style.zIndex=0;
		this.aux.style.top=this.aux.style.left="110%";
		this.text.style.textAlign=this.link.style.textAlign="center";
		this.link.style.display="block";
		this.link.style.overflow="hidden";
		this.link.appendChild(this.obj);
		this.link.appendChild(this.aux);
		this.link.appendChild(this.text);
		if(opt){
			if(opt.src)this.setSrc(opt.src);
			if(opt.layerbg)this.setLayerBg(opt.layerbg);
			if(opt.text)this.setText(opt.text);
			if(opt.textcolor)this.setTextColor(opt.textcolor);
			if(opt.textbg)this.setTextBg(opt.textbg);
			if(opt.link)this.setLink(opt.link);
			if(opt.zindex)this.setZindex(opt.zindex);
		}
	},
	slideshow:function(id,opt){ // id=slideshow_id(typically a div),opt{timeView:ms, timeEffect:ms, defaultPic:url, layerbg:colorORpic, textbg:colorORpic, textcolor:color}
		this.timeEffect=(opt && opt.timeEffect)?opt.timeEffect:1500;
		this.timeView=(opt && opt.timeView)?opt.timeView:3000;
		this.defaultpic=(opt && opt.defaultPic)?opt.defaultPic:"";
		this.pics=new D.dArray();
		this.running=false;
		var layerOpt={};
		if(opt){
			layerOpt.src=(opt.defaultPic)?opt.defaultPic:null;
			layerOpt.layerbg=(opt.layerbg)?opt.layerbg:null;
			layerOpt.textbg=(opt.textbg)?opt.textbg:null;
			layerOpt.textcolor=(opt.textcolor)?opt.textcolor:null;
		}
		layerOpt.zindex=1;
		this.layer1=new D.layer(layerOpt);
		layerOpt.zindex=0;
		this.layer2=new D.layer(layerOpt);
		var contener=document.createElement('div');
		contener.style.position="relative";
		contener.style.width=contener.style.height="100%";
		contener.style.padding=contener.style.margin="0";
		contener.style.overflow="hidden";
		document.getElementById(id).appendChild(contener);
		this.layer1.setOpacity(100);
		this.layer2.setOpacity(100);
		this.layer1.setSrc(this.defaultpic);
		this.layer2.setSrc(this.defaultpic);
		this.layer1.appendTo(contener);
		this.layer2.appendTo(contener);
	}
};
D.dArray.prototype={
	increase:function(){this.index++;return (this.index < this.length)?this.index:null},
	decrease:function(){this.index--;return (this.index-1 >= 0)?this.index:null},
	current:function(){return this.getPos(this.index)},
	each:function(circle){var elem=this.current();if(this.increase()==null && circle)this.reset();return elem;},
	next:function(circle){
		if(this.increase()!=null)return this.current();
		else if(circle){this.reset();this.current()}
		else return null;
	},
	prev:function(circle){
		if(this.decrease()!=null)return this.current();
		else if(circle){this.end();this.current()}
		else return null;
	},
	reset:function(){this.index=0;},
	end:function(){this.index=this.length-1;},
	pos:function(){return this.index},
	getPos:function(pos){return (pos<this.length)?this.elems[pos]:null;},
	setPos:function(pos){if(pos<this.length && pos>=0)return (this.index=pos); else return null;},
	pop:function(dA){
		if(this && this.length!=0){
			this.length-=1;
			if(this.index==this.length)this.end();
			return this.elems.pop();
		}
		else if(D.is_dArray(dA)){
			dA.length-=1;
			if(dA.index==dA.length)dA.end();
			return dA.elems.pop();
		}
		else return null;
	},
	push:function(){
		for(var i=0;i<arguments.length;i++) this.elems.push(arguments[i]);
		this.length+=arguments.length;
	},
	shift:function(){
		if(this && this.length!=0){
			this.length-=1;
			if(this.index==this.length)this.end();
			return this.elems.shift();
		}
		else if(D.is_dArray(dA) && dA.length!=0){
			dA.length-=1;
			if(dA.index==dA.length)dA.end();
			return dA.elems.shift();
		}
		else return null;
	},
	unshift:function(){
		for(var i=0;i<arguments.length;i++) this.elems.shift(arguments[i]);
		this.length+=arguments.length;
		this.index+=arguments.length;
	},
	concat:function(){
		var temp_dArray=new D.dArray();
		for(var i=0;i<arguments.length;i++){
			if(D.is_Array(arguments[i])){
				for(var j=0;j<arguments[i].length;j++)temp_dArray.push(arguments[i][j]);
			}
			else if(D.isdArray(arguments[i])){
				var pos=arguments[i].pos;
				arguments[i].reset();
				var j;
				while(j=arguments[i].each())temp_dArray.push(j);
				arguments[i].setPos(pos);
			}
		}
		return temp_dArray;
	}
};
D.layer.prototype={
	getSrc:function(){return this.obj.src},
	setSrc:function(url){
		this.aux.src=url;
var size=new Array();
size[0]=this.link.clientWidth;
size[1]=this.link.clientHeight-this.text.clientHeight;
//		var size=[this.link.clientWidth,this.link.clientHeight];
		if(this.aux.width/this.aux.height >= size[0]/size[1]){
			this.obj.style.width=size[0]+"px";
			this.obj.style.height="";
		}
		else {
			this.obj.style.height=size[1]+"px";
			this.obj.style.width="";
		}
		this.obj.src=url
	},
	getLink:function(){return this.link.href},
	setLink:function(url){if(url!="")this.link.href=url; else this.link.removeAttribute("href")},
	getText:function(){return this.text.innerHTML},
	setText:function(text){this.text.innerHTML=text;this.text.style.height=(text=="")?0:""},
	setOpacity:function(lvl){D.setOpacity(this.link,lvl)},
	fadeOut:function(time,callback){D.fadeOut(this.link,time,callback)},
	fadeIn:function(time,callback){D.fadeIn(this.link,time,callback)},
	setZindex:function(z){this.link.style.zIndex=z},
	setTextBg:function(bg){this.text.style.background=(D.is_file(bg))?"url("+bg+")":bg},
	setLayerBg:function(bg){this.link.style.background=(D.is_file(bg))?"url("+bg+")":bg},
	setTextColor:function(color){this.text.style.color=this.link.style.color=color},
	setZindex:function(zindex){this.link.style.zIndex=zindex},
	appendTo:function(node){node.appendChild(this.link)}
};
D.slideshow.prototype={
	addPic:function(opt){
		if(opt){
			D.preload([opt.src]);
			var pic={
				src:(opt.src),
				text:(opt.text)?opt.text:"",
				link:(opt.link)?opt.link:""
			};
			this.pics.push(pic);
		}
	},
	stop:function(){
		this.running=false;
	},
	start:function(){
		this.running=true;
		this.run();
	},
	run:function(){
		if(!this.running)return;
		var cur=this.pics.each(true);
		this.layer2.setText(cur.text);
		this.layer2.setLink(cur.link);
		this.layer2.setSrc(cur.src);
		if(this.running){
			this.layer1.fadeOut(this.timeEffect,function(that){return function(){
				that.layer1.setText(that.layer2.getText());
				that.layer1.setLink(that.layer2.getLink());
				that.layer1.setSrc(that.layer2.getSrc());
				that.layer1.setOpacity(100);
				setTimeout(function(){that.run()},that.timeView);
			}}(this));
		}
	}
}
if(!window.$D){window.$D=D;}
})();
