var module_gallery={
img:{}
,rev:{}
,loaded:{}
,conf:{}
,cur:0
,h:null
,pre:{}
,isLoading:false
,init:function(c){
   module_gallery.img=c.f;
   module_gallery.conf=c.conf;
   $.each(c.f,function(k,v){
      module_gallery.rev[v]=k;
   });
   if(isset(c.def.c)) module_gallery.cur=c.def.c;
   module_gallery.setImg(module_gallery.cur);
   module_gallery.h=$('.'+c.conf.cls);
   module_gallery.setNav();
   $(document).unbind('keydown').keydown(function(el){
      switch(el.keyCode){
        case 37:
          if(!module_gallery.isLoading) module_gallery.goTo(-1);
        break;
        case 39:
          if(!module_gallery.isLoading) module_gallery.goTo(+1);
        break;
      }
   });
   if(location.hash.length){
      module_gallery.getHash(1);
   }
   else{
      module_gallery.setHash();
   }
   $(window).bind( 'hashchange', function(){
      module_gallery.getHash(1);
   });
}
,goTo:function(nm){
   module_gallery.setImg(((module_gallery.cur*1)+nm))
}
,getHash:function(li){
    var f=_GET('f');
    if(isset(f)){
       if(isset(li)) module_gallery.setImg(module_gallery.rev[f]);
    }
}
,setHash:function(){
  var t=module_gallery.img[module_gallery.cur];
  if(t!=_GET('f')){
     noReload=1;
     setHash(getvals({'f':t}));
  }
}
,setNav:function(){
   var nk=(module_gallery.cur*1)+1;
   var pk=module_gallery.cur-1;
   module_gallery.h.find('.next').unbind().click(function(e){
      e.preventDefault();
      module_gallery.setImg(nk);
   });
   module_gallery.h.find('.prev').unbind().click(function(e){
      e.preventDefault();
      module_gallery.setImg(pk);
   });
   if(!isset(module_gallery.img[nk])) module_gallery.h.find('.next').addClass('disabled');
   else module_gallery.h.find('.next').removeClass('disabled');
   if(!isset(module_gallery.img[pk])) module_gallery.h.find('.prev').addClass('disabled');
   else module_gallery.h.find('.prev').removeClass('disabled');
}
,setImg:function(k){
   if(!isset(module_gallery.img[k])) return false;
   if(isset(module_gallery.loaded[k])){
      module_gallery.h.find('.screen .img')
         .html(module_gallery.loaded[k].img);

      module_gallery.h.find('.comments').html(module_gallery.loaded[k].c)
         .find('form').attr('action',location.href);
      module_gallery.h.find('.imgtext .text').html(module_gallery.loaded[k].text);
      module_gallery.h.find('.imgtext .by').html((module_gallery.loaded[k].photo?module_gallery.loaded[k].photo:module_gallery.conf.photo));
      module_gallery.cur=k;
      module_gallery.setHash();
      module_gallery.setNav();
      module_gallery.preload();
   }
   if(k==module_gallery.cur){
      module_gallery.preload();
   }
   else{
      module_gallery.loadImg(k);
   }
}
,preload:function(){
  var ki=module_gallery.cur*1;
  for(var n=1;n<=3;n++){
     var t=ki+n;
     if(!isset(module_gallery.pre[t])) module_gallery.pre[t]=1;
     var t=ki-n;
     if(!isset(module_gallery.pre[t])) module_gallery.pre[t]=1;
  }
  var nx=null;
  var k=0;
  $.each(module_gallery.pre,function(k,v){
     if(v==1&&k>=0&&nx==null) nx=k;
  });
  if(nx!=null){
     module_gallery.loadImg(nx,1);
  }
}
,loadImg:function(k,pl){
     if(!isset(pl)) pl=false;
     if(!pl){
        setLoading(1);
	module_gallery.isLoading=true;
     }
     $.get('index.php?module=gallery&ajax=1&act=get&m=1&table='+module_gallery.conf.table
        +'&id='+module_gallery.img[k]+'&comt='+module_gallery.conf.comt,function(data){
      module_gallery.loaded[k]=strToJson(data);
//      module_gallery.loaded[k].imge=new Image();
      module_gallery.loaded[k].imge=$('<img>');
      $(module_gallery.loaded[k].imge).attr('src',module_gallery.loaded[k].url)
         .load(function(){
         if(!pl){
	    setLoading(0);
	    module_gallery.isLoading=false;
	 }
         if(!pl) module_gallery.setImg(k);
	 module_gallery.pre[k]=0;
	 if(pl) module_gallery.preload();
      });//.attr('src',module_gallery.loaded[k].url);
   });
}
};
