var base_url = 'http://www.rcn-radio.org/'
//var base_url = 'http://127.0.0.1/rcn/'


$(document).ready(function()
{
	$("#coups_de_coeur .slider").jCarouselLite({mouseWheel: true,speed:700,easing: "easeout",visible: 7});
	// Datepicker recherche de titre
		$('#title_date').datepicker({dateFormat: "dd-mm-yy"}, $.datepicker.regional['fr']);
	//Placement du "cache coup de coeur"
		$('#coups_de_coeur').append('<div id="cache"></div>');
	
	//Slider
		$.ajax(
			{
				url:base_url + "home/slider",
				success:function(response)
				{
					$('#slider_images').prepend(response);
					$('#slider_images').innerfade({
							animationtype: 'fade',
							speed: 5000,
							timeout: 8000,
							type: 'random'
					});
					
				}
			});
	// Login
	$("#lien_enregistrement").append('<a id="close" class="login_toggle" style="display: none;"  >Fermer</a>');
	
	$("#open").click(function(){
		$("#user_box").animate({ top: 0}, 600);	
		$("#close").css({"display":"block"});
		$("#open").css({"display":"none"});
		return false;	
	});	
	
	$("#close").click(function(){
		$("#user_box").animate({ top: -537}, 600);	
		$("#close").css({"display":"none"});
		$("#open").css({"display":"block"});
		return false;	
	});	
	
	// Compte
	$("#user_bar #user_bar_btn").append('<a id="compte_close" class="login_toggle" style="display: none;"  >Fermer</a>');
	
	$("#user_bar #parametres").click(function(){return false;})
	$("#user_bar #feeds").click(function(){return false;})
	
	// Concours
	$("#user_bar #jeux").click(function()
	{
		$("#user_bar_data").fadeOut("slow");
		
		$.ajax({
			url: base_url + 'concours/concours/concours/',
			success : function(response)
			{
				$("#user_bar_data").html(response);
				$("#user_bar_data").fadeIn("slow");
				triggerConcours();
			}
		})	
		return false;
	});
	
	
	function triggerConcours()
	{
		$("#user_bar .participer").bind('click', function()
		{
			var id = this.id;
			var self = $(this);
			$(this).css({"background": "url("+ base_url + "themes/rcn/img/ajax-loader.gif) center 0 no-repeat"});
			$.post(base_url + "concours/concours/participer/", {concours_id: id} , 
			function()
			{
				self.removeClass("participer");self.addClass("enregistre");
				self.css({"background": "url("+ base_url + "themes/rcn/img/concours.jpg) 0 -70px no-repeat"});
				self.unbind('click');
			});
		})
	}
	
	
	$("#compte_open").click(function(){
		$("#user_bar").animate({bottom: 0}, 600, function(){$("#compte_close").css({"display":"block"});
		$("#compte_open").css({"display":"none"});});	
		return false;	
	});	
	
	$("#compte_close").click(function(){
		$("#user_bar").animate({ bottom: -537}, 600);	
		$("#compte_close").css({"display":"none"});
		$("#compte_open").css({"display":"block"});
		return false;	
	});	
		
	// Coups de coeur
	
	$('#coups_de_coeur a').bt({
							offsetParent:'body', 
							positions: 'bottom', 
							spikeLength: 8,
							spikeGirth: 10,
							cornerRadius: 5,
							fill: 'rgba(0, 0, 0, 1)',
							strokeWidth: 2,
							cssStyles: {color: '#FFF', fontWeight: 'bold', fontSize: '0.8em', textAlign: 'center'}
						});
						
	$("#coups_de_coeur .cc").each(function(){this.href = base_url + 'cc/show/' + this.id;})
	$("#coups_de_coeur .cc").click(function(){return false;})
	$("#coups_de_coeur .cc").fancybox({'overlayOpacity' : 0.6,'overlayColor' : '#000'});
	
	//Sliders
	$('#news_list').jslider({url : base_url + "news/news/getJson/"})
	$('#podcasts_list').jslider({url : base_url + "podcasts/podcasts/getJson/", success_callback : podcasts_options})
	
	$('#invites_list').jslider({url : base_url + "invites/invites/getJson/", background_color:'none',
																			 pagination_bottom:'5px',
																			 pagination_width:'290px' 
																			 })
	//Evenenements
	var bg = '<div style="background:#000000;text-align:center;width:100%;position:absolute;bottom:0" class="loader"><img src="themes/rcn/img/ajax-loader.gif" /></div>';
	bg = $(bg);
	bg.hide();
	$('#events_list').append(bg);
	$('#events_list p').remove();
	$('#events_list table').css({'margin-top':'120px'})
	function loadEvents()
	{
		$('#events_list th a').click(
		function()
		{
			var self = this;
			var href = this.href.split('/');
			var nbSegments = href.length;
			href = base_url + "events/events/ajax_events/" + href[4] + "/" + href[5] + "/";
			bg.show();
			$.ajax({
				url: href,
				success : function(response)
				{
					$('#events_list').html(response);
					$('#events_list').append(bg);
					bg.hide();
					loadEvents();
				}
			})
			return false;
		})
		triggerEvents()
	}
	loadEvents();
	
	function triggerEvents()
	{
		$('#events_list .hasEvent').mouseover(function(){
		bg.show();
		var href = this.href.split("#");
		href = href[0].split('/');
		href = base_url + "events/events/ajax_events_list/" + href[4] + "/" + href[5] + "/" + href[6] + "/";
		$.ajax({
			url: href,
			success : function(response)
			{
				var liste = $(response);
				$('#events_list').append(liste);
				liste.fadeIn('slow');
				bg.hide();
				liste.mouseleave(function(){liste.fadeOut('slow')})
			}
		})	
	});
	}

	//Podcasts
	function podcasts_options()
	{
		var shown = false;
		
		$('.titre', '#podcasts_list .main').click(function(){return false;})
		
		$("#podcasts_list .slider").mouseenter(
		function()
		{
			$("#podcasts_list #informations_conteneur").animate({
				height: 376
			}, 200, function(){
				shown = true;
			});
		}).mouseleave(function(){
				$("#podcasts_list #informations_conteneur").html('').animate({
					height: 0
				}, 200, function(){
					shown = false;
				});
		})

		$("#podcasts_list ul li").mouseenter(function()
		{	
			$("div.podcasts_options", $(this)).stop(true, true).show().animate({left:0}, 300);
			var contenu = $(".informations_cache", $(this)).html();
			$("#informations_conteneur").html(contenu);
			
			$(this).mouseleave(function()
			{
				$("div.podcasts_options", $(this)).animate({left:220}, 300);
			});
		});
		
		$('.ecouter', '.podcasts_options').click(function(){	
				file = this.href;
				$('.player_container').fadeIn("slow");
				$('#player').jPlayer("setFile", file).jPlayer("play");
				return false;
	 	 })
	}
	
	$(".live_in").click(function(){
		file = this.href;
		$('.player_container').fadeIn("slow");
		$('#player').jPlayer("setFile", file).jPlayer("play");
		return false;
	})

	//Player
	player()
	function player(){
		var jpPlayTime = $("#play_time");
		var jpTotalTime = $("#total_time");
		var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

		$("#player").jPlayer({
			ready: function () {
				
			},
			nativeSupport : false
		})
		.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
			jpPlayTime.text($.jPlayer.convertTime(playedTime));
			jpTotalTime.text($.jPlayer.convertTime(totalTime));
	 	})
		.jPlayer("onSoundComplete", function() {
			$('.player_container').fadeOut("slow");
		});
		$('.player_container').draggable();
	}
	
	
	//Titrage
	$.fn.longPoll({
			onSuccess : function(a)
  		   	{
  		   		var html = a.artiste+" - "+ a.titre;
				$("#on_air").html(html);
  		   	}
  	});
});