var slideshowTimer = null;
$(document).ready(function(){
    
   if($('#rotator').size() > 0){
		doSlideshow('rotator', true, 10000, 'nav');
    
        $('#nav li a').click(function(){
            var currObj = $('#rotator li.current');
            var nextObj = $('#rotator li#' + $(this).attr('rel') + '');  
            $('#nav li').removeClass('active');
            $(this).parent().addClass('active');
            
            nextObj.addClass('current');
            nextObj.css('opacity', 0).css('z-index','2');
            currObj.removeClass('current').css('z-index','1');
            currObj.animate({opacity:0}, 2000);
            nextObj.animate({opacity:1}, 2000);
            $('.button a').attr('href', nextObj.find('.hide').attr('href'));
            
            return false;
        });
    
	}    
  
});

function isVisible(obj) { return (obj.css('display') == 'block'); } 

function doSlideshow(objid, start, time, navId){
	if(!$('#' + objid) || !isVisible($('#' + objid))){
		if(slideshowTimer != null){
			clearTimeout(slideshowTimer);
			slideshowTimer = null;
		}
		return;
	}
	
	if(start){
		clearTimeout(slideshowTimer);
		slideshowTimer = null;
	}
	
	var currObj = $('#' + objid + ' li.current');
	var nextObj = currObj.next();
	if($('#' + objid + ' li:last').hasClass('current')){ 
		nextObj = $('#' + objid + ' li:first');
	}
    
    nextObj.addClass('current');
	nextObj.css('opacity', 0).css('z-index','2');
	currObj.removeClass('current').css('z-index','1');
	currObj.animate({opacity:0}, 2000);
	nextObj.animate({opacity:1}, 2000);
    
    
    if(navId != ''){        
        $('#' + navId + ' .'+currObj.attr('id')).removeClass('active');
        $('#' + navId + ' .'+nextObj.attr('id')).addClass('active');
    }
	
	slideshowTimer = setTimeout('doSlideshow("' + objid + '", false, ' + time + ',"' + navId + '");', time);
}
