$.fn.cycle.defaults.speed   = 900;
$.fn.cycle.defaults.timeout = 6000;

var pages = 6;
var currentPage = 0;
var pageTo = 1;
var t;
var scroll = "scrollRight";

$(function()
	{
    	$('#pages').cycle({ 
	        fx:      'scrollRight',
		    click:   '#page',
		    timeout:  0,
		    after:	nextPage,
		    before: scrollDirection,
	        startingSlide: 0
	    });
	    $('#link1').click(function()
    	{ 
        	gotoPage(1); 
   		});
    	$('#link2').click(function()
    	{ 
        	gotoPage(2); 
   		});
   		$('#link3').click(function()
    	{ 
        	gotoPage(3); 
   		});
   		$('#link4').click(function()
    	{ 
        	gotoPage(4); 
   		});
   		$('#link5').click(function()
    	{ 
        	gotoPage(5);  
   		});
   		$('#link6').click(function()
    	{ 
        	gotoPage(6); 
   		});
});

function scrollDirection(curr,next,opts)
{
	opts.changeFX(scroll);
}

function gotoPage(id)
{
	pageTo = id;
	
	$('#select'+currentPage).css("background-color","#f8f8f8");
	$('#select'+id).css("background-color","#ed1c24");
		
	nextPage();
}

function cycleTo(id)
{
	if(currentPage <= pages && currentPage != 0)
	{
		id--;
		$('#pages').cycle(id);
    	return false;
    }
}

function nextPage()
{
	if(currentPage != 0)
	{
		bk = currentPage;
		if(pageTo > currentPage)
		{
			scroll = "scrollRight";
			currentPage++;
		}
		else if(pageTo < currentPage)
		{
			scroll = "scrollLeft";
			currentPage--;		
		}
		cycleTo(bk);
		cycleTo(currentPage);
	}
	else currentPage = 1;
}