// supporting variables
var holderWidth = 960;
var itemWidth = 320;
var currentPage = 1;
var positionLeft = 0;
var totalPages = 0;

// jquery code runs after DOM is loaded
$(document).ready(function() {
    // handle scrolling container
	var totalItems = $("#top_feature_scroller div.top_layout_holder > div").length;
	var totalWidth = totalItems * itemWidth;
	if (totalWidth > holderWidth)
    {
		var totalWidth = totalItems*itemWidth;
		totalPages = totalWidth / $("div.top_layout_features").width();
		// make holder wide enough to hold items
		$("#top_feature_scroller div.top_layout_holder").css({width:totalWidth});
		
		// add left and right button
		$("#top_feature_scroller div.top_layout_features").before("<a href='javascript:void(0);' onclick='scrollingPrev();' class='top_layout_button_left'><span>Previous</span></a>");
		$("#top_feature_scroller div.top_layout_features").after("<a href='javascript:void(0);' onclick='scrollingNext();' class='top_layout_button_right'><span>Next</span></a>");
	
		
		adjustArrows();
	}

    //setInterval(autoScroll, 12000);

});

// supporting functions
function autoScroll() {
	if (currentPage < totalPages) {
		scrollingNext();
	} else {
		$("#top_feature_scroller div.top_layout_holder").animate({ "left": "0px" }, "slow");
		currentPage = 1;
	}

}
function scrollingPrev() {
	if (currentPage > 1) {
		$("#top_feature_scroller div.top_layout_holder").animate({ "left": "+=" + holderWidth + "px" }, "slow");
		currentPage--;
		adjustArrows();
	}
	//console.log("previous; currentPage = "+currentPage);
}
function scrollingNext() {
	if (currentPage < totalPages) {
		$("#top_feature_scroller div.top_layout_holder").animate({ "left": "-=" + holderWidth + "px" }, "slow");
		currentPage++;
		adjustArrows();
	}
	//console.log("next; currentPage = "+currentPage);
}
function adjustArrows() {
	if (currentPage < totalPages && currentPage > 1) {
		$("#top_feature_scroller a.top_layout_button_right").fadeTo("slow", 1);
		$("#top_feature_scroller a.top_layout_button_left").fadeTo("slow", 1);
	} else if (currentPage < totalPages && currentPage == 1) {
		$("#top_feature_scroller a.top_layout_button_right").fadeTo("slow", 1);
		$("#top_feature_scroller a.top_layout_button_left").fadeTo("slow", 0.3);
	} else if (currentPage == totalPages && currentPage > 1) {
		$("#top_feature_scroller a.top_layout_button_right").fadeTo("slow", 0.3);
		$("#top_feature_scroller a.top_layout_button_left").fadeTo("slow", 1);
	} else if (currentPage == totalPages && currentPage == 1) {
		$("#top_feature_scroller a.top_layout_button_right").fadeTo("slow", 0.3);
		$("#top_feature_scroller a.top_layout_button_left").fadeTo("slow", 0.3);
	}
}

