// Copyright © 1992-2007 华南师范大学《师大青年》版权所有
var fileLoadingImage="images/lightbox/loadingAnimation.gif",fileBottomNavCloseImage="images/lightbox/closelabel.gif",overlayOpacity=0.8,borderSize=10,imageArray=new Array,activeImage;
Array.prototype.removeDuplicates=function(){
  for(i=0;i<this.length;i++){
    for(j=this.length-1;j>i;j--){
      if(this[i][0]==this[j][0]){
        this.splice(j,1);
      }
    }
  }
};
Array.prototype.empty=function(){
  for(i=0;i<=this.length;i++){
    this.shift();
  }
};
var Lightbox={start:function(A){
    showCovering();
    imageArray=[];
    imageNum=0;
    var _=[];
    if(A.getAttribute("rel")!=""){
      _=$("a[href][rel="+A.getAttribute("rel")+"]");
    }
    if(_.length<2){
      imageArray.push(new Array(A.getAttribute("href"),A.getAttribute("title")));
    }else {
      for(var C=0;C<_.length;C++){
        var B=_[C];
        if(B.getAttribute("rel")==A.getAttribute("rel")){
          imageArray.push(new Array(B.getAttribute("href"),B.getAttribute("title")));
        }
      }
      imageArray.removeDuplicates();
      while(imageArray[imageNum][0]!=A.getAttribute("href")){
        imageNum++;
      }
    }
    this.fixLoading();
    this.changeImage(imageNum);
  },fixLoading:function(){
    var _=getViewportSize(),A=getScrollPosition();
    $("#loading").setStyle({left:(A[0]+(_[0]-100)/2)+"px",top:(A[1]+((_[1]-100)/2))+"px"});
  },changeImage:function(A){
    activeImage=A;
    $("lightboxImage").hide();
    $("hoverNav").hide();
    $("prevLink").hide();
    $("nextLink").hide();
    $("imageDataContainer").hide();
    $("numberDisplay").hide();
    $("lightbox").hide();
    $("loading").show();
    imgPreloader=new Image();
    var _=this;
    imgPreloader.onload=function(){
      $("lightboxImage").src=imageArray[activeImage][0];
      _.resizeImageContainer(imgPreloader.width,imgPreloader.height);
      imgPreloader.onload=function(){
      };
    };
    imgPreloader.src=imageArray[activeImage][0];
  },resizeImageContainer:function(E,I){
    var _=getViewportSize(),H=_[0]-borderSize*4;
    if(E>H){
      I=Math.round(I*(H/E));
      E=H;
    }
    var B=(E+(borderSize*2)),A=(I+(borderSize*2));
    $("outerImageContainer").setStyle({"height":A+"px","width":B+"px"});
    var D=getViewportSize(),C=getScrollPosition(),G=Math.round(C[1]+(D[1]/15)),F=C[0]+Math.round((D[0]-B)/2);
    $("lightbox").setStyle({"top":G+"px","left":F+"px"});
    $("prevLink").setStyle({"height":I+"px"});
    $("nextLink").setStyle({"height":I+"px"});
    $("imageDataContainer").setStyle({"width":B+"px"});
    this.showImage();
  },showImage:function(){
    $("loading").hide();
    $("lightbox").show();
    $("lightboxImage").show();
    $("imageDataContainer").show();
    $("numberDisplay").show();
    setTimeout(this.preloadNeighborImages,1);
    this.updateDetails();
  },updateDetails:function(){
    $("caption").clearChildNodes();
    $("numberDisplay").clearChildNodes();
    if(imageArray[activeImage][1]){
      $("caption").show().innerHTML=imageArray[activeImage][1];
    }
    if(imageArray.length>1){
      $("numberDisplay").show().innerHTML="Image "+eval(activeImage+1)+" of "+imageArray.length;
    }
    this.updateNav();
  },updateNav:function(){
    $("hoverNav").show();
    var _=this;
    if(activeImage!=0){
      $("prevLink").show().onclick=function(){
        _.disableKeyboardNav();
        _.changeImage(activeImage-1);
        return false;
      };
    }
    if(activeImage!=(imageArray.length-1)){
      $("nextLink").show().onclick=function(){
        _.disableKeyboardNav();
        _.changeImage(activeImage+1);
        return false;
      };
    }
    this.enableKeyboardNav();
  },enableKeyboardNav:function(){
    $.EventListener.add(document,"keyup",this.keyboardAction,true);
  },disableKeyboardNav:function(){
    $.EventListener.remove(document,"keyup",this.keyboardAction,true);
  },keyboardAction:function(_){
    var B=Lightbox,A,$=27;
    if(_==null){
      A=event.keyCode;
    }else {
      A=_.keyCode;
    }
    key=String.fromCharCode(A).toLowerCase();
    if((key=="x")||(key=="o")||(key=="c")||(A==$)){
      B.end();
    }else {
      if((key=="p")||(A==37)){
        if(activeImage!=0){
          B.disableKeyboardNav();
          B.changeImage(activeImage-1);
        }
      }else {
        if((key=="n")||(A==39)){
          if(activeImage!=(imageArray.length-1)){
            B.disableKeyboardNav();
            B.changeImage(activeImage+1);
          }
        }
      }
    }
  },preloadNeighborImages:function(){
    if((imageArray.length-1)>activeImage){
      preloadNextImage=new Image();
      preloadNextImage.src=imageArray[activeImage+1][0];
    }
    if(activeImage>0){
      preloadPrevImage=new Image();
      preloadPrevImage.src=imageArray[activeImage-1][0];
    }
  },end:function(){
    this.disableKeyboardNav();
    $("lightbox").hide();
    hideCovering();
  },init:function(){
    var _=$("a.lightbox[href]");
    if(_==null){
      return ;
    }
    _.each(function($){
      $.onclick=function(){
        Lightbox.start(this);
        return false;
      };
    });
  }};
$.Load.ready(function(){
  Lightbox.init();
  var Q=document.getElementsByTagName("body").item(0),N=$("covering");
  N.onclick=function(){
    N.onclick=null;
    Lightbox.end();
  };
  N=null;
  var A=document.createElement("div");
  A.setAttribute("id","lightbox");
  A.onclick=function($){
    if(!$){
      var $=window.event;
    }
    var _=$.target||$.srcElement;
    if(_.id=="lightbox"){
      Lightbox.end();
    }
  };
  Q.appendChild(A);
  var R=document.createElement("div");
  R.setAttribute("id","loading");
  Q.appendChild(R);
  var M=document.createElement("div");
  M.setAttribute("id","loadingLink");
  M.setAttribute("title","Click to Cancle");
  M.onclick=function(){
    $("loading").hide();
    Lightbox.end();
    return false;
  };
  R.appendChild(M);
  var H=document.createElement("img");
  H.setAttribute("src",fileLoadingImage);
  M.appendChild(H);
  var P=document.createElement("div");
  P.setAttribute("id","outerImageContainer");
  A.appendChild(P);
  var I=document.createElement("div");
  I.setAttribute("id","imageContainer");
  P.appendChild(I);
  var O=document.createElement("img");
  O.setAttribute("id","lightboxImage");
  I.appendChild(O);
  var S=document.createElement("div");
  S.setAttribute("id","hoverNav");
  I.appendChild(S);
  var _=document.createElement("a");
  _.setAttribute("id","prevLink");
  _.setAttribute("href","javascript:void(0);");
  _.setAttribute("title","Prev Photo [\u2190]");
  S.appendChild(_);
  var F=document.createElement("a");
  F.setAttribute("id","nextLink");
  F.setAttribute("href","javascript:void(0);");
  F.setAttribute("title","Next Photo [\u2192]");
  S.appendChild(F);
  var K=document.createElement("div");
  K.setAttribute("id","imageDataContainer");
  A.appendChild(K);
  var G=document.createElement("div");
  G.setAttribute("id","imageData");
  K.appendChild(G);
  var B=document.createElement("div");
  B.setAttribute("id","imageDetails");
  G.appendChild(B);
  var L=document.createElement("span");
  L.setAttribute("id","caption");
  B.appendChild(L);
  var D=document.createElement("span");
  D.setAttribute("id","numberDisplay");
  B.appendChild(D);
  var J=document.createElement("div");
  J.setAttribute("id","bottomNav");
  G.appendChild(J);
  var C=document.createElement("a");
  C.setAttribute("id","bottomNavClose");
  C.setAttribute("href","javascript:void(0);");
  C.setAttribute("title","close");
  C.onclick=function(){
    Lightbox.end();
    return false;
  };
  J.appendChild(C);
  var E=document.createElement("img");
  E.setAttribute("src",fileBottomNavCloseImage);
  C.appendChild(E);
});
