/*************************************************

Janola - a handy dandy ajax library.
Author: Geoff Munn (geoff@projectwolverine.com)
Copyright (c): 2007 Project Wolverine
Version: 0.9

*************************************************/

var POLL_TIME=2000;

function janola_object(){
	var READY_STATE_UNINITIALIZED=0;
	var READY_STATE_LOADING=1;
	var READY_STATE_LOADED=2;
	var READY_STATE_INTERACTIVE=3;
	var READY_STATE_COMPLETE=4;
	
	var _loadingMessage='';
	var _message_displayed=false;
	var _finished_callback=null;
	var _update_status_callback=null;
	
	var _token=0;
	
	this.load=function janola_manager(url){
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		}
		if (x){
			x.onreadystatechange = function(){
				switch(x.readyState){
					case(READY_STATE_COMPLETE):
						if(x.status==200)
							_buildXMLdata(x);
							
						break;		
					case(READY_STATE_LOADING):
						_update_status(false);
						break;
					default:
						_update_status(false);	
				}
				 
			}
			
			x.open("GET", url, true);
			x.send(null);					
		}
	}
	
	this.loading_message=function set_loading_message(msg){
		_loadingMessage=msg;
	}
	
	this.finished_function=function set_callback_function(func){
		_finished_callback=func;	
	}
	
	this.update_function=function set_update_status_callback_function(func){
		_update_status_callback=func;	
	}
	
	this.set_token=function set_token(token){
		_token=token;
	}
	
	function _update_status(){
		if(!_message_displayed && _update_status_callback){
			_update_status_callback();
			_message_displayed=true;
		}
	}
	
	function _buildXMLdata(x){
		if(_finished_callback){
			var results=x.responseXML;
			if(_finished_callback!=null)
				_finished_callback(results, _token);
		}
	}	
	
}

function timer_object(){
	var start_time=new Date();
	
	this.create_timer=function create_timer(wait_time, callback){
		_timer(start_time.getTime(), wait_time, callback);	
	};
}

function _timer(start_time, wait_time, callback){
	var current_time=new Date();
	if((current_time.getTime()-start_time)>wait_time){
		callback();
	} else {
		setTimeout('_timer(' + start_time + ', ' + wait_time + ', ' + callback + ')', POLL_TIME);
	}
}

function clear_nodes(el){
	if(el){
		if(el.hasChildNodes){
			while (el.hasChildNodes()){
			  el.removeChild(el.firstChild);
			}	
		}
	}
}

function add_generic_node(el, node_type, html_args, css_args, pos, before_el){
	
	var childEl=document.createElement(node_type);
	
	pos=pos || 'after';
	
	var attributes=Array();
	
	for(property in html_args){
		switch(property){
			case 'text':
				//we need to do a switcheroony on &amp; - but how?
				text=html_args['text']
				text=text.replace(/&lt;/gi, '\u003c');
				text=text.replace(/&gt;/gi, '\u003e');
				text=text.replace(/&quot;/gi, '\u0022');
				text=text.replace(/&amp;/gi, '\u0026');
				
				txtNode=document.createTextNode(text);
				childEl.appendChild(txtNode);	
				break;
			
			default:
				value=html_args[property];
				prop=property;
				if(prop=='className' || prop=='classname')
					prop='class';
								
				childEl.setAttribute(prop, value);
		}
			
	}
	
	for(property in css_args){
		//NOTE: all stylesheet attributes must be in the javascript format, ie fontFamily, not font-family
		eval('childEl.style.' + property + "='" + css_args[property] + "'");
	}
	
	if(pos=='after')
		el.appendChild(childEl);
	
	if(pos=='before')
		el.insertBefore(childEl, before_el);
	
	return childEl;

}

function add_element(el, node_name, html_args, css_args, pos, before_el){
	
	//Div is the default element
	node_name=node_name || 'div';
	
	pos=pos || 'after';
	
	var new_node=add_generic_node(el, node_name, html_args, css_args, pos, before_el);
	
	return new_node;
	
}

function add_text(el, text){
	if(el && text!=''){
		var txtNode=document.createTextNode(text);
		el.appendChild(txtNode);
	}
}

function set_attribute(el, attribute_name, attribute_value){
	if(el){
		el.setAttribute(attribute_name, attribute_value);
	}
}

function set_style_attribute(el, attribute_name, attribute_value){
	if(el){
		eval('el.style.' + attribute_name + "='" + attribute_value + "'");
	}
}