function slideSwitch() {
	
    var $active = $('#slideshow div.active');

    if ( $active.length == 0 ) $active = $('#slideshow div:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow div:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, fadeSpeed, function() {
            $active.removeClass('active last-active');
            $active.css({opacity: 0.0})
        });
}

function quickSlideSwitch(direction) {
	
    var $active = $('#slideshow div.active');

	if (direction == "prev") {
		var $next =  $active.prev().length ? $active.prev()
        : $('#slideshow div:last');
	} else {
 	   var $next =  $active.next().length ? $active.next()
        : $('#slideshow div:first');
    }
    
    $next.css({opacity: 1.0});
    $active.css({opacity: 0.0})
    $next.addClass('active');
	$active.removeClass('active last-active');
}

function saveSetting(speedvalue) {
	$.ajax({
   		type: "POST", 
   		data: "control=slideshow&speed="+speedvalue,
   		url: '/settingscontrol/detail/' + Number(new Date()),
   		success: function(dataBack) {      					   		
   		}
 	}); 
 }
 
$(document).ready(function() {
	ceiling = maxPauseInterval + fadeSpeed + 500;
	var newvalue = ceiling - startingInterval;	
	var slideInterval="";
	slideInterval = window.setInterval( "slideSwitch()", ceiling - startingInterval );
	
	speedstatus = Math.round( ((startingInterval*5)/(maxPauseInterval/1000) / 1000) + .4);
	$("#slideshowcontrols p.status").html("x" + speedstatus);
    
    $("a.next").click(function() {
        window.clearInterval(slideInterval);
        slideInterval = "";
    	quickSlideSwitch("next");
    	slideInterval = window.setInterval("slideSwitch()", newvalue);
    });
    
    $("a.prev").click(function() {
        window.clearInterval(slideInterval);
        slideInterval = "";
    	quickSlideSwitch("prev");
    	slideInterval = window.setInterval("slideSwitch()", newvalue);
    });
    
  
    $("#slider").slider({
    	min: minPauseInterval,
    	max: maxPauseInterval,
    	slide: function(event, ui) {
    		newvalue = (ceiling-ui.value);
    		speedstatus = Math.round( ((ui.value*5)/(maxPauseInterval/1000) / 1000) + .4);
    		$("#slideshowcontrols p.status").html("x" + speedstatus);
    	},
    	stop: function(event, ui) {   	
    		newvalue = (ceiling-ui.value);
        	window.clearInterval(slideInterval);
        	slideInterval = "";        	
        	slideInterval = window.setInterval("slideSwitch()", newvalue );
        	speedstatus = Math.round( ((ui.value*5)/(maxPauseInterval/1000) / 1000) + .4);
        	$("#slideshowcontrols p.status").html("x" + speedstatus);
        	saveSetting(ui.value);
	    }    
    });

    $("#slider").slider('option', 'value', startingInterval); 
 
});

