/*
 * JavaScript to display the track now playing on a Shoutcast server
 * Uses readserverstring.php to retrieve shoutcast data from a remote
 * source and then parses it and returns the string parsed. It then
 * updates the string according to the time set.
 *
 * Script Copyright © 2008 Kurtuluş Radyo ve Televizyon Ltd. Şti.
 * Written by Hawke AI
 * 
 * This script is only licensed for use on the web site www.radioshema.com
 *
 * Any use of this script by companies or individuals based in the Republic of Turkey,
 * web servers located within the Republic of Turkey or on web servers
 * located outside of the Republic of Turkey but hosting material belonging
 * to companies or individuals who reside in the Republic of Turkey is expressly
 * forbidden without prior written consent from the owners of the script.
 *
 * You may write to soru@radioshema.com to obtain permission.
 *
 *
 * Bu JavaSkript Shoutcast sunucusndan Şu an çalan parçayı göstermek içindir.
 * Bu verileri okumak için readserverstring.php skriptini kullanıp, bunları
 * ayıklar ve sayfanın istenilen yerinde göserilir. İstediğiniz zaman aşamında
 * kendisin günceller.
 *
 * Skript'in her hakkın saklıdır. © 2008 Kurtuluş Radyo ve Televizyon Ltd. Şti. 
 * Yazar: Hawke AI
 *
 * Bu skript yalnızca www.radioshema.com web sitesinde kullanılmak üzere lisanslıdır.
 * Türkiye Cumhuriyeti'nde konumlanan gerçek ve tüzel kişilerin, Türkiye Cumhuriyeti'nde
 * bulunan web sunucuları üzerinde veya Türkiye Cumhuriyeti'nin dışında bulunan ama 
 * hosting malzemelerini Türkiye Cumhuriyende bulunan gerçek tüzel kişilere ait olan
 * sitelerde daha önceden yazılı rıza olmaksızın kullanımı yasaktır.
 *
 * İzin almak için soru@radioshema.com adresine yazabilirsiniz.
 *
 *
 * Last Updated: 2008-11-22 | JMW
 */
 
// Setup Variables
// Layer or Span to display the text in. Modified using innerHTML
var layerName = "nowPlaying";

// Layer or Span to check for Language content in
var descName = "nowPlString";

// Text to check for 
var textType = new Array();
textType[0] = "Now Playing";
textType[1] = "Şu An Yayında";

// Text to Replace with: connecting to server to retreive data
var contentLoading = new Array();
contentLoading[0] = "Connecting to Server ...";
contentLoading[1] = "Sunucya bağlantı açılıyor ...";

// Text to Replace with: message to display if server off-line
var offlineMsg = new Array();
offlineMsg[0] = "Connection to Server Failed";
offlineMsg[1] = "Sunucya bulunamadı ...";

// interval to update the script in (in seconds)
var updateInterval = 30;

// instantiate other global variables
var url = "readserverstring.html";
var ajaxObj;
var interVar;
var firstRun = true;

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


/*
 * Function Name: requestObject
 * Arguments: none
 * Description: instantiates XMLHttpRequest object
 */
function requestObject() {
	var ajaxObj;
	if (document.all) {
		ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		ajaxObj = new XMLHttpRequest();
    }
    return ajaxObj;
}


/*
 * Funtion Name: parseScString
 * Arguments: none
 * Description: parses the string returned by nowPlaying();
 *              and writes it to the desired span or layer
 */
function parseSCString() {
	if(ajaxObj.readyState == 4){
		var response = ajaxObj.responseText;
		var scString = new Array();

		if(response.indexOf(',' != -1)) {
    		scString = response.split(',');
    		if (scString[1] == 1) {
    			document.getElementById(layerName).innerHTML = scString[6];
	    	} else {
				var lang = getLanguage();
				document.getElementById(layerName).innerHTML = offlineMsg[lang];
    		}
    	}
    }
}


/*
 * Funtion Name: nowPlaying
 * Arguments: none
 * Description: master function that creates the connection
 *              calls the write command and recurses itself
 *              to update the information
 */
function nowPlaying() {
	if (firstRun) { // write the content loading text
		var lang = getLanguage();
		document.getElementById(layerName).innerHTML = contentLoading[lang];
		firstRun = false;
	}
	ajaxObj = requestObject();
	ajaxObj.open('http://www.radioshema.com/GET', url);
	ajaxObj.onreadystatechange = parseSCString;
	ajaxObj.send(null);
	if (updateInterval > 0) {
		clearInterval(interVar);
		interVar = setInterval(nowPlaying, (updateInterval * 1000));
	}
}

// start nowPlaying when window loads
// disabled for use at Radio Shema Website to not conflict with other on-loads
// window.onload = nowPlaying; 