var rotatorsCnt = 0;

$(function() {
	$(".rotators").each(function() {
		var $this=$(this), subels = $(".rotatorEl", $this), nextLink=$(".next", $this.next()), prevLink=$(".prev", $this.next());
		$this.addClass("rotators"+(++rotatorsCnt));
		$this.next().addClass("rotatorTools"+(rotatorsCnt));
		$this.splitUp(4, "<div class='group'>");
		if (subels.length > 4) {
			$this.cycle({ fx: 'scrollHorz', prev: prevLink, next: nextLink, timeout: 0 });
		} else {
			prevLink.remove(); nextLink.remove();
		}
	});
	$(".rotatorEl").bind("mouseover", function() { $(this).addClass("rotatorElOver"); })
	               .bind("mouseout", function() { $(this).removeClass("rotatorElOver"); })
	               .bind("click", function() { window.location = $(this).attr("rel"); });
})

$.fn.splitUp=function(splitBy,wrapper){
  $all= $(this).find('>*');
  var fragment=Math.ceil($all.length/splitBy);  
  for(i=0; i< fragment; i++) 
    $all.slice(splitBy*i,splitBy*(i+1)).wrapAll(wrapper);
  return $(this);
}
