function GetCookie(name)
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function SetCookie(name,value)
{
	document.cookie = name + "=" + escape (value);
}

var nb = 1, nb_max = 4, change_time = null, already_load = 0, last_nb;

function launch_carrousel()
{
        change_time = setInterval("changeStoryAuto()", 5000);

        cookie = GetCookie('carousel'+carousel_id);
        if( cookie == null ) {
                nb = 1;
        } else {
                nb = cookie;
                already_load = 1;
        }

	last_nb = nb;

        changeStory(nb);

	$("#a"+nb).css({ backgroundImage: "url(/images/carousel/fond2.png)"});
	
	for( i = 1; i <= nb_max; i++ ) {
		if( i != nb ) { $("#a"+i).css({ backgroundImage: "url(/images/carousel/fond1.png)"}); }
	}

	$("#carousel_load").css({ display: "none"});
	$("#carousel").css({ display: "block"});
}

function changeStoryAuto()
{
	if( nb < nb_max ) { nb++; } else { nb = 1; }
	already_load = 0;
	changeStory(nb);
}

function changeStory(id_nb, manual)
{
		// Verification changement manuel
		if( manual == 1 ) {
			nb = id_nb;
			clearInterval(change_time); change_time = null;
			change_time = setInterval("changeStoryAuto()", 5000);
		}
	
		$("#t"+last_nb).css({ display: "none"});
		$("#a"+last_nb).css({ backgroundImage: "url(/images/carousel/fond1.png)"});

		$("#a"+id_nb).css({ backgroundImage: "url(/images/carousel/fond2.png)"});

		if( manual != 1 && already_load != 1 && lightbox_open != 1 )
		{ $("#t"+id_nb).fadeIn(1250); }
		else
		{ $("#t"+id_nb).css({ display: "block"}); }

		// Sauvegarde du numéro actuel 1-4
		SetCookie('carousel'+carousel_id, id_nb);

		last_nb = id_nb;
}

function send_link()
{
	document.location = array_url[(nb-1)];
	return false;
}
