////////////////////////////////////////////////////////////
// Gadget IMS RADIO PLAYER V2 - Vista Widget Engine
// gadget.js      
// ©2007 InfoMed Solutions (www.infomedsolutions.com)
////////////////////////////////////////////////////////////

var stationNames;
var stationURLs;
var currentStation;

function loadCurrentStation()
{
	var tempName = System.Gadget.Settings.read("stationName");
	var currentURL = "";
	if (tempName != "")
	{
		currentURL = getURLByStationName(tempName);
	}
	if (currentURL == "") // station par défaut si pas choisie
	{
		currentStation = stationNames[0];
		currentURL = stationURLs[0];
	}
	else
	{
		currentStation = tempName;
	}
	setMediaPlayer(currentURL);
}

function getURLByStationName(stationName)
{
	for (i=0;i<stationNames.length;i++)
	{
		if (stationNames[i] == stationName)
		return stationURLs[i];
	}
	return "";
}

function setMediaPlayer(stationURL)
{
	mediaPlayer.url = stationURL;
	updateNowPlaying();
}

function initStationList()
{
	stationNames = new Array();
	stationURLs = new Array();
	stationIMGs = new Array();
	
	stationNames[0] = "Contact FM";
    stationURLs[0]  = "http://213.186.39.130:8000/live64.m3u";
	stationIMGs[0] = "img/0.png";
	
	stationNames[1] = "Nostalgie";
	stationURLs[1]  = "http://player.nostalgie.fr/V4/nostalgie/nostalgie.asx";
	stationIMGs[1] = "img/1.png";
	
	stationNames[2] = "Fun Radio";
	stationURLs[2]  = "http://radio.funradio.fr/funradio.asx";
	stationIMGs[2] = "img/2.png";
	
	stationNames[3] = "Voltage";
	stationURLs[3]  = "http://www.tv-radio.com/cgi-bin/tagger.pl?tag=site&metafile=voltage/voltage-20k.asx";
	stationIMGs[3] = "img/3.png";
	
	stationNames[4] = "Rire et chansons";
	stationURLs[4]  = "http://player.rireetchansons.fr/V4/rireetchansons/rireetchansons.asx";
	stationIMGs[4] = "img/4.png";
	
	stationNames[5] = "Cherie FM";
	stationURLs[5]  = "http://player.cheriefm.fr/V4/cheriefm/cheriefm.asx";
	stationIMGs[5] = "img/5.png";
	
	stationNames[6] = "Skyrock";
	stationURLs[6]  = "http://vip1.yacast.fr/encoderskyrock/.WMA?MSWMExt=.asf";
	stationIMGs[6] = "img/6.png";
	
	stationNames[7] = "NRJ";
	stationURLs[7]  = "http://player.nrj.fr/V4/nrj/nrj.asx";
	stationIMGs[7] = "img/7.png";

	stationNames[8] = "Oui FM";
	stationURLs[8]  = "http://www.ouifm.fr/player/metafile/Ouifm-hautdebit-wmp.asx";
	stationIMGs[8] = "img/8.png";
	
	stationNames[9] = "Europe 2";
	stationURLs[9]  = "http://viphttp.yacast.net/V4/player_europe2/europe2.asx";
	stationIMGs[9] = "img/9.png";
	
	stationNames[10] = "France Musique";
	stationURLs[10]  = "http://viphttp.yacast.net/V4/francemusique/fmusique.asx";
	stationIMGs[10] = "img/10.png";
	
	stationNames[11] = "Fip";
	stationURLs[11]  = "http://viphttp.yacast.net/V4/fip/fip.asx";
	stationIMGs[11] = "img/11.png";
	
	stationNames[12] = "Le Mouv";
	stationURLs[12]  = "http://cache.yacast.fr/V4/lemouv/lemouv.asx";
	stationIMGs[12] = "img/12.png";
	
	stationNames[13] = "RFM";
	stationURLs[13]  = "http://viphttp.yacast.net/V4/player_rfm/rfm.asx";
	stationIMGs[13] = "img/13.png";
	
	stationNames[14] = "Vibration";
	stationURLs[14]  = "http://www.tv-radio.com/cgi-bin/tagger.pl?tag=site&metafile=vibration/vibration-20k.asx";
	stationIMGs[14] = "img/14.png";
	
	stationNames[15] = "TSF Jazz";
	stationURLs[15]  = "http://www.tsfjazz.com/player/tsfjazz.m3u.php";
	stationIMGs[15] = "img/15.png";
	
	stationNames[16] = "Radio FG";
	stationURLs[16]  = "http://fg.impek.tv:80";
	stationIMGs[16] = "img/16.png";
	
	stationNames[17] = "RTL 2";
	stationURLs[17]  = "http://streaming.radio.rtl2.fr/rtl2-1-44-96";
	stationIMGs[17] = "img/17.png";
	
	stationNames[18] = "Radio Nova";
	stationURLs[18]  = "http://www.novaplanet.com/radio-nova/player/radionova.m3u.php";
	stationIMGs[18] = "img/18.png";
	
	stationNames[19] = "RTL";
	stationURLs[19]  = "http://radio.rtl.fr/rtl.asx";
	stationIMGs[19] = "img/19.png";
	
	stationNames[20] = "RMC";
	stationURLs[20]  = "http://cache.yacast.fr/V4/rmc/rmc.m3u";
	stationIMGs[20] = "img/20.png";
	
	stationNames[21] = "Europe 1";
	stationURLs[21]  = "http://live.europe1.fr/V4/europe1/europe1.asx";
	stationIMGs[21] = "img/21.png";
	
	stationNames[22] = "France Inter";
	stationURLs[22]  = "http://viphttp.yacast.net/V4/franceinter/finter.asx";
	stationIMGs[22] = "img/22.png";
	
	stationNames[23] = "France Info";
	stationURLs[23]  = "http://viphttp.yacast.net/V4/radiofrance/franceinfo_bd.m3u";
	stationIMGs[23] = "img/23.png";
	
	stationNames[24] = "France Culture";
	stationURLs[24]  = "http://viphttp.yacast.net/V4/franceculture/fculture.asx";
	stationIMGs[24] = "img/24.png";
	
	stationNames[25] = "RVE";
	stationURLs[25]  = "http://players.creacast.com/creacast/rve/playlist.asx";
	stationIMGs[25] = "img/25.png";
	
	stationNames[26] = "Deepinside European";
	stationURLs[26]  = "http://eu.deepinside.co.uk:8040/";
	stationIMGs[26] = "img/26.png";
	
	stationNames[27] = "Deepinside American";
	stationURLs[27]  = "http://us.deepinside.co.uk:8020/";
	stationIMGs[27] = "img/27.png";
	
	stationNames[28] = "HotMix House";
	stationURLs[28]  = "http://hotmixradio.fr/stream/hotmix.asx";
	stationIMGs[28] = "img/28.png";
	
	stationNames[29] = "HotMix 100% hits";
	stationURLs[29]  = "http://www.hotmixradio.fr/stream/hotmixHIT.asx";
	stationIMGs[29] = "img/29.png";
	
	stationNames[30] = "HotMix Années 80";
	stationURLs[30]  = "http://hotmixradio.fr/stream/hotmix80.asx";
	stationIMGs[30] = "img/30.png";
	
	stationNames[31] = "HotMix Années 90";
	stationURLs[31]  = "http://hotmixradio.fr/stream/hotmix90.asx";
	stationIMGs[31] = "img/31.png";
	
	stationNames[32] = "HotMix Disco Funk";
	stationURLs[32]  = "http://hotmixradio.fr/stream/funky.asx";
	stationIMGs[32] = "img/32.png";
	
	stationNames[33] = "Radio Belle Vue";
	stationURLs[33]  = "http://www.radiobellevue.fr:8000/hd";
	stationIMGs[33] = "img/33.png";
	
	// Ajout des stations persos :
	var nbRadios = 0
	nbRadios = System.Gadget.Settings.read("nbRadios");
	if (nbRadios>0)
	{
		var i = 0 ;
		var l = 32;
		var j = nbRadios + 32 ;
		for (var k=l ; k<j ; k++)
		{
			i=i+1;
			stationNames[k] = System.Gadget.Settings.read("vNom"+i.toString());
			stationURLs[k] = System.Gadget.Settings.read("vURL"+i.toString());
			stationIMGs[k] = System.Gadget.Settings.read("vImage"+i.toString());
		}
	}
	updateStationSetting();
}

function updateStationSetting()
{
	var stationCount = stationNames.length;
	System.Gadget.Settings.write("stationCount", stationCount);
	for (i=0;i<stationCount;i++)
	{
		System.Gadget.Settings.writeString("station"+i.toString(), stationNames[i]);
		System.Gadget.Settings.writeString("url"+i.toString(), stationURLs[i]);
		System.Gadget.Settings.writeString("img"+i.toString(), stationIMGs[i]);
	}
}

function loadStationList()
{
	stationNames = new Array();
	stationURLs = new Array();
	stationIMGs = new Array();
	
	var stationCount = 0;
	stationCount = System.Gadget.Settings.read("stationCount");
	if (stationCount > 0)
	{
		for (i=0;i<stationCount;i++)
		{
			stationNames[i] = System.Gadget.Settings.readString("station"+i.toString());
			stationURLs[i]  = System.Gadget.Settings.readString("url"+i.toString());
			stationIMGs[i]  = System.Gadget.Settings.readString("img"+i.toString());
		}
	}
	else
	{
		initStationList();
	}
}

function dockStateChange()
{
	with(document.body.style)
		width=130,
		height=67;
	with(playbackStatus.style)
		left=10,
		top=5,
		width=90;
	with(nowPlaying.style)
		left=10,
		top=30,
		width=70;
	with(browsePlaylist.style)
		left=75,
		top=35;
	with(playbackControls.style)
		left=100,
		top=4;
	with(updateRadio.style)
		left=100,
		top=35;
	controlImageMouse(false);
	updateImageMouse(false);
	playlistImageMouse(false);
}

function apresFlyout() 
{
	loadMain();
}

function controlImageClick()
{		
	if (controlImage.src.indexOf("stop") > 0)
	{
		mediaPlayer.controls.stop();
	}
	else
	{
		mediaPlayer.controls.play();
	}
}

function browseImageClick()
{		
	loadMain();
	var nowPlayingText = "<div id='nowPlayingText'><font color='white'><b>Playlist locale</b></font></div>";
	nowPlaying.innerHTML = nowPlayingText;
	var fichier;
    fichier = playlisturl.click();
	mediaPlayer.url = playlisturl.value;

}

function controlImageMouse(isMouseOn)
{
	var isDocked = System.Gadget.docked;
	if (controlImage.src.indexOf("stop") > 0)
	{
		controlImage.src = "img/stop" + (isMouseOn?"_hover":"") + (isDocked?"_sm.png":".png");
	}
	else
	{
		controlImage.src = "img/play" + (isMouseOn?"_hover":"") + (isDocked?"_sm.png":".png");
	}
}

function playlistImageMouse(isMouseOn)
{
	var isDocked = System.Gadget.docked;
	browseImage.src = "img/browse" + (isMouseOn?"_hover":"") + (isDocked?"_sm.png":".png");
}

function updateImageMouse(isMouseOn)
{
	var isDocked = System.Gadget.docked;
	updateImage.src = "img/update" + (isMouseOn?"_hover":"") + (isDocked?"_sm.png":".png");
}

function updateNowPlaying()
{
	var nowPlayingText = "<div id='nowPlayingText'><font color='white'><b>" + currentStation + "</b></font></div>";
	nowPlaying.innerHTML = nowPlayingText;
}

function playStateChange(newstate)
{
	switch (newstate){ 
	case 1: // Stop
		playbackStatus.innerHTML = "Stop";
		controlImage.src = controlImage.src.replace(/stop/, "play");
		break;
	case 2: // Pause
		playbackStatus.innerHTML = "Pause";
		controlImage.src = controlImage.src.replace(/stop/, "play");
		break;
	case 3: // Lecture
		playbackStatus.innerHTML = "Lecture";
		controlImage.src = controlImage.src.replace(/play/, "stop");
		break;
	case 6: // Cache
		playbackStatus.innerHTML = "Mise en cache";
		break;
	case 7: // Attente
		playbackStatus.innerHTML = "Attente";
		break;
	case 8: // Fin
		playbackStatus.innerHTML = "Fin";
		controlImage.src = controlImage.src.replace(/stop/, "play");
		break;
	case 9: // Connexion
		playbackStatus.innerHTML = "Connexion";
		break;
	case 10: // Prêt
		playbackStatus.innerHTML = "Pret";
		controlImage.src = controlImage.src.replace(/stop/, "play");
		break;
	}
}

function loadMain()
{	
	loadStationList();
	loadCurrentStation();
	dockStateChange(); // force le refresh au lancement de Sidebar.exe 

	System.Gadget.onDock = dockStateChange;
	System.Gadget.onUndock = dockStateChange;
    
	System.Gadget.settingsUI = "settings.html";
	System.Gadget.Flyout.file = "flyout.html";
	System.Gadget.Flyout.onHide = apresFlyout;
}

var BackgroundTheme = "12";

function init() 
{
	System.Gadget.settingsUI = "Settings.html";
	System.Gadget.onSettingsClosed = settingsClosed;
			
	if (System.Gadget.Settings.read("BackgroundTheme")) 
		{
			BackgroundTheme = System.Gadget.Settings.read("BackgroundTheme");
		} 
	else 
	{
		System.Gadget.Settings.write("BackgroundTheme", BackgroundTheme);
	}

	System.Gadget.background = "images/bg" + BackgroundTheme + ".png";
			
	loadMain();		
}
				
function settingsClosed()
{
    BackgroundTheme = System.Gadget.Settings.read("BackgroundTheme");
	System.Gadget.background = "images/bg" + BackgroundTheme + ".png";
}



