﻿/*
 * (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
 * Special thanks to Dan Webb's domready.js Prototype extension
 * and Simon Willison's addLoadEvent
 *
 * For more info, see:
 * http://www.thefutureoftheweb.com/blog/adddomloadevent
 * http://dean.edwards.name/weblog/2006/06/again/
 * http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * 
 *
 * To use: call addDOMLoadEvent one or more times with functions, ie:
 *
 *    function something() {
 *       // do something
 *    }
 *    addDOMLoadEvent(something);
 *
 *    addDOMLoadEvent(function() {
 *        // do other stuff
 *    });
 *
 */
 
addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();

function initLinks() {
  for (i in document.links) {
    link = document.links[i];
    if (link.rel && link.rel.indexOf('external')!=-1) {
      link.onclick = onExternalLinkActivate;
      link.onkeypress = onExternalLinkActivate;
    }
  }
}

function onExternalLinkActivate() {
  window.open(this.href);
  return false;
}

addDOMLoadEvent(initLinks);

addDOMLoadEvent(function() {
    // find the map container
    var map = document.getElementById("map");    
    // if map exists on the page
    if (map) {    
        // create popup container and give it an ID
        var mapDetail = document.createElement("div");
	    mapDetail.setAttribute("id", "map-detail");
	    // find the body element and add popup container as a child
	    var body_element = document.getElementsByTagName("body")[0];
	    body_element.appendChild(mapDetail);    	
	    mapDetail = document.getElementById("map-detail");
        
        // find all marker anchors within the map
        var markers = map.getElementsByTagName("a");
        for (var i=0; i < markers.length; i++) {
            var marker = markers[i];
            // add event to show popup
            marker.onmouseover = function() {
                showMapDetail(this, mapDetail);
            }
        }
    }
    
    function showMapDetail(marker, container) {
        // find the spans inside the active anchor element
        var spans = marker.getElementsByTagName("span");
        var woodUrl = marker.getAttribute("href");
        var store, wood, blurb;
        for (var i=0; i < spans.length; i++) {
            var span = spans[i];
            // find the right spans inside the anchor and get their values
            switch (span.className) {
                case "store":
                    store = span.innerHTML;
                    break;
                case "wood":
                    wood = span.innerHTML;
                    break;
                case "blurb":
                    blurb = span.innerHTML;
                    break;
            }
        }
        // write the text into the marker detail container and show
        container.innerHTML = "<span class=\"store\">" + store + "</span> <span class=\"blurb\">" + blurb + "</span> <a onclick=\"window.open( this.href ); return false;\" href=\"" + woodUrl + "\" class=\"wood\">" + wood + "</a> <a <a onclick=\"window.open( this.href ); return false;\" href=\"http://www.woodland-trust.org.uk/woods/index.htm\" class=\"search\">Search other woods</a>";        
        // get the x,y of the anchor
        var coors = findPos(marker);
        var top = coors[1] - container.offsetHeight;
        // centre the popup above the anchor
		var left = coors[0] - (container.offsetWidth / 2) + (marker.offsetWidth / 2);       
        container.style.top = top + "px";
		container.style.left = left + "px";
    }
    
    function hideMapDetail(container) {
        // hide popup
        container.style.left = "-9999px";
    }
    
    function findPos(obj) {
	var curleft = curtop = 0;
	// if the element has a parent
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		// continue up the DOM tree through all parent elements
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
});