// ping function
function remoteCall(url){
        if(navigator.appName == "Microsoft Internet Explorer") {
                http = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
                http = new XMLHttpRequest();
        }
        http.open("GET", url +'?r=' + Math.floor(Math.random()*1000000000), true);
        http.send(null);
} 
// ajax error handler
function ajaxError(requestVars) {
	if(requestVars.error_callback) {
		return requestVars.error_callback;
	} else {
		return function (XMLHttpRequest, textStatus, errorThrown) {
			// The request has failed. We may want to shoot out the request very soon
			// currenlty the timeout handler will handle this and start sending requests after a specified timeout

			log("Error sending AJAX request:\n" + textStatus + "\n" + errorThrown + "\n" + XMLHttpRequest.responseText);
		}
	}
}

function beforeSendFactory(requestVars) {
	return function (XMLHttpRequest) { 
		if(requestVars.timeout_callback) {
			requestVars.timeout_id = setTimeout(
					function () {
						XMLHttpRequest.abort();
						requestVars.timeout_callback();
					},
					requestVars.timeout_time * 1000
			);
		}
		
		requestVars.request = XMLHttpRequest;
	}
}

function successFactory(requestVars) {
	return function(json) {
		if(requestVars.timeout_id) {
			clearTimeout(requestVars.timeout_id);
		}
		
		if(requestVars.success) {
			requestVars.success(json);
		}
	}
}

function requestVars(url, success, timeout_callback, timeout_time, error_callback) {
	return {url: url, success: success, timeout_callback: timeout_callback, timeout_time: timeout_time, error_callback: error_callback};
}

function requestHooksFactory(vars) {
	return {
		cancel: function() {
			vars.request.abort();
			if(vars.timeout_id) {
				clearTimeout(vars.timeout_id);
			}
		}
	}
}

// jquery ajax GET wrapper
function ajax(url, success, timeout_callback, timeout_time, error_callback) {
	var vars = requestVars(url, success, timeout_callback, timeout_time, error_callback);  
	$.ajax({
		type: 		"GET",
		url: 		url,
		dataType: 	"json",
		cache:		false,
		success:	successFactory(vars),
		error:		ajaxError(vars),
		beforeSend:	beforeSendFactory(vars)
	});
	
	return requestHooksFactory(vars);
}

// jquery ajax POST wrapper
function ajaxPost(url,postData,success, timeout_callback, timeout_time, error_callback) {
	var vars = requestVars(url, success, timeout_callback, timeout_time, error_callback);  
	$.ajax({
		type:		"POST",
		url: 		url,
		dataType: 	"json",
		data: 		postData,
		success:	successFactory(vars),	 
		error:		ajaxError(vars),
		beforeSend:	beforeSendFactory(vars)
	});
	
	return requestHooksFactory(vars);
}

function log(msg) { /*if(console) console.log(msg); else alert(msg);*/ } 

function HTMLescape(unsafe_text) {
	return $('<div/>').text(unsafe_text).html();
}

function updateTime(dom_callback) {
	var months = new Array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');

	function updateTimeHelper() {
		var d = new Date();
		var time = {month: months[d.getMonth()], hours: d.getHours(), day: d.getDate(), min: d.getMinutes(), am_pm: 'am'};

		if(time.day < 10) { time.day = '0' + time.day; }
		if(time.hours >= 12) { time.hours-= 12; time.am_pm = 'pm';}
		if(time.hours == 0) { time.hours = 12; }
		if(time.min < 10) { time.min = '0' + time.min; }
		dom_callback(time);
		setTimeout(updateTimeHelper, 15000);
	}
	
	updateTimeHelper();
}
	
function registerButtonClicks() {
	for(var btn in buttons) {
		$("#" + btn).bind("mousedown", {}, buttonClick);
		$("#" + btn).bind("mouseup", {}, buttonClick);
		$("#" + btn).bind("mouseout", {}, buttonClick);
	}
}

function buttonClick(e) {
	var src = '/images/portal/';
	if(e.type == 'mousedown') {
		src += buttons[e.target.id].clicked;
	} else {
		src += buttons[e.target.id].normal;
	}
	$(this).attr("src",src);
}