var easeFunc = "easeInOutQuint";
var duration = 750;
var box = 1;   // remembering current box number
var intarbal = 5000;
var boxCount;
var boxWidth;

var increment = function(){
    box++;
    if(box > boxCount)
    {
        // select the first box
        box = 1;
    }
    pageTo(box);
}

var decrement = function(){
    box--;
    if(box < 1)
    {
        // select the last box
        box = boxCount;
    }
    pageTo(box);
}

var pageTo = function(page){
    // update index
    $("#indexHolder > li.current").removeClass("current");
    $("#indexHolder > li:contains('" + page + "')").addClass("current");

    $("#boxwrapper").animate(
        {left: - (page - 1) * parseInt(boxWidth) },
        duration,
        easeFunc
    );
    
    // reset auto paging
    $(document).stopTime("paging");
    $(document).everyTime(intarbal, "paging", increment);
}

$(document).ready(function(){
    // INITIALIZE
    boxCount = $("#boxwrapper > div").length;
    boxWidth = $(".sliderBox").width();
	if(boxCount < 2){
		$(".switch").remove();
		$("#indexHolder").remove();
		return;
	}
    for(i = 0; i < boxCount; i++)
    {
        $('<li><a href="#">' + (i + 1) + "</a></li>").appendTo("#indexHolder");
    }
    $("#indexHolder").css( { "left" : boxWidth - $("#indexHolder").width() } );
    var wrapperWidth = boxCount * boxWidth;
    $("#boxwrapper").width(wrapperWidth);
    $("#switchLeft").click(decrement);
    $("#switchRight").click(increment)
    $("#indexHolder > li > a").click(function(){
        pageTo(this.innerHTML);
    });
    pageTo(1);
    $(document).everyTime(intarbal, "paging", increment);
});
