$(function(){
			// Reset all scrollable panes to (0,0)
		
		
		var first = $('#media li:first').clone();
		var last = $('#media li:last').clone();
		
		$(first).insertAfter("#media li:last");
		$(last).insertBefore("#media li:first");
		
		$("#mediaframe").scrollTo( $("#media li.active"),0 );

		
		$('#mediabox_navigation a').click(function(){
		
		var box = $("#media li.active");
		
		var smer = $(this).attr("class");
		
		$('#media p').fadeOut(200);

		$('#media li').each(function(){
		$(this).removeClass("active");
		});
		
		
		
		/*$('#mediatext p').each(function(){
		$(this).fadeOut(400).removeClass("active");
		});
		
		$(this).parent().attr("class","active");
		$('h2.'+$(this).attr("href").replace("#","")).delay(400).fadeIn(400);
		$('p.'+$(this).attr("href").replace("#","")).delay(400).fadeIn(400);
		*/
		
		if (smer=="next")
		{
		if ( box.next("li").length === 0)
		{
			$("#mediaframe").scrollTo($('#media li:first').next("li"),0);
			$("#mediaframe").scrollTo($('#media li:first').next("li").next("li"), 800, {onAfter:function(){ $('#media li:first').next("li").next("li").attr("class","active");  $('#media li:first').next("li").next("li").children("p").fadeIn(800); }} );
		}
		else { $("#mediaframe").scrollTo(box.next("li"), 800, {onAfter:function(){ box.next("li").attr("class","active");  box.next("li").children("p").fadeIn(800); }} ); }
		}
		
		else
		{
		if ( box.prev("li").length === 0)
		{
			$("#mediaframe").scrollTo($('#media li:last').prev("li"),0);
			$("#mediaframe").scrollTo($('#media li:last').prev("li").prev("li"), 800, {onAfter:function(){ $('#media li:last').prev("li").prev("li").attr("class","active");  $('#media li:last').prev("li").prev("li").children("p").fadeIn(800); }} );
		}
		else { 	$("#mediaframe").scrollTo(box.prev("li"), 800 , {onAfter:function(){ box.prev("li").attr("class","active");  box.prev("li").children("p").fadeIn(800); }} ); }
		}
		return false;
		});
		
		
		rotate = function(){
		
		$('#media li').each(function(){
		$(this).removeClass("active");
		$('#media p').fadeOut(200);
		});
		
		$active.attr("class","active"); //Add active class (the $active is declared in the rotateSwitch function)
		//$active.children("p").fadeIn(800);

		$("#mediaframe").scrollTo($active, 800,  {onAfter:function(){ $active.children("p").fadeIn(800); }} );
	
	}; 
	
	rotateSwitch = function(){
    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
        $active = $('#media li.active').next("li"); //Move to the next paging
        if ( $active.length === 0) { //If paging reaches the end...
			$("#mediaframe").scrollTo( $('#media li:first').next("li"),0 );
            $active = $('#media li:first').next("li").next("li"); //go back to first
        }
        rotate(); //Trigger the paging and slider function
    }, 4000); //Timer speed in milliseconds (7 seconds)
};

rotateSwitch();


$("#mediabox").hover(function() {
    clearInterval(play); //Stop the rotation
}, function() {
    rotateSwitch(); //Resume rotation timer
});


});
