(function($) {
    $.fn.extend({
        slider : function(opt) {
          var defaults = {
              cssNor : {
			  	'height' : '72px',
                'margin': '5px 2px'
              },
              cssHover : {
                'height' : '82px',
				'margin': '0px 2px'
              },
              cssAct   : {
                'height' : '72px',
				'margin': '5px 2px'
              },
              PixelSteps : 2,
              IntervalMS : 10,
              HoverSpeed : 100
          };
          var opt = $.extend(defaults, opt);
          return this.each(function() {
            $('.box .cnt',obj).css('left','0px');
            var inter = null;
            var obj = $(this);
            var ActI = null;
            var BoxWidth = $('.box',obj).width();
            var BoxLeft = $('.box',obj).offset().left;
            var init = function() {
              if(obj.attr('rel')) {
                  ActI = parseInt(obj.attr('rel'))-1;
                  $('.box .cnt img:eq('+ActI+')',obj).css(opt.cssAct);
                  var l = (BoxWidth/2)+BoxLeft-$('.box .cnt img:eq('+ActI+')',obj).offset().left-($('.box .cnt img:eq('+ActI+')',obj).width()/2);
                  $('.box .cnt',obj).animate({'left':l},200);
              }
            }; 
            
            var start = function(v) {
              inter = window.setInterval(function() {
                var l =parseInt(parseInt($('.box .cnt',obj).css('left'))+v);
				if(v>0 && (BoxLeft-$('.box .cnt img:eq(0)',obj).offset().left)<=0) { stop(); }
                if(v<0 && ($('.box .cnt img:last',obj).offset().left)<=BoxLeft+BoxWidth-$('.box .cnt img:last',obj).width()) { stop(); }
                $('.box .cnt',obj).css('left',l);
              },opt.IntervalMS);
            };
            var stop = function() {
              window.clearInterval(inter);
              inter = null;
            };
            var setAct = function(i) {
              $('.box .cnt img:eq('+ActI+')',obj).animate(opt.cssNor,opt.HoverSpeed);
              ActI = i;
              $('.box .cnt img:eq('+ActI+')',obj).animate(opt.cssAct,opt.HoverSpeed);
            };
             
             $('.next',obj).hover(function() {start(-opt.PixelSteps);},stop);
             $('.prev',obj).hover(function() {start(opt.PixelSteps);},stop);
             $('.box .cnt img',obj).each(function(i) {
                $(this).css(opt.cssNor).hover(function() {
                  //if(ActI==i) return false;
                  $(this).animate(opt.cssHover,opt.HoverSpeed);  
                },function() {
                  //if(ActI==i) return false;
                  $(this).animate(opt.cssNor,opt.HoverSpeed);
                  
                }).click(function() {
                  setAct(i);
                });
             });
          	if ($('.box .cnt img:last',obj).width() > 50) {
          	  window.setTimeout(init,200);
          	}
          	else {
          		$('.box .cnt img:last',obj).load(init);
          	}

             
             
          });
        }
    });
})(jQuery);
