
$(document).ready(function(){

	if($('#header').length /* && !$.browser.msie*/) {

		$('#logo-bg').fadeIn('slow',function(){
			$('#logo-bg').fadeOut('slow');
		})

		$('#carousel').css('display','none');
		
		$('#logo-fade').fadeIn('slow',function(){
			
			$('#logo-fade').fadeOut('slow',function(){
			$('#carousel').fadeIn('fast',function(){$('#carousel').goCarousel({easing: 'swing'})})
			});
		})
		
		var $header = $('#header');
		var $footer = $('#footer');
		var $logo = $('#logo');
		
		$logo.bind({
			'mouseover': function(){ $(this).data('over',true); },
			'mouseout': function(){ $(this).data('over',false); }
		}).data('bottom', $('#header').css('bottom'));
		$header.bind({
			'mouseover': function(){ $(this).data('over',!$logo.data('over')); },
			'mouseout': function(){ $(this).data('over',false); }
		}).data('bottom', $('#header').css('bottom'));
		$footer.bind({
			'mouseover': function(){ $(this).data('over',true); },
			'mouseout': function(){ $(this).data('over',false); }
		});
		
		$(document).bind({
			'mousemove': function(){
				if($header.data('over') || $footer.data('over')){
					if(!$header.data('entered'))
						$header.stop().animate({
							'bottom': $footer.height()
						}, 550);
					$header.data('entered', true);
				}else{
					if($header.data('entered'))
						$header.stop().animate({
							'bottom': $header.data('bottom')
						}, 550);
					$header.data('entered', false);
				}
			}
		});
		
	}
	
    // Menu stile
    $('#menu_top').menu();
	
    // Liste fancybox
    $(".fancybox-list").fancyboxList();
    $(".mycarousel").fancyboxList();
				
    // Liste fancybox
    $("a.fancybox").fancybox();

    // Link fancybox
    $("a.fancybox-iframe").fancybox({
        'zoomSpeedIn': 500,
        'zoomSpeedOut': 500,
        'speedIn': 600,
        'speedOut': 200,
        'autoScale': false,
        'type': 'iframe',
        'width': 640,
        'height': $(window).height()*0.85
    });




	if($('.marque').length){	

/* 	$('.marque').vTicker({
           speed: 800,
           pause: 7500,
           showItems: 2,
           animation: ' ',
           mousePause: true
          });	
*/
		  
	} 
	
	
	
	
	
	
	
	// -- Funzioni creazione playlist jPlayer ----------------------------------- //

	var Playlist = function(instance, playlist, options) {
	var self = this;

	this.instance = instance; // String: To associate specific HTML with this playlist
	this.playlist = playlist; // Array of Objects: The playlist
	this.options = options; // Object: The jPlayer constructor options for this playlist

	this.current = 0;

		this.cssId = {
			jPlayer: "jquery_jplayer_",
			interface: "jp_interface_",
			playlist: "jp_playlist_"
		};

	this.cssSelector = {};

	$.each(this.cssId, function(entity, id) {
			self.cssSelector[entity] = "#" + id + self.instance;
	});

	if(!this.options.cssSelectorAncestor) {
		this.options.cssSelectorAncestor = this.cssSelector.interface;
	}

	$(this.cssSelector.jPlayer).jPlayer(this.options);

	$(this.cssSelector.interface + " .jp-previous").click(function() {
		self.playlistPrev();
		$(this).blur();
		return false;
	});

	$(this.cssSelector.interface + " .jp-next").click(function() {
		self.playlistNext();
		$(this).blur();
		return false;
	});

	};



	Playlist.prototype = {

		displayPlaylist: function() {
			var self = this;
			$(this.cssSelector.playlist + " ul").empty();
			for (i=0; i < this.playlist.length; i++) {
				var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last'>" : "<li>";
				listItem += "<a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i +"' tabindex='1'>"+ this.playlist[i].name +"</a>";



				// Create links to  media

				if(this.playlist[i].free) {
					var first = true;
					listItem += "<div class='jp-free-media'>(";
					$.each(this.playlist[i], function(property,value) {
						if($.jPlayer.prototype.format[property]) { // Check property is a media format.
							if(first) {
								first = false;
							} else {
								listItem += " | ";
							}
							listItem += "<a id='" + self.cssId.playlist + self.instance + "_item_" + i + "_" + property + "' href='" + value + "' tabindex='1'>" + property + "</a>";
						}
					});
					listItem += ")</span>";
				}


				listItem += "</li>";

				// Associate playlist items with their media

				$(this.cssSelector.playlist + " ul").append(listItem);
				$(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {
					var index = $(this).data("index");
					if(self.current !== index) {
						self.playlistChange(index);
					} else {
						$(self.cssSelector.jPlayer).jPlayer("play");
					}
					$(this).blur();
					return false;
				});



				// Disable free media links to force access via right click

				if(this.playlist[i].free) {
					$.each(this.playlist[i], function(property,value) {
						if($.jPlayer.prototype.format[property]) { // Check property is a media format.
							$(self.cssSelector.playlist + "_item_" + i + "_" + property).data("index", i).click(function() {
								var index = $(this).data("index");
								$(self.cssSelector.playlist + "_item_" + index).click();
								$(this).blur();
								return false;

							});
						}
					});
				}
			}
		},

		playlistInit: function(autoplay) 
		{

			if(autoplay) {
				this.playlistChange(this.current);
			} else {
				this.playlistConfig(this.current);
			}

		},
		

		
		playlistConfig: function(index) {

			$(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
			$(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
			this.current = index;
			$(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);

		},

		playlistChange: function(index) {
			this.playlistConfig(index);
			$(this.cssSelector.jPlayer).jPlayer("play");

		},

		playlistNext: function() {
			var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
			this.playlistChange(index);

		},

		playlistPrev: function() {
			var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
			this.playlistChange(index);

		}

	};





	var audioPlaylist = new Playlist("myPlayList", [

		{	name:"Je ne veux pas travailler",
			mp3:"templates/frontend/media/je_ne_veux_pas_travailler.mp3", 
			oga:"templates/frontend/media/je_ne_veux_pas_travailler.ogg", 
			m4a:"templates/frontend/media/je_ne_veux_pas_travailler.m4a"
		},
		{	name:"La vie en rose",
			mp3:"templates/frontend/media/la_vie_en_rose.mp3", 
			oga:"templates/frontend/media/la_vie_en_rose.ogg",
			m4a: "templates/frontend/media/la_vie_en_rose.m4a"		
		},
		{	name:"La foule",
			mp3:"templates/frontend/media/la_foule.mp3", 
			oga:"templates/frontend/media/la_foule.ogg",
			m4a:"templates/frontend/media/la_foule.m4a"		
		}

/* 		{	name:"Lentement",
			free:true,
			mp3:"http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3",
			oga:"http://www.jplayer.org/audio/ogg/Miaow-03-Lentement.ogg"
		}
 */

	], {

		ready: function() {

			audioPlaylist.displayPlaylist();
			audioPlaylist.playlistInit(true); // Parameter is a boolean for autoplay.

		},

		ended: function() {

			audioPlaylist.playlistNext();

		},

		play: function() {

			$(this).jPlayer("pauseOthers");

		},

		swfPath: "jquery.jplayer",
		volume: 0.4,
		supplied: "oga, mp3, m4a"

	});
	
	
	
	
});





// -- 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);







// -- Plugin Liste Fancybox ------------------------------------------------- //

(function($){
    $.fn.fancyboxList = function(config) {
        var counter = 0;
        return this.each(function() {
            counter++;
            $(this).find('a').each(function(){
                $(this).attr('rel', 'fancybox-list-'+counter);
                $(this).fancybox(config);
            });
        });
    };
})(jQuery);
	

// -- jCarousel ------------------------------------------------- //

function mycarousel_initCallback(carousel)
		// documentation http://sorgalla.com/projects/jcarousel/
		{
		    // Disable autoscrolling if the user clicks the prev or next button.
		    carousel.buttonNext.bind('click', function() {
		        carousel.startAuto(0);
		    });
		
		    carousel.buttonPrev.bind('click', function() {
		        carousel.startAuto(0);
		    });
		
		    // Pause autoscrolling if the user moves with the cursor over the clip.
		    carousel.clip.hover(function() {
		        carousel.stopAuto();
		    }, function() {
		        carousel.startAuto();
		    });
		};
		
		jQuery(document).ready(function() {
			if(jQuery('.mycarousel').length)
		    jQuery('.mycarousel').jcarousel({
		        auto: 1,
		        wrap: 'circular',
				animation: 2500,
				scroll: 1,
				easing: 'swing', // VAR: easeOutBack, easeOutElastic, swing, linear
				buttonNextHTML: null,
				buttonPrevHTML: null,
		        initCallback: mycarousel_initCallback
		    });
		});
