var IntervalId = 0;
var Interval = 2000;

var $j = jQuery.noConflict();
var first = 0;

$j(document).ready(function(){
	
	IntervalId = setTimeout ( "slideSwitch()", Interval );
    
    $j(".img IMG").hover(
    	function () 
    	{ 
    		clearTimeout( IntervalId ); 
    	},
    	function () 
    	{ 
    		IntervalId = setTimeout ( "slideSwitch()", Interval );
    	}
    );
    
    $j(".numbers DIV").hover(
        	function () 
        	{ 
        		slidePause(this);
        	},
        	function () 
        	{ 
        		IntervalId = setTimeout ( "slideSwitch()", Interval );
        	}
        );
});

var slidePause = function(elt)
{
	clearTimeout( IntervalId ); 

	var $active = $j('.slideshow DIV.active');
	var $active_n = $j('.numbers DIV.active');

	var index = $j(elt).html();

	var $next =  $j('#slide-'+index);		
	var $next_n =  $j(elt);

	$active.addClass('last-active');
    
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 300, function() {
            $active.removeClass('active last-active');	    		
        });	   
    
    $next_n.css({opacity: 0.3}).addClass('active').animate({opacity: 1.0}, 300);
    
    $active_n.css({opacity: 1.0})
    	.animate({opacity: 0.3}, 300, function() {
       	$active_n.removeClass('active last-active');		
    });
    
};

var slideSwitch = function() {
	
	var $active = $j('.slideshow DIV.active');
	var $active_n = $j('.numbers DIV.active');
	
    var $next =  $active.next().length ? $active.next() : $j('.slideshow DIV:first');		
	var $next_n =  $active_n.next().length ? $active_n.next() : $j('.numbers DIV:first');

	$active.addClass('last-active');
    
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 500, function() {
            $active.removeClass('active last-active');	    		
    });	   
    	   
    
    $next_n.css({opacity: 0.3}).addClass('active').animate({opacity: 1.0}, 500);
    
    $active_n.css({opacity: 1.0})
    	.animate({opacity: 0.3}, 500, function() {
       	$active_n.removeClass('active last-active');		
    });
    
    IntervalId = setTimeout ( "slideSwitch()", Interval );
		
};

var rewind = function()
{
	$j('.numbers DIV').show();
};

var get_first_visible = function()
{
	var visible = $j('.numbers DIV:visible');
	
	if(visible.length > 0) {
		return visible[0];
	}
	
	return null;
};

var scroll_left = function()
{
	var numbers = $j('.numbers DIV');	
	var invisible = $j('.numbers DIV').not(':visible');
	
	var list = $j('.numbers DIV:visible');
	var last = $j(numbers[numbers.length-1]).offset();
	var first = $j(list[0]).offset();
	
	if(last.top != first.top) {
		if(list.length > 1) {
			list[0].hide();		     
		};
	};
};

var scroll_right = function()
{
	var list = $j('.numbers DIV').not(':visible');
	if(list.length > 0) {
		list[list.length-1].show();		     
	};
};
	
	