jQuery(function($) {
	var carousel = {
	  selectors: {
		wrapper: ".list_wrap",
		list: ".b-carousel_list",
		item: ".b-carousel_item",
		controls_forward: ".forward",
		controls_backward: ".backward"
	  },
	  $canvas: null,
	  $wrapper: null,
	  wrapper_height: null,
	  content_height: null,
	  current_pos: 0,
	  step: 50,
	  animation_speed: 100,
	  controls_timer: null,
	  controls_interval: 100,
	  fwd_timer: null,
	  fwd_interval: 10000,
	  fwd_is_active: true,
	  fwd_direction: "down",
	  items: [],
	  items_per_step: 0,
	  init: function($canvas, items_per_step, animation_speed, fwd_interval){
		if (typeof fwd_interval != "undefined")
			this.fwd_interval = fwd_interval;
		if (typeof animation_speed != "undefined")
			this.animation_speed = animation_speed;
		this.items_per_step = Number(items_per_step);
		this.$canvas = $canvas;
		this.$wrapper = $(this.selectors.wrapper, this.$canvas);
		this.wrapper_height = this.$wrapper.height();
		this.items[0] = 0;
		$(this.selectors.item, this.$canvas).each(function(remote){return function(){
			var h = $(this).outerHeight();
			$(this).click(function(remote){return function(){
				remote.fwd_is_active = false;
			}}(remote));
			remote.content_height += h;
			remote.items[remote.items.length] = remote.items.length? (remote.items[remote.items.length - 1] + h): h;
		}}(this));
		this.items.pop();
		
		this.check_controls();
		
		this.getUpArrow().click(function(remote){
			return function(){
				if ($(this).is(".inactive"))
					return false;
				var i, step = - remote.step;
				if (remote.items_per_step)
					for (i=0;i<remote.items.length;i++)
						if (Number(remote.items[i]) >= remote.current_pos)
						{
							step = ((i - remote.items_per_step) > -1)? (- remote.current_pos + remote.items[i - remote.items_per_step]): (- remote.current_pos + remote.items[0]);
							break;
						}
				remote.navigate(step);
			}
		}(this));				
		
		this.getDownArrow().click(function(remote){
			return function(){
				if ($(this).is(".inactive"))
					return false;
				var i, step = + remote.step;
				if (remote.items_per_step)
					for (i=0;i<remote.items.length;i++)
					{
						if (Number(remote.items[i]) >= remote.current_pos)
						{
							step = ((i + remote.items_per_step) < remote.items.length)? (- remote.current_pos + remote.items[i + remote.items_per_step]): (- remote.current_pos + remote.items[remote.items.length - 1]);
							break;
						}
					}
				remote.navigate(step);
			}
		}(this)).hover(function(remote){return function(){
			remote.fwd_is_active = false;
		}}(this));
		
		this.initTimer();
		
		this.$canvas.hover(function(remote){return function () {
			remote.clearTimer();
		}}(this), function(remote){return function () {
			if (remote.fwd_is_active)
				remote.initTimer();
		}}(this)).hover(function(remote){return function(){
			remote.fwd_is_active = false;
		}}(this));
	  },
	  initTimer: function()
	  {
		  return false;
			this.fwd_timer = window.setInterval(function(remote){return function () {
				if (!remote.fwd_is_active) return false;
				if (remote.fwd_direction == "down")
					remote.getDownArrow().click();
				if (remote.fwd_direction == "up")
					remote.getUpArrow().click();
			}}(this), this.fwd_interval);
	  },
	  clearTimer: function()
	  {
		  clearInterval(this.fwd_timer);		  
	  },
	  navigate: function(step)
	  {
		  this.clearTimer();
		  $(this.$canvas).queue(function(remote, step){
			return function(){
			  var sign = "-";
			  if (Number(step) < 0)
		        sign = "+";
			  if (remote.current_pos + step < 0)
				  step = - remote.current_pos;
			  if (remote.current_pos + step > remote.content_height)
				  step = remote.content_height - remote.current_pos;
			  remote.current_pos = remote.current_pos + step;  
			  remote.check_controls();			  
			  $(remote.selectors.list, remote.$canvas).animate({"marginTop": sign + "=" + String(Math.abs(step)) + "px"}, remote.animation_speed, function(remote){ return function(){
				  remote.check_controls(true);
			  }}(remote));
			  $(this).dequeue();
			}}(this, step)
		  );		  
	  },
	  check_controls: function(fwd_check)
	  {
		  if (typeof fwd_check == "undefined")
			fwd_check = false;		  
		  var $uparr = this.getUpArrow(); 
		  if (this.current_pos >  0)
			  $uparr.css("opacity", 1).removeClass("inactive");
		  else
		  {			  
			  $uparr.css("opacity", 0.2).addClass("inactive");
			  if (fwd_check)
				  this.fwd_direction = "down"; 
		  }
		  var $dwarr = this.getDownArrow(); 
		  
		  if ((this.current_pos + this.wrapper_height + 20) <  this.content_height)
			  $dwarr.css("opacity", 1).removeClass("inactive");
		  else
		  {
			  $dwarr.css("opacity", 0.2).addClass("inactive");
			  if (fwd_check)
				  this.fwd_direction = "up"; 			  
		  }
	  },
	  getUpArrow: function()
	  {
		  return $(this.selectors.controls_backward, this.$canvas);
	  },
	  getDownArrow: function()
	  {
		  return $(this.selectors.controls_forward, this.$canvas);
	  },
	  
	};
	var carousel_new_works = {};
	$.extend(true, carousel_new_works, carousel);
	carousel_new_works.init($("#new-works-carousel"), 4, 500, 14000);	
	var carousel_flow = carousel;
	carousel_flow.init($("#flow-carousel"), 2, 500, 10000);
});
