$(function(){
  
  $('.fancyboxlink').each(function(i, elm){
    $(elm).fancybox({
      href: $(elm).attr("data-href"),
      transitionIn: "elastic"
  	});
	});
  
  $('*[data-ajax-render=true]').each(function(id, elm) {
    // #TODO : partial view rendering based on data-ajax-render parameter of links
  });
  
  launch_player = function() {
    $("#jqplayer").jPlayer("play");
    $('#player').addClass("opened");
    
    setTimeout(function(){
      $('#player').CreateBubblePopup($.extend(popup_options, {
        innerHtml: "Vous pouvez contrôler la lecture ici"
      })).ShowBubblePopup();
    }, 500);
    
    setTimeout(function() {
      $('#player').removeClass("opened");
      $('#player').HideBubblePopup();
      $("#player").RemoveBubblePopup();
    }, 4000);
  }

  popup_options = {
    position : 'top',
    align	 : 'center',
    innerHtmlStyle: {
					    color:'#FFFFFF', 
					    'text-align':'center'
				    },
    themeName: 	'all-black',
    themePath: 	'images/jquerybubblepopup-theme',
    themeMargins: {
      total: '5px',
      difference: '3px'
    }
  };
  
  $('a[title], *[data-popup]').each(function() {
    var $this = $(this);
    var title = $this.attr("data-popup");
    if (!title) {
      title = $this.attr("title");
      $this.removeAttr("title"); // Remove attribute to avoid double popups
    }
    
    popup = $this.CreateBubblePopup($.extend(popup_options, {
	    innerHtml: title
    }));

    if ($this.hasClass("widget") && $this.hasClass('little')) {
      setTimeout(function() {
        $('.widget.little').ShowBubblePopup($.extend(popup_options, {
          innerHtml: title, 
          position: 'bottom', 
        }), false);
        setTimeout(function() {
          $('.widget.little').HideBubblePopup();
        }, 3000);
      }, 1000);
    }    
  });
  
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
	  var args_len = arguments.length;
	  for (var i = args_len; i--;) {
	    var cacheImage = document.createElement('img');
	    cacheImage.src = arguments[i];
	    cache.push(cacheImage);
	  }
	}
	
  $(window).hashchange(function(){
  });

  newsong = function(artist, title, albumart, time) {
    if ($('#OnAirSearch').size() > 0) return;
    
    if ($('div.songs.onair').size() > 0) {
      $("div.songs.onair div.song:first").removeClass("current");
      $("div.songs.onair div.song:last").remove();

      newsong_elm = $("<div class=\"song current\">" +
          "<div class=\"time\">"+time+"</div>" +
          "<div class=\"albumart\">" +
          "<img src=\""+albumart+"\" style=\"width: 40px;\"/>" +
          "</div>" +
          "<div class=\"infos\">"+
          "<p class=\"artist\">"+artist+"</p>" +
          "<p class=\"title\">"+title+"</p>" +
          "</div>" +
          "</div>");

      if ($('.playerpub').size() > 0) {
    	    $('.playerpub').fadeOut(function() {
    	        $('.playerpub').remove();
        	    $('#onair_link').show();
        	    $('div.songs.onair').show();
              $("div.songs.onair div.song:first").before(newsong_elm);
              $("div.songs.onair div.song:first").show("slide", {duration: 1500});
    	    });
      } else {
          $("div.songs.onair div.song:first").before(newsong_elm);
          $("div.songs.onair div.song:first").show("slide", {duration: 1500});
      }
    }
    
    if ($('div#player.bar').size() > 0) {
      infos = $("div#player.bar .albumart figcaption");
      if (infos.length) {
        $('div#player.bar .albumart').fadeOut();
        img = $("<img src=\""+albumart+"\"/>");
        $('div#player.bar .albumart img').replaceWith(img);

        $("h1", infos).html(artist);
        $("h2", infos).html(title);
        $('div#player.bar .albumart').fadeIn();
      }
      
      infos = $("div#player.bar div.infos:first");
      if (infos.length) {
        infos.fadeOut(function() {
          $("p.artist", infos).html(artist);
          $("p.title", infos).html(title);
          infos.fadeIn();
        });
      }
    }

	}
	
	showpub = function(id) {
  }
	
});
