var ADS_DEBUG = false;
			
var DIRECTION = {
    LeftToRight: 1, 
    RightToLeft: -1, 
    TopToBottom: 2, 
    BottomToTop: -2
};

function swapFrame(what, inc, manuel){
				
    var config = diapo[what];
    var newFrame, oldFrame;
    
    if (config.action) return;
    config.action = true;
    
    oldFrame = config.current;
    newFrame = oldFrame + inc;
    
	//alert("swapFrame [" + what + "," + oldFrame + "," + newFrame + "," + manuel + "]" );
	
    if (ADS_DEBUG) console.debug("swapFrame [" + what + "," + oldFrame + "," + newFrame + "," + manuel + "]" );
    
    // calcul de la frame suivante
    if ( newFrame < 1 ) newFrame = config.total;
    if ( newFrame > config.total ) newFrame = 1;
    // annule l'automatisme
    if ( manuel && config.timer ) clearTimeout( config.timer ); 
    // animation
    if (oldFrame == newFrame){
        // 1 er affichage
        $('#' + config.id + '-' + newFrame).css("left", "0px").css("top", "0px");
        config.action = false;
    }else{
        direction = config.direction;
        if (inc<0) direction = -direction;
        var axe, pos1, pos2;
        switch (direction){
            case DIRECTION.LeftToRight:
                axe= "left"; pos1 = "-" + config.width + "px"; pos2 = config.width + "px"; break;
            case DIRECTION.RightToLeft:
                axe= "left"; pos1 = config.width + "px"; pos2 = "-" + config.width + "px"; break;
            case DIRECTION.TopToBottom:
                axe= "top"; pos1 = "-" + config.height + "px"; pos2 = config.height + "px"; break;
            case DIRECTION.BottomToTop:
                axe= "top"; pos1 = config.height + "px"; pos2 = "-" + config.height + "px"; break;
            default:
                axe= "left"; pos1 = "-" + config.width + "px"; pos2 = config.width + "px"; break;
        }
        if (ADS_DEBUG) console.debug("swapFrame [" + what + ",[oldFrame " + oldFrame + " " + axe + " 0->" + pos2 + "]," + ",[newFrame " + newFrame + " " + axe + " " + pos1 + "->0]]" );

        if ( Math.abs( config.direction ) == 1 ){
            $('#' + config.id + '-' + newFrame).css( axe, pos1).css("top", "0px");
            $('#' + config.id + '-' + newFrame).animate( {left: "0px"}, config.duration );
            $('#' + config.id + '-' + oldFrame).animate( {left: pos2}, config.duration, function(){config.action = false;} );
        }else{
            $('#' + config.id + '-' + newFrame).css( axe, pos1).css("left", "0px");
            $('#' + config.id + '-' + newFrame).animate( {top: "0px"}, config.duration );
            $('#' + config.id + '-' + oldFrame).animate( {top: pos2}, config.duration, function(){config.action = false;} );
        }
    }
     
    config.current = newFrame;
    jsMajNavigation(config.id, newFrame - 1);
	
    config.timer = setTimeout( "swapFrame('" + what + "', 1, false)", config.timeout );
};

function swapFrameOpacity_(what, iFrame_, manuel){
	var config = diapo[what];
	var iInc = (iFrame_+1) - config.current; //(config.current >= iFrame_) ? (config.current - iFrame_) :
	if (ADS_DEBUG) console.debug("swapFrameOpacity_ [" + config.current + "," + iFrame_ + "," + iInc + "]" );
	swapFrameOpacity( what, iInc, true);
}
function swapFrameOpacity(what, inc, manuel){
				
    var config = diapo[what];
    var newFrame, oldFrame;
    
    if (config.action) return;
    config.action = true;
    
    oldFrame = config.current;
    newFrame = oldFrame + inc;
    
	//alert("swapFrame [" + what + "," + oldFrame + "," + newFrame + "," + manuel + "]" );
	
    if (ADS_DEBUG) console.debug("swapFrameOpacity [" + what + "," + oldFrame + "," + newFrame + "," + manuel + "]" );
    
    // calcul de la frame suivante
    if ( newFrame < 1 ) newFrame = config.total;
    if ( newFrame > config.total ) newFrame = 1;
    // annule l'automatisme
    if ( manuel && config.timer ) clearTimeout( config.timer ); 
    // animation
    if (oldFrame == newFrame){
        // 1 er affichage
        $('#' + config.id + '-' + newFrame).fadeIn(config.duration);
        config.action = false;
    }else{
        if (ADS_DEBUG) console.debug("swapFrameOpacity [oldFrame " + oldFrame + "]," + ",[newFrame " + newFrame + "]" );

		$('#' + config.id + '-' + newFrame).fadeIn( config.duration );
		$('#' + config.id + '-' + oldFrame).fadeOut( config.duration, function(){config.action = false;} );
	
    }
     
    config.current = newFrame;
    jsMajNavigation(config.id, newFrame - 1);
	
    config.timer = setTimeout( "swapFrameOpacity('" + what + "', 1, false)", config.timeout );
};

function jsHorShowNavigation(conf){
	oConf = diapo[conf];
	// console.debug("ICI ", oConf);
	$("#" + oConf.id + " .pub").each( function(i){
		// console.debug("image " + i);
		$("#nav-" + oConf.id).append("<span><a href='javascript:swapFrameOpacity_(\"" + conf + "\", " + i + ");'>" + (i+1) + "</a></span>")
	});
}

function jsMajNavigation(id_, iFrame_){
	// on met à jour la navigation
	if (ADS_DEBUG) console.debug("jsMajNavigation: " + id_ + "==" + iFrame_);
	$('#nav-' + id_ + " a").removeClass('on');
	$('#nav-' + id_ + " a").each( function(i){
		if (ADS_DEBUG) console.debug("i : " + i);
		if (i == iFrame_){ 
			$(this).addClass('on');
			return false;
		}
	});
}