function toggleImg() {
	var imgs = document.getElementsByClassName('switchable');
	(imgs.length).times(function(i) {
			imgs[i].switchState = switchState;
			imgs[i].onmouseup = function() { imgs[i].switchState(); };
			imgs[i].onmousedown = function() { imgs[i].switchState(); };
		}
	);
}

function switchState() {
	var regexp = new RegExp("_o[nf]{1,2}", "g");
	try {
		var results = regexp.exec(this.src);
		if(results[0] == '_off') {
			this.src = this.src.replace(regexp, "_on");
		} else if(results[0] == '_on') {
			this.src = this.src.replace(regexp, "_off");
		}
	} catch(e) { }
}


function allStateOff() {
	var regexp = new RegExp("_o[nf]{1,2}", "g");
	var imgs = document.getElementsByClassName('switchable');

	for(i=0; i<imgs.length; i++) {
		var results = regexp.exec(imgs[i].src);
		imgs[i].src = imgs[i].src.replace(regexp, "_off");

	}
}

function switchStateOn(element) {
	toggleImg();
	allStateOff();
	var regexp = new RegExp("_o[nf]{1,2}", "g");
	$(element).src = $(element).src.replace(regexp, "_on");
	$(element).onmouseup = null;
	$(element).onmousedown = null;
}

function reload(url) {
	try {
		var tmp = url.split("?");
		var now = new Date();
		this.src = tmp[0] + '?' + now.getTime();
	} catch(e) { }
}

function RTLPlayer(element) {

	if(BrowserDetect.OS != "Windows") {
		this.quicktime = true;
	}

	// Prototype hack : dunno why I can't use $(element) call with IE ...
	this.player = document.getElementById(element);

	this.isPlaying = true;
	this.isMuted = false;
	this.addControl = addControl;
	this.play = play;
	this.pause = pause;
	this.soundUp = soundUp;
	this.soundDown = soundDown;
	this.autoReconnect = autoReconnect;
}

function addControl(obj) {
	var player = this;
	try {
		if(obj.type == 'default') {
			if(obj.state == 'on') {
				$(obj.element).onclick = function() {
						switchStateOn(obj.element);
						eval('player.'+obj.action+'()');
					};
			} else {
				$(obj.element).onclick = function() {eval('player.'+obj.action+'()');};
			}
		} else {
			$(obj.element).onclick = obj.action;
		}
	} catch(e) { }
}

// hack to autoreconnect QT plugin on disconnect
function autoReconnect() {
	if(this.quicktime && BrowserDetect.browser != "Safari") {
		if(this.player.GetPluginStatus().indexOf(",") != -1) {
			this.player.Play();
		}
	}
}

function play() {
	if(!this.isPlaying && this.isMuted) {
		if(this.quicktime) {
			this.player.SetMute(false);
		} else {
			this.player.Mute = false;
		}
//		this.player.Play();
		this.isPlaying = true;
	}
}

function pause() {
	if(this.isPlaying) {
		if(this.quicktime) {
			this.player.SetMute(true);
		} else {
			// IE hacks : Mute seems to down the sound with a random value between 1 and 7 ...
			// need to save and restore the sound level before and after the "mute" action
			var _tmp_ = this.player.Volume;
			this.player.Mute = true;
			this.player.Volume = _tmp_;
		}
		this.isPlaying = false;
		this.isMuted = true;
	}
}


function soundUp() {
	if($('volumeLayer').style.top == '') $('volumeLayer').style.top = "46px";
	var _top_ = parseInt(($('volumeLayer').style.top).replace("px", ""));
	_top_ -= 10;
	if(this.quicktime) {
		if (this.player.GetVolume() + 64 <=448) {
			this.player.SetVolume(this.player.GetVolume() + 64);
			$('volumeLayer').style.top = _top_ + 'px';
		} else this.player.SetVolume(448);

	} else {
		try {
			var volume = this.player.Volume;
			if(volume >= -602 && volume <= -1) {
				this.player.Volume = this.player.Volume + 200;
				$('volumeLayer').style.top = _top_ + 'px';
			} else if(volume < -602) {
				this.player.Volume = this.player.Volume + 725;
				$('volumeLayer').style.top = _top_ + 'px';
			} else {
				this.player.Volume = -2;
			}
		} catch(e) { }
	}
}

function soundDown() {
	if($('volumeLayer').style.top == '') $('volumeLayer').style.top = "46px";
	var _top_ = parseInt(($('volumeLayer').style.top).replace("px", ""));
	_top_ += 10;

	if(this.quicktime) {
		try {
			if (this.player.GetVolume() - 64 >= 0) {
				this.player.SetVolume(this.player.GetVolume() - 64);
				$('volumeLayer').style.top = _top_ + 'px';
			} else this.player.SetVolume(0);
		} catch(e) { }
	} else {
		var volume = this.player.Volume;
		if(volume > -602) {
			this.player.Volume = this.player.Volume - 200;
			$('volumeLayer').style.top = _top_ + 'px';
		} else if(volume <= -602 && volume - 725 > -3503) {
			this.player.Volume = this.player.Volume - 725;
			$('volumeLayer').style.top = _top_ + 'px';
		} else if (volume - 725 <= -3503) {
			this.player.Volume = -3500;
			//$('volumeLayer').style.top = _top_ + 'px';
		}
	}
}

function getData() {
	var now = new Date();
	var url = "streamingMetadata.xml";
	var opts = { method : "get",
		parameters : "tmp="+now.getTime(),
		onComplete : updateDataLayout,
		asynchronous : true };
	var req = new Ajax.Request(url, opts);
}


var doc, timer = 0, cpt = 0;

function updateDataLayout(remote) {
	if(remote.responseXML.documentElement != doc) {
		doc = remote.responseXML.documentElement;
		if (doc && cpt == 0) {
			$("artist").innerHTML = nodeValue(doc, 0, "artist");
			$("title").innerHTML = nodeValue(doc, 0, "title");
			$("cover").src = "cover/" + nodeValue(doc, 1, "picture");
			$("live").innerHTML = "EN CE MOMENT SUR FUNRADIO";
			$("animateur").innerHTML = nodeValue(doc, 0, "startTime")+' - '+nodeValue(doc, 0, "endTime")+' '+nodeValue(doc, 0, "presentator");
		}
	}
}

function nodeValue(node, child, name) {
    var childs = node.getElementsByTagName(name);
    if (childs.length > 0 && childs.item(child).childNodes.length > 0)
        return childs.item(child).childNodes.item(0).nodeValue;
    return "";
}

function fadeLastTitle() {
	Effect.Fade('cover');
	setTimeout("displayLastTitle()", 1000);
}

function fadeTitle() {
	Effect.Fade('cover');
	setTimeout("displayActualTitle()", 1000);
}

function displayLastTitle() {
	if(cpt >= 4) {
		cpt = 0;
		$("live").innerHTML = "EN CE MOMENT SUR FUNRADIO";
	} else {
		cpt++;
		$("live").innerHTML = "DIFFUSE A " + nodeValue(doc, cpt, "broadcastTime");
	}
	try {
		$("artist").innerHTML = nodeValue(doc, cpt, "artist");
		$("title").innerHTML = nodeValue(doc, cpt, "title");
		$("cover").src = "cover/" + nodeValue(doc, cpt +1 , "picture");
		timer = 20;
	} catch(e) { }
	Effect.Appear('cover');
}

function displayActualTitle() {
	$("live").style.visibility = 'visible';
	cpt = 0;
	try {
		$("artist").innerHTML = nodeValue(doc, cpt, "artist");
		$("title").innerHTML = nodeValue(doc, cpt, "title");
		$("live").innerHTML = "EN CE MOMENT SUR FUNRADIO";
		$("cover").src = "cover/" + nodeValue(doc, cpt + 1, "picture");
	} catch(e) { }
	Effect.Appear('cover');
}

function refreshTitle() {
	timer--;
	if(timer <= 0 && cpt != 0) {
		fadeTitle();
	}
}

function partnerLink(partner) {
	var _tmp = cpt;
	//if(cpt - 1 <= 0) _tmp = 0; else _tmp = cpt - 1;
	var _link_;
	switch(partner) {
		case 'fnac':
			_link_ = "http://www3.fnac.com/search/quick.do?Wkos=1&;SearchType=QUICKSEARCH&;RNID=-13&Origin=FUNRADIO&category=-28&text=" + nodeValue(doc, _tmp, "artist") + "+" +nodeValue(doc, _tmp, "album");
			break;

		case 'itunes':
			_link_ = 'http://clk.atdmt.com/APE/go/fnrdffri0040000078ape/direct/01/?href=http://ad.doubleclick.net/clk;26253286;12511023;s?http://clk.tradedoubler.com/click?p=23753&a=1131413&epi=player&url=http%3A%2F%2Fphobos.apple.com%2FWebObjects%2FMZSearch.woa%2Fwa%2FadvancedSearchResults%3FartistTerm%3D'+nodeValue(doc, _tmp, "artist")+'%26songTerm%3D'+nodeValue(doc, _tmp, "title")+'%26s%3D143442%26partnerId%3D2003';
			break;
	}

	blank(_link_);
}

var playerVersion = 1;

if(BrowserDetect.OS == "Mac" && BrowserDetect.browser == "Safari") {
	playerVersion = 3;
}

if(BrowserDetect.OS == "Mac" && BrowserDetect.browser != "Safari") {
	playerVersion = 3;
}

if(BrowserDetect.OS == "Linux") {
	playerVersion = 3;
}

function insertPlayer(index) {
	var htmlPlayer = "";
	switch(index) {
		case 1: // No QuickTime Windows IE
			htmlPlayer = '<ob'+'ject id="NSPlay" name="NSPlay" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" width="0" height="0"\ codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,1,5,217"\ standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">\
			<param name="Filename" value="radio.asx">\
			<param name="AudioStream" value="-1">\
			<param name="AutoSize" value="0">\
			<param name="AutoStart" value="-1">\
			<param name="AnimationAtStart" value="0">\
			<param name="AllowScan" value="-1">\
			<param name="AllowChangeDisplaySize" value="-1">\
			<param name="AutoRewind" value="0">\
			<param name="Balance" value="0">\
			<param name="BaseURL" value> <param name="BufferingTime" value="5">\
			<param name="CaptioningID" value> <param name="ClickToPlay" value="-1">\
			<param name="CursorType" value="0">\
			<param name="CurrentPosition" value="-1">\
			<param name="CurrentMarker" value="0">\
			<param name="DefaultFrame" value> <param name="DisplayBackColor" value="0">\
			<param name="DisplayForeColor" value="16777215">\
			<param name="DisplayMode" value="0">\
			<param name="DisplaySize" value="4">\
			<param name="Enabled" value="-1">\
			<param name="EnableContextMenu" value="-1">\
			<param name="EnablePositionControls" value="0">\
			<param name="EnableFullScreenControls" value="0">\
			<param name="EnableTracker" value="0">\
			<param name="InvokeURLs" value="-1">\
			<param name="Language" value="-1">\
			<param name="Mute" value="0">\
			<param name="PlayCount" value="1">\
			<param name="PreviewMode" value="0">\
			<param name="Rate" value="1">\
			<param name="SelectionEnd" value="-1">\
			<param name="SendOpenStateChangeEvents" value="-1">\
			<param name="SendWarningEvents" value="-1">\
			<param name="SendErrorEvents" value="-1">\
			<param name="SendKeyboardEvents" value="0">\
			<param name="SendMouseClickEvents" value="0">\
			<param name="SendMouseMoveEvents" value="0">\
			<param name="SendPlayStateChangeEvents" value="-1">\
			<param name="ShowCaptioning" value="0">\
			<param name="ShowControls" value="1">\
			<param name="ShowAudioControls" value="1">\
			<param name="ShowDisplay" value="0">\
			<param name="ShowGotoBar" value="0">\
			<param name="ShowPositionControls" value="0">\
			<param name="ShowStatusBar" value="1">\
			<param name="ShowTracker" value="0">\
			<param name="TransparentAtStart" value="-1">\
			<param name="VideoBorderWidth" value="0">\
			<param name="VideoBorderColor" value="0">\
			<param name="VideoBorder3D" value="0">\
			<param name="Volume" value="50000">\
			<param name="WindowlessVideo" value="-1">\
			<embed type="application/x-mplayer2"\ pluginspage="http://www.microsoft.com/isapi/redir.dll?prd=windows&amp;sbp=mediaplayer&amp;ar=Media&amp;sba=Plugin&amp;" src="radio.asx" name="NSPlay" width="0" height="0" showstatusbar="1" showcontrols="1" showdisplay="0" showgotobar="0" autosize="0" showcaptioning="0" enabletracker="0" showtracker="0" enablecontextmenu="0" volume="0" showpositioncontrols="0" showaudiocontrols="1">\
			</ob'+'ject>';
			break;

		case 2: // Quicktime Windows IE
			htmlPlayer = '<ob'+'ject id="NSPlay" CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"  CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab" WIDTH="0" HEIGHT="0" ><PARAM NAME="src" VALUE="funradio.pls" ><PARAM NAME="autoplay" VALUE="true" ><EMBED PLUGINSPAGE="http://www.apple.com/quicktime/download/"                     TYPE="image/x-quicktime" WIDTH="0" HEIGHT="0" QTSRC="funradio.pls" AUTOPLAY="true" ></EMBED></ob'+'ject>';
			break;

		case 3: // QuickTime Windows Not IE && MacOS && Linux
			htmlPlayer = '<ob'+'ject id="NSPlay" type="video/quicktime" data="funradio.pls" width="0" height="0"><param name="src" value="funradio.pls" /><param name="autoplay" value="true" /><param name="controller" value="false" /><param name="cache" value="true" /><param name="href" value="funradio.pls" /><param name="target" value="myself" /></ob'+'ject>';
			break;

		default:
			htmlPlayer = '<ob'+'ject id="NSPlay" name="NSPlay" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" width="0" height="0"\ codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,1,5,217"\ standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">\
			<param name="Filename" value="radio.asx">\
			<param name="AudioStream" value="-1">\
			<param name="AutoSize" value="0">\
			<param name="AutoStart" value="-1">\
			<param name="AnimationAtStart" value="0">\
			<param name="AllowScan" value="-1">\
			<param name="AllowChangeDisplaySize" value="-1">\
			<param name="AutoRewind" value="0">\
			<param name="Balance" value="0">\
			<param name="BaseURL" value> <param name="BufferingTime" value="5">\
			<param name="CaptioningID" value> <param name="ClickToPlay" value="-1">\
			<param name="CursorType" value="0">\
			<param name="CurrentPosition" value="-1">\
			<param name="CurrentMarker" value="0">\
			<param name="DefaultFrame" value> <param name="DisplayBackColor" value="0">\
			<param name="DisplayForeColor" value="16777215">\
			<param name="DisplayMode" value="0">\
			<param name="DisplaySize" value="4">\
			<param name="Enabled" value="-1">\
			<param name="EnableContextMenu" value="-1">\
			<param name="EnablePositionControls" value="0">\
			<param name="EnableFullScreenControls" value="0">\
			<param name="EnableTracker" value="0">\
			<param name="InvokeURLs" value="-1">\
			<param name="Language" value="-1">\
			<param name="Mute" value="0">\
			<param name="PlayCount" value="1">\
			<param name="PreviewMode" value="0">\
			<param name="Rate" value="1">\
			<param name="SelectionEnd" value="-1">\
			<param name="SendOpenStateChangeEvents" value="-1">\
			<param name="SendWarningEvents" value="-1">\
			<param name="SendErrorEvents" value="-1">\
			<param name="SendKeyboardEvents" value="0">\
			<param name="SendMouseClickEvents" value="0">\
			<param name="SendMouseMoveEvents" value="0">\
			<param name="SendPlayStateChangeEvents" value="-1">\
			<param name="ShowCaptioning" value="0">\
			<param name="ShowControls" value="1">\
			<param name="ShowAudioControls" value="1">\
			<param name="ShowDisplay" value="0">\
			<param name="ShowGotoBar" value="0">\
			<param name="ShowPositionControls" value="0">\
			<param name="ShowStatusBar" value="1">\
			<param name="ShowTracker" value="0">\
			<param name="TransparentAtStart" value="-1">\
			<param name="VideoBorderWidth" value="0">\
			<param name="VideoBorderColor" value="0">\
			<param name="VideoBorder3D" value="0">\
			<param name="Volume" value="50000">\
			<param name="WindowlessVideo" value="-1">\
			<embed type="application/x-mplayer2"\ pluginspage="http://www.microsoft.com/isapi/redir.dll?prd=windows&amp;sbp=mediaplayer&amp;ar=Media&amp;sba=Plugin&amp;" src="radio.asx" name="NSPlay" width="0" height="0" showstatusbar="1" showcontrols="1" showdisplay="0" showgotobar="0" autosize="0" showcaptioning="0" enabletracker="0" showtracker="0" enablecontextmenu="0" volume="0" showpositioncontrols="0" showaudiocontrols="1">\
			</ob'+'ject>';
			break;
	}

	$('player').innerHTML = htmlPlayer;
}
function blank (url) {
	window.open(url,'',"toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes")
}