$(function(){

  $('.barabanItem').hover(function(){
    bItemMOver(this);		
  }, function(){
		bItemMOut(this);
  });
	
	$('.barabanHolder').hover(function(){
  }, function(){
		bringToNormal();
  });		


  $('.topBHolder .control.left').click(function(){

    diff = 126;

    if($('.barabanItem:first').offset().left + diff - 10 < $('.barabanHolder').offset().left) {
      if( ($('.barabanItem:first').parent().parent().offset().left - $('.barabanItem:first').offset().left) % diff != 0 ) {
        diff = ($('.barabanItem:first').parent().parent().offset().left - $('.barabanItem:first').offset().left) % diff;
      }
      $('.barabanCont').animate({left: '+='+diff+'px' }, 240, function(){
        $(this).clearQueue();
      });
    }
		return false;
  });


   $('.topBHolder .control.right').click(function(){

     diff = -126;

      if( ($('.barabanItem:first').parent().parent().offset().left + $('.barabanItem:first').parent().parent().width() - $('.barabanItem:first').offset().left) % diff != 0 ) {
        diff = diff + ($('.barabanItem:first').parent().parent().offset().left + $('.barabanItem:first').parent().parent().width() - $('.barabanItem:first').offset().left) % diff;
      }
      
      if($('.barabanItem:last').offset().left + $('.barabanItem:last').width() - Math.abs(diff) < $('.barabanHolder').offset().left + $('.barabanHolder').width() ) {
        diff = $('.barabanHolder').offset().left + $('.barabanHolder').width() - ($('.barabanItem:last').offset().left + $('.barabanItem:last').width());
      }     

      $('.barabanCont').animate({left: '+='+diff+'px' }, 240, function(){
          $(this).clearQueue();
      });
			
			return false;
     

  });



});


var checkForRollBeck = false;


function bItemMOver(item) {

   $('body').oneTime(250, function() { 

    diff = -68;

    if( $(item).offset().left - 65 < $(item).parent().parent().offset().left ) {
      diff = diff + $(item).parent().parent().offset().left - $(item).offset().left + 68;
    }

    if( $(item).offset().left + $(item).width() + 130 > $(item).parent().parent().offset().left + $(item).parent().parent().width() ) {
      diff = diff + $(item).parent().parent().offset().left + $(item).parent().parent().width() - ($(item).offset().left + $(item).width() + 68);
    }

    $(item).parent().animate({left: '+='+diff+'px' }, 240, function(){ checkForRollBeck = true;    $(this).clearQueue();  });


    $(item).animate({'margin-top': '0px', width: '250px'}, 240, function() {
      $(item).clearQueue();
    });


    $(item).children('.inner').animate({ height: '280px' }, 240, function() {
      $(this).parent().addClass('blue');

      $(item).children('.inner').children('.body').children('.big').fadeIn("fast");
      $(item).children('.inner').children('.body').children('.small').hide();
      $(item).clearQueue();
    });

    $(item).clearQueue();

   });

}

function bItemMOut(item) {

  $('body').stopTime();

  diff = 68;
  lastItem = $('.barabanItem:last');
  firstItam = $('.barabanItem:first');

  if( $(item).offset().left == lastItem.offset().left ) {
    if( $(item).parent().parent().offset().left + $(item).parent().parent().width() > $(item).offset().left + $(item).width() - 130) {
      diff = 130;
    }
  } else if( $(item).offset().left == firstItam.offset().left ){
    diff = 0;
  }

  if(checkForRollBeck) {
    checkForRollBeck = false;
    $(item).parent().animate({left: '+='+diff+'px' }, 240, function(){ $(this).clearQueue();   });
  }

  $(item).clearQueue();
  $(item).removeClass('blue');
  $(item).children('.inner').children('.body').children('.big').hide();
  $(item).children('.inner').children('.body').children('.small').fadeIn("fast");  

  $(item).animate({'margin-top': '40px', width: '120px'}, 240, function() {  });

  /*$(item).children('.inner').animate({ height: '200px' }, 240, function() {
    if($(item).hasClass('blue')) {
      $(item).removeClass('blue');
      $(item).children('.inner').children('.body').children('.big').hide();
      $(item).children('.inner').children('.body').children('.small').fadeIn("fast");
    }
    $(item).clearQueue();
  });
	*/
	$(item).children('.inner').animate({ height: '200px' }, 0, function() {
    if($(item).hasClass('blue')) {
      $(item).removeClass('blue');
      $(item).children('.inner').children('.body').children('.big').hide();
      $(item).children('.inner').children('.body').children('.small').fadeIn("fast");
    }
    $(item).clearQueue();
  });

}

function bringToNormal() {
	
	$.each($('.barabanItem'), function(index, item) {
			$(item).removeClass('blue');
      $(item).children('.inner').children('.body').children('.big').hide();
      $(item).children('.inner').children('.body').children('.small').fadeIn("fast");
	});		
}

