(function() {

var xmlHttp;
var link;
var timer;
var timerContainerid;
var loadingText;
var timerDelay     = 1000;       //milliseconds between loadingTimer updates 1000 = 1 sec
var timerAddOn     = '.';        //text added onto loadingText for every loadingTimer update
var highlightOn    = 'red';      //color of the answer border when the mouse is over the answer
var highlightOff   = '#FFFFFF';  //color when the mouse goes off the answer
var xmlHttpCurrent = false;

function getXmlHttpObject() {
	var xmlHttp = null;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (xmlHttp == null) {
		alert ("Oop! Browser does not support HTTP Request.")
		return;
	}
	return xmlHttp;
}

function register(wpurl, containerid, action, id, answer, category) {
	xmlHttp        = getXmlHttpObject();
    xmlHttpCurrent = true;

	var url = wpurl;
	url += "?action=abq_ajax_" + action;
	url += "&abq_id=" + id;
	url += "&abq_answer=" + answer;
	url += "&abq_category=" + category;
	
	if('click'==action) {
	    xmlHttp.onreadystatechange = function(){delayedRedirect()};
	} else {
	    if('related_content'==action) {
	        xmlHttp.onreadystatechange = function(){loadContent(containerid)};
	    } else {
	        var loadingText = ('view'==action) ? 'Loading Quiz...' : 'Registering Answer...' ;
    	    if('priority'==category) containerid += "-priority";
            startLoadingTimer(containerid, loadingText);
	        xmlHttp.onreadystatechange = function(){loadingContent(containerid)};
    	}
	}
	
	try {
    	xmlHttp.open("GET", url, true);
    	xmlHttp.send(null);
    } catch(e) {
        stopLoadingTimer();  //stop this in case it had been started
        var errorText = "<span class='abq-error'>AJAX error connecting to : </span><br>" + 
            url + '<br><a href="javascript:void(0);" onClick="' + 
            "ABQJS.register('" + wpurl + "', '" + action + "', '" + id + "', '" + link + "');" + 
            '">Click here to try again...</a>';
        showContent(containerid, errorText);
        xmlHttpCurrent = false;
    }
}

function view(wpurl, id, category) {
    if(xmlHttpCurrent === true) {
        var viewTimer = setTimeout("ABQJS.view('"+wpurl+"', '"+id+"', '"+category+"')", 300);
    } else {
        register(wpurl, 'abq', 'view', id, '', category);
    }
}

function answer(wpurl, id, answer, category) {
    if(xmlHttpCurrent === true) {
        var answerTimer = setTimeout("ABQJS.answer('"+wpurl+"', '"+id+"', '"+answer+"', '"+category+"')", 300);
    } else {
        register(wpurl, 'abq-content', 'answer', id, answer, category);
        var relatedContentTimer = setTimeout("ABQJS.related_content('"+wpurl+"', '"+id+"')", 300);
    }
}

function related_content(wpurl, id) {
    if(xmlHttpCurrent === true) {
        var relatedContentTimer = setTimeout("ABQJS.related_content('"+wpurl+"', '"+id+"')", 300);
    } else {
        register(wpurl, 'abq-related-content', 'related_content', id);
    }
}

function click(wpurl, id, answer, url) {
    if(xmlHttpCurrent === true) {
        var clickTimer = setTimeout("ABQJS.click('"+wpurl+"', '"+id+"', '"+answer+"', '"+url+"')", 300);
    } else {
        link = url;
        register(wpurl, '', 'click', id, answer);
    }
}

function delayedRedirect() {
    if (xmlHttp.readyState < 4) {
		document.body.style.cursor = 'wait';
	} else if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
		document.location.href = link;
	}
}

function loadingContent(containerid) {
	if (xmlHttp.readyState < 4) {
		document.body.style.cursor = 'wait';
		loadingText += timerAddOn;
		showContent(containerid, loadingText);
	} else if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
	    stopLoadingTimer();
	    loadContent(containerid);
	}
}

function loadContent(containerid) {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
	    showContent(containerid, xmlHttp.responseText);
        document.body.style.cursor = 'auto';
        xmlHttpCurrent = false;
	}
}

function startLoadingTimer(containerid, loading) {
    timerContainerid = containerid;
    loadingText = (loading == undefined) ? "Loading..." : loading ;
    loadingTimer();
}

function stopLoadingTimer() {
    clearTimeout(timer);
}

function loadingTimer() {
    showContent(timerContainerid, loadingText);
    loadingText += timerAddOn;
    timer = setTimeout("ABQJS.loadingTimer()", 1000);
}

function showContent(containerid, body) {
    var container = document.getElementById(containerid);
    container.innerHTML = body;
}

function highlight(which, action) {
    var color = ('on'==action) ? highlightOn : highlightOff ;
    which.style.borderColor = color;
}
    

window['ABQJS'] = {};
window['ABQJS']['view'] = view;
window['ABQJS']['answer'] = answer;
window['ABQJS']['click'] = click;
window['ABQJS']['register'] = register;
window['ABQJS']['highlight'] = highlight;
window['ABQJS']['related_content'] = related_content;

})();

