
(function($){

    $.fn.trombinoscope = function()
    {
	$(this).attr('id', 'cadre_photo');

	//initialisation
	$(this).find('ul img').each(function(){$(this).hide();});
	$(this).find('.face').hide();
	$(this).find('.part_trombi').each(function(){$(this).show();});
	$(this).find('.com_name').each(function(){$(this).hide();});

	var nb = $('#cadre_photo ul:eq(0) li').size();
	$(this).attr('tag', nb);


	//generation aleatoire ==============================
	$(this).find('li div').each(function(){
	    $(this).hide();
	});

	var nb_photos = $(this).attr('tag');

	var tab = new Array();
	for(var i = 0; i < nb_photos; i++)
	{
	    tab[i] = i;
	}
	for(; i < 20; i++)
	    tab[i] = Math.floor(Math.random() * nb_photos);
	tab = melanger(tab);
	//alert(tab);
	

	i = 0;

	$(this).children('ul').each(function(){
	    $(this).children('li:eq(' + tab[i] + ')').children('div').show();
	    i++;
	});

	//bind ==============================================
	$(this).find('.part_trombi').bind('click', click_part_trombi);
	$(this).find('.part_trombi').parent().bind({
	    mouseenter: mouseenter_part_trombi,
	    mouseleave: mouseleave_part_trombi
	});
	$(this).find('.com_name').bind('click', click_com_name);
	$(this).find('.face').bind('click', click_face);
	$(this).find('#btn_reload').bind('click', reload_trombi);
	$(document).everyTime(6000, 'timerTrombi', changeRandomPicture);


	//fonctions bind =====================================
	function click_part_trombi()
	{
	    if($(this).next().html() == '')
		return false;

	    $('#cadre_photo .face').attr('src', $(this).attr('alt')).attr('alt', $(this).next().html())
	    $('#cadre_photo .part_trombi').each(function(index){
		if($('#cadre_photo .part_trombi').size() == index + 1)
		    $(this).fadeOut(function(){
			$('#cadre_photo .face').fadeIn();
		    });
		else
		    $(this).fadeOut();
	    });

	    $('#cadre_photo').children('ul').each(function(){$(this).hide();});

	    var fichename = '#fiche_' + $(this).attr('tag');
	    $('#desc_content').html($(fichename).html());

	    return false;
	}

	function mouseenter_part_trombi()
	{
	    var com_object = $('#cadre_photo .com_name[coord="'+$(this).find('img:eq(0)').attr('coord')+'"]');
	    //com_object.css('left', com_object.width()+'px').show();
	    //com_object.css('left', '3px').show();
	    if(com_object.html() != '')
		com_object.show();
	}

	function mouseleave_part_trombi()
	{
	    $('#cadre_photo .com_name[coord="'+$(this).find('img:eq(0)').attr('coord')+'"]').hide();
	}

	function click_com_name()
	{
	    $(this).parent().find('img:eq(0)').click();
	}

	function click_face()
	{
	    $(this).fadeOut("fast", function(){
		$('#cadre_photo .part_trombi').each(function(){
		    $(this).fadeIn();
		});
	    });

	    var fichename = '#fiche_base';
	    $('#desc_content').html($(fichename).html());

	    $('#cadre_photo').children('ul').each(function(){$(this).show();});
	}


	function reload_trombi()
	{
	    $(document).stopTime('timerTrombi');
	    $('#btn_reload').unbind('click');

	    var delay = 0;
	    $('#cadre_photo').find('li div:visible').each(function(){
		delay++;
		$(this).delay(50 * delay).fadeOut((20 - delay) * 50);
	    });

	    var nb_photos = $('#cadre_photo').attr('tag');

	    var tab = new Array();
	    for(var i = 0; i < nb_photos; i++)
	    {
		tab[i] = i;
	    }
	    for(; i < 20; i++)
		tab[i] = Math.floor(Math.random() * nb_photos);
	    tab = melanger(tab);
	    

	    delay = 0;
	    $('#cadre_photo').children('ul').each(function(){
		$(this).children('li:eq(' + tab[delay] + ')').children('div').delay(((50 * delay) + 1100)).fadeIn((20 - delay) * 50);
		delay++;
	    });

	    $(document).everyTime(6000, 'timerTrombi', changeRandomPicture);

	    $('#btn_reload').bind('click', reload_trombi);

	    return false;
	}

	function changeRandomPicture()
	{
	    if(!$('#s_equipe').is(':visible') || $('#trombinoscope').is(':visible'))
		return;

	    var nb_photos = $('#cadre_photo').attr('tag');

	    var rand = Math.random() * 20;
	    rand = Math.floor(rand);

	    var rand_2 = Math.random() * nb_photos;
	    rand_2 = Math.floor(rand_2);

	    var ul = $('#cadre_photo').children('ul:eq(' + rand + ')');
	    ul.children('li').children('div:visible').fadeOut('fast', function(){
		ul.children('li:eq(' + rand_2 + ')').children('div').fadeIn('fast');
	    });

	}

	function melanger(tab)
	{
	    var it = tab.length - 1;
	    var rand = 0, tmp = 0;
	    for(; it >= 0; it--)
	    {
		rand = Math.floor(Math.random() * it);
		tmp = tab[it];
		tab[it] = tab[rand];
		tab[rand] = tmp;
	    }
	    return tab;
	}

    };

})( jQuery );



$(document).ready(function(){
    //chargement en ajax
    $.post('ajax_trombinoscope.php', {}, function(html){
	$('#cadre_photo').append(html);
    });
})



