var $j = jQuery.noConflict();

$j(document).ready(function() {
	
	
	
	
	
	/* Initialize the scrollers
	-----------------------------------------------------*/
	$j('.scrollerContainer').each(function(index) {
		
		if ($j(this).hasClass("delayInit")) {
		} else {
		
			// Set the custom heights
			if ($j(this).attr("custom_height")) {
				$newHeight = $j(this).attr("custom_height");
				$newHeight = ($newHeight * 23) - 3;
				$j(this).css("height", String($newHeight+"px"));
			}
			
			if ($j(this).height() > $j(this).children(".selections").height())
			$j(this).css("height", String($j(this).children(".selections").height()));
		
			getScrollContentInit($j(this));
	
			$j(this).children(".selections").children(".selected").each(function(index) {
				setupSelected($j(this));
			});
			
			if ($minY == 0) 
			$j(this).addClass("noScroll");
		}
		

	});
	
	$j('.toggleButton').each(function(index) {
		
		if ($j(this).hasClass("closed")) {
			$j(this).next().hide();
		}
	});
	
	
	
	/* Set the start position of the scrollbar
	-----------------------------------------------------*/
	
	 function setupSelected($selected) {
		 
		getScrollContentAlt($selected);
		$selectPos = $j($selected).position();
		
		$newPos = -$selectPos.top;
		if ($newPos < $minY) $newPos = $minY;
		
		$percent = $newPos/$minY;
		$newDraggerPos = ($dragHeight * $percent);
		
		$selection.animate({top:$newPos},{duration:300,queue:false});
		$dragger.animate({top:$newDraggerPos},{duration:300,queue:false});
	};
	
	
	
	/* Mouse Wheel listener
	-----------------------------------------------------*/
	$j('.scrollerContainer').mousewheel(function(e, delta) {
		
		e.preventDefault();
		
		getScrollContentInit($j(this));
		
		$selectPos = $j(this).children(".selections").position();
		
		//$newPos = ($selectPos.top + (Number(delta)*(Math.abs($minY)/10)));
		$newPos = ($selectPos.top + (Number(delta)*20));
		if ($newPos < $minY) $newPos = $minY;
		if ($newPos > $maxY) $newPos = $maxY;
		
		$percent = $newPos/$minY;
		$newDraggerPos = ($dragHeight * $percent);
		
		$selection.animate({top:$newPos},{duration:300,queue:false});
		$dragger.animate({top:$newDraggerPos},{duration:300,queue:false});
		
	});
	
	
	
	/* Drag listener
	-----------------------------------------------------*/
	$j(".dragger").draggable({
		axis: 'y',
		containment: 'parent',
		start: function(event, ui) {
			getScrollContent( this );
		},
		
	   drag: function(event, ui) { 
		   $percent = $j(this).position().top/$dragHeight;
		   $newPos = ($minY * $percent);
		   $selection.animate({top:$newPos},{duration:300,queue:false});
	   }
	});
	
	function getScrollContentInit($container) {
		
		$selection = $container.find(".selections");
		$selectionHeight = $selection.height();
		$container = $container;
		$containerHeight = $container.height();
		$minY = $containerHeight - $selectionHeight;
		$maxY = 0;
		$dragger = $selection.next().children(".dragger");
		$dragHeight = $containerHeight - $j($dragger).height();
	}
	
	function getScrollContent($dragger) {
		
		$selection = $j($dragger).parent().prev();
		$selectionHeight = $selection.height();
		$container = $j($dragger).parent().parent();
		$containerHeight = $container.height();
		$minY = $containerHeight - $selectionHeight;
		$maxY = 0;
		$dragHeight = $containerHeight - $j($dragger).height();
	}
	
	function getScrollContentAlt($selected) {
		
		$selection = $j($selected).parent();
		$selectionHeight = $selection.height();
		$container = $j($selected).parent().parent();
		$containerHeight = $container.height();
		$minY = $containerHeight - $selectionHeight;
		$maxY = 0;
		$dragger = $selection.next().children(".dragger");
		$dragHeight = $containerHeight - $j($dragger).height();
	}
	
	
	
	/* Toggle button listener
	-----------------------------------------------------*/
	$j('.toggleButton').click(function(e) {
		
		if ($j(this).hasClass("resetBtn")){
		} else {
		
		e.preventDefault();
		
			$closed = false;
			if ($j(this).hasClass("closed"))
			$closed = true;
			
			if ($j(this).attr("group") != "") {
				$j(".toggleButton[group='"+$j(this).attr("group")+"']").addClass("closed");
				$j(".toggleButton[group='"+$j(this).attr("group")+"']").next().slideUp();
			}
			
			if ($j(this).hasClass("closed")) {
				if ($closed == true) {
					$j(this).next().slideDown();
					$j(this).removeClass("closed");
				}
			} else {
				$j(this).next().slideUp();
				$j(this).addClass("closed");
			}
		}
	});
	
	function setNewHeight(sc) {
		
		// Set the custom heights
		if (sc.attr("custom_height")) {
			$newHeight = sc.attr("custom_height");
			$newHeight = ($newHeight * 23) - 3;
			sc.css("height", String($newHeight+"px"));
		}
		
		if (sc.height() > sc.children(".selections").height())
		sc.css("height", String(sc.children(".selections").height()));
	
		getScrollContentInit(sc);

		sc.children(".selections").children(".selected").each(function(index) {
			setupSelected(sc);
		});
		
		if ($minY == 0) 
		sc.addClass("noScroll");
	}
	
	/*
	$j('.scrollerContainer').bind('contentchanged', function() {
		// Set the custom heights
		if ($j(this).attr("custom_height")) {
			$newHeight = $j(this).attr("custom_height");
			$newHeight = ($newHeight * 23) - 3;
			$j(this).css("height", String($newHeight+"px"));
		}
	
		if ($j(this).height() > $j(this).children(".selections").height()) {
			$j(this).css("height", String($j(this).children(".selections").height()));
		}
	
		getScrollContentInit($j(this));

		$j(this).children(".selections").children(".selected").each(function(index) {
			setupSelected($j(this));
		});
		
		if ($minY == 0) 
		$j(this).addClass("noScroll");
		
		$j(this).slideUp("fast", function() {
	
			$j(this).parent().css("visibility", "visible");
			
		});
	});
	*/
	
});
