SlideShow = RequestBase.extend({
	initialize: function() {
		//播放器的id
		this.parent();
		if($("albumContent")){
			//$("albumContent").innerHTML='<div id="mySlideshow" style="background-position:center center;background-repeat:no-repeat;"></div>';
			var html='<div id="slideContainer">';
  			html+='<div id="middleContainer">';
      		html+='<div id="mySlideshow">';
	  		//html+='<img src="../photos/p_square/0a397189fff9589e73f897862937615b.gif">';
      		html+='</div>';
  			html+='</div>';
			html+='</div>';
			$("albumContent").innerHTML=html;
		}
		this.id="mySlideshow";
		this.element=$(this.id);
		var width=this.element.parentNode.parentNode.parentNode.offsetWidth;
		this.element.style.margin="0px auto";
		$("slideContainer").style.width=width-22+"px";
		$("slideContainer").style.height=width-22+"px";
		this.element.style.width=width-22+"px";
		//this.element.style.height=width-20+"px";
		//
		this.createUI();
		if(width>=400){
			this.previewMethod="medium";
			width=500;
		}	
		else{
			this.previewMethod="small";
			width=250;
		}	
		/*else{
			this.previewMethod="thumbnail";		
			width=150;
		}*/
		//设置层的高度
		
		
		this.currentCategory="new";
		
		//创建一个点击层
		/*
		var div=document.createElement('div');
		with(div.style){
			position="absolute";
			left=parseInt(this.element.offsetWidth/4)+"px";
			top=parseInt(this.element.offsetHeight/4)+"px";
			width=parseInt(this.element.offsetWidth/2)+"px";
			height=parseInt(this.element.offsetHeight/2)+"px";
			//cursor="pointer";	
		}
		if(window.ie)
			cursor="hand";
		else
			cursor="pointer";
		div.style.cursor=cursor;		
		this.element.appendChild(div);
		//绑定事件
		div.onclick=this.goTo.bind(this);
		*/
        if(navigator.appVersion.indexOf("MSIE 7") > 0){
            var cont = $("middleContainer");
            if(cont){
                cont.style['position']='absolute';
            }
        }
		this._post("bloghome.photo.getSlideShow",this.onGetData.bind(this),{user_id:window.user_id,cat_id:this.currentCategory,preview:this.previewMethod});
		//数据
		this.slideShowData=new Array();
		
		//防止连续点击相册列表
		this.loadingAlbums=false								
		//延迟
		this.slideShowDelay=3000;
		this.currentIndex = 0;
		//尝试加载图片的次数
		this.tryTimes=0;
		this.currentImgPreloader={};
		this.element.effect = new Fx.Style(
				this.element,
				'opacity',
				{duration: 1000}
			);
		//this.doSlideShow();
	},
	//创建控制按钮 
	createUI:function(){
		var div=document.createElement('div');
		div.id="slidePanel";
		if(typeof(ie)!="undefined")
			div.style.height="1%";
		var html=' <a title="相册列表" style="float: right; padding-bottom: 1px;" href="javascript:;"><img src="/imgs/blog/player/bt_playlist.gif" id="showa"/></a>';
    	html+='<a title="播放/暂停"  href="javascript:;"><img src="/imgs/blog/player/bt_pause.gif" id="playi"/></a> ';
    	html+='<a title="停止"  href="javascript:;"><img src="/imgs/blog/player/bt_stop.gif" id="stopi"/></a> ';
    	html+='<a title="上一张"  href="javascript:;"><img src="/imgs/blog/player/bt_pre.gif" id="previ"/></a> ';
    	html+='<a title="下一张"  href="javascript:;"><img src="/imgs/blog/player/bt_next.gif" id="nexti"/></a> ';
    	html+='<a title="播放全部照片"  style="margin-left: 20px;" href="javascript:;"><img src="/imgs/blog/player/bt_playall.gif" id="playa"/></a> ';
    	
    	div.innerHTML=html;
    	
    	$("albumContent").appendChild(div);
    	this.controlElement=div;
		//绑定事件
		$("playi").onclick=this.play.bindAsEventListener(this);
		$('stopi').onclick=this.stop.bindAsEventListener(this);
		$('previ').onclick=this.prev.bindAsEventListener(this);
		$('nexti').onclick=this.next.bindAsEventListener(this);
		$('playa').onclick=this.playAll.bindAsEventListener(this);
		$('showa').onclick=this.showAlbums.bindAsEventListener(this);
	
	},
	//跳转到照片url
	goTo:function(){
		if(this.currentImg){
			document.location=this.currentImg.url;
		}
	
	},
	//播放,暂停
	play:function(){
		//修改图片
		var img= $("playi").src;
		$("playi").src=img.indexOf('pause')>0?'/imgs/blog/player/bt_play.gif':'/imgs/blog/player/bt_pause.gif'; 
		this.playing=!this.playing;
		
		if(this.playing)
			this.startSlideShow();
		else
			clearTimeout(this.timer);	
	
	},
	//停止
	stop:function(){
		$("playi").src='/imgs/blog/player/bt_play.gif';
		//初始化图片
		this.currentIndex=0;
		this.playing=false;
		this.element.setStyle('opacity',1);
		//修改背景为第一幅照片
		if(this.slideShowData&&this.slideShowData[this.currentCategory]&&this.slideShowData[this.currentCategory][this.currentIndex]){
			this.currentImg=this.slideShowData[this.currentCategory][this.currentIndex];
			var html='<a href="'+this.currentImg.url+'" target="_blank"><img src="'+this.currentImg.img+'"></a>';
			this.element.innerHTML=html;
		}	
		//清除定时器	
		if(this.timer)
			clearTimeout(this.timer);
	
	},
	//前一张照片
	prev:function(){
		if(this.playing){
			if(this.timer)
				clearTimeout(this.timer);
			this.playing=false;
			$("playi").src="/imgs/blog/player/bt_play.gif";
		}
		if(this.currentIndex==0)
			return;
		this.currentIndex--;
		//this.element.style.backgroundImage="url("+this.slideShowData[this.currentCategory][this.currentIndex].img+")";	
		this.currentImg=this.slideShowData[this.currentCategory][this.currentIndex];
		var html='<a href="'+this.currentImg.url+'" target="_blank"><img src="'+this.currentImg.img+'"></a>';
		this.element.innerHTML=html;
	
	},
	//下一张照片
	next:function(){
		if(this.playing){
			if(this.timer)
				clearTimeout(this.timer);
			this.playing=false;
			$("playi").src="/imgs/blog/player/bt_play.gif";
		}
		if(this.currentIndex==this.maxIndex-1)
			return;
		this.currentIndex++;
		//this.element.style.backgroundImage="url("+this.slideShowData[this.currentCategory][this.currentIndex].img+")";		
		this.currentImg=this.slideShowData[this.currentCategory][this.currentIndex];
		var html='<a href="'+this.currentImg.url+'" target="_blank"><img src="'+this.currentImg.img+'"></a>';
		this.element.innerHTML=html;
	},
	//播放所有照片
	playAll:function(){
		this.currentCategory="all";
		$("playi").src='/imgs/blog/player/bt_pause.gif';
		
		if(this.timer)
			clearTimeout(this.timer);
			
		if(this.slideShowData[this.currentCategory]){//如果有数据
			this.currentIndex=0;
			this.maxIndex=this.slideShowData[this.currentCategory].length;
			this.startSlideShow();
		}else{//加载数据
			this._post("bloghome.photo.getSlideShow",this.onGetData.bind(this),{user_id:window.user_id,cat_id:this.currentCategory,preview:this.previewMethod});
		}
	},
	//显示相册列表
	showAlbums:function(){
	//防止连续点击，出现多个相册列表
		if(this.loadingAlbums)
			return;
		if(!this.albumsElement){
			this.loadingAlbums=true;
			this._post("bloghome.photo.getAlbums",this.onGetAlbums.bind(this),{user_id:window.user_id,page:1,perpage:10});
		}	
		else if(this.albumsElement.style.display==""){
			this.albumsElement.style.display="none";
		}else{
			this.albumsElement.style.display="";
		}	
	},
	destroySlideShow: function(element) {
		var myClassName = element.className;
		var newElement = document.createElement('div');
		newElement.className = myClassName;
		element.parentNode.replaceChild(newElement, element);
	},
	//开始播放
	startSlideShow: function() {
		
		this.playing=true;
		this.slideShowInit = 0;
		//没有数据
		if(this.maxIndex==0)
			return;
		this.timer=setTimeout(this.nextSlideShow.bind(this),0);
	},
	nextSlideShow: function() {
		if(!this.currentImgPreloader.complete&&this.tryTimes<100){
		   this.tryTimes++;	
		   this.timer=setTimeout(this.nextSlideShow.bind(this),10);
		   return;	
		}
		this.tryTimes=0;
		this.currentImgPreloader=null;
		this.lastIndex = this.currentIndex;
		this.currentIndex++;
		if (this.currentIndex >= this.maxIndex)
		{
			this.currentIndex = 0;
			this.lastIndex = this.maxIndex - 1;
		}
		this.slideShowInit = 0;
		this.doSlideShow.bind(this)();
	},
	doSlideShow: function() {
		if (this.slideShowInit == 1)
		{
			imgPreloader = new Image();
			// once image is preloaded, start slideshow
			imgPreloader.onload=function(){
				setTimeout(this.startSlideShow.bind(this),10);
			}.bind(this);
			imgPreloader.src = this.slideShowData[0][0];
		} else {
			if (this.currentIndex != 0) {
				this.element.setStyle('opacity',0);
				this.element.effect.custom(0,1);
				this.currentImg=this.slideShowData[this.currentCategory][this.currentIndex];				
				//this.element.style.backgroundImage="url("+this.currentImg.img+")";
				var html='<a href="'+this.currentImg.url+'" target="_blank"><img src="'+this.currentImg.img+'"></a>';
				this.element.innerHTML=html;
				
			} else {
				//this.element.setStyle('opacity',1);
				this.element.setStyle('opacity',0);
				this.element.effect.custom(0,1);
				this.currentImg=this.slideShowData[this.currentCategory][this.currentIndex];
				//this.element.style.backgroundImage="url("+this.currentImg.img+")";
				var html='<a href="'+this.currentImg.url+'" target="_blank"><img src="'+this.currentImg.img+'"></a>';
				this.element.innerHTML=html;
			}
			var nextIndex=this.getNextSlideIndex();
			var imgUrl=this.slideShowData[this.currentCategory][nextIndex].img;
			imgPreloader = new Image();
			// once image is preloaded, start slideshow
			/*imgPreloader.onload=function(){
				this.imgTimer=setTimeout(this.checkImgLoader.bind(this),10);
			}.bind(this);*/
			imgPreloader.src = imgUrl;
			this.currentImgPreloader=imgPreloader;
			this.timer=setTimeout(this.nextSlideShow.bind(this),this.slideShowDelay);
		}
	},
	checkImgLoader:function(){
	
	
	},
	/**
	 * 得到下一个图片的索引
	 */
	getNextSlideIndex:function(){
		var index=this.currentIndex+1;
		if (index >= this.maxIndex)
		{
			index = 0;
		}
		return index;
	}, 
	/**
	 * 返回数据
	 */
	onGetData:function(xmlhttp){
		if(this.timer)
			clearTimeout(this.timer);
		this.slideShowData[this.currentCategory]=this.parseData(xmlhttp);
		this.currentIndex = 0;
		this.maxIndex=this.slideShowData[this.currentCategory].length;
		this.startSlideShow();
	},
	
	//返回相册数据
	onGetAlbums:function(xmlhttp){
		this.albums=this.parseData(xmlhttp);
		if(this.albums.length>0){
			var html='<div style="height:120px;overflow:auto" id="albumsDiv">';
			for(var i=0;i<this.albums.length;i++){
				var album=this.albums[i];
				html+='<div class="slidealbum" onmousemove="this.style.backgroundColor=\'#ffd\'" onmouseout="this.style.backgroundColor=\'\'" id="slide_album_'+album.id+'">';
	        	html+='<img src="'+album.img+'">';
	            html+='相册:'+album.name+'<br>'+album.num+'张照片<br>'+album.date;//+album.date;
	     		html+='</div>';
			}
			html+='</div>';
			new Insertion.Before(this.controlElement,html)
			
			this.albumsElement=$("albumsDiv");
			//绑定相册的点击事件
			var elements=$$("#albumContent .slidealbum");
			if(elements){
				elements.forEach(function(el){
					el.onclick=this.showAlbum.bindAsEventListener(this);
				}.bind(this));
			}
		}
		this.loadingAlbums=false;
	
	},
	
	//显示单个相册
	showAlbum:function(e){
		var element=Event.element(e);
		
		while(!element.tagName||element.tagName.toLowerCase()!='div'){
			element=element.parentNode;
		}
		var id=element.id;
		id=id.substring(12);
		this.currentCategory=id;
		if(!this.slideShowData[this.currentCategory]){
			this._post("bloghome.photo.getSlideShow",this.onGetData.bind(this),{user_id:window.user_id,cat_id:this.currentCategory,preview:this.previewMethod});
		}else{
			this.stop();
			this.currentIndex=0;
			this.maxIndex=this.slideShowData[this.currentCategory].length;
			this.startSlideShow();
		
		}
	
	},
	parseData:function(xmlhttp){
		var dataNode=xmlhttp.responseXML.getElementsByTagName("data");
		var nodeName=dataNode[0].getAttribute("name");;

		var data=[];
		var nodes=xmlhttp.responseXML.getElementsByTagName(nodeName);
		
		if(nodes){
			var da=[];
			for(var i=0;i<nodes.length;i++){
				var node=nodes[i];
				var cnodes=node.childNodes;
				var d=[];
				for(var j=0;j<cnodes.length;j++){
					var name=cnodes[j].tagName;
					if(cnodes[j].firstChild)
						var value=cnodes[j].firstChild.data;
					else
						var value="";	
					d[name]=value;
					
				}
				da[da.length]=d;
			}
			data[nodeName]=da;
			return data[nodeName];
		}
		return data;
	
	}
});

function initTimedSlideShow(element, data) {
	var slideshow = new SlideShow(element, data);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
