//momofiona 090320
//QQ:125058687
(function($){
  $.fn.gallery=function(o){
	var J=this,
    cw=300,//控制条宽
	pw=800,//相片容器宽
	ph=500,//相片容器高
	sw=42,//缩略图宽
	sh=42,//缩略图高
	total=0,//相片总数
	index=0,//当前相片索引
	pc=0,//缩略图容器宽-容器宽-10
	per=0,//百分点
	data=null,//存储xml/json数据
	thumbs=[],//缩略图加载
	cursor=1,//鼠标指针
	cstate=1,//鼠标状态
	mode=0,//大图预加载模式1：完全预载，0：相邻预载
	easing="",//缩略图滑动特效
	gallerydoor,gallerydoorbody,gallerycontrol,galleryphotos,gallerywin,gallerybar,gallerycontrolimg,gallerycount,galleryname,D
	//切换特效
	J.easing=function(sth){
		  easing=sth
		}
	//跳转总控制
	J.goto=function(i){
	  i=Math.floor(i)
	  index=i
	  if(i>total-1||i<0) return false
	  gallerycount.text(i+1)
	  galleryname.text(D[i].title)
	  index=i
	  image(i)
	  per=(i*(sw+5)-((pw-10)-(sw+5))/2)/pc
	  if(pc>0) hi()
	}
	//预加载
	J.preLoad=function(){
		for(var i=total-1;i>=0;i--){
			  D[i].win=new Image()
			  D[i].win[0]=i
			  D[i].win.onload=function(){if(this[0]==index){J.resize(this)}}
			  if(mode==1&&i!=0)D[i].win.src=D[i].image
			}
		 J.goto(0)
		}
	//大图变换
	function image(i){
	  gallerywin.css("opacity",0.3)
	  D[i].win.src=D[i].image
	  if(mode==0){
		  if(index+1<total) D[index+1].win.src=D[index+1].image
		  if(index>0) D[index-1].win.src=D[index-1].image
		}
	  }
	//大图大小控制,此接口可改写
	J.resize=function(_o){
      var _w,_h
      if(_o.width>pw || _o.height>ph){
		if(_o.width/_o.height>pw/ph){
			_w=pw
			_h=pw*_o.height/_o.width
		}else{
			_h=ph
			_w=ph*_o.width/_o.height
		}
	  }else{_w=_o.width;_h=_o.height}
	  gallerywin.attr("src",D[index].image).height(_h).width(_w).css("opacity",1)
	 }
	//
	J.next=function(){
	  if(index>total-2) return false
	  J.goto(++index)
	 }
	//
	J.prev=function(){
	  if(index<1) return false
	  J.goto(--index)
	  }
	//
	function hi(){
	  per=per<(1/total)?0:per
	  per=per>(1-1/total)?1:per
	  galleryphotos.animate({"left":-pc*per},{queue:false,duration:1000,easing:easing})
      gallerycontrolimg.animate({"left":cw*per},{queue:false,duration:1000})
	  }
	//获取数据(xml/json)
	function loaddata(){
	  if(o.xml!=null){
		  data=[]
		  D=data
		  $.ajax({
			 url:o.xml,
			 dateType:"xml",
			 error:function(){alert("Failed in loading XML.")},
			 success:function(msg){
			   $(msg).find("item").each(function(){
				 D.push({title:$("title",this).text(),thumb:$("thumb",this).text(),image:$("image",this).text()})
				 total++
			   })
			   construction()
			 }
		  }); 
	  }else if(o.json!=null){
		  $.getJSON(o.json,function(json){
									data=eval(json)
									D=data
									total=D.length
									construction()
									}); 
		  }
		  /////////////^data from html
		  else if($("img",J).size()){
			   data=[]
		       D=data
			   $("img",J).each(function(){
				 D.push({title:$(this).attr("title"),thumb:$(this).attr("thumb")||$(this).attr("src"),image:$(this).attr("src")})
				 total++
			   })
			   construction() 
			  }
		  //////////////$data from html
			  else{alert("xml/json/html missed!");return false}
	}
	//搭建框架
	function construction(){
      J.html('<table width="100%" height="320" border="0" cellspacing="0" cellpadding="0"><tr><td align="center" valign="middle"><img class="gallerywin" src="images/ajax.gif" /></td></tr></table> \
    <div class="gallerydoor">\
      <span class="gallerybar_center"></span>\
      <div class="gallerydoorbody">\
        <div class="galleryinfo">\
          <div class="gallerycontrol"><span class="gallerycontrolimg"></span></div>\
          <span class="gallerycount"><strong>1</strong>/'+total+'</span>\
          <span class="galleryname"></span>\
        </div>\
        <div class="galleryphotos"></div>\
      </div>\
    </div>').addClass("galleryshowbox").hide()
	design()
	sizetogallery()
	datafill()
	J.preLoad()
    galleryevent()
	J.show()
	}
	//界面量化
  function sizetogallery(){
	if(o ==null) o={}
	easing=o.easing==null?"":o.easing
	mode=o.mode==null?0:1
	J.width(pw=o.pw==null?pw:o.pw).height(ph=o.ph==null?ph:o.ph)
	gallerydoor.width(J.width())
	gallerycontrol.width(cw=o.cw==null?cw:o.cw)
	galleryphotos.find("span").width(sw=o.sw==null?sw:o.sw).height(sh=o.sh==null?sh:o.sh).end().width(pc=total*(sw+5)).height(sh)
	pc-=pw-10
  }
  //填充数据
  function datafill(){
    var _b=sw/sh,_t=thumbs
    for(var i=0;i<total;i++){
	  $("<span />").width(sw).height(sh).appendTo(galleryphotos).html("<img rel='"+i+"' style='display:none'/>")
	  _t[i]=new Image(),_t[i][0]=$("img:eq("+i+")",galleryphotos)
	  _t[i].onload=function(){
		  var _this=this;
		  if(this.width/this.height>_b){
			this[0].attr("src",_this.src).height(sh).show().parent().css("background","")
		  }else{
		    this[0].attr("src",_this.src).width(sw).show().parent().css("background","")
		  };
	  }
	  _t[i].src=D[i].thumb
	}
  }
	//绑事件
	function galleryevent(){
	    //大图的一天
		gallerywin.mousemove(function(e){
		  if(e.pageX-$(this).offset().left>$(this).width()/2){cursor=0}else{cursor=1}
		  if(index==total-1)cursor=1
		  if(index==0){cursor=0}
		  if(cursor==0 && cstate==1){$(this).css("cursor","images/next.ani");cstate=0}
		  if(cursor==1 && cstate==0){$(this).css("cursor","images/pre.ani");cstate=1}
		}).click(function(){if(cursor==0){J.next()}else{J.prev()}})
	    //缩略图
		$("span",galleryphotos).click(function(){
		  var _this=$(this).find("img")
		  if(_this.attr("src")=="") return false
		  J.goto(_this.attr("rel"))
		})
		//导航显/隐
		gallerybar.toggle(function(){gallerydoorbody.hide();$(this).text("显示")},function(){gallerydoorbody.show();$(this).text("隐藏")})
		//滑轮
		gallerycontrol.mousedown(function(e){
          per=(e.pageX-$(this).offset().left)/cw
		  hi()
          })
		 //滚轮
		 J.mousewheel(function(e,v){
		   if(document.addEventListener){e.preventDefault()}else{event.returnValue = false;}
		   if(per==0&&v>0) return false
		   if(per==1&&v<0) return false
		   per-=v/total
		   hi()
		 })
	  }
	  //标记框架
	function design(){
	    gallerydoor=$(".gallerydoor",J),
		gallerydoorbody=$(".gallerydoorbody",gallerydoor),
	    gallerycontrol=$(".gallerycontrol",gallerydoorbody),
		galleryphotos=$(".galleryphotos",gallerydoor),
		gallerywin=$(".gallerywin",J),
		gallerybar=$(".gallerybar",gallerydoor),
		gallerycontrolimg=$(".gallerycontrolimg",gallerycontrol),
		gallerycount=$(".gallerycount strong",gallerydoorbody),
		galleryname=$(".galleryname",gallerydoorbody)
		}
	//运行开始
	loaddata()
	return J 
  }
})(jQuery)
/*上面源码中的鼠标样式"http://www.25318.cn/next.ani"、搭建框架中images/ajax.gif替换成绝对路径就行了 */
/*function imgPreLoad(a){
  for(var i=0;i<a.length;i++){
    var _a=new Image()
	_a.src=a[i]
  }
}
imgPreLoad(["images/text_bg.png","images/line.png","images/next.ani","images/pre.ani"])*/