/*
*
*
*  Cube Scroller v. 2.1.0
*
*  By george @ Cube 2011
*
*/




$(function() {
	$.fn.CubeScroller = function(options){
		
		var container = '.'+$(this).attr('class');
		
		var defaults = {
			containerH : null,			// container height, default null
			containerW : null,			// container width, default null
			content : 'content', 		// class of content div inside container
			speed : 5,					// scroller speed
			distance : 10,				// scroller distance
			arrup : 'cubearrup',		// class for move up
			arrdown : 'cubearrdown',	// class for move down
			hideNav : true,
			allowMore : false
		};
		
		var options = $.extend(defaults, options);
		
		applyDefaultCss(container,options.containerH,options.containerW,options.content);
		
		var topDistance = parseInt($(container+' .'+options.content).css('margin-top'));
		
		var containerWidth = parseInt($(container).css('width'));
		var containerHeight = parseInt($(container).css('height'));
		var contentHeight = parseInt($(container+' .'+options.content).css('height'));
		
		if(options.hideNav){
			if(topDistance==0)
				$('.'+options.arrup).hide();
			if(contentHeight<=containerHeight)
				$('.'+options.arrdown).hide();
		}
		
		
		if(options.allowMore){
			var arrup = container+' a.'+options.arrup;
			var arrdown = container+' a.'+options.arrdown;
		}
		else{
			var arrup = ' a.'+options.arrup;
			var arrdown = ' a.'+options.arrdown;
		}
		
		
		$(arrup).mouseover(function(){
			$(arrdown).show();
		  	var animation = setInterval(function(){
		  		var topVal = parseInt($(container+' .'+options.content).css('margin-top'));
		  		
			    if(topVal==0){
			    	if(options.hideNav)
			    		$(arrup).hide();
			    	forceStop(animation);
			    	return;
			    }
			    
			    $(container+' .'+options.content).css("margin-top", topVal+options.distance);
			    var newTopVal = parseInt($(container+' .'+options.content).css("margin-top"));
		  		
		  		$(arrup).mouseout(function(){
					clearInterval(animation);
				});
		  	},options.speed*10);
		 });
		 
		 $(arrdown).mouseover(function(){
		 	$(arrup).show();
		  	var animation = setInterval(function(){
		  		var topVal = parseInt($(container+' .'+options.content).css('margin-top'));
		  		
		  		
			    if(((topVal*(-1))+((90*containerHeight)/100))>=contentHeight){
			    	if(options.hideNav)
			    		$(arrdown).hide();
			    	forceStop(animation);
			    	return;
			    }
			    
			    $(container+' .'+options.content).css("margin-top", topVal-options.distance);
			    var newTopVal = parseInt($(container+' .'+options.content).css("margin-top"));
		  		
		  		$(arrdown).mouseout(function(){
					clearInterval(animation);
				});
		  	},options.speed*10);
		 });
		
		
		function forceStop(animation){
			clearInterval(animation);
		}
		
		function applyDefaultCss(container,containerH,containerW,content){
			if(containerH)	$(container).css('height',containerH); // Set user define content height
			if(containerW)	$(container).css('width',containerW); // Set user define content width
			
			$(container).css({'overflow':'hidden','position':'relative'});
			$(container+' .'+content).css({'margin-top':'0px'});
		}
		
	};
});
