function hash()
{
	if(location.hash != "")
	{
		window.location.href = self.document.location.hash.substring(2);
	}
}
window.onload = function()
{
	hash();
}
/* Menu */
window.addEvent('domready', function()
{
	$$('#menu > ul > li > ul').setStyle('display', 'none');
	$$('#menu > ul > li').each(function(el)
	{
		el.addEvents(
		{
			'mouseover':function(e)
			{
				e.stop();
				this.getChildren('ul').setStyle('display', 'block');
				
			},
			'mouseout':function(e)
			{
				e.stop();
				this.getChildren('ul').setStyle('display', 'none');
				
			}
		});
	});
});

/* Ajax */
window.addEvent('domready', function() {
	$$('.ajax').addEvent('click', function() {
		if(this.get('href') == '/')
		{
			var url = 'home.inc.php';
			var url_rewrite = '#!/';
			var url_stats = '/';
		}
		else
		{
			var url = this.get('href').replace('.html', '.inc.php');
			var url_rewrite = '#!/' + this.get('href');
			var url_stats = this.get('href');
		}
		var req = new Request.HTML({
			url:url,
			evalScripts:false,
			evalResponse:false,
			onSuccess: function(html) {
				$('container').set('html', '');
				$('main').set('html', this.response.html);
				$exec(this.response.javascript);
				
			},
			onFailure: function() {
				$('leftColumn').set('text', 'The request failed.');
			}
		});
		window.location.href = url_rewrite;
		pageTracker._trackPageview(url_stats);
		req.send();
		return false;
	});
});

/* Titrage page */
function refresh_mdata()
{
	var req = new Request.HTML(
	{
		url:'/includes/titrage.php', 
		onSuccess: function() {
			var data = this.response.text.split('|');
			$('cover').set('src', data[0]);
			$('cover').set('alt', data[1]+' - '+data[2]);
			var urlinfo = 'title-'+data[1]+' - '+data[2]+'.html';
			var urlinfo2 = urlinfo.replace(/ /g, '+');
			var urllink = urlinfo2.replace('&', 'AND_');
			$('coverlink').set('href', urllink);
			$('artist').set('text', data[1]);
			$('title').set('text', data[2]);
			$$('#lastone a').set('text', data[3]);
			$$('#lasttwo a').set('text', data[4]);
			var twitterLink = 'http://twitter.com/home?status=J\'écoute '+data[1]+' - '+data[2]+' sur http://c9.fr %23nowplaying';
			$$('.twitterShare').set('href', twitterLink.replace(/ /g, '+'));
			setTimeout("refresh_mdata()", 30000);
		}
	});
	req.send();
}
setTimeout("refresh_mdata()", 30000);


/* Stop on quit (crash prevent) */
window.onunload = function()
{
	$('fmp256').playpause(0);
}

/* Titrage navigateur */
var playStart = 0;
function refresh_title()
{
	var titlepage = $('cover').get('alt');
	if(playStart == 1)
	{
		document.title = titlepage+' - C9 Radio - Radio numérique française';
		setTimeout("refresh_title()", 30000);
	}
	else
	{
		document.title =  'C9 Radio - Radio numérique française';
	}
}

/* Controle player */
window.addEvent('domready', function() {
	if ( $('play') ) {
		$('play').addEvent('click', function() {
			if(playStart == 0)
			{
				this.getChildren('img').set('src', '/images/picto/stop.png');
				this.getChildren('img').set('alt', 'Stop');
				this.set('title', 'Stop');
				playStart = 1;
				refresh_title();
				$('fmp256').playpause(1);
				$('fmp256').volume(30);
			}
			else
			{
				this.getChildren('img').set('src', '/images/picto/play.png');
				this.getChildren('img').set('alt', 'Play');
				this.set('title', 'Play');
				playStart = 0;
				document.title = 'C9 Radio - Radio numérique française';
				$('fmp256').playpause(0);	
			}
			return false;
		});
	}
});

window.addEvent('domready', function() {
	
	$('webchat').addEvent('submit', function(e)
	{
		nick=$('pseudo').get('value');
		if(nick=="" || nick=="Pseudo")
		{
			alert("Veuillez saisir un pseudo.");
		}
		else
		{
			targ='http://chat.c9.fr/?nick='+nick;
			window.open(targ,'c9chat','top=1,left=1,width=990,height=680,toolbar=no,scrollbars=no,status=no');
		}
		return false;
	});
	
});


