var sp1;
var sp2;
var sp3;
var sp4;
var timeout = 1000;
var is_ie = typeof(document.all) != 'undefined';
var opacity = (is_ie) ? "filter" : "opacity";
var track_count = 0;

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function ajax_do (url) {
	// Create new JS element
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;
	jsel.id = "js1";
	// Append JS element (therefore executing the 'AJAX' call)
	// it will be added as a child of id "addme"
	sp4 = document.getElementById("addme");
  sp4.appendChild (jsel);
}

function so_clearInnerHTML(obj) {
	// so long as obj has children, remove them
	while(obj.firstChild)obj.removeChild(obj.firstChild);
}

// pass in the content string & the location of where you want the child added
function do_output(content, loc, ref){
	// call your function to remove all the children from your element
	so_clearInnerHTML(document.getElementById(loc));
	
	// update the contents of the element with a new node
	// create an empty element node
	// without an ID, any attributes, or any content
	var sp5 = document.createElement("div");
	
	// fix the text
	sp5.innerHTML = content;

	// give it an id attribute called randomstring
	sp5.setAttribute("id", "updated_"+ref);
	
	// build a reference to the existing node to be replaced
	var sp6 = document.getElementById(loc);
	
	// add sp6 to sp5
	sp6.appendChild(sp5);
}

function changeOpacity(id, opacity){
  var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function locate(){
  var myDate = new Date();
  timerID = setTimeout("locate()", timeout);
	timeout = 15000;
	// build a reference to the existing node to be replaced
	sp3 = document.getElementById("js1");
	sp4 = document.getElementById("addme");

	// check for added javascript & if its been added detete it.
	if(sp3 != null){
  	so_clearInnerHTML(sp4);
	}
	// gets the php vars
 	ajax_do ("http://www.witchwoodradio.co.uk/scripts/webtwo.php?ts=" + myDate.getDate() + myDate.getTime());
	var out_1 = "";
	
	try{
		out_1 = get_out_1();
	}
	catch(err){
		// nothiing to do first time error suppression;
	}
	do_output(out_1, "content_id_1", 1);

	var out_2 = "";
	try{
		out_2 = get_out_2();
	}
	catch(err){
		// nothiing to do first time error suppression;
	}
	do_output(out_2, "content_id_2", 2);
	var hb7 = new HelpBalloon({
	title: 'Album',
	dataURL: 'ajax7.htm',
	useEvent: ['mouseover'],
	tipSize: 0,
	icon: $('sticky1')});
	var hb8 = new HelpBalloon({
	title: 'Artist',
	dataURL: 'ajax8.htm',
	useEvent: ['mouseover'],
	tipSize: 0,
	icon: $('sticky2')});

	if(track_count > 5){
		for(i = (track_count - 5); i < track_count; i++){
			changeOpacity('track' + i, (track_count / i) * 25);
		}
	}
}
