/* function $
	-shorthand for document.getElementById('element');
*/
function $(element){
	return document.getElementById(element);
}

/**
 * Temporary function to set up zoomToLastExtent
 * Once History is properly added to Openlayers we can get rid of this and modify the zoomToLastExtent tool to use native OL History functionality
 */
function setupZTLE(map)
{
    //Array to store history of extents
    map.extents = [];
    
    var func = function()    {
        map.extents.push(this.getExtent());
    }
    map.events.register("moveend", map, func );

    map.zoomToLastExtent = function() {
        if(map.extents.length > 1)        {
            var tmp = map.extents.pop();
            var last_extent = map.extents.pop();
            this.zoomToExtent(last_extent);
            /*console.log(map.extents.length);*/
        }
        else{
            map.zoomToMaxExtent();
            map.extents.pop();map.extents.pop();//original extent should leave extents empty
        }
    }
}

//sets up a cross-browser XMLHttpRequest object
function newXHR(){
    var r;
    try{
        r = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e){
        try{
            r = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(f){
            r = null;
        }
    }
    if( !r && typeof XMLHttpRequest != "undefined" ){
        r = new XMLHttpRequest();
    }
    return r;
}

Array.prototype.index = function(s){
    var l = this.length;
    for(var i=0;i<l;i++)
       if(this[i] == s)
          return i;
    return -1;
}

/**
 *      Event handling object. Uses the native Event listening/attaching as well as keeping
 *      and array of the event source, method, and handler for later deconstruction. Also
 *      includes a custom event scheme which is explained in detail below.
 *      @author David Arthur, 2007
 *      @class   
 */
var _Event = {
    DOMEvents : new Array(),
    customEvents : new Array(),
    /**
     *  Adds a DOM event to an HTML element. Valid events are things like mousedown, mouseup,
     *  mousemove, click, dblclick, etc. N.B. the "on" is ommited when specifying the 
     *  event method. See http://www.w3schools.com/jsref/jsref_events.asp for valid events.
     *  @param {HTMLElement} a This is the element to which you want to attach an event
     *  @param {String} b This is the event method - "mousemove","click","keypress",etc
     *  @param {Function} c This is the event handler. This function will be called when the event is fired
     *  @returns #EventHandle
     *  @see _Event#releaseDOMEvent
     *  @see _Event#releaseDOMEvents
     *  @see _Event#releaseEventsFromDOMNode
     */
    addDOMEvent : function(a,b,c) {
        if(window.addEventListener)
            a.addEventListener(b,c,true);
        else if(window.attachEvent)
            a.attachEvent('on'+b,c);  
        else
            eval("a.on"+b+" = c");  
        var h = new _EventHandle(a,b,c)    
        _Event.DOMEvents.push(h);
        return h;
  
    //	eval("a.on"+b+" = c");
    },
    /**
     *  Adds a custom event to an object. This is primarily used for giving the user (programmer) a
     *  way to emulate a chain of events, or to schedule certain things to happen at certain times. 
     *
     *  Usage: Event name "myEvent", Function H(s)
     *  addCustomEvent("myEvent",H(s));
     *  ...
     *  else where in the code
     *  ...
     *  fireCustomEvent(S,"myEvent"); 
     *  
     *  This will cause H(s) to be called with the object S passed to it. 
     *  Custom events like this are very versitile and can be
     *  used to accomplish all sorts of fun things.
     *  @param {String} b The event method. Also, anything you want - hence custom
     *  @param {Function} c The event handler. This gets called when you fire your custom event.
     *  @returns EventHandle
     */
    /*addCustomEvent : function(a,b,c) {
        var h = new _EventHandle(a,b,c);
        _Event.customEvents.push(h);
        return h; 
    },*/
    addCustomEvent : function(b,c) {
        var h = new _EventHandle(null,b,c);
        _Event.customEvents.push(h);
        return h; 
    },
    /**
     *  Release a DOM event.
     *  @param {EventHandle} h An EventHandle object. These are returned by addDOMEvent.
     *  @see _Event#addDOMEvent
     */ 
    releaseDOMEvent : function(h) {
       var i = _Event.DOMEvents.index(h);
       if(i != -1) {
          if(window.removeEventListener)
             h.source.removeEventListener(h.method,h.handler,true);
          else
             h.source.detachEvent("on"+h.method,h.handler);
          h.active = false;
       }
    },
    /**
     *  Releases all DOM events set through addDOMEvent
     *  @see _Event#addDOMEvent
     */
    releaseDOMEvents : function() {
       var e = _Event.DOMEvents;
       var l = e.length;
       for(var i=0;i<l;i++)
         _Event.releaseDOMEvent(e[i]);
    },
    /**
     *  Releases all events attached to a particular DOM Node (HTMLElement)
     *  @param {HTMLElement} s The node which you want to strip of its precious events.
     */
    releaseEventsFromDOMNode : function(s) {
       var e = _Event.DOMEvents;
       for(var i=0;i<e.length;i++)
          if(e[i].source == s)
            _Event.releaseDOMEvent(e[i]);
    },
    releaseCustomEvent : function(h) {
        var i = _Event.customEvents.index(h);
        if(i != -1)
            _Event.customEvents.splice(i,1);
    },
    cancelEvent : function(e) {
        if(window.addEventListener)
	    e.preventDefault();
	else
	    e.returnValue = false;
    },
    /*fireCustomEvent : function(a,b) {
        for(var i=0;i<_Event.customEvents.length;i++){
            if(_Event.customEvents[i][0] == a && _Event.customEvents[i][1] == b)
                _Event.customEvents[i][2]();
        }
    }*/
    fireCustomEvent : function(a,b) 
    {
        for(var i=0;i<_Event.customEvents.length;i++)
        {
            if(_Event.customEvents[i].method == b)
            {
                _Event.customEvents[i].handler(a);
            }
        }
    }
}

function _EventHandle(a,b,c){
    this.source = a;
    this.method = b;
    this.handler = c;
    this.active = true;
}
_Event.addDOMEvent(window,"unload",_Event.releaseDOMEvents);


/*Get Style*/
function getStyle(element,attribute,isInt){
    /*if isInt is set to 1, then return the found style as an int*/
    var a;

    if(window.getComputedStyle)
        a = window.getComputedStyle(element,attribute).getPropertyValue(attribute);
    else
        a = eval("element.currentStyle."+attribute);
    return (isInt==1)?parseInt(a):a
}

function infoPopUp(link, title)
{
	if (! window.focus)return true;
        	var href;
    	if (typeof(link) == 'string')
       		href=link;
	else
		href=link.href;
	window.open(href, title, 'width=500,height=400,scrollbars=yes');
	return false;
}
