/*
	FROM ROSS,
	
	Hey coders, this plugin handles multiple containers that have to scroll inside of a page, without a nasty scroll bar in the middle of the page.
	
	only good if one such container is preset at a time
*/

(function($) {
	//public functions
	var methods = {
		init : function(options) {
			currTopMargin=0;
			var defaults = {
				bottomMargin: 100
			};			
			opts = $.extend(defaults, options);
			
			$arrowUp = null;
			$arrowDown = null;	
			$container = $(this);
			$scrollDiv = $container.children();
			$parent = $("#wrapper");
			setUpScroll();
			//there might be images thar in there
			$container.preloadit({
				onComplete: setUpScroll
			});					
			
			try{
				this[0].addEventListener("touchstart", onTouchStart, true);
				this[0].addEventListener("touchmove", onTouchMove, true);
			}catch(e){}			
			
			$(window).unbind("resize", getContainerSize).resize(getContainerSize);
			return this;
		}
	};
	
	/*
		private vars
	*/
	var opts, minTopMargin, maxTopMargin = 0, currTopMargin, scrollInterval=0,
	$container, $scrollDiv, $arrowUp, $arrowDown, lineHeight=10, $parent,
	prevTouchY;//keep track of which div is mouse over
				
	function setUpScroll(){
		getContainerSize();

		if(minTopMargin >= 0 || minTopMargin===null){
			setTimeout(setUpScroll, 10);
			return false;
		}
		
		//if the arrows have been create there is no need to re attatch these links
		if($arrowUp === null){
			$arrowUp = $(document.createElement("a"));
			$arrowUp.attr("class", "scrollArea top").html($(document.createElement("div")).attr("class", "arrowUp"));
			$arrowUp.attr("href", "#scroll");
			$arrowUp.mouseup(stopScroll).mousedown(scrollUp)
			.hover(scrollUp, stopScroll).css({opacity: 0}).click(function(){
				return false;
			});
		
			$arrowDown = $(document.createElement("a"));
			$arrowDown.attr("class", "scrollArea bottom").html($(document.createElement("div")).attr("class", "arrowDown"));
			$arrowDown.mouseup(stopScroll).mousedown(scrollDown).hover(scrollDown, stopScroll);		
			
			$container.mousewheel(function(event, delta){
				onScroll(delta);
			}).before($arrowUp).after($arrowDown);			
		}
	}
	
	function calcMinTopMargin(){
		var scrollDivHeight = $scrollDiv.outerHeight();
		minTopMargin = $container.height() - scrollDivHeight;
		if(scrollDivHeight <= 0 || minTopMargin === undefined){			
			setTimeout(calcMinTopMargin, 10);
			return;
		}
		showHideArrows();		
	}	
	function showHideArrows(){
		if($arrowUp === null){
			return;
		}
		if(minTopMargin >= 0){
			$arrowUp.hide();
			$arrowDown.hide();
			
			currTopMargin = 0;
			$scrollDiv.css("margin-top", currTopMargin);
		}else{
			$arrowUp.show();
			$arrowDown.show().css({opacity: 1});
		}
	}
	
	function scrollDown(){
		clearInterval(scrollInterval);
		scrollInterval = setInterval(function(){
			currTopMargin-=2;
			if(currTopMargin<minTopMargin){
				//restric top margin and hide appropriate arrows
				currTopMargin = minTopMargin;
				clearInterval(scrollInterval);
				$arrowDown.css({opacity: 0});
	        }else{
				$arrowDown.css({opacity: 1});
			}
	        $arrowUp.css({opacity: 1});
			$scrollDiv.css("margin-top", currTopMargin);
		}, 16);
	}
	function scrollUp(){
		clearInterval(scrollInterval);
		scrollInterval = setInterval(function(){
			currTopMargin+=2;
			if(currTopMargin>maxTopMargin){
				//restric top margin and hide appropriate arrows
				currTopMargin = maxTopMargin;
				$arrowUp.css({opacity: 0});
				clearInterval(scrollInterval);
	        }else{
				$arrowUp.css({opacity: 1});
	        }
	        $arrowDown.css({opacity: 1});
			$scrollDiv.css("margin-top", currTopMargin);
		}, 16);				
	}
	function stopScroll(){
		clearInterval(scrollInterval);
		return false;
	}
	
	//event liseteners
	function onScroll(delta) {
        currTopMargin += delta*lineHeight;
        
        if(currTopMargin < minTopMargin){
			currTopMargin = minTopMargin;
			$arrowDown.css({opacity: 0});
        }else{
			$arrowDown.css({opacity: 1});
        }
        
       if(currTopMargin > maxTopMargin){
			currTopMargin = maxTopMargin;
			$arrowUp.css({opacity: 0});
        }else{
			$arrowUp.css({opacity: 1});
        }
        
        $scrollDiv.css("margin-top", currTopMargin);
	}	
	function onTouchStart(event){		
		var touches = event.changedTouches;
		
		if(touches.length !== 1){
			return;
		}
		var first = touches[0];
		prevTouchY = first.pageY;
		event.preventDefault();
	}	
	function onTouchMove(event, $scrollDiv){
		var touches = event.changedTouches;
		
		if(touches.length !== 1){
			return;
		}
		var first = touches[0];
		
		onScroll((first.pageY-prevTouchY)/lineHeight, $scrollDiv);
		prevTouchY = first.pageY;
		
		event.preventDefault();
	}
	function getContainerSize(skipCalMargin){
		var newHeight = $parent.height()-$container.offset().top-opts.bottomMargin;
		$container.height(newHeight);
		
		if(skipCalMargin === true){
			return;
		}		
		calcMinTopMargin();	
	}	
	$.fn.interiorScroll = function(method) {
		//Method Calling Login
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.');
		}
	};
})(jQuery);
