function getOnairInfo() {
	var url = '/backend/onairInfo.aspx?channel=' + playerChannel;
	new Ajax.Request(url, {
		method: "get",
		requestHeaders: { Accept: 'application/json' },
		onComplete: paintOnair
	});
}
var usesScriptCommands = false;
function paintOnair(originalRequest) {
	var onairInfo = originalRequest.responseText.evalJSON(false);
	var host1 = ""; var host2 = "";

	var channelInfo = getChannel(onairInfo);

	if (channelInfo != null && channelInfo.OnairInfo != null) {
	    var onairInfo = channelInfo.OnairInfo;

	    if (!usesScriptCommands) {

	        var ncl = new NCL();
	        ncl.Last.Artist = onairInfo.lastArtist;
	        ncl.Last.Title = onairInfo.lastTitle;
	        ncl.Current.Artist = onairInfo.currentArtist;
	        ncl.Current.Title = onairInfo.currentTitle;
	        ncl.Next.Artist = onairInfo.nextArtist;
	        ncl.Next.Title = onairInfo.nextTitle;

	        updateNCL(ncl);
	    }

	    updateDisplay($('PlayerProgramText'), onairInfo.programName, false);

	    if ($('PlayerProgramlederImg')) {
	        var imgSrc = onairInfo.imageSrc;
	        if (imgSrc != undefined) {
	            $('PlayerProgramlederImg').src = imgSrc;
	            $('PlayerProgramlederImg').show();
	            if (typeof (pngfix_image) == "function") {
	                pngfix_image($('PlayerProgramlederImg'));
	            }
	        }
	    }

	    if (onairInfo.homepageSrc != undefined) {
	        if (playerChannel == 6) onairInfo.homepageSrc = "http://www.bandit.no" + onairInfo.homepageSrc;
	        $('PlayerProgramHomepageLink').href = onairInfo.homepageSrc;
	        $('PlayerProgramHomepageLink').show();
	    }
	    else
	        $('PlayerProgramHomepageLink').hide();

	    // If program on air - set label
	    if ($('PlayerProgramText').innerHTML != "&nbsp;")
	        $('PlayerProgramLabel').innerHTML = "PROGRAM:";


	    var hosts = "";
	    if (isArray(onairInfo.programHosts)) {
	        hosts = onairInfo.programHosts[0];
	        for (var i = 1; i < onairInfo.programHosts.length - 1; i++) {
	            hosts += ", " + onairInfo.programHosts[i];
	        }
	        if (i < onairInfo.programHosts.length) {
	            hosts += " og " + onairInfo.programHosts[onairInfo.programHosts.length - 1];
	            $('PlayerProgramlederLabel').innerHTML = "PROGRAMLEDERE:";
	        }
	        else if (onairInfo.programHosts.length > 0) {
	            hosts = onairInfo.programHosts;
	            $('PlayerProgramlederLabel').innerHTML = "PROGRAMLEDER:";
	        }
            
            $('PlayerProgramlederText').innerHTML = hosts;
	    }
	}
}

function isArray(obj) {
    return obj && (obj.constructor == Array);
}

function updateDisplay(elm, text, bAdd) {
	if (text != undefined) {
		if (bAdd) {
			if (elm.innerHTML != "&nbsp;")
				elm.innerHTML += " - " + text;
			else elm.innerHTML = text;
		}
		else elm.innerHTML = text;
	} else elm.innerHTML = "&nbsp;";
}

function getChannel(onairInfo) {
	var channelInfo = null;
	if (onairInfo.Channels.ChannelInfo.length != undefined) {
		channelInfo = onairInfo.Channels.ChannelInfo.find(
            function(channelInfo) { return channelInfo.ChannelId == playerChannel; })
	}
	else if (onairInfo.Channels.ChannelInfo.ChannelId == playerChannel) {
		channelInfo = onairInfo.Channels.ChannelInfo;
	}
	return channelInfo;
}
function CallScriptCommand(scType, scParam) {
	if (scType != "data")
		return;

	var ncl = new NCL();
	if (scParam.length > 0) {
		var params = scParam.split("&");
		for (var i = 0; i < params.length; i++) {
			if (params[i].length > 0) {
				var item = params[i].split("=");
				var key = unescape(item[0]);
				var value = unescape(item[1]);
				switch (key) {
					case "artist":
						ncl.Current.Artist = value;
						break;
					case "title":
						ncl.Current.Title = value;
						break;
					case "nextartist":
						ncl.Next.Artist = value;
						break;
					case "nexttitle":
						ncl.Next.Title = value;
						break;
					case "lastartist":
						ncl.Last.Artist = value;
						break;
					case "lasttitle":
						ncl.Last.Title = value;
						break;
				}
			}
		}
	}

	if (!usesScriptCommands) {
		usesScriptCommands = true;
		window.clearInterval(onairInfoInterval);
		window.setInterval("getOnairInfo(true)", 120000);
	}
	updateNCL(ncl);
	// NCL format: artist=Destiny%27s%20Child&title=Emotion&nextartist=James%20Blunt&nexttitle=&lastartist=Justin%20Timberlake&lasttitle=Like%20I%20Love%20You
}

function updateNCL(ncl) {
	updateDisplay($('PlayerLastArtistText'), ncl.Last.Title, false);
	updateDisplay($('PlayerLastArtistText'), ncl.Last.Artist, true);
	updateDisplay($('PlayerCurrentArtistText'), ncl.Current.Artist, false);
	updateDisplay($('PlayerCurrentTitleText'), ncl.Current.Title, false);
	updateDisplay($('PlayerNextArtistText'), ncl.Next.Title, false);
	updateDisplay($('PlayerNextArtistText'), ncl.Next.Artist, true);
	// Change display if live from studio
	if (ncl.Current.Artist == "Studio/direkte") {
		$('PlayerCurrentArtistLabel').innerHTML = "N&Aring;:";
		$('PlayerCurrentTitleLabel').innerHTML = "&nbsp;";
	}
	else {
		$('PlayerCurrentArtistLabel').innerHTML = "ARTIST:";
		$('PlayerCurrentTitleLabel').innerHTML = "SANG:";
	}
}

// NCLItem object
function NCLItem(artist, title) {
	this.Artist = artist;
	this.Title = title;
}

// NCL object
function NCL() {
	this.Current = new NCLItem();
	this.Last = new NCLItem();
	this.Next = new NCLItem();
}
