// Google analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24010027-1']);
_gaq.push(['_trackPageview']);

// Classic Rap
var cover_alt = "http://classicrap.fr/wp-content/themes/classic-rap/images/on-air/cover-unknown.jpg";
var vote_up = "http://classicrap.fr/wp-content/themes/classic-rap/images/on-air/up.png";
var vote_down = "http://classicrap.fr/wp-content/themes/classic-rap/images/on-air/down.png";

function setCurrentPageItem()
{
	var elements = document.getElementsByTagName("li");
	for(var i = 0; i < elements.length; i++)
	{
		if(elements[i].className == "page_item page-item-335")
			elements[i].className = "page_item page-item-335 current_page_item";
	}
}

function openPlayerPopup(url)
{
	window.open(url, "popUpWindow","height=218,width=165,top=0,left=0");
}

$(document).ready(function()
{
	loadOnAirContent();
	$("#marquee-content").marquee({yScroll: "top", pauseSpeed: 500, pauseOnHover: true, loop: -1});
	$('#s1').cycle({fx: 'fade', speed: 800, prev: '.news-back', next: '.news-forward', timeout: 5000});	
	loadFacebook();
});

$(function()
{
	$('.dedicaces').click(function() { $("#dedicaces-foo").toggle("slow"); });
	$('input:submit[name=dedicaces-post]').click(function() {
		challenge = $("#recaptcha_challenge_field").val();
		response = $("#recaptcha_response_field").val();
		nom = $("#dedicaces-nom").val();
		message = $("#dedicaces-com").val();
		$.post("http://classicrap.fr/wp-content/themes/classic-rap/dedicaces.php",
			{ nom: nom, commentaire: message, recaptcha_challenge_field: challenge, recaptcha_response_field: response },
			function(data){ $("#dedicaces-foo").html(data);
		});
	});

	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	
	$('.letter').hide();
	$('#letter-NB').show();
});

function showLetter(letter)
{
	$('.letter').hide();
	$(letter).show();
}

function loadFacebook()
{
	var fb = document.getElementById("fb-like");
	var e = document.createElement('fb:like-box');

	if(fb != null)
	{	
		e.setAttribute('href', 'http://www.facebook.com/pages/Classic-Rap/119749068044268');
		e.setAttribute('show_faces', 'false');
		e.setAttribute('stream', 'false');
		e.setAttribute('header', 'true');
		e.setAttribute('style', 'background-color: #efefef');
		e.setAttribute('width', '293');
		e.setAttribute('height', '70');
		fb.appendChild(e);
	}
}

function loadOnAirContent()
{
	$.ajax({type: "GET", url: "http://classicrap.fr/wp-content/themes/classic-rap/currentsong.php",
	success: function(xml)  { setTimeout(updateOnAirContent(xml), 3000); },
	error: function() { setTimeout(loadOnAirContent, 3000); }
	});
}

function loadOnAirVoteContent(url)
{
	$.get(url, function(data) { $("#on-air-vote").html(data); });
}

function updateOnAirContent(xml)
{
	var callmeback = $(xml).find("callmeback").text();
	if(parseInt(callmeback) == 0)
		callmeback = 30000; 
	setTimeout(loadOnAirContent, callmeback);

	var cover = $(xml).find("cover").text();
	var title = $(xml).find("artists").text();
	title = title + " - " + $(xml).find("title").text();
	
	var event = $(xml).find("event").text();

	if(cover == "")
		cover = cover_alt;

	// Generate HTML code
	var html = '<div id="on-air-event"><p>' + event +'</p></div>';
	html = html + '<div id="on-air-cover"><div id="on-air-play" onClick="JavaScript:openPlayerPopup(\'http://classicrap.fr/player\');"></div>';
	html = html + '<img id="cover" src="" height="171" width="164" /></div>';
	html = html + '<div id="on-air-track"><p id="on-air-track-title"></p>';
	html = html + '<p id="on-air-vote">';
	html = html + '<a href="#"><img src="' + vote_up + '" onClick="loadOnAirVoteContent(\'http://classicrap.fr/wp-content/themes/classic-rap/load-vote.php?vote=yes\')" /></a>';
	html = html + '<a href="#"><img src="' + vote_down + '" onClick="loadOnAirVoteContent(\'http://classicrap.fr/wp-content/themes/classic-rap/load-vote.php?vote=no\')" /></a>';
	html = html + '</p></div>';

	$("#header-right").html(html);
	$("#on-air-track-title").html(title);
	$("#cover").attr("src", cover);
}

function clearInput(input)
{
  if(!input._changed) input.value='';
  input._changed=true;
}
