
var myPlayList = [
	{name:"Je ne veux pas travailler",mp3:"templates/frontend/mp3/je_ne_veux_pas_travailler.mp3", ogg:"templates/frontend/mp3/je_ne_veux_pas_travailler.ogg"},
	{name:"La vie en rose",mp3:"templates/frontend/mp3/la_vie_en_rose.mp3", ogg:"templates/frontend/mp3/la_vie_en_rose.ogg"},
	{name:"La foule",mp3:"templates/frontend/mp3/la_foule.mp3", ogg:"templates/frontend/mp3/la_foule.ogg"},
//	{name:"Tempered Song",mp3:"http://www.miaowmusic.com/mp3/Miaow-01-Tempered-song.mp3",ogg:"http://www.miaowmusic.com/ogg/Miaow-01-Tempered-song.ogg"},
];

var playItem = 0;




$(document).ready(function(){

    // Menu stile
    $('#menu_top').menu();
	
	$('#slideshow').goslide({timeout: 5000});
	$("a.zoom").fancybox();
	$("a.gallery").fancybox();

	$("#slider").slider({
		value: 50,
		max: 100,
		min: 10,
		slide: function(event, ui) {
			$('ul#grid li').css('font-size',ui.value+"px");
		}
	});
	
	$("#jquery_jplayer").jPlayer({
		ready: function () {
			displayPlayList();
			playListInit(true); // Parameter is a boolean for autoplay.
		},
		nativeSupport: true,
		oggSupport: true,
		customCssIds: false
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	$("#jplayer_previous").click( function() {
		playListPrev();
		return false;
	});
	$("#jplayer_next").click( function() {
		playListNext();
		return false;
	});

});





// -- Plugin menu stili ----------------------------------------------------- //

(function($){
    $.fn.menu = function() {

        return this.each(function() {

            $(this).find('li').each(function(){

                var $container = $(this);
                var $trigger = $container.children('a');
                var $menu = $container.children('ul');

                if($menu.length == 0)
                    return true

                $menu.mouseover(function(){
                    $container.data('hover', true);
                }).mouseout(function(){
                    $container.data('hover', false);
                }).css({'display': 'none'});

                $trigger.click(function(event){
                    event.preventDefault();
                    event.stopPropagation();

                    // Chiusura menu
                    if($container.data('open')){
                        $menu.css('display', 'none');
                        $(document).unbind('click.menu');
                        $container.data('open', false);

                    // Apertura menu
                    }else{
                        $menu.fadeIn(200);
                        $container.data('open', true);
                        $(document).bind('click.menu', function(){
                            if(!$container.data('hover')){
                                $menu.css('display', 'none');
                                $(document).unbind('click.menu');
                                $container.data('open', false);
                            }
                        });
                    }
                });
            });
        });
    };
})(jQuery);





// -- Funzioni creazione playlist jPlayer ----------------------------------- //

function displayPlayList() {
	$("#jplayer_playlist").append('<ul>');
	for (i=0; i < myPlayList.length; i++) {
		$("#jplayer_playlist ul").append("<li id='jplayer_playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
		$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
			var index = $(this).data("index");
			if (playItem != index) {
				playListChange( index );
			} else {
				$("#jquery_jplayer").jPlayer("play");
			}
		});
	}
}

function playListInit(autoplay) {
	if(autoplay) {
		playListChange( playItem );
	} else {
		playListConfig( playItem );
	}
}

function playListConfig( index ) {
	$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current");
	$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current");
	playItem = index;
	$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
}

function playListChange( index ) {
	playListConfig( index );
	$("#jquery_jplayer").jPlayer("play");
}

function playListNext() {
	var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
	playListChange( index );
}

function playListPrev() {
	var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
	playListChange( index );
}
