(function($){
  $.fn.gallery=function(o){
	var J=this,
    cw=300,
	pw=800,
	ph=500,
	sw=80,
	sh=50,
	total=0,
	index=0,
	pc=0,
	per=0,
	data=null,
	thumbs=[],
	cursor=1,
	cstate=1,
	mode=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})
	  }
	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()
									}); 
		  }
		  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() 
			  }
			  else{alert("xml/json/html missed!");return false}
	}
	function construction(){
      J.hide().addClass("galleryshowbox").html('<img class="gallerywin" src="'+D[0].image+'" />\
    <div class="gallerydoor">\
      <span class="gallerybar">Ausblenden</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>');
	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("Anzeigen")},function(){gallerydoorbody.show();$(this).text("Verbergen")})
		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)

