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

var stationNames;
var stationURLs;
var currentStationID;

window.onload = function()
{
	loadStationList();
	
	var currentStation = System.Gadget.Settings.read("stationName");
	var currentID = 0;
	
	if (currentStation != "") {
		currentID = getIDByStationName(currentStation);
	}
	
	if (currentID >= 0 && currentID < stationNames.length)
		currentStationID = currentID;
	else
		currentStationID = 0;
		generateStationList(stationPickerField);
}

function getIDByStationName(stationName)
{
	for (i=0;i<stationNames.length;i++)
	{
		if (stationNames[i] == stationName)
			return i;
	}
	return -1;
}

function changeRadio(stationmemorisee)
{
	System.Gadget.Settings.write("stationName", stationNames[stationmemorisee]);
	System.Gadget.Flyout.show = false;
}

function generateStationList(parentObj)
{
	var strHTML = "<table style='width: 100%; heigth: 100%;'>";
	var stationCount = System.Gadget.Settings.read("stationCount");
	
	for (i=0;i<stationCount;i++)
	{
		var cheminImage = "img"+i.toString(); 
		if (i == 0){
			strHTML += ("<tr><td><a href='' onclick='changeRadio(" + i.toString() + ");return false;'><img width='50' height='50' src='" + System.Gadget.Settings.read(cheminImage) + "' /></a></td>");
		}else{
			if (i == (stationCount-1)){
				if (i%5 == 0){
					strHTML += ("</tr><tr><td><a href='' onclick='changeRadio(" + i.toString() + ");return false;'><img width='50' height='50' src='" + System.Gadget.Settings.read(cheminImage) + "' /></a></td></tr>");
				}else{
					strHTML += ("<td><a href='' onclick='changeRadio(" + i.toString() + ");return false;'><img width='50' height='50' src='" + System.Gadget.Settings.read(cheminImage) + "' /></a></td></tr>");
				}
			}else{
				if (i%5 == 0){
					strHTML += ("</tr><tr><td><a href='' onclick='changeRadio(" + i.toString() + ");return false;'><img width='50' height='50' src='" + System.Gadget.Settings.read(cheminImage) + "' /></a></td>");
				}else{
					strHTML += ("<td><a href='' onclick='changeRadio(" + i.toString() + ");return false;'><img width='50' height='50' src='" + System.Gadget.Settings.read(cheminImage) + "' /></a></td>");
				}
			}
		}
	}
	
	strHTML += "</table>";
	
	parentObj.innerHTML = strHTML;
}

function loadStationList()
{
	stationNames = new Array();
	stationURLs = new Array();
	var stationCount = System.Gadget.Settings.read("stationCount");
	
	if (!stationCount) {
		alert("La liste des stations est vide. Veuillez relancer le gadget.");
	}
	else
	{
		for (i=0;i<stationCount;i++)
		{
			stationNames[i] = System.Gadget.Settings.readString("station"+i.toString());
			stationURLs[i]  = System.Gadget.Settings.readString("url"+i.toString());
		}
	}
}