/****************mirrors.js**********************
*	This file does several operations for	*
*	mirror discovery.  First is makes a 	*
*	request to host.json, to find out which	*
*	mirrors host the current page.  Then it	*
*	makes inline requests to each host.  	*
*	Upon response of a host the status of	*
*	that mirror is updated or left unknown	*
*	if no response is returned.		*
************************************************/

var mirrorTool = function() {

	this.current_dir = location.href.split( document.domain )[1];
	this.ie;
	this.jsonObject;
	this.priority_domain = 0;
	this.timeoutID = new Array(3);
	this.xmlhttp = new Array(5);
	this.milli = new Array(3);
	this.mirrors;
	this.mirrors_name;
	this.displayedAboutTable = 0;

	this.div = document.getElementById("mirrorDiv");
	this.mainTable = document.createElement("table");
	this.mainTable.className = "yui-dt-table";
	this.mainTableBody = this.mainTable.appendChild(document.createElement("tbody"));
	this.leftTable = document.getElementById("mirrorDivLeft").appendChild(document.createElement("table"));
	this.leftTable.className = "yui-dt-table";
	this.leftTableBody = this.leftTable.appendChild(document.createElement("tbody"));
	this.leftTableArray = new Array();
	this.mainTableArray = new Array();

	this.ajaxComm = new ajaxObj();

	// sets all table rows to unknown in the main status table (popup tab)
	this.create_tags = function(){
		var thead = this.mainTable.appendChild(document.createElement("thead"));
		var tr = thead.appendChild(document.createElement("tr"));
		var th = tr.appendChild(document.createElement("th"));
		th.innerHTML = "Mirror";
		th = tr.appendChild(document.createElement("th"));
		th.innerHTML = "Status";
		tr.appendChild(document.createElement("th"));
		for( var i = 0; i < this.mirrors.length; i++ ){
			var row = this.mainTableBody.appendChild(document.createElement("tr"));
			if( i % 2 == 0 ){
				row.className = "yui-dt-even";
			}
			this.mainTableArray.push(row);
			var mirrorTD = row.appendChild(document.createElement("td"));
			mirrorTD.id = "mirror_"+i+"_url";
			mirrorTD.innerHTML = "<a href=http://"+this.mirrors[i] + this.current_dir +">http://"+this.mirrors[i]+this.current_dir+"</a>";
			var statusTD = row.appendChild(document.createElement("td"));
			statusTD.id = "mirror_"+i+"_status"
			statusTD.innerHTML = "Condition: <b>Unknown</b>";
			var iconTD = row.appendChild(document.createElement("td"));
			iconTD.align = "center";
			iconTD.id = "mirror_"+i+"_icon";
			iconTD.innerHTML = "<img src='/website/images/mirrorUnknown36.png'/>";
		} 	
		this.div.appendChild(this.mainTable);
		var abouttxt= "<br><div id='status_table_about' style='color: #5D7493; cursor: pointer' onmouseup='show_about_table();'><font size=2>About This Table</font>\n";
		abouttxt += "<div id='status_table_about_content' style='color: #5D7493; border: 1px solid #dddddd; background-color: #eeeeee; padding: 5px; margin 5px; display:none; cursor: default'>";
		abouttxt += "<font size=2>The table above shows possible mirror sites for this CaSIL directory and the mirror's current working status.";
		abouttxt += "<ul><li style='list-style-image: none'><img src='/website/images/mirrorOK24.png' /> means the mirror is up and running.</li>";
		abouttxt += "<li style='list-style-image: none'><img src='/website/images/mirrorDown24.png' /> means the site is down, the current directory doesn't exist on that mirror, or did not return a 200 HTTP status.</li>";
		abouttxt += "<li style='list-style-image: none'><img src='/website/images/mirrorUnknown24.png' /> means the we are currently unable to determine the status of the mirror.</li></ul>";
		abouttxt += "**Note: These tests are not always 100% accurate due to the nature of the internet and are for helpful reference only. </font></div>\n</div>\n";
		document.getElementById("mirrorDiv").innerHTML += abouttxt;	
		this.make_left_table();
		this.check_mirrors();
	}

	// make a head request to a mirror (to check for status)
	this.check_mirrors = function(addr){
		function onSuccess(r, tVars){
			try {
				var ajaxComm = tVars[0];
				var id = tVars[1];
				var url_status = r;
				//var url_status = requestObj.status;
				var now = new Date();
				var new_milli = (now.getSeconds() * 1000)  +  now.getMilliseconds();
				//var finaltime = new_milli - milli[id];
				var finaltime = 0;
			} catch(e) {
			}
			ajaxComm.display(finaltime, url_status, id);
			ajaxComm.display_left(finaltime, url_status, id);
		}
		for( var i in this.mirrors ){
			var address = "http://"+document.domain+"/casil/mirror/"+this.mirrors[i]+this.current_dir;
			this.ajaxComm.headRequest(address, onSuccess, null, null, [this, i]);
		}
	}

	// display the status of a returned http code
	this.display = function(finaltime, url_status, id){
		var statusTD = document.getElementById("mirror_"+id+"_status");
		var iconTD = document.getElementById("mirror_"+id+"_icon");
		if( url_status == 200 || url_status == 301 || url_status == 0){
			statusTD.innerHTML = "Condition, <b>OK</b>";
			iconTD.innerHTML = "<img src='/website/images/mirrorOK36.png'/>";
		} else if( url_status == 408 ){
			statusTD.innerHTML = "Condition, <b>Unknown</b>";
			iconTD.innerHTML = " <img src='/website/images/mirrorUnknown36.png'/> ";
		} else {
			statusTD.innerHTML = "Condition, <b>Down</b>";
			iconTD.innerHTML = " <img src='/website/images/mirrorDown36.png'/> ";
		}
	}

	// set left content bar table to unknown status
	this.make_left_table = function(){
		var dir = this.current_dir.split( "/" );
		var thead = this.leftTable.appendChild(document.createElement("thead"));
		var tr = thead.appendChild(document.createElement("tr"));
		var th = tr.appendChild(document.createElement("th"));
		th.innerHTML = "Mirror";
		th = tr.appendChild(document.createElement("th"));
		th.innerHTML = "Status";
		th.align = "center";
		for( var i = 0; i < this.mirrors.length; i++ ){
			var row = this.leftTableBody.appendChild(document.createElement("tr"));
			if( i % 2 == 0 ){
				row.className = "yui-dt-even";
			}
			this.leftTableArray.push(row);
			var mirrorTD = row.appendChild(document.createElement("td"));
			mirrorTD.id = "left_mirror_"+i+"_url";
			mirrorTD.innerHTML = "<a href=http://"+this.mirrors[i] + this.current_dir +">"+this.mirrors_name[i]+"</a>";
			var statusTD = row.appendChild(document.createElement("td"));
			statusTD.align = "center";
			statusTD.id = "left_mirror_"+i+"_status";
			statusTD.innerHTML = "<img src='/website/images/mirrorUnknown24.png'/>";
		} 
	}

	// set left content bar mirror to return http code
	this.display_left = function(finaltime, url_status, id){
		var statusTD = document.getElementById("left_mirror_"+id+"_status");
	//	mirrorTD.innerHTML = "<a href=http://"+this.mirrors[id]+this.current_dir+">" + this.mirrors_name[id]+"</a>";
		if( url_status == 200 || url_status == 301 || url_status == 0){
			statusTD.innerHTML = "<img src='/website/images/mirrorOK24.png' />";
		} else if(url_status == 408) {
			statusTD.innerHTML = "<img src='/website/images/mirrorUnknown24.png'/>";
		} else {
			statusTD.innerHTML = "<img src='/website/images/mirrorDown24.png'/> ";
		}
	}


	// find out which mirrors host current page, and start making requests
	// also sets up main popup staus table
	this.findMirrors = function(){
		function onSuccess(text, mirrorObj){
			jsonObject = eval('(' + text + ')');
			var host_num = 0;
			mirrorObj.mirrors = new Array();
			mirrorObj.mirrors_name = new Array();
			for (var i in jsonObject){
				var isfound = 0;
				for( j = 0; j < jsonObject[i].contents.length; j++ ){
					var reg = new RegExp(jsonObject[i].contents[j] );
					var ismatch = reg.test( mirrorObj.current_dir );
					var match = mirrorObj.current_dir.match( reg );
					if( jsonObject[i].http == "casil-mirror1.ceres.ca.gov" ){
						if( !ismatch ){
							isfound = 1;
							break;
						}
					} else if ( ismatch ){
						isfound = 1;
						break;
					}
				}
				if( isfound && jsonObject[i].status != "down" ){
					mirrorObj.mirrors[host_num] = jsonObject[i].http;
					mirrorObj.mirrors_name[host_num] = i;
					host_num++;
				}
			}
			mirrorObj.create_tags();
		}
		var address = "http://"+document.domain+"/casil/etc/website/js/hosts.json";
		this.ajaxComm.localRequest( address, onSuccess, null, null, this );
	}

}
function show_about_table(){
	if( document.getElementById('status_table_about_content').style.display == "block" ){
		document.getElementById('status_table_about_content').style.display = "none";
	} else {
		document.getElementById('status_table_about_content').style.display = "block";
	}
}
