$(document).ready(function(){

  //var totalwidth1 = $("#articleStrip li").size() * 586;
  var ul1 = $("#articleStrip");
  var effect_in_progress1 = false;
  
  var recurrentFunc1 = function() {
	  if ($(this).hasClass('CMSBut')) {
		  return true;
	  }
	  
    if (effect_in_progress1) {
      return false;
    }
  
    var left = (1 - parseInt($(this).attr('rel'))) * 586;
    var diff = (parseInt(ul1.css('marginLeft')) - left) / 586;
    
    if (diff < 0) {
      diff *= -1;
    }
    
    if (diff == 0) {
      return false;
    }
    
    $('#tabs li a.selected').removeClass('selected');
    $(this).addClass('selected');
    
    effect_in_progress1 = true;
    
    ul1.animate({
        marginLeft: left + 'px'
    }, diff * 200, 'linear', function() { effect_in_progress1 = false; });
   
    return false;
  };
  
  $("#tabs li a").click(recurrentFunc1);
  
  /* ------------------ */
  
  //var totalwidth2 = $("#cats-subcats li").size() * 586;
  var ul2 = $("#cats-subcats");
  var effect_in_progress2 = false;
  
  var recurrentFunc2 = function() {
    if (effect_in_progress2) {
      return false;
    }
  
    var left = (1 - parseInt($(this).attr('rel'))) * 586;
    var diff = (parseInt(ul2.css('marginLeft')) - left) / 586;
    
    if (diff < 0) {
      diff *= -1;
    }
    
    if (diff == 0) {
      return false;
    }
    
    $('#tabs-slider li a.selected').removeClass('selected');
    $(this).addClass('selected');
    
    effect_in_progress2 = true;
    
    ul2.animate({
        marginLeft: left + 'px'
    }, diff * 200, 'linear', function() { effect_in_progress2 = false; });
   
    return false;
  };
  
  $("#tabs-slider li a").click(recurrentFunc2);
  
  /* ------------------- */
  
  //var totalwidth2 = $("#cats-subcats li").size() * 586;
  var ul3 = $("#events");
  var effect_in_progress3 = false;
  
  var recurrentFunc3 = function() {
    if (effect_in_progress3) {
      return false;
    }
  
    var left = (1 - parseInt($(this).attr('rel'))) * 586;
    var diff = (parseInt(ul3.css('marginLeft')) - left) / 586;
    
    if (diff < 0) {
      diff *= -1;
    }
    
    if (diff == 0) {
      return false;
    }
    
    $('#events-slider li a.selected').removeClass('selected');
    $(this).addClass('selected');
    
    effect_in_progress3 = true;
    
    ul3.animate({
        marginLeft: left + 'px'
    }, diff * 200, 'linear', function() { effect_in_progress3 = false; });
   
    return false;
  };
  
  $("#events-slider li a").click(recurrentFunc3);
  
  /* ------------------- */
	
	$('div.holder').each(function() {
		var gal = $(this);
		var galHolder = gal.find('ul.items');
		var maxScroll = -135 * (galHolder.children().size() - 4);
		
		var onButton = function() {

			if (galHolder.attr('scrollin') == '1') {
				return false;
			}
			
			var but = $(this);
			var lftShift = parseInt(galHolder.css('left'));
			
			if (but.hasClass('b-next')) {
				if (lftShift > maxScroll) {
					galHolder.attr('scrollin', '1');
					galHolder.animate({left: '-=135'}, 150, 'linear', function() {
						galHolder.attr('scrollin', '0');
					});
				}
			} else {
				if (lftShift < 0) {
					galHolder.attr('scrollin', '1');
					galHolder.animate({left: '+=135'}, 150, 'linear', function() {
						galHolder.attr('scrollin', '0');
					});
				}
			}
			
			return false;
		};
		
		gal.find('a.b-prev').click(onButton);
		gal.find('a.b-next').click(onButton);
        
    });
   
  /* ------------------- */
  
  var ul4 = $("#bottabs-content");
  var effect_in_progress4 = false;
  
  var recurrentFunc4 = function() {
    if (effect_in_progress4) {
      return false;
    }
  
    var left = (1 - parseInt($(this).attr('rel'))) * 586;
    var diff = (parseInt(ul4.css('marginLeft')) - left) / 586;
    
    if (diff < 0) {
      diff *= -1;
    }
    
    if (diff == 0) {
      return false;
    }
    
    $('#bottabs-tabs li a.selected').removeClass('selected');
    $(this).addClass('selected');
    
    effect_in_progress4 = true;
    
    ul4.animate({
        marginLeft: left + 'px'
    }, diff * 200, 'linear', function() { effect_in_progress4 = false; });
   
    return false;
  };
  
  $("#bottabs-tabs li a").click(recurrentFunc4);
  
  /* ------------------- */

});
