var mwhs_server = "http://www.hostgator.com/webhostingspeed/";
var mwhs_updater = "http://www.hostgator.com/webhostingspeed/tracker.php";
var mwhs_image = 'http://whs.hostgator.com/images/star_on.gif';
var mwhs_image_2 = 'http://whs.hostgator.com/images/star_off.gif';
var mwhs_image_3 = 'http://whs.hostgator.com/images/star_half.gif';
var mwhs_rank_ceil = 5;
var mwhs_load_delay = 2000; // in milliseconds

var mwhs_title = "<b style=\"font-size:14px;font-weight:bold;font-family:Arial;padding:0;margin:0;text-align:center;\">How fast did my<br/>site load?</b>";	

var mwhs_loading_text = 'Loading...';
var mwhs_voting_text = 'Voting...';

var mwhs_r = {'average':0,'votes':0,'uniqid':''}; // set our default values;

// need to preload our images
var img = new Image();
img.src = mwhs_image;
var img2 = new Image();
img2.src = mwhs_image_2;
var img3 = new Image();
img3.src = mwhs_image_3;


/**************************
Start of JSON Class
**************************/

function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl; 
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () { // buildScriptTag method
    this.scriptObj = document.createElement("script");    
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
JSONscriptRequest.prototype.removeScriptTag = function () {     // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}
JSONscriptRequest.prototype.addScriptTag = function () { // addScriptTag method
    this.headLoc.appendChild(this.scriptObj);
}

/**************************
Start of MWHS Widget Class
**************************/

function MWHSWidget(container_id,uniq_id,rate,url) {
	this.container_id = container_id;
	this.uniq_id = uniq_id;
	this.url = url;
	this.average = 0; // default to 0;
	this.mwhs_r = mwhs_r;
	this.updating = false;
	
	var mwhs = document.createElement('span'); // star container


	var mwhs_t = document.createElement('span');
	mwhs_t.innerHTML = mwhs_title;
	
	mwhs.appendChild(mwhs_t);

	var mwhs_b = document.createElement('br');
	mwhs.appendChild(mwhs_b);
		
	for(var i=1;i<=mwhs_rank_ceil;i++) {
		var img = document.createElement('img');
		img.src = mwhs_image_2;
		
		img.style.display = 'none';
		img.style.width = "16px";img.style.height = "16px";img.style.cursor = 'pointer';

		img.mwhs = this;
		img.value = i; // assign the value for this star element

		img.onclick = function() {
			var newwidget = new MWHSWidget(this.mwhs.container_id,this.mwhs.uniq_id,this.value,this.mwhs.url);
		
			newwidget.update_rank(this.value);
			newwidget.re_rank(this.value);

			this.mwhs.toggle_stars('none');
			this.mwhs.update_stat(mwhs_voting_text);
			this.mwhs.updating = true; // mark the current container as updating to turn off our onmouseout event

			window.setTimeout(function() {
				newwidget.mwhs_r = mwhs_r;
				newwidget.re_rank(newwidget.mwhs_r.average);
				
				newwidget.toggle_stars('inline');
				newwidget.update_stat('');	

				var oldcontainer = document.getElementById(newwidget.container_id);
				oldcontainer.innerHTML = newwidget.mwhs.innerHTML;	// since we don't need anyother events just display them
				
			},mwhs_load_delay);
			
		};
			
		img.onmousemove = function() {this.mwhs.re_rank(this.value);};
		mwhs.appendChild(img);		
	}

	// create our status tag
	var mwhs_s = document.createElement('span');				
	mwhs.appendChild(mwhs_s);

	this.mwhs_stat = mwhs_s;
	this.mwhs = mwhs;
	return this;
}

MWHSWidget.prototype.update_stat = function(value) {
	this.mwhs_stat.innerHTML = value;	
};

MWHSWidget.prototype.toggle_stars = function(value) {
	var imgArray = this.mwhs.getElementsByTagName('img');		
	var imgCount = imgArray.length - 1;
	
	for(var i = 0; i <= imgCount; i++) {
		var img = imgArray[i];
		img.style.display = value;		
	}
	
};

MWHSWidget.prototype.re_rank = function(value) {
	var imgArray = this.mwhs.getElementsByTagName('img');		
	var imgCount = imgArray.length - 1;

	for(var i = 0; i <= imgCount; i++) {
		var img = imgArray[i];
		
		if(i < (value)) {
			img.src = mwhs_image;
		} else {
			img.src = mwhs_image_2;	
		}
		
		// check our half star 
		if(i == Math.floor(value)) {
			var x = Math.floor(value);
			if(value >= (x + .3) && value <= (x + .7)) {
				img.src = mwhs_image_3;	// send in a half star
				return true;
			}									   
		}
		
	}
	
};

MWHSWidget.prototype.update_rank = function(rate) {
	var req = mwhs_updater+"?id="+this.uniq_id+"&rate="+rate+"&url="+escape(this.url);
	var bObj = new JSONscriptRequest(req); 
	bObj.buildScriptTag(); // Build the dynamic script tag
	bObj.addScriptTag(); 	// Add the script tag to the page
};

var url = location.href;
var widget = new MWHSWidget(container_id,uniq_id,0,url);
widget.update_rank(0); // since we're just retrieving the average, post a 0 value.
widget.update_stat(mwhs_loading_text);

window.setTimeout(function() {
	if(mwhs_r) {
		widget.mwhs_r = mwhs_r;
		widget.re_rank(widget.mwhs_r.average);
		widget.toggle_stars('inline');
		widget.update_stat('');
	}
},mwhs_load_delay);

widget.mwhs.onmouseout = function() {	
	if(!widget.updating) {
		widget.re_rank(widget.mwhs_r.average);
	}
};

document.getElementById(container_id).appendChild(widget.mwhs);