// depends on jquery

var scroller = {
	control : '<div class="scroll-control"><a class="scroll left-off" href="#">&lsaquo; Previous <%=type%></a><a class="scroll right" href="#">Next <%=type%> &rsaquo;</a></div>',
	width	: 328
};
var motion = false;
$(document).ready(function(){

	$(".scroller h3").each(function(){
		var $this = $(this);
		var type = $this.parent("#whatsbrewing").length!=0 ? "What's Brewing item" : "Agency Buzz item";
		$this.after(scroller.control.replace(/<%=type%>/g,type));
	});
	
	// homepage
	if($(".homepage").length > 0) {		
		$(".scroller").each(function() {
			var itemwidth = parseInt($(this).find("li").width());
			var items = $(this).find("li").length;
			$(this).find("ul").width(itemwidth*items+"px");
		});	
		$(".scroll-control a.left-off").addClass("left").removeClass("left-off");
		
		var whatsheight = 0;
		$("#whatsbrewing li").each(function(){
			whatsheight = $(this).height() > whatsheight ? $(this).height() : whatsheight;	
		});
		$("#whatsbrewing .scrollcut").css("height",whatsheight+"px");
		$(".scroller li").css({position: "absolute", top: "0", left: "0"});
		
		var buzzheight = 0;
		$("#agencybuzz li").each(function(){
			buzzheight = $(this).height() > buzzheight ? $(this).height() : buzzheight;			
		});
		$("#agencybuzz .scrollcut").css("height",buzzheight+"px");
		
		$("#whatsbrewing li:last").clone().prependTo($("#whatsbrewing ul"));
		$("#agencybuzz li:last").clone().prependTo($("#agencybuzz ul"));
		$("#whatsbrewing li:last,#agencybuzz li:last").remove();
		$("#whatsbrewing li").each(function(i){
			var width = parseInt($(this).width());
			$(this).css("left",((i*width)-width)+"px");										
		});
		$("#agencybuzz li").each(function(i){
			var width = parseInt($(this).width());
			$(this).css("left",((i*width)-width)+"px");										
		});

		$("a.scroll").click(function(){	
	
			if(motion) { return false; }
			
			var $this = $(this);
			var width = parseInt($this.parents(".scroller").find("li").width());
			var distance =  $this.is(".right") ? width*-1 : width;

			motion = true;
			replaced = false;
			$this.parents(".scroller").find("li").each(function(){
				
				var position = parseInt($(this).css("left"));
				$(this).animate({ left : (distance+position)+"px" }, 800, "swing", function(){
					motion = false;				
					if(!replaced) {
						if($this.is(".right")) {
							var $clone = $this.parents(".scroller").find("li:first").clone();
							var endleft = parseInt($this.parents(".scroller").find("li:last").css("left"));							
							$clone.css("left",(endleft+width)+"px").addClass("new");
							$this.parents(".scroller").find("ul").append($clone);
							$this.parents(".scroller").find("li:first").remove();
						} else {
							var $clone = $this.parents(".scroller").find("li:last").clone();
							var endleft = parseInt($this.parents(".scroller").find("li:first").css("left"));
							$clone.css("left",(endleft-width)+"px");
							$this.parents(".scroller").find("ul").prepend($clone);	
							$this.parents(".scroller").find("li:last").remove();
						}
						replaced = true;
					}
				
				});
			
			});
			
			return false;
			
		});
		
	} else {
	
		$("a.scroll").click(function(){
				
			var $this  = $(this);
			var $block = $this.parents(".scroller").find(".scrollcut div");
			
			if(motion || $this.is(".right-off") || $this.is(".left-off")) { return false; }
			
			if($this.is(".left")) {			
				$block.left = parseInt($block.css("left")) + scroller.width;		
			}
			
			if($this.is(".right")) {					
				$block.left = parseInt($block.css("left")) - scroller.width;		
			}
			motion = true;
			$block.animate({ left : $block.left+"px" }, 800, "swing", function(){
			
				var active = $block.left + parseInt($block.css("width"));
							
				if((active - scroller.width ) < 20) {
					$this.parent().find("a.right").addClass("right-off").removeClass("right");
				}
				if((active + scroller.width ) > 340) {
					$this.parent().find("a.left").addClass("left-off").removeClass("left");
				}
				if(active < parseInt($block.css("width"))) {	
					$this.parent().find("a.left-off").addClass("left").removeClass("left-off");
				}
				if(active - 40 >= scroller.width) {	
					$this.parent().find("a.right-off").addClass("right").removeClass("right-off");
				}			
				motion = false;
			});
			
			return false;
			
		});
	}
						   
});
