//<![CDATA[

// Désolé, Sam, c'est horrible, aaaarghhh :
if (/MSIE 6/i.test(navigator.userAgent)) {
	var btn_vote = "/emergences/files/images/btn-vote.gif";
	var btn_vote_cancel = "/emergences/files/images/btn-vote-cancel.gif";
} else {
	var btn_vote = "/emergences/files/images/btn-vote.png";
	var btn_vote_cancel = "/emergences/files/images/btn-vote-cancel.png";
}

// create flash player's event listener
var evl = new Object();

// player functions
evl.play = function() {
	if (this.parentEl) {
		this.parentEl.addClassName('playing');
		this.parentEl.removeClassName('paused');
	}
	this.forwardEl.show();
	this.fplay.SetVariable("enabled", "true");
	this.fplay.SetVariable("method:play", "");
	this.isPlaying = true;
	this.playOrPause = evl.pause;
};

evl.pause = function() {
	if (this.parentEl) {
		this.parentEl.addClassName('paused');
		this.parentEl.removeClassName('playing');
	}
	this.forwardEl.hide();
	this.fplay.SetVariable("method:pause", "");
	this.fplay.SetVariable("enabled", "false");	
	this.isPlaying = false;
	this.playOrPause = evl.play;
};

// listener's builtin events 
evl.onInit = function() {
	this.position = 0;
	this.el = null;
	this.parentEl = null;
	this.forwardEl = null;
	this.playOrPause = evl.play;
};

evl.onUpdate = function() {
	if(this.parentEl){
		meter = Math.ceil(this.parentEl.offsetWidth * this.position / this.duration) - 1000;
		this.parentEl.style.backgroundPosition = meter+"px 0";
	}
	// play next track
	if(this.isPlaying == 'false') {
		id = this.el ? parseInt(this.el.id.substr(1)) + 1: 0;
		track = $('t'+id);
		if(!track) track = $('t0');
		track.fire('play:next');
		if(timeinterval) emReleaseForward();
	}
};

// when the user presses play
function emClick(event) {

	// catch the flash player
	if(! evl.fplay) {
		evl.fplay = $('fplay');
	}	
	
	// catch the clicked element
	var el = event.element();
	el.blur();
	
	// load the song if it's not loaded yet
	if(!evl.el || evl.el.href != el.href) {
		if(evl.parentEl) {
			evl.parentEl.removeClassName('playing');
			evl.parentEl.removeClassName('paused');
		}
		if(evl.forwardEl) evl.forwardEl.hide();
		evl.fplay.SetVariable("method:setUrl", el.href);
		evl.el = el;
		evl.parentEl = el.up(".mediarow");
		evl.forwardEl = el.next("a");
		evl.playOrPause = evl.play;
	}
	
	// play or pause the song
	evl.playOrPause();
	
	event.stop();
}


// forward a song
var timeinterval = null;

function emForward() {
	if(evl.isPlaying) {
		evl.fplay.SetVariable("method:setPosition",
							  (parseInt(evl.position) + 30000) + '');
	}
}

function emPressForward() {
	timeinterval = window.setInterval("emForward()", 500);
	emForward(); /* FD : Comme ça les gens voient qq chose...*/
}

function emReleaseForward() {
	window.clearInterval(timeinterval);
}


// vote hijacking
function castVote(e) {
	e.stop();
	e.element().request({
		onSuccess: displayVote.bind(e.element())
	});
}

function displayVote(transport, el) {
	v = parseInt(transport.responseText);
	em = this.up().down('em');
	em.update(v);
	span = this.up().down('span');
	span.update(v > 1 ? 'votes': 'vote');
	action = this.readAttribute('action').split("?", 2);
	action[0] = action[0].sub(/\/[a-z-]+\/$/, '/');
	submit = this.up().down('input', 1);
	if(this.readAttribute('class') == 'upvote') {
		this.writeAttribute('class', 'clearvote');
		this.writeAttribute('action', action[0] + 'annuler-vote/' + '?' + action[1]);
		submit.writeAttribute('class', 'vote-cancel');
		submit.value = "annuler";
	} else {
		this.writeAttribute('class', 'upvote');
		this.writeAttribute('action', action[0] + 'voter/' + '?' + action[1]);
		submit.writeAttribute('class', 'vote-process');
		submit.value = "voter";
	}
}

// comment vote
function commentVote(e) {
	e.stop();
	el = e.element();
	new Ajax.Request(el.href, {
		onSuccess: displayCommentVote.bind(el)
		});
}

function displayCommentVote(transport, el) {
	v = parseInt(transport.responseText);
	html = "&#x25B2; ";
	if (this.readAttribute('class') == 'down') html = "&#x25BC; ";
	html += "<strong>" + v + "</strong>";
	html += v > 1 ? 'pts': 'pt';
	this.up().update(html);
}

// when the DOM is ready, please
document.observe("dom:loaded", function() {
	// catch clicks on links with the "track" class
	$$(".track").invoke('observe', 'click', emClick
		   ).invoke('observe', 'play:next', emClick);
	$$(".forward").invoke('observe', 'mousedown', emPressForward
		     ).invoke('observe', 'mouseup', emReleaseForward
		     ).invoke('hide');
});


//]]>

