﻿//allows useful functions on the selected element
function $(value)
{
	var element = value;
	if(typeof value == "string")
	{
		element = document.getElementById(value);
	}
	return Controller.extendForEvents(element);
}

var Controller = {
	extend: function(destination, source) {
	  for (var property in source)
	    destination[property] = source[property];
	  return destination;
	},
	createElement:function(type){
		var ele = document.createElement(type);
		this.extendForEvents(ele);
		return ele;
	},
	extendForEvents: function(ele){
		return Controller.extend(ele,Controller.Event);
	},
	Event:function(){
		var returned = new Object();
		if (document.addEventListener)
		{
			returned.listenOnEvent = function(eventName, handler)
			{
				this.addEventListener(eventName, handler, false);
			}
		}
		else if (document.attachEvent)
		{
			returned.listenOnEvent = function(eventName, handler)
			{
				this.attachEvent("on" + eventName, handler);
			}
		}
		else
		{
			returned.listenOnEvent = function(eventName, handler)
			{
				this["on" + eventName] = handler;
			}
		}
		return returned;
	}(),
	getEvent: function(event)
	{
		if (!event && window.event) {
			event = window.event;
		}
		if (!event.target && event.srcElement)
		{
			event.target = event.srcElement;
		}
		return event;
	}
}

function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}
Controller.extendForEvents(window);

//eventsExtensionMethods:function(){
//		Controller.extend(ele,{listenOnEvent:function(type,func){
//				if(this.addEventListener)
//			    {
//			        this.addEventListener(type,func,false);
//			    }
//			    else if(this.attachEvent)
//			    {
//			        this.attachEvent("on"+type,func);
//			    }
//			    else
//			    {
//			        this["on"+type] = func;
//			    }
//			},
//			endListenOnEvent:function(type,func){
//		        if(this.removeEventListener)
//		        {
//		            return this.removeEventListener(type,func,false);
//		        }
//		        else if(this.detachEvent)
//		        {
//		            return this.detachEvent("on"+type,func);
//		        }
//		        else
//		        {
//		            return this["on"+type] = null;
//		        }
//		    }
//		})
//	}