$(document).ready(function(){
	//Instantiate APE Client
	var client = new APE.Client();
	
	//Load APE Core
	client.load();
	
	//Intercept 'load' event. This event is fired when the Core is loaded and ready to connect to APE Server
	client.addEvent('load', function() {
	    //Call core start function to connect to APE Server
	    client.core.start();
	});
	
	//4) Listen to the ready event to know when your client is connected
	client.addEvent('ready', function() {
	    //1) join 'testChannel'
	    client.core.join('hitradio');
	
	    //4) Intercept the reception of new message.
	    client.onRaw('songchanged', function(raw, pipe) {
	        title = unescape(raw.data.title);
	        artist = unescape(raw.data.artist);
	        albumart = unescape(raw.data.albumart);
	        time = unescape(raw.data.time);
	        newsong(artist, title, albumart, time);
	    });
	});
});
$(function(){
	
	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);
	  }
	}
	
    newsong = function(artist, title, albumart, time) {
	    $(".widget.w2 div.songs div.song:first").removeClass("current");
	    $(".widget.w2 div.songs div.song:last").remove();
	    newsong_elm = $("<div class=\"song current\">" +
	        "<div class=\"time\">"+time+"</div>" +
	        "<div class=\"albumart\">" +
	        "<img src=\""+albumart+"\" alt=\"pochette\" style=\"width: 40px;\"/>" +
	        "</div>" +
	        "<div class=\"infos\">"+
	        "<p class=\"artist\">"+artist+"</p>" +
	        "<p class=\"title\">"+title+"</p>" +
	        "</div>" +
	        "</div>");
	    $(".widget.w2 div.songs div.song:first").before(newsong_elm);
	    $(".widget.w2 div.songs div.song:first").show("slide", {duration: 1500});
    }
	
	$("#playvideo").click(function(){
		$("#videothumb").hide();
		$("#playvideo").hide();
		$("#video").show();

	    var params = { allowScriptAccess: "always", menu: false };
	    var atts = { id: "myytplayer" };
	    swfobject.embedSWF("http://www.youtube.com/v/KqRwxxhK5Bo?enablejsapi=1&playerapiid=ytplayer", 
	                       "flash", "450", "290", "8", null, null, params, atts);
		return false;
	});

	// Embed flash banner if supported
	swfobject.embedSWF("/banner.swf", "flashbanner", "988", "213", "8", null, null, {menu: false});

	var current_ad = 0;
	var current_ad_timer = 0;

	switch_ad = function(id,callback) {
		$('#mini'+current_ad).removeClass("current");
		current_ad = id;
		current_ad_timer = id;
		$('#mini'+current_ad).addClass("current");
		$('#pubmini').scrollTo( '#mini'+current_ad, 1000, {offset:-50});
		
		_rotate_timer.stop();
		$("#adpic").fadeOut(500, function() {
			$("#adpic").attr('src', pubs[id]['img']);
			$("#adpic").fadeIn(500);
		});
		$("#adtext").fadeOut(500, function() {
			$("#adtext .line1").html(pubs[id]['title']);
			$("#adtext .line2").html(pubs[id]['desc']);
			$("#adtext").fadeIn(500);
			_rotate_timer.reset(8000);
		});
	}

	var _rotate_timer = null;
	function rotate_ads_timer() {
		_rotate_timer = $.timer(8000, function(timer){
			_rotate_timer = timer;
			current_ad_timer++;
			if (current_ad_timer == current_ad)
				current_ad_timer++;
			if (current_ad_timer >= pubs.length) {
				current_ad_timer = 0;
			}
			switch_ad(current_ad_timer);
		});
	}
	rotate_ads_timer();
	
	
	var current_entry = null;
	$("#menu .entry").hover(function() {
		current_entry = $(this);
		$(this).css("background-image", "url('/images/bouton02.jpg')");	
		setTimeout(function() { third_state(); }, 70);
	});
	$("#menu .entry").mouseout(function() {
		if (current_entry)
		  current_entry.css("background-image", "none");
		current_entry = null;
	});
	function third_state(elm) {
		if (current_entry)
			current_entry.css("background-image", "url('/images/bouton01.jpg')");
	}
	
	$("#showmentions").click(function() {
		show_in_grayshape($("#mentions"));
		return false;
	});

	$("#showpubcontact").click(function() {
		show_in_grayshape($("#contact"));
		return false;
	});

	$("#grayshape").click(function() {
		hide_grayshape();
	});
	
	var current_module_in_grayshape = null;
	show_in_grayshape = function(elm){
		$("#flashbanner").hide();
		elm = elm.clone();
		$("#grayshape .content").append(elm);
		current_module_in_grayshape = elm;
		current_module_in_grayshape.show();
		$("#grayshape").fadeIn(500);
	}
	show_img_in_grayshape = function(url){
		show_in_grayshape($('<img src="'+url+'" alt=""/>'));
	};
	hide_grayshape = function() {
		$("#grayshape").fadeOut(500, function() {
			if (current_module_in_grayshape) {
				current_module_in_grayshape.remove();
			}
			$("#flashbanner").show();
		});
	}
});

function onYouTubePlayerReady(playerid) {
    ytplayer = document.getElementById("myytplayer");
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    ytplayer.playVideo();
}

function onytplayerStateChange(newState) {
   if (newState == 0) {
	   $("#video").hide();
	   $("#videothumb").show();
	   $("#playvideo").show();
   }
}
