jQuery(document).ready(function() {

    jQuery('<div id="fixBtns">&nbsp;</div><!-- fixBtns -->').prependTo("#c4535");
    jQuery('<a href="#" id="fixPrev">prev</a><a href="#" id="fixNext">next</a><div class="clear"></div>').appendTo("#c4535 #fixBtns");
    //jQuery("#c4535 .carousel ul li").removeAttr("style");

	//rotation speed and timer
	var speed = 5000;
	var run = setInterval('rotate()', speed);	
	
	//grab the width and calculate left value
	var item_width = jQuery('#c4535 .carousel li').outerWidth(); 
	var left_value = item_width * (-1); 
        
    //move the last item before first item, just in case user click prev button
	jQuery('#c4535 .carousel li:first').before(jQuery('#c4535 .carousel li:last'));
	
	//set the default item to the correct position 
	jQuery('#c4535 .carousel ul').css({'left' : left_value});

    //if user clicked on prev button
	jQuery('#fixPrev').click(function() {

		//get the right position            
		var left_indent = parseInt(jQuery('#c4535 .carousel ul').css('left')) + item_width;

		//slide the item            
		jQuery('#c4535 .carousel ul:not(:animated)').animate({'left' : left_indent}, 100,function(){    

            //move the last item and put it as first item            	
			jQuery('#c4535 .carousel li:first').before(jQuery('#c4535 .carousel li:last'));           

			//set the default item to correct position
			jQuery('#c4535 .carousel ul').css({'left' : left_value});
		
		});

		//cancel the link behavior            
		return false;
            
	});

 
    //if user clicked on next button
	jQuery('#fixNext').click(function() {
		
		//get the right position
		var left_indent = parseInt(jQuery('#c4535 .carousel ul').css('left')) - item_width;
		
		//slide the item
		jQuery('#c4535 .carousel ul:not(:animated)').animate({'left' : left_indent}, 100, function () {
            
            //move the first item and put it as last item
			jQuery('#c4535 .carousel li:last').after(jQuery('#c4535 .carousel li:first'));                 	
			
			//set the default item to correct position
			jQuery('#c4535 .carousel ul').css({'left' : left_value});
		
		});
		         
		//cancel the link behavior
		return false;
		
	});        
	
	//if mouse hover, pause the auto rotation, otherwise rotate it
	jQuery('#c4535 .carousel').hover(
		
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('rotate()', speed);	
		}
	); 
        
});

//a simple function to click next link
//a timer will call this function, and the rotation will begin :)  
function rotate() {
	jQuery('#fixNext').click();
}
