bkwr = true;
ptitle = document.title;
bkwr_notify_dyn = false;

function xmlhttpGet(step) {
	if (step == 0) {
		document.getElementById("bkwr_info_activity").style.visibility = "visible";
	}
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', '/scripts/shoutcast.php?ajax=true'+(step == 1 ? '&title=true' : '')+(bkwr_notify_dyn == true ? '&notify=true' : ''), true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, step);
        }
    }
    self.xmlHttpReq.send(null);
}

function updatepage(str, step){
	switch (step) {
		case 1:
			if (str.length > 0) {
				if (bkwr_notify_dyn == true) {
					bkwr_notify_dyn = false;
					document.title = "BKWR Online!" + " - " + ptitle;
					window.alert("Blue Kaffee Web Radio is Online!");
				} else {
					var new_title = str + " - " + ptitle;
					if (document.title != new_title) {document.title = new_title;}
				}
			} else {
				if (document.title != ptitle && document.title != "* " + ptitle) {document.title = ptitle;}
			}
		break;
		default:
    		document.getElementById("bkwr_info").innerHTML = str;
    		xmlhttpGet(1);
    }
}

function autorefresh(freq){
	// document.getElementById("bkwr_info").innerHTML = "<p><i>Retrieving station information...</i></p>";
    xmlhttpGet();
    ar = setInterval("xmlhttpGet(0);",freq);
}

function bkwr_notify_me () {
		var check = document.getElementById("bkwr_notify").checked;
		bkwr_notify_dyn = check;
		if (check == true) {
			document.title = "* " + ptitle;
		} else {
			document.title = ptitle;
		}
}