/*图片处理*/
var ploy={
    chk : "/inc/chk.aspx",
    ajax :"/inc/ajax.aspx"
}
var photoLoad=Class.create();
photoLoad.prototype={
    initialize:function()
    {
        this.op = 80; //移动量
        this.num=10;
        this.locaload = 0;//本次加载
        this.loadnum = 0;//加载列表次数 
        this.loadphotonum=0;//加载总张数
        //this.center = parseInt(this.loadphotonum/2)+1;//中心轴
        this.ajax = "inc/ajax.aspx";
        this.maxwidth = 708;
        this.maxheight= 520;

        this.prenum = 1;//游标 
        this.nextnum = 0;
        this.loca = 0;//当前指向
        this.movepre = 2;//加载图片接入点 
        this.movenext = 8;
        
        this.preload = true;//向上加载 
        this.nextload = true;//向下加载
        this.loadtype;
        this.state = false;//加载状态，为false不允许用户连点 
        
        this.showpic = $("showpic");
        this.showpicopen = $("showpicopen");
        this.photoinfo = $("photoinfo");
        this.attwidow = $("mqq");
        this.scrollwidow = $("mq");
        this.loaddata = $("photolist");
        this.title = $("phototitle");
        this.showphotonum = $("photolistnum");
        this.loadingImg = [];//大图 
        this.loadlistimg = [];//列表图片
        //this.loadlistinfo = [,];
    }
    ,get:function(pid)
    {
        AlertDIV(this.showpic,"<img src=images/loading.gif border=0/><br>正在加载图片，请稍候......");
        this.state = false;
        _sendx("inc/ajax.aspx?t=getpicinfo&pid="+pid,this.databind.bind(this),pid);
	this.bindreply(pid);
    }
    ,load:function(num)
    {
        if((this.loca+num<this.prenum) || ((this.loca+num)>this.nextnum)) 
        {
            if((this.loca+num)<this.prenum) this.loca=this.prenum;
            if((this.loca+num)>this.nextnum) this.loca=this.nextnum-1;
            return;
        }

        var pid = this.loadlistimg[this.loca+num].pid;

        if(this.loadingImg[pid]==null)
        {
            this.get(pid);
        }
        else
        {
            this.imgonload(this,pid);
	    this.bindreply(pid);          
        }
    }
    ,pre:function()//向上 
    {
        if(!this.state) 
        {
            alert("正在加载中，请稍候");
            return;
        }
        this.load(-1);
        this.move(-1);
        if(this.loca <= this.movepre && this.preload)
        {
            this.loadtype = "pre";
            var pid = this.loadlistimg[this.prenum].pid
            this.ListOnLoad(pid,this.loadtype);
        }
    }
    ,next:function()
    {
        if(!this.state){
            alert("正在加载中，请稍候");
            return;
        }
        this.load(1);
        this.move(1);
        
        if(this.loca >= this.movenext && this.nextload)
        {
            this.loadtype = "next";
            var pid = this.loadlistimg[this.nextnum].pid
            this.ListOnLoad(pid,this.loadtype);
        }
    }
    ,reset:function()
    {
        if(!this.state) return;
        this.loca=0;
        this.move();
    }
    ,databind : function(p,v)
    {
        var dataarray = v.split(':|||:');
        var items = new Array();
        for(i=0;i < dataarray.size();i++)
        {
            var itemc = dataarray[i].split(':')
            items[itemc[0]] = itemc[1];
            //alert(itemc[0]+":"+items[itemc[0]]);
        }
        if(typeof(itemc["error"])!="undefined")
        {
            alert(items["error"]);
            return;
        }
        //alert(this.loadingImg[this.loca]);
        if(this.loadingImg[p]==null)
        {
            this.showpic.innerHTML = "<img src='"+items["src"]+"' alt='正在加载...' width='700' />";
            this.title.innerText = "";
            this.loadingImg[p] = new Image();
            this.loadingImg[p].onload = this.imgonload.callWith(this,p);
            this.loadingImg[p].alt = items["title"];
            this.loadingImg[p].poster = items["poster"];
            this.loadingImg[p].nickname = items["nickname"];
            this.loadingImg[p].online = items["online"];
            this.loadingImg[p].browse = items["browse"];
            this.loadingImg[p].reply = items["reply"];
            this.loadingImg[p].uptime = items["uptime"];
            this.loadingImg[p].tag = items["tag"];
            this.loadingImg[p].src = items["src"];
            //alert(this.loadlistinfo[p,"title"]);
        }

    }
    ,imgonload:function(t,p)
    {
        var img = t.loadingImg[p];
        try
        {
        if(img.width > t.maxwidth)
        {
            var percent = Math.round((t.maxwidth/img.width*100));
            img.resized=true;
            img.width=t.maxwidth;
            img.height = Math.round(img.height * percent/100);
            img.style.cursor='hand';
        }}catch(ex){
            
        }

        t.title.innerText = img.alt;
        t.showpic.innerHTML = img.outerHTML;
        t.showpicopen.onclick = function()
        {
            window.open(img.src,'bigpic','');
        };
        t.hidealert();
        t.loadingImg[p].onload = null;
	t.bindpicinfo(p);
    }
    ,bindpicinfo:function(p)
    {
        var htmlstr='<dl>'
          +'  <dt>浏览(<span>'+this.loadingImg[p].browse+'</span>)　评论(<span>'+this.loadingImg[p].reply+'</span>)</dt>'
          +'  <dd>发布人：<a href="http://my.5lin.com/?username='+this.loadingImg[p].poster+'" target="_blank">'+this.loadingImg[p].nickname+'</a><span name="onlineIcon" rel="'+this.loadingImg[p].poster+';'+this.loadingImg[p].nickname+';'+this.loadingImg[p].online+'">　</span></dd>'
          +'  <dd>'
          +'  发布日期：'+this.loadingImg[p].uptime
          +'  </dd>'
          +'  <dd>'
          +'  TAG：'+this.loadingImg[p].tag
          +'  </dd>'
          +'</dl>';
        this.photoinfo.innerHTML = htmlstr;
        new ChatInfoPopUp.DownMenu(this.photoinfo.id);
    }
    ,bindreply:function(p)
    {
        AlertDIV("PL_List","<img src=images/loading.gif border=0/><br>正在加载评论，请稍候......");
        XQ.AU.get("inc/photo_PL_List.aspx","PL_List","pid="+p+"&tmp="+getDataTime());
    }
    ,hidealert:function()
    {
        if($("alertDIV")) $("alertDIV").removeNode(true);
        this.state = true;
    }
    ,move:function(num)
    {
        if(typeof(num)!="undefined") this.loca = this.loca+num;
        var movelong=0;
        if(this.prenum<1)
            movelong = ((-(this.prenum-1))+this.loca)*this.op
        else movelong = this.loca*this.op;
        
        this.attwidow.scrollLeft = movelong;
        this.bindclick();
    }
    ,bindclick:function()
    {
        if(this.loca>=this.prenum)
            this.loadlistimg[this.loca].onclick = this.pre.bind(this);
        if((this.loca+1)<=this.nextnum && (this.loca+1)>=this.prenum)
            this.loadlistimg[this.loca+1].onclick = this.next.bind(this);
    }
    ,ListOnLoad:function(pid,types)//加载列表每次加载最大10张图片 types为空取中间开始 pre前面多少张，next是后面多少张 
    {
        //AlertDIV(this.attwidow,"正在加载列表......");
        //alert("加载列表");
        _sendx("inc/ajax.aspx?t=getpiclist&pid="+pid+"&type="+types,this.dataListbind.bind(this),pid);
    }
    ,dataListbind:function(p,v)
    {
        var dataarray = v.split(':|||:');
        //alert(v);
        var items = new Array();
        for(i=1;i < dataarray.size();i++)
        {
            var itemc = dataarray[i].split(':')
            if(parseInt(itemc[0])==p) this.loca = i;
            var _div = document.createElement("div");
            var _img = document.createElement("img");
            _img.pid = itemc[0];
            _img.src = itemc[1];
            _img.width = 75;
            _img.height = 70;
            _img.style.cursor='hand'
            _div.appendChild(_img);
            if(this.loadtype=="pre")
            {
                this.loadlistimg[this.prenum-i] = _img;
                this.loaddata.insertBefore(_div,this.loaddata.firstChild);
            }
            else
            {
                this.loadlistimg[this.nextnum+i] = _img;
                this.loaddata.appendChild(_div);
            }
        }
        if(this.loadtype=="pre")
        {
            this.prenum -= (dataarray.size()-1);
            this.movepre -= ((this.loadnum-1)*this.num);
        }else
        {
            this.nextnum += (dataarray.size()-1);
            this.movenext += ((this.loadnum-1)*this.num);
        }
        //alert(this.prenum+"|"+this.nextnum+"|"+this.loca+"|"+"ppid:"+this.loadlistimg[this.prenum].pid);
        this.locaload = parseInt(dataarray[0]);
        if(this.locaload<10) this.nextload = false;
        if(this.locaload>0) this.loadnum++;
        this.loadphotonum = this.loadphotonum + this.locaload;
        if(this.locaload==0) 
        {   
            if(this.loadtype=="pre")
                this.preload = false;
            else if(this.loadtype=="next")
                this.nextload = false;
        }
        this.showphotonum.innerText = this.loadphotonum;
        this.move();
    }
    ,wtphotoreply :function(f)//发表评论 
    {
        if(!login.state()) return false;
        var pid = f.pid.value;
        f.postMessage.value = XQ.Editor.getContent();

        var v = f.postMessage.value;
        if(v.len()<15)
        {
            alert("内容过短，无法提交")
            return false;
        }

        f.send.disabled = true;
        AlertDIV(f,"<img src=images/loading.gif border=0/><br>正在提交，请稍候.....");
        var pars = "pid="+pid+"&message="+v;
        _sendx(XQ.ajax+'?t=replyphoto'
		    ,function(p,v)
		    {
                if(v.length<100)
                {
                    if(v=="操作成功！")
                    {
                        photo.bindreply(pid);
                    }
                    else alert(v);
                }
                else
                {
                    alert(v);
                }
                p.reset();
                f.send.disabled = false;
                if($("alertDIV")) $("alertDIV").removeNode(true);
           }
		   ,f
		   ,"POST"
		   ,pars
		);
        return false;
    }
};
//照片信息
function del(pid,xqid){
        var conf=confirm('确定要删除吗？');
			if(!conf)
				return ;
        var pars = "t=delphoto&pid="+pid+"&xqid="+xqid;
          _sendx(ploy.ajax+"?"+pars,function(p,v)
		    {alert(v);
		        if(v.length<100)
                { 
                        if(v=="删除照片成功！")
                    {
                  if(parseInt(pid)>0)
                            location.reload();
                        else
                            init();
                    }
                   else alert(v);
             
               }else alert("数据处理出现异常，请稍候重试！");
		    }
		    ,pid);
		
}
