(function($){

 $(function(){
   $(".news-gallery").gallery().css({"visibility":"visible"});
   });//onload
 var mousescroll  = function(e, delta) {
 if(delta > 0){
 e.data.sc.prev(e.data.sp);
 } else {
 e.data.sc.next(e.data.sp);
 }
 e.stopPropagation();
 e.preventDefault();
 };


 var setData = function(source){

 var $items = $("li", source);
 var $current = $items.slice(0,1); // первоначально текущий элемент совпадает с первым видимым

 var _setCurrent = function(c){
   $current.removeClass("currentItem");
   $current = c;
   $current.addClass("currentItem");
 };

 var _nextItem = function(){
   if($current.next().length != 0){
     _setCurrent($current.next());
   }
   else {
     _setCurrent($current.prevAll().slice(-1));
   }
 };//nextItem

 var _prevItem = function(){
   if($current.prev().length != 0){
     _setCurrent($current.prev());
   }
   else {
     _setCurrent($current.nextAll().slice(-1));
   }
 };//prevItem

 return {
setCurrent : function(c){
               _setCurrent($(c));
               return $current;
             },
getCurrent : function(){
               return $current;
             },
next : function(){
         _nextItem();
         return $current;
       },
prev : function(){
         _prevItem();
         return $current;
       }
 };
 };//small

 var setBig = function(t, par){

   var prevImg = function(){
     if(par.canChange){
       showImg(t.prev());
       par.trigger("tocur");
     }
   };//prevImg

   var nextImg = function(){
     if(par.canChange){
       showImg(t.next());
       par.trigger("tocur");
     }
   };//nextImg


   return {
next : function(){
         showImg(t.next());
       },
prev : function(){
         showImg(t.prev());
       },
show : function(){
         showImg(t.getCurrent());
       }
   };
 };//big

 var setScroll = function(t, opts){

   var $target = $(t);
   var $top = $target.find("li").slice(0,1).attr({"id":"first-item"}); // первый видимый элемент
   $target.find("li").slice(-5,-4).attr({"id":"last-visible-top-item"}); // помечаем последний элемент, после которого прокрутка невозможна
   $target.find("li").slice(-1).attr({"id":"last-item"}); // помечаем последний элемент

   var _setState = function(){
     if(opts.infinity == true) {
       $target.css({"top":"-" + $top.position().top + "px"});
     } else {
       if ($top.is("#first-item")){
         $("span#arrow-up").css({"visibility":"hidden", "cursor":"none"});
       } else {
         $("span#arrow-up").css({"visibility":"visible", "cursor":"pointer"});
       };

       if ($top.is("#last-visible-top-item")){
         $("span#arrow-down").css({"visibility":"hidden", "cursor":"none"});
       } else {
         $("span#arrow-down").css({"visibility":"visible", "cursor":"pointer"});
       };
     }
     t.canChange = true;
   };//setState

   var _inf_scrollF = function(speed){ // бесконечная прокрутка вперед
     t.canChange = false;
     $target.animate({"top":"-" + $top.next().position().top + "px"}, speed,function(){
         $top.prev().appendTo($target);
         $top = $top.next();
         _setState();
         });
   };//_inf_scrollF

   var _inf_scrollB = function(speed){ // бесконечная прокрутка назад
     t.canChange = false;
     $target.animate({"top":"0px"}, speed, function(){
         $top.nextAll().slice(-1).prependTo($target);
         $top = $top.prev();
         _setState();
         });
   };//_inf_scrollB

   var _scrollF = function(speed){
     if(!$top.is("#last-visible-top-item")){
       t.canChange = false;
       $target.animate({"top":"-" +  $top.next().position().top + "px"}, speed, function(){
           $top = $top.next();
           _setState();
           });
     };
   };//_scrollB

   var _scrollB = function(speed){
     if(!$top.is("#first-item")){
       t.canChange = false;
       $target.animate({"top":"-" +  $top.prev().position().top + "px"}, speed, function(){
           $top = $top.prev();
           _setState();
           });
     };
   };//_scrollB

   var _scrollTo = function(it, speed){//прокрутка к конкретному элементу
     t.canChange = false;
     var $it = $(it);
     if($it.is("#first-item")){ //первый элемент
       $target.animate({"top": "0"}, speed, function(){
           $top = $it;
           _setState();
           });
     } else if($it.is("#last-item")) {//последний элемент
       $target.animate({"top": "-"+ $it.prevAll().slice(1,2).position().top  +"px"}, speed, function(){
           $top = $it.prevAll().slice(1,2);
           _setState();
           });

     } else{
       $target.animate({"top": "-" + $it.prev().position().top + "px"}, speed, function(){
           $top = $it.prev();
           _setState();
           });
     };
   };

   return {
init : function(){
         if(opts.infinity == true ){
           $target.find("li").slice(-1).prependTo($target); //переставляем последний элемент в начало
         }
         _setState();
       },
tocur : function(it, speed){
          _scrollTo(it, speed);
        },
prev : function(speed){
         if(t.canChange){
           if(opts.infinity){
             _inf_scrollB(speed);
           } else {
             _scrollB(speed);
           }
         }
       },
next : function(speed){
         if(t.canChange){
           if (opts.infinity){
             _inf_scrollF(speed);
           } else {
             _scrollF(speed);
           }
         }
       }
   }

 };//scroll

 $.fn.gallery = function(){
   return this.each(function(){

       $this = $(this);
       $this.canChange = true;
       var data = setData($this);
       var opts = {
infinity : false,
speed : 500
};

if ($this.find("li").length >= 4){
var scroller = setScroll($this, opts);
$next = $("<span>&#160;</span>").attr({"id":"arrow-down"}).bind("click", function(e){$this.trigger("next"); e.stopPropagation();});
$prev = $("<span>&#160;</span>").attr({"id":"arrow-up"}).bind("click", function(e){$this.trigger("prev"); e.stopPropagation();});
$wrapper = $("<div></div>").attr({"class":"gallery-wrapper"});
$this.css({"position":"absolute"});
$prev.insertBefore($this);
$next.insertAfter($this);
$this.wrap($wrapper);
scroller.init();
}


$this.find("li").bind("click", function(){
    data.setCurrent(this);
    frame.show();
    return false;
    });

$this.bind("prev", function(){
    if(scroller){
    scroller.prev(opts.speed);
    }
    });

$this.bind("next", function(){
    if(scroller){
    scroller.next(opts.speed);
    }
    });

$this.bind("tocur", function(){
    if(scroller){
    scroller.tocur(data.getCurrent(), opts.speed);
    }
    });

});
};//gallery
})(jQuery);
