FC.vars = {
    current: 0,    
    currentT: 0
}





$(function(){
	$("body").addClass("js");
	
	if($("div.carousel").length>0){
		scroller($('div.carousel'));
	}

	
})



// Scroller */
var scroller = function($scroller) {
	
    var __scrollAmount = 1, __scrollIndex = 0, __axis = { axis: 'x' };
	
    var __controls = (FC.hasLteIE7) ? (function() { var __$tmp = $("div.carousel"); return __$tmp[0] })() : document.body.querySelector("div.carousel");
	
    var __$container = $scroller.find("div.carousel-view-pane");
	var __$down, __$up, __$mDown, __$mUp;

    __$down = $("a.carousel-next", __controls);
    __$up = $("a.carousel-prev", __controls);

    __$container.css('overflow-x', 'hidden');

    __$down[0].style.display = __$up[0].style.display //= __$mDown[0].style.display = "block";

    var __$contentBoxes = $("li", __$container[0]);
    var __length = (__$contentBoxes.length-1);
    var __max = __scrollAmount * Math.ceil(__length / __scrollAmount);
	var __lastScroll = Math.floor(__length / __scrollAmount)*__scrollAmount;
	
    //if the number of boxes is the same as the scroll amount - then hide the 'next' arrow - it's not needed
    if ((__length + 1) <= __scrollAmount) {
        __$down.hide();
    }

    //Make sure the scroller is at the start
    __$container.scrollTo('li:eq(0)', 200, __axis);

    // show/hide the prev buttons
    __$up.hide();
    //__$mUp.hide();

    __$up.bind("click", function() {//Left scroller button
        if (FC.vars.currentT > 0) {
			
			$(document).stopTime("Scroller");
			
            __scrollIndex = __scrollIndex - __scrollAmount;
            FC.vars.currentT = __scrollIndex + __scrollAmount - 1;
            __$container.scrollTo('li:eq(' + __scrollIndex + ')', 500, __axis);
            __$down.show();
			
			updateStages();

            if ((FC.vars.currentT === 0) || (__scrollIndex == 0)) {
                __$up.hide();
            }
			
			auto();

        }
        return false;
    });
	
	
    __$down.bind("click", function() { //Right scroller button				   
								   
        if (FC.vars.currentT < __max) {
			
			$(document).stopTime("Scroller");
			
            __scrollIndex = __scrollIndex + __scrollAmount;
            FC.vars.currentT = __scrollIndex;
            __$container.scrollTo('li:eq(' + __scrollIndex + ')', 500, __axis);
            __$up.show();
			
			updateStages();

            if ((FC.vars.currentT + __scrollAmount) > __max) {
                __$down.hide();
            }
			
			auto();

        }

        return false;
    });
	
	//Build stage indicators
	// chris - adjust left margin according to number of images
	var margin_left=165;
	var dif=10-__$contentBoxes.length;
	// move dots along by 10px per image less than 10
	margin_left=margin_left+(dif*10);
	
	$("div.carousel").append("<ul id='stages' style='left:"+margin_left+"px'></ul>")
	for(i=0;i<__$contentBoxes.length;i++){
		var __label = i+1;
		$("ul#stages").append("<li id='stage"+i+"'>"+__label+"</li>");
	}
	$("li#stage0").addClass("selected");
	
	
	//Events for the stage indicators
	var __stages = $("ul#stages li");
	__stages.bind("click",function(){
		
		$(document).stopTime("Scroller");
		
		var __item = parseInt(($(this).text()));
		__item--;
		__scrollIndex = __item;
		FC.vars.currentT = __item;
		
		__$up.show();
		__$down.show();
		
		if ((FC.vars.currentT === 0) || (__scrollIndex == 0)) {
                __$up.hide();
        }
		
		
		if ((FC.vars.currentT + __scrollAmount) > __max) {
                __$down.hide();
        }
		
		__$container.scrollTo('li:eq(' + __item + ')', 500, __axis);
		updateStages();
		auto();
		
	});
	
	
	//Update the stage indicator
	function updateStages(){
		
		var __str = "stage"+__scrollIndex;
		$("ul#stages li").removeClass("selected");
		$("li#"+__str).addClass("selected");
		
		
	}
	
	//Auto Scrollers
	function auto(){
		$(document).everyTime(5000, "Scroller", function(){
			if (FC.vars.currentT < __max) {
				__$down.click();
			}
			else 
				if (FC.vars.currentT == __max) {
					FC.vars.currentT = 0;
					__scrollIndex = 0;
					//Make sure the scroller is at the start
					__$container.scrollTo('li:eq(0)', 200, __axis);
					// show/hide the prev buttons
					__$up.hide();
					__$down.show();
					updateStages();
				}
		});
	}
	auto();

}

