//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
var loadingImage = '<p class="centerimage"><img src="http://radioverve.com/wp-content/themes/Verve/images/loading.gif" /></p>';
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function trackChanged(channelId) {
	//updateNowPlaying(channelId);
	updateBandBio(channelId);
}

function updateNowPlaying(channelId) {
	xmlhttp2 = getHTTPObject();
	//update now playing info
	var path = "http://radioverve.com/wp-content/plugins/verve-player/getinfo.php?channelid=" + channelId + "&info=now";
	xmlhttp2.open("GET", path);
	xmlhttp2.onreadystatechange = function() {
		try {
			if (xmlhttp2.readyState == 4) {
				document.getElementById("nowplayingdata").innerHTML = xmlhttp2.responseText;
			}
		} catch(e){}
	}
	xmlhttp2.send(null);
}

function updateBandBio(channelId) {
	var xmlhttp;
	xmlhttp = getHTTPObject();
	//update band info
	var path = "http://radioverve.com/wp-content/plugins/verve-player/getinfo.php?channelid=" + channelId + "&info=band";
	xmlhttp.open("GET", path);
	xmlhttp.onreadystatechange = function() {
		try {
			document.getElementById("bandbio").innerHTML  = loadingImage;
			if (xmlhttp.readyState == 4) {
				document.getElementById("bandbio").innerHTML = xmlhttp.responseText;
			}
		} catch(e){}
	}
	xmlhttp.send(null);
}

function updateRecentlyPlayed(channelId) {
	var xmlhttp;
	xmlhttp = getHTTPObject();
	//update band info
	var path = "http://radioverve.com/wp-content/plugins/verve-player/stationinfo.php?channelid=" + channelId + "&data=recent";
	xmlhttp.open("GET", path);
	xmlhttp.onreadystatechange = function() {
		try {
			if (xmlhttp.readyState == 4) {
				document.getElementById("recentlyplayed").innerHTML = xmlhttp.responseText;
			}
		} catch(e){}
	}
	xmlhttp.send(null);
}

function switchChannel(channelId, channelName, channelLink, autoplay){
	window.location.replace("http://radioverve.com/"+channelLink+"?autoplay="+autoplay);
}

/* post link manager*/
var blogTab = 'blogdata';
function managePostLinks(parentElement) {
	if(parentElement == '') parentElement = document;
	else parentElement = document.getElementById(parentElement);
	var postLinks = document.getElementsByTagName('A');
	for(var i = 0; i < postLinks.length; i++ ) {
		if( postLinks[i].href.search(/p=/) >= 0 ) {
			if(document.getElementById(blogTab)) {
				postLinks[i].oldHref = postLinks[i].href;
				postLinks[i].onclick = function () {
											var xmlhttp;
											document.getElementById('bandinfonav2').onclick();
											xmlhttp = getHTTPObject();
											xmlhttp.open("GET", this.oldHref);
											document.getElementById(blogTab).innerHTML  = loadingImage;
											xmlhttp.onreadystatechange = function() {
												try {
													if (xmlhttp.readyState == 4) {
														document.getElementById(blogTab).innerHTML = xmlhttp.responseText;
														managePostLinks('blogdata');
														document.getElementById('commentform').onsubmit = ajax_comments_submit;
													}
												} catch(e){}
											}
											xmlhttp.send(null);
										};
				postLinks[i].href = "javascript:void(null)";
			} else {
				postLinks[i].href = postLinks[i].href.replace("?p=", "radioverve?post=");
			}
		}
	}
}
/*
										if(!document.getElementById(blogTab)) {
											x = this.oldHref;
											postid = x.replace(/[a-z\/.:]*\?/, "");
											postid = postid.match(/p=[0-9]*\/).replace("p=", "");
											window.location.replace("http://radioverve.com/radioverve?post="+postid);
										}
										
function getQueryVariable(variable, querystr) {
	try{
	  var query = querystr.substring(querystr.indexOf("?")+1);
	  var vars = query.split("&");
	  for (var i=0;i<vars.length;i++) {
	    var pair = vars[i].split("=");
	    if (pair[0] == variable) {
	      return pair[1];
	    }
	  }
	} catch(e){}
}
/*
function handlePostQuery() {
	postlink = "http://radioverve.com/?p=" + getQueryVariable("p", window.location);
	xmlhttp = getHTTPObject();
	xmlhttp.open("GET", postlink );
	document.getElementById(blogTab).innerHTML  = loadingImage;
	xmlhttp.onreadystatechange = function() {
		try {
			if (xmlhttp.readyState == 4) {
				document.getElementById(blogTab).innerHTML = xmlhttp.responseText;
				managePostLinks('blogdata');
				document.getElementById('commentform').onsubmit = ajax_comments_submit;
			}
		} catch(e){}
	}
	xmlhttp.send(null);
}
*/
try {
	addEvent(window, 'load', managePostLinks, false);
	addEvent(window, 'load', handlePageLinks, false);
} catch(xx) {}
//addEvent(window, 'load', handlePostQuery, false);

function handlePageLinks() {
	if( listenPage ) {
		list = document.getElementById("navpages");
		links = list.getElementsByTagName("a");
		for( i = 0; i < links.length; i++ ) {
			links[i].target = "blank";
		}
	}
}