/* General javascript utils loaded on all pages */

//Global variable

var IsUserHolding = false;


/* Use this when wanting a function to run onload. That way you allow
multiple functions to run onload without needing to be aware of the other */
function addLoadEvent(func) {
	if (typeof window.attachEvent != 'undefined') {
		window.attachEvent("onload", func);
	} else {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			};
		}
	}
}

/* This adds JSON processing to the string class
See http://json.org for more info */
String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};

/* The below functions are for handling the XMLHttpRequest object */

/* Request a HTTP object. If there are multiple http requests going on then
we may have more than one object being used at a time */
function getHTTPObject(func) {
	
	function requestHandler() {
		var httpRequester = null;
		if (typeof window.XMLHttpRequest != 'undefined') {
			httpRequester = new XMLHttpRequest();
		} else if (typeof window.ActiveXObject != 'undefined') {
			try {
				httpRequester = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					httpRequester = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					httpRequester = null;
				}
			}
		}
		
		if (httpRequester) this.ready = true;
		else this.ready = false;
		
		var stateHandler = function() {
				if (httpRequester.readyState == 4) {
					func(httpRequester.responseText);
				}                             
			};
			
		this.requestData = function() {
			var pathName = location.pathname;
			
			var url = pathName.substring(pathName.lastIndexOf("/") + 1, pathName.length);
			
			if (arguments.length > 0) {
				url += '?a=' + arguments[0];
			}
			for (var i=1; (i + 1) < arguments.length; i+=2) {
				url += "&" + arguments[i] + "=" + escape(arguments[i+1]);
			}
			
			httpRequester.open("GET", url, true);
			httpRequester.onreadystatechange = stateHandler;
			httpRequester.send(null);
		};
	}
	
	var waystation = new requestHandler();
	
	if (!waystation.ready) return null;
	
	return waystation
}

/* this can be run in an onload event. It will make an HTTP request for the language
data for javascript and store it in a variable. Once done it will run the function
passed to 'alert' any other scripts */
var js_lang = null;
var js_lang_httpFetcher = null;

function load_js_lang(func) {
	if (js_lang === null) {
		var handler = function(text) {
			js_lang = text.parseJSON();
			func();
		};
		js_lang = false; // Set to false so we don't try again!
		js_lang_httpFetcher = getHTTPObject(handler);
		if (js_lang_httpFetcher === null) return false;
		
		js_lang_httpFetcher.requestData('jslang');

		return true; // It's okay. We are doing a request
	} else if (js_lang === false && js_lang_httpFetcher != null) {
		// Request may be already underway. Add the func to the alert list
		var oldAlert = js_lang_httpFetcher.onreadystatechange;
		js_lang_httpFetcher.onreadystatechange = function() {
			oldAlert();
			func();
		};
		return true;
	} else if (js_lang != false && js_lang != null) {
		// It's been run already
		func();
		return true;
	}
	return false; // Can't do it
}

function showHideTopics(fid, src, doParentDiv){
			
					
			
		var dt = document.getElementById(fid);
		var objParent = null;
		
		if(doParentDiv == 1){
			objParent = src.parentNode.parentNode.parentNode.parentNode.parentNode;
		}
		
		if(dt){
		
			if(src.innerHTML == 'Hide Topics'){
				//then we must hide it
				
				
				dt.style.display = 'none';
				src.innerHTML = 'Show topics';
			
				objParent.style.height = '45px';
				
			}
			else{
				dt.style.display = 'block';
				src.innerHTML = 'Hide Topics';
				objParent.style.height = '400px';
			}
		
		
		}
	
}


//Show the lastopic, enter when user hovers over the topic
function showLastTopic(lpID, e, src){
	var divLastPostTitle = document.getElementById(lpID);
	
	divLastPostTitle.setAttribute("IsUserHolding", true.toString());
	
    var timerID = window.setTimeout(function() {showLastBlock(lpID) },1000);

  
}

//Func called by the timeout occurs
function  showLastBlock(lpID, blnHide){

	var divLastPostTitle = document.getElementById(lpID);

	//only show when it is not displayed as yet, and the user has entered
	if(divLastPostTitle.getAttribute("IsUserHolding") == "true" && divLastPostTitle.style.display != "block"){
		
		//clear all
		//var divs = document.getElementsByName('divLastPost');
		//hideAll(divs); //alert(divs);
    	divLastPostTitle.style.display = "block";
    	//reset it
    	divLastPostTitle.setAttribute("IsUserHolding", false.toString())
	}
	else{
		if(blnHide && divLastPostTitle.getAttribute("hasEntered") != "true"){
			divLastPostTitle.style.display = "none";
    		//reset it
    		divLastPostTitle.setAttribute("IsUserHolding", false.toString())
		}
		
	}

}

//checkIfHide occurs when user leaves the topic
function checkIfHide(lpID){
	
	//check if the is div shown.  If div is shown
	var divLastPostTitle = document.getElementById(lpID);
	//but it hasn't entered the div as yet
	if(divLastPostTitle.style.display == "block"){
		//create a timeout to hide it
		 var timerID = window.setTimeout(function() {showLastBlock(lpID, true); },2000);
	}
    //reset it
    
    

	
	//don't show when the user enters but the timeout has not been called as yet
	divLastPostTitle.setAttribute("IsUserHolding", false);

	
}

function hasEntered(lpID){
	var divLastPostTitle = document.getElementById(lpID);
	
	divLastPostTitle.setAttribute("hasEntered", true.toString());
}

function hideAll(divs){
	
	for(var i = 0; i < divs.length; i++){
		
		var objDiv = divs[i];
		objDiv.style.display = "none";
	}
}



function hideLastTopic(lpID, e){
	var divLastPostTitle = document.getElementById(lpID);
	divLastPostTitle.setAttribute("hasEntered", false.toString());
	 var timerID = window.setTimeout(function() {showLastBlock(lpID, true); },1000);
	
}