﻿// JScript 文件
function G(obj)
{
	return typeof(obj)=='object'?obj:document.getElementById(obj);
}
Array.prototype.addBefore=function(arr)
{
	if(this==[]) return arr;
	else return arr.concat(this);
}
Array.prototype.addAfter=function(arr)
{
	if(this==[]) return arr;
	else return this.concat(arr);
}
Array.prototype.indexOf=function(substr,start){
	var ta,rt,d='\0';
	if(start!=null){ta=this.slice(start);rt=start;}else{ta=this;rt=0;}
	var str=d+ta.join(d)+d,t=str.indexOf(d+substr+d);
	if(t==-1)return -1;rt+=str.slice(0,t).replace(/[^\0]/g,'').length;
	return rt;
}
//自动转行函数, encStr为实体转换后的字符串,如abc&#34;:ds&#39;&#60;!!!!!?&#62;&#47;&#92;@#$%
function insertWbr(encStr,len)
{
	var ta=document.createElement('textarea');
	ta.innerHTML=encStr;
	document.body.appendChild(ta);

	var rsStr=ta.value;	rsStr=rsStr.replace(new RegExp("(\\S{" + len + "})", "img"), "$1<wbr>");
	document.body.removeChild(ta);
	rsStr=rsStr.replace(/<wbr>/g,"\u0001");
	rsStr=rsStr.replace(/</g,"&lt;");
	rsStr=rsStr.replace(/>/g,"&gt;");
	rsStr=rsStr.replace(/\u0001/g,"<wbr>");
	
	return rsStr;
}
function pageLoad(spaceURL){
	var imgID=window.location.hash;
	//alert(imgID);

	if(imgID.replace(/#/g,"").length>1){
		if(imgID.indexOf("#IMG=")>-1){
			//异步图片进入
			imgID=imgID.replace('#IMG=','');
			var baseURL='http://space.hengchu.cn/'+imgID+'.html';
			window.location=baseURL;
		}
	}
	
}

function autoSize(obj,isAlb)
{
	MAX_WIDTH=95;
	MAX_HEIGHT=95;
	IMG_MARGIN=7;
	
	var tImg=new Image();
	tImg.onload=function(){
		var w=this.width;
		var h=this.height;
		if(w<MAX_WIDTH && h<MAX_HEIGHT){
				obj.width=w;
                obj.height=h;
				obj.style.marginLeft=IMG_MARGIN+(MAX_WIDTH-w)/2+'px';
				obj.style.marginTop=IMG_MARGIN+(MAX_HEIGHT-h)/2+'px';
				//obj.hspace=(MAX_WIDTH-w)/2; obj.vspace=(MAX_HEIGHT-h)/2;
				return;
			}
			if(w>h) {
				obj.width=MAX_WIDTH;obj.height=MAX_WIDTH*h/w; 
			}
			else{
				obj.height=MAX_HEIGHT;obj.width=MAX_HEIGHT*w/h;   
				//obj.width+=2; 
			}
	    if(isAlb){
            if(document.all){ obj.style.marginLeft=IMG_MARGIN+(MAX_WIDTH-obj.width)/2+6+'px';
				obj.width=obj.width-4;
            }
            else obj.style.marginLeft=IMG_MARGIN+(MAX_WIDTH-obj.width)/2+3+'px';
        }
        else   obj.style.marginLeft=IMG_MARGIN+(MAX_WIDTH-obj.width)/2+'px';
		obj.style.marginTop=IMG_MARGIN+(MAX_HEIGHT-obj.height)/2+'px';
		//obj.hspace=(MAX_WIDTH-obj.width)/2; obj.vspace=(MAX_HEIGHT-obj.height)/2;
	}
	tImg.src=obj.src;
}

function autoSizeBig(objW,objH)
{
	
	MAX_WIDTH=560;
	MAX_HEIGHT=420;
	var W=objW;
	var H=objH;
	if(W<MAX_WIDTH && H<MAX_HEIGHT){
		return {w:W,h:H};
	}
	if(W/H>MAX_WIDTH/MAX_HEIGHT)
	{
		return {w:MAX_WIDTH,h:MAX_WIDTH*H/W}
	}else{
		return {w:MAX_HEIGHT*W/H,h:MAX_HEIGHT} 
	}
}



ScrollImg=function(coverObj,imgNodeBox,bigImgObj,spaceName,uid){
	_me=this;
	this.imgNodeBox=G(imgNodeBox);
	this.bigImgObj=G(bigImgObj);
	this.isContinue=false;
	this.imgIDList=[];
	this.pid=null;
	this.obj=G(coverObj);
	this.oneStep=95+7+12+1+2;	//
	this.allSkip=0;				//
	this.goNum=1;				//
	this.skip=9;				//
	this.skipTime=25;			//
	this.skipSum=0;
	this.direction=1;
	this.selNode=null;
	this.getNode=null;
	this.addLen=0;
	this.bnt1=null;
	this.bnt2=null;
	this.disBnt=0;
	this.lastIsEnd=0;			//加载的图片中最后一张是否是"所有图片中最后一张图片"
	this.lastIsAlbum=0;			//加载的最后一张图片是否是相册封面
	this.spName=spaceName;
	this.nowPicSn=0;
	this.allPicNum=0;
	this.isGoing=false;
	this.nowBigPic=null;
	this.uid=uid;
	
	//alert(this.uid);
	var isLoading=false;
	
	this.clear=function(){
		this.skipSum=0;
		this.allSkip=0;
		this.goNum=1;
		this.isGoing=false;
	}
	//单步滚动,滚动一个图片的距离后停止
	this.scroll=function(){
		if(_me.checkStat()) {
			//向前到本相册头, stop.  向后到 所有 相册尾||下一相册, stop.
			if(this.imgIDList[0].picSn<=1 && this.direction==-1){
				this.clear(); this.disableBnt(this.bnt1,3); this.disableBnt(-1);
				return false;
			}
			if((this.lastIsEnd || this.lastIsAlbum) && this.direction==1){
				this.clear();this.disableBnt(1); 
				return false;
			}
			if(isLoading) return;
			isLoading=true;
			_me.loadImg();
			return false;
		}
		if(this.disBnt) this.enableBnt(this.disBnt);
		_me.allSkip=_me.oneStep*_me.goNum;
		if(_me.skipSum<_me.allSkip){
			var nowLeft=parseInt(_me.obj.style.left);
			_me.obj.style.left=(nowLeft-_me.skip*_me.direction)+'px';
			_me.skipSum+=_me.skip;
			setTimeout('_me.scroll()',_me.skipTime);
		}else{
			_me.clear();
			_me.scrollEnd();
		}
	}
	this.scrollEnd=function(){}
	this.setCnt=function(arg){
		this.isContinue=arg;
	}
	this.go=function(dirc){
		this.isGoing=true;
		this.goNum=1;
		this.direction=dirc;
		this.scroll();
		this.canGoon(this.goNum*this.oneStep/this.skip*this.skipTime);
		this.scrollEnd=function()
		{
			if(this.isContinue) this.scroll();
		}
	}
	this.sel=function(obj){
		var node=obj;
		this.selScroll(node);
	}
	this.goAsel=function(direc){
		var imgs=this.obj.getElementsByTagName('img');
		var iLen=imgs.length;
		var node=null;
		for(var i=0;i<iLen;i++)
		{
			if(this.selNode==imgs[i])
			{
				if(direc==-1)if(i-1>=0) node=imgs[i-1]; else node=imgs[0];
				else if(direc==1) if(i+1<=iLen-1) node=imgs[i+1]; else node=imgs[iLen-1];
				break;
			}
		}
		this.selScroll(node);
	}
	this.selScroll=function(selNode)
	{
		var node=selNode;
		var imgs=this.obj.getElementsByTagName('img');
		var iLen=imgs.length;
		var nodeNum=-1;
		for(var i=0;i<iLen;i++)
		{
			if(imgs[i]==node) {nodeNum=i; imgs[i].parentNode.className='selImg';}
			else if(imgs[i].parentNode.className!='albumImg') imgs[i].parentNode.className='spanImg';
		}		
		//选中相册封面
        if(nodeNum==this.imgIDList.length-1 && this.lastIsAlbum){_me.gotoUrl(nodeNum); return;}
		if(this.selNode==node) return;  //同一个图片被选中
		this.selNode=node;

        var offsetStep=nodeNum-parseInt(Math.abs(parseInt(this.obj.style.left))/this.oneStep)-2;
		this.goNum=Math.abs(offsetStep);
		this.direction=offsetStep/this.goNum;
		
        this.scroll();
		this.canGoon(this.goNum*this.oneStep/this.skip*this.skipTime);
			
		var isrc=_me.selNode.id;
		var ibigSrc=_me.selNode.id;
		
		var zimg=new Image();
		zimg.src=isrc.replace('S_','L_');
		var imgObj=autoSizeBig(zimg.width, zimg.height);
		this.pid=this.imgIDList[nodeNum].pid;
			
//	alert(isrc);
		
		var _iDiv = G("__imgDiv__");
		if(!_iDiv) _iDiv=document.createElement('DIV');
		_iDiv.id="__imgDiv__";
		_iDiv.style.display = 'none';
		document.body.appendChild(_iDiv);
		if(!G("imgDiv_"+_me.selNode.id))
		{
			var i="<a href='"+ibigSrc+"' target='_blank' title='点击可查看原图'><img id='showBigImg' width='"+imgObj.w+"'  height='"+imgObj.h+"' style='width:"+imgObj.w+"px; height:"+imgObj.h+"px;'  border=0 src='"+isrc.replace('S_','L_')+"'  ></a>";
			_me.bigImgObj.innerHTML	=i;

        	setTimeout(function(){
        		var _div = G("__img__");
        		if(!_div){
           			var _div = document.createElement('DIV');
            		_div.id = "__img__";
            		_div.style.display = 'none';
            		document.body.appendChild(_div);
        		}
        		//alert(ibigSrc.replace('S_','L_') +  '第一测试点');
				_iDiv.innerHTML+='<div id="imgDiv_'+_me.selNode.id+'"></div>';  //缓存对象（仅作标志记录）;
       			_div.innerHTML = '<img src="' + ibigSrc.replace('S_','L_') + '" onload="setTimeout(function(){ _me.gotoUrl(\'' + nodeNum + '\');}, 10)">';
        	}, this.skipTime-6);
		
		}else{
		
		  // alert(ibigSrc.replace('S_','L_'));  //付值
		  
		    
		    
		    var ibigSrc=isrc.replace('S_','L_')
		    
		    //alert(ibigSrc +  '第二测试点');
			var i="<a href='"+ibigSrc+"' target='_blank' title='点击可查看原图'><img id='showBigImg'  width='"+imgObj.w+"'  height='"+imgObj.h+"' style='width:"+imgObj.w+"px; height:"+imgObj.h+"px;'  border=0 src='"+ibigSrc+"' ></a>";
			_me.bigImgObj.innerHTML	=i;
			_me.gotoUrl(nodeNum);
		}
		this.showInfo();
		
	
	}

	
	this.checkStat=function(){
		var objLeft=parseInt(this.obj.style.left);
		var minLeft=this.minLeft();
		var imgs=this.obj.getElementsByTagName('img');
		if(this.direction==-1 && objLeft+1>0) {this.getNode=imgs[0]; return true;}
		if(this.direction==1 && objLeft-1<minLeft) {this.getNode=imgs[imgs.length-1]; return true;}
		//这里的1为故意增加的余量,防止判断过度	
		return false;
	}
	this.minLeft=function(){
		return this.oneStep*(this.obj.getElementsByTagName('img').length-5)*(-1);
	}
	this.loadImg=function(){
	
	
	
		var baseUrl='http://space.hengchu.cn'+this.getNode.pid+'.html/';
		var jsUrl="";
		if(this.direction==-1) jsUrl=baseUrl+'pre?t='+Math.random();
		if(this.direction==1) jsUrl=baseUrl+'next?t='+Math.random();
		
		var myJsld=new JsLoader();
		myJsld.onsuccess=function(){
				if(picListErrorNum) {showError(picListErrorNum); G('albSpan').innerHTML=""; return;};
				if(isNext)
				{
					_me.lastIsEnd=lastIsEnd;
					_me.lastIsAlbum=lastIsAlbum;
				}
				var imglist=loadImgList; 
				_me.addData(imglist);
			}
		myJsld.onfailure=function(){alert('error!');}
		myJsld.load(jsUrl);
	}
	
	this.addData=function(imgList){
		this.addLen=imgList.length;
		if(this.direction==1) this.imgIDList=this.imgIDList.addAfter(imgList);
		else this.imgIDList=this.imgIDList.addBefore(imgList);	
		this.initNode(this.selNode.id);
	}
	this.initNode=function(selImgID){
	
	
		while(this.imgNodeBox.firstChild)
			this.imgNodeBox.removeChild(this.imgNodeBox.firstChild);

		var iLen=this.imgIDList.length;
		for(var i=0;i<iLen;i++){
			var nNode=this.imgIDList[i];
			var tempNode=null;
			if(i==iLen-1)tempNode=this.createNode(nNode.imgID,this.lastIsAlbum);
			else tempNode=this.createNode(nNode.imgID,0);			
			this.imgNodeBox.appendChild(tempNode);
		}
		//G('bigImgBox').innerHTML="<a href='"+selImgID+"' target='_blank' title='点击可查看原图'><img id='showBigImg'  border=0 src='"+selImgID.replace('S_','L_')+"' ></a>";
		this.init(selImgID||this.selNode.id);
		
		
		this.addLen=0;
	}
	this.init=function(imgID){
		var imgid=imgID||this.selNode.id;
		var imgs=this.obj.getElementsByTagName('img');
		var iLen=imgs.length;
		for(var i=0;i<iLen;i++)
		{
			if(imgs[i].id==imgid) {
				imgs[i].parentNode.className='selImg'; this.selNode=imgs[i];
				
			
				
				if(!isLoading) 
				{
					if(i>2 && iLen-i>2)  this.obj.style.left=(-1)*(i-2)*this.oneStep+'px';
					if(i<=2) this.obj.style.left="0px";
					else if(iLen-i<=2 && iLen-5>0) this.obj.style.left=(-1)*this.oneStep*(iLen-5)+'px';
				}
				var objLeft=parseInt(this.obj.style.left);	
				var minLeft=this.minLeft();		
				if(objLeft+1>0 && this.imgIDList[0].picSn==1){ this.disableBnt(-1);}
				else if(objLeft-1<minLeft && (this.lastIsEnd ||this.lastIsAlbum)){this.disableBnt(1);}
			}
			else if(imgs[i].parentNode.className!='albumImg') imgs[i].parentNode.className='spanImg';
			imgs[i].onclick=function(evt){ if(_me.isGoing) return; else _me.sel(this);}
			imgs[i].parentNode.onclick=function(){if(_me.isGoing) return; else _me.sel(this.getElementsByTagName('img')[0]);}
		}
		
		if(this.addLen) if(this.direction==-1) this.obj.style.left=(-1)*this.addLen*this.oneStep+'px';
		else if(this.direction==1) this.obj.style.left=(-1)*(this.imgIDList.length-this.addLen-5)*this.oneStep+'px';
		
		if(isLoading) { isLoading=false; this.scroll();}
	}
	this.createNode=function(imgID,isAlbum){
	   
		var rNode=document.createElement('td');
		rNode.width='114';
		rNode.height='114';
		var nImgsrc='http://space.hengchu.cn'+imgID;
		var nImg='';
		var albumURL='http://space.hengchu.cn'+imgID+'.html';
		if(isAlbum) {nImg="<DIV class='albumImg'><div class='nextAlbum'><br><br><br>下个相册</div><IMG id='"+imgID+"'  width='95' height='95' src="+nImgsrc.replace('S_','L_')+" ></DIV>";}
		else {nImg="<DIV class='spanImg'><IMG id='"+imgID+"' width='95' height='95' src="+nImgsrc.replace('S_','L_')+" ></DIV>";}
		
		rNode.innerHTML=nImg;		
		return rNode;
	}
	this.gotoUrl=function(nodenum){
		
	
	//alert(nodenum);
		if(nodenum==this.imgIDList.length-1 && this.lastIsAlbum)
		{
			window.location='http://space.hengchu.cn/album/photo.aspx?id='+ this.uid +'&pid='+ this.imgIDList[nodenum].pid ;
		}else{
			window.location.hash ='id='+this.uid+'&pid='+ this.imgIDList[nodenum].pid;
	
				
			G('oper').innerHTML=null;
			G('oper').innerHTML=unescape(this.imgIDList[nodenum].oper);
			G('oper1').innerHTML=unescape(this.imgIDList[nodenum].oper1);
			
			var isrc='http://space.hengchu.cn'+_me.selNode.id;
			//try{G('showBigImg').src=isrc.replace('S_','L_');} catch(e){}
			//this.getImg();
		}	
		
	}


	
		//获取图片信息
	this.showInfo=function(){
		
		//var baseURL='http://space.hengchu.cn'+this.selNode.id+'.html/info?t='+Math.random();
	
			
		var ta=document.createElement('textarea');
				ta.innerHTML	="空间相册浏览";
				document.body.appendChild(ta);
				document.title	=ta.innerHTML+"_"+_me.spName;
				document.body.removeChild(ta);
				
				//alert(_me.pid);
				loadComment(_me.pid);	//加载评论
		        G('pid12').value=_me.pid;
                    
	}
	
	this.bntPicName=function(Isrc)
	{
		var srcArr=Isrc.split('/');
		return srcArr[srcArr.length-1];
	}
	this.setBnt=function(bnt1,bnt2)
	{
		this.bnt1=G(bnt1);
		this.bnt2=G(bnt2);
		if(this.bnt1 && this.bnt2) this.bandBnt();
	}
	this.bandBnt=function(){
		
		/*
		this.bnt1.onmouseover	=function(){_me.bnt1.src =_me.bnt1.src.replace('/Templates/space1/images/album/right_pre_18.jpg','/Templates/space1/images/album/right_pre_18.jpg');}
		this.bnt1.onmouseout	=function(){ _me.bnt1.src =_me.bnt1.src.replace('/Templates/space1/images/album/right_pre_18.jpg','/Templates/space1/images/album/right_pre_18.jpg');}
		this.bnt2.onmouseover	=function(){_me.bnt2.src =_me.bnt2.src.replace('/Templates/space1/images/album/left_next_18.jpg','/Templates/space1/images/album/left_next_18.jpg');}
		this.bnt2.onmouseout	=function(){ _me.bnt2.src =_me.bnt2.src.replace('/Templates/space1/images/album/left_next_18.jpg','/Templates/space1/images/album/left_next_18.jpg');}
		*/
	    this.bnt1.onclick	=function(){if(_me.isGoing) return; _me.go(-1);   return false;}
		//this.bnt1.ondragstart	=function(){return false;}
		this.bnt1.onmouseup		=function(){_me.setCnt(0);return false;}
		this.bnt2.onclick	=function(){if(_me.isGoing) return;  _me.go(1);  return false;}
		//this.bnt2.ondragstart	=function(){return false;}
		this.bnt2.onmouseup		=function(){_me.setCnt(0); return false;}
		
		//document.onmouseup=function(){_me.setCnt(0); return false;}
		
		
		
		/*
		单击是好的测试时注的
		 this.bnt1.onmouseover	=function(){if(_me.isGoing) return; _me.go(-1); _me.setCnt(1);  return false;}
		 this.bnt1.onmouseout	=function(){_me.setCnt(0);return false;}
		  
		 this.bnt2.onmouseover	=function(){if(_me.isGoing) return;  _me.go(1); _me.setCnt(1); return false;}
		 this.bnt2.onmouseout	=function(){_me.setCnt(0);return false;}
		 */
		 
	}
	this.enableBnt=function(dBnt){
		switch(dBnt){
			//case -1:this.bnt1.src=this.bnt1.src.replace('/Templates/space1/images/album/right_pre_18.jpg','/Templates/space1/images/album/right_pre_18.jpg');  this.bnt1.title=''; this.bnt1.style.cursor='auto'; break;
			//case 1 :this.bnt2.src=this.bnt2.src.replace('/Templates/space1/images/album/left_next_18.jpg','/Templates/space1/images/album/left_next_18.jpg');  this.bnt2.title=''; this.bnt2.style.cursor='auto'; break;
		}
	}
	
	this.disableBnt=function(direc){
		//direc, 哪个按钮, -1 向前, 1向后
		if(!direc) return;
		if(!this.bnt1 || !this.bnt2) return;
		this.disBnt=direc;
		switch(direc)
		{
			case -1:this.bnt1.src='/Templates/space1/images/album/right_pre_18.jpg';  
					this.bnt1.title='已到达第一张照片'; this.bnt1.style.cursor='default'; break;
			case 1:this.bnt2.src='/Templates/space1/images/album/left_next_18.jpg';  
					this.bnt2.title='已到达最后一张照片'; this.bnt2.style.cursor='default';  break;
		}
	}
	this.canGoon=function(t)
	{
		this.isGoing=true;
		setTimeout("_me.isGoing=false;",t);
	}
	
	var keypress=false;
	
	
	
}


function loadComment(id)
{

       go('1',id);
  
}
    function gopinglun(index,id)
    {
        var d = new Date();
        var post="keyword="+id+"&type=1&currentindex="+index+"&pagenum=15&guid=" + d.getTime();
        Request.sendPOST("/album/photoListlve.ashx",post,recSearch1,null,null);
    }   
        
        
            
    function recSearch1(req,data)
    {
        var retext=req.responseText;
        
       // alert(retext);
        
        if(!retext || retext=="")
         {
           // return showDialog("没有你要查的数据！");
           $("list1").innerHTML="";
            return;
         }
           $("list1").innerHTML=retext;          
          
     }
     
     
     
     
     
     
     //图片按比例缩放
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth = 560; //定义允许图片宽度，当宽度大于这个值时等比例缩小
var iheight = 373; //定义允许图片高度，当宽度大于这个值时等比例缩小
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){ 
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width; 
ImgD.height=image.height;
}

ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){ 
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height; 
}else{
ImgD.width=image.width; 
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
} 
//调用：<img src="图片" onload="javascript:DrawImage(this)">
