/*
 * Radio Shema Dynamic JavaScripts 
 *
 * Copyright © 2008, 2011 Hawke AI, Kurtulus Radyo TV ve Org. A.S.
 *
 * Last Updated: 2011-11-17 | JMW
 *
 * Dojo toolkit must be present for these scripts to work
 *
 */
 
/* Functions */
/*
 * Name: advanceExgRates
 * Arguments: none
 * Returns: nothing
 * Description: Scrolls the
 */
function advanceExgRates() {
	dojox.fx.smoothScroll({ node: dojo.byId(exgRateList[currentExgRate]),
							win: dojo.byId(exgName)}).play();
	currentExgRate++;
	if (currentExgRate >= exgRateList.length) currentExgRate = 0;
	window.setTimeout("advanceExgRates()", 5000);
}

/*
 * Function Name: getLanguage
 * Arguments: checkText = reference to textType
  * Returns: an index value related to the array position of the language selected
* Description: Checks for what language to display
 */
function getLanguage() {
	var index;
	var result = 0;
	var checkLayer =dojo.byId(descName).innerHTML;
	
	for (index=0; index<textType.length; index++) {
		if (checkLayer.indexOf(textType[index]) > -1) {
			result = index;
		}
	}
	
	return result;
}

/*
 * Name: nowPlaying
 * Arguments: none
 * Returns: nothing
 * Description: Makes initial connection to server and triggers "update songs"
 */
function nowPlaying() {
	/* if this is the initial load, trigger the exchange rate function first */
	if (initConnection) {
		advanceExgRates();
		initConnection = false;
	}
	
	/* write out  loading message */
	dojo.byId(nowPlName).innerHTML = txtConnecting[getLanguage()]; 
	var xhrArgs = {
		url: "serverstring2.php",
		handleAs: "json",
		load: function(data) {
			updateNowPlaying(data);
		},
		error: function(err) {
			dojo.byId(nowPlName).innerHTML = txtNoConnection[getLanguage()]; 
		}
	}
	dojo.xhrGet(xhrArgs);
}

/*
 * Name: updateLastTen
 * Arguments: songlist = JSON array of songs
 * Returns: nothing
 * Description: Updates the last ten items played and bolds any that contain
 *              the artist of the day.
 */
function updateLastTen(songlist) {
	var test = "";
	var artist = "";
	var songline = "";
	/* double-check to see if we have the last ten */
	if (dojo.byId(tenName) && (typeof(songlist) == "object")) {
		dojo.empty(tenName);
		/* get artist */
		artist = dojo.byId(artistName).innerHTML;
		/* write out song list */
		for (var i=0; i<songlist.length; i++) {
			test = songlist[i].song.toLowerCase();
			if (test.indexOf(artist.toLowerCase()) > -1) {
				songline = "<strong>" + songlist[i].song + "</strong>";
			} else {
				songline = songlist[i].song;
			}
			dojo.create("p", {innerHTML: songline}, dojo.byId(tenName));
		}
	}
}

/*
 * Name: updateNowPlaying
 * Arguments: songlist = JSON array of songs
 * Returns: nothing
 * Description: Updates the Now Playing field and triggers updateLastTen if
 *              on front page.
 */
function updateNowPlaying(songlist) {
	if (typeof(songlist) != "object") {
		dojo.byId(nowPlName).innerHTML = txtNoConnection[getLanguage()];
		return;
	}
	dojo.byId(nowPlName).innerHTML = songlist[0].song;
	
	/* make sure that we're on the front page before calling the updateLastTen
	 * function */
	if (dojo.byId(tenName)) {
		updateLastTen(songlist);
	}
}


/* initialize language content */
var textType = new Array();       // code to check to figure out what language we're dealing with
var txtConnecting = new Array();  // message to display when connecting
var txtNoConnection = new Array();	// message to display when connection failed

/* Turkish takes [0] */
/* Because page is set to ISO-8859-9, use decimal characters for Turkish characters as follows:
 * +---+--------+---+--------++---+--------+---+--------+
 * | Â | \u00C2 | â | \u00C4 || Ç | \u00C7 | ç | \u00E7 |
 * +---+--------+---+--------++---+--------+---+--------+
 * | Ğ | \u011E | ğ | \u011F || İ | \u0130 | ı | \u0131 |
 * +---+--------+---+--------++---+--------+---+--------+
 * | Ö | \u00D6 | ö | \u00F6 || Ş | \u015E | ş | \u015F |
 * +---+--------+---+--------++---+--------+---+--------+
 * | Ü | \u00DC | ü | \u00FC ||   |        |   |        |
 * +---+--------+---+--------++---+--------+---+--------+ */
textType[0] = "\u015Eu An Yay\u0131nda";
txtConnecting[0] = "Yay\u0131n sunucusu ile ba\u011Flant\u0131 kuruluyor...";
txtNoConnection[0] =  "Yay\u0131n sunucusu bulunmad\u0131!";

/* English takes [1] */
textType[1] = "Now Playing";
txtConnecting[1] = "Opening Connection to Server...";
txtNoConnection[1] = "Server not found!";


/* intialize global variables */
var initConnection = true; 			// boolean variable that tells if we've initally connected
var exgRateList = Array("USD", "EUR", 
						"GBP", "Xkur"); // array of exchange rate names; Xkur = çapraz USD/EUR
var currentExgRate = 0;                 // which exchange rate to start with

/* initialize layer id's */
var nowPlName = "nowPlaying";	// Layer or Span to display the text in
var descName = "nowPlString";	// Layer or Span to check for Language content in
var tenName = "sonon_veri";		// layer to put the last 10 in
var artistName = "sanatci_adi";	// layer that the artist name is in
var exgName = "kur_veri";		// layer for the exchange rate data

/* initialize dojox scroll plugin */
dojo.require("dojox.fx.scroll");        // needed to scroll exchange rate stuff
