$(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("/popup/mentions");
		return false;
	});

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

	$("#grayshape").click(function(eventObject) {
		if ($(eventObject.target).attr("id") == "grayshape")
			hide_grayshape();
	});
	
	show_in_grayshape = function(url){
		$("#flashbanner").hide();
		
		$.get(url, function(data) { 
			$('#grayshape .content').html(data); 
			$("#grayshape").fadeIn(500);
		});
		
	}
	show_img_in_grayshape = function(url){
		$("#flashbanner").hide();
		$('#grayshape .content').html($('<img src="'+url+'" alt=""/>')); 
		$("#grayshape").fadeIn(500);
	};
	hide_grayshape = function() {
		$("#grayshape").fadeOut(500, function() {
			$("#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();
   }
}
