// NRK user script
// version 0.02 Beta
// Richard H. Tingstad
//
// Provides a link to stream URL in NRK (Norwegian Broadcasting Corporation) web TV.
// Link is called "Straumpeikar" and placed under description, near "Tips/del video".
//
// Simplifies downloading/dumping the stream. For example, copy the URL and use:
// mplayer -dumpstream -dumpfile file.wmv URL
// Tip: Set your bandwidth high in settings to get highest quality.
//
// To use this script with Firefox, you need Greasemonkey. Works with Opera as well.
//
// ==UserScript==
// @name           NRK
// @namespace      http://drop.by
// @description    Provides link to NRK (Norwegian Broadcasting Corporation) web TV stream URL.
// @include        http://www1.nrk.no/nett-tv/*
// ==/UserScript==
(function () {

var xmlhttp = new XMLHttpRequest();
var url = document.getElementById('ctl00_ucPlayer_Player').getAttribute('url');

if (xmlhttp != null) {
	xmlhttp.onreadystatechange = stateChange;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function stateChange() {
if (xmlhttp.readyState == 4) {
	if (xmlhttp.status == 200) {
//		xmlhttp.responseXML.getElementsByTagName('ref')[0];
		var s = xmlhttp.responseText.indexOf('mms://');
		var i = xmlhttp.responseText.substr(s, xmlhttp.responseText.length - s);
		s = i.indexOf('"');
		i = i.substring(0, s);

		var li = document.createElement('li');
		var newLink = document.createElement('a');
		var textNode = document.createTextNode('Straumpeikar');
		newLink.setAttribute('href', i);
		newLink.appendChild(textNode);
		li.appendChild(newLink);
	}
	else {
		var li = document.createElement('li');
		var textNode = document.createTextNode('Status ' + xmlhttp.status);
		li.appendChild(textNode);
	}
	document.getElementById('player-extras-chooser').childNodes[0].appendChild(li);
}
}


})();
