//scroll up down
function superScrollerUD(wrapping_element, scrolling_element){
		
	if (jQuery(wrapping_element).length > 0){
	scrolling_area = jQuery(scrolling_element).height() - jQuery(wrapping_element).height();
	
	var SCROLL_FIX = jQuery(wrapping_element).height();
	var scroll_amount = 0;
	var currentPos = jQuery(scrolling_element).position();
	var animating = false;
	var ticker=0;
	var height_of_rest=0;


	if (scrolling_area > 0){
		jQuery(".scrolldown").bind("click",function(){scroll_items("down",SCROLL_FIX)});
		jQuery(".scrollup").bind("click",function(){scroll_items("up",SCROLL_FIX)});
		
		function scroll_items(direction,amount){
			if (animating == false){
				animating = true;	
				currentPos = jQuery(scrolling_element).position();
			
				if (direction == "down") {
					if ((currentPos.top*-1) + amount < jQuery(scrolling_element).height() - jQuery(wrapping_element).height()){
						scroll_amount += amount;
					}else{
						scroll_amount = jQuery(scrolling_element).height() - jQuery(wrapping_element).height();
						jQuery(".scrolldown").addClass("disabled");
					}jQuery(".scrollup").removeClass("disabled");
				}else
				if (direction == "up"){
					if(scroll_amount -  amount >= 0){
					scroll_amount -= amount;
					}else{
					scroll_amount = 0;
					jQuery(".scrollup").addClass("disabled");
					}jQuery(".scrolldown").removeClass("disabled");
				}
				jQuery(scrolling_element).animate({top: -scroll_amount},400, function(){animating = false;});
				}
			}
		}
	}
}

//scroll left right
function superScrollerLR(wrapping_element, scrolling_element, lScroll, rScroll){
	if (jQuery(wrapping_element).length > 0){
	scrolling_area = jQuery(scrolling_element).width() - jQuery(wrapping_element).width();
	
	var SCROLL_FIX = jQuery(wrapping_element).width();
	var scroll_amount = 0;
	var currentPos = jQuery(scrolling_element).position();
	var animating = false;
	var ticker=0;
	var height_of_rest=0;

    //alert(scrolling_area);
	if (scrolling_area > 0){
		jQuery(lScroll).bind("click",function(){scroll_items("left",SCROLL_FIX)});
		jQuery(rScroll).bind("click",function(){scroll_items("right",SCROLL_FIX)});
		//alert(scrolling_area);
		//if((currentPos.left*-1) + amount <= jQuery(scrolling_element).width())
        		//alert('');
		function scroll_items(direction,amount){
			if (animating == false){
				animating = true;	
				currentPos = jQuery(scrolling_element).position();
			
				if (direction == "right") {
					if ((currentPos.left*-1) + amount < jQuery(scrolling_element).width() - jQuery(wrapping_element).width()){
						scroll_amount += amount;
					}else{
						scroll_amount = jQuery(scrolling_element).width() - jQuery(wrapping_element).width();
						jQuery(rScroll).addClass("disabled");
					}jQuery(lScroll).removeClass("disabled");
				}else
				if (direction == "left"){
					if(scroll_amount - amount >= 0){
					scroll_amount -= amount;
					}else{
					scroll_amount = 0;
					jQuery(lScroll).addClass("disabled");
					}jQuery(rScroll).removeClass("disabled");
				}
				jQuery(scrolling_element).animate({left: -scroll_amount},400, function(){animating = false;});
				}
			}
		} else {
        	jQuery(rScroll).addClass("disabled");
		}
		
		if(scrolling_area == currentPos.left *-1) {
		  jQuery(lScroll).removeClass("disabled");
		  jQuery(rScroll).addClass("disabled");
	    } else if (currentPos.left != 0) {
	        jQuery(lScroll).removeClass("disabled");
	        jQuery(rScroll).removeClass("disabled");
	    }
		  //alert(scrolling_area + ' ' + currentPos.left);
	}
}

jQuery(document).ready(function(){
	superScrollerUD(".equipmentClipper", ".equipmentClipper ul");
	superScrollerLR(".additionsClipper", ".additionsClipper ul", ".circleScrollLeft", ".circleScrollRight");
	superScrollerLR(".bodyColorsClipper", ".bodyColorsClipper ul", "#carBodyLArrow", "#carBodyRArrow");
});
