/*
*   JavaScript for STATEMAP project webpage
*   Florida Geological Survey
*   David Arthur, 2007
*/


// Event engine

var _Event = {
    DOMEvents : new Array(),
    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;
    },
    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);
          _Event.DOMEvents.splice(i,1);
       }
    },
    releaseDOMEvents : function()
    {
       var e = _Event.DOMEvents;
       for(var i=0;i<e.length;i++)
       {
          _Event.releaseDOMEvent(e[i]);
       }
    },
    releaseEventsFromDOMNode : function(s)
    {
       var e = _Event.DOMEvents;
       for(var i=0;i<e.length;i++)
       {
          if(e[i].source == s)
            _Event.releaseDOMEvent(e[i]);
       }
    },
    cancelEvent : function(e)
    {
		if(window.addEventListener)
			e.preventDefault();
		else
			e.returnValue = false;
    }
}

function _EventHandle(a,b,c)
{
    this.source = a;
    this.method = b;
    this.handler = c;
}
_Event.addDOMEvent(window,"unload",_Event.releaseDOMEvents);



//  Functions and objects for the interactive map

var sTitles = ["1995 - Homestead East","1996 - Homestead West","1997 - Sarasota West","1998 - Sarasota / Arcadia","1999 - Arcadia East",
                "2000 - Crestview North","2001 - Crestview South","2002 - Marianna West","2003 - Marianna East","2004 - Gainesville West",
                "2005 - Gainesville East","2006 - Lake City West","2007 - Perry East","2008 - Perry West","2009 - Ocala East","Current Area",
                 "Proposed Area"];
function Coordinate(x,y)
{
    this.x = x;
    this.y = y;
}
function CoordSys()
{
    var titleWin = new TitleWin();
    var that = this;
    var tl = new Coordinate(-88.55,31.61);
    var br = new Coordinate(-79.41,23.60);
    var w = Math.abs(br.x - tl.x);
    var h = Math.abs(br.y - tl.y);
    var o,o_tl,o_w,o_h; //htmlobject properties
    var xx,yy;
    this.apply = function(d) {
        o_tl = new Coordinate(getStyle(d,"top",1),getStyle(d,"left",1));
        o_w = getStyle(d,"width",1);
        o_h = getStyle(d,"height",1);
        xx = o_w/w;
        yy = o_h/h;
        o = d;
        _Event.addDOMEvent(o,"mousemove",function(){
            x = o.clientX-o_tl.x;
            y = o.clientY-o_tl.y;
            
        });
    }
    this.addBox = function(ax,ay,aw,ah,id) {
        var d = document.createElement("div");/*here is where I made changes for the boxes*/

        if(id == 16) // id for the current area
        	d.className = "dim45 e";
        else if(id == 17) // id for proposed area
        	d.className = "dim45 c";
        else if(id)
            d.className = "dim45 a";
        d.setAttribute("id","box"+id);
        d.style.top = Math.floor((tl.y - ay)*yy+6)+"px";
        d.style.left = Math.floor((ax - tl.x)*xx-8)+"px";
        d.style.height = Math.floor(ah*yy)+"px";
        d.style.width = Math.floor(aw*xx-2)+"px";
        d = o.appendChild(d);
        function fixScope()
        {
            var dd = d;
            _Event.addDOMEvent(dd,"mouseover",function(e){
                dd.className = dd.className.replace(/\s[ae]\b/," b"); // only highlight if class name is a or e
                titleWin.move(e.clientX-55,e.clientY-64+document.documentElement.scrollTop);
                titleWin.setTitle(sTitles[id-1]);
                titleWin.delayShow(400);
            });
            _Event.addDOMEvent(dd,"mouseout",function(e){
                var id;
                if(window.attachEvent)
                    id = e.srcElement.id;
                else
                    id = this.id;
                //alert(id);
                // These two IDs (box16 and box17) need to coorespond with the currnet and the proposed areas.
                if(id == "box16")
                    dd.className = dd.className.replace(/\sb\b/," e"); /*check here too*/
                else if(id == "box17")
                    dd.className = dd.className.replace(/\sb\b/," c"); /*check here too*/
                else
                    dd.className = dd.className.replace(/\sb\b/," a");
                titleWin.hide();
                
            });
            _Event.addDOMEvent(dd,"click",function(e){
                //alert(id);
                window.location.href = window.location.href.split("#")[0]+"#"+id;
                setActive(id);
                seemore(id);
            });
        }
        fixScope()
    }
}
function TitleWin()
{
    var that = this;
    var d = document.createElement("div");
    d.className = "d";
    d.style.display = "none";   
    this.htmlobj = document.body.appendChild(d);
    
    var ts;
    this.delayShow = function(t)
    {        
        ts = setTimeout(function(){that.htmlobj.style.display="block"},t);
    }
    this.hide = function() {
        clearTimeout(ts);
        this.htmlobj.style.display="none";
    }
    this.move = function(x,y) {
        this.htmlobj.style.top = y+"px";
        this.htmlobj.style.left = x+"px";
    }
    this.setTitle = function(s)
    {
        this.htmlobj.innerHTML = s;
    }
}
function seemore(n)
{
    var o = document.getElementById("bib"+n).getElementsByTagName("div")[0];
    if (o.style.display == "block")
    {
        o.style.display = "none";
    }
    else
    {
        o.style.display = "block";
        setActive(n);
    }
}
function setActive(n)
{    
    for(var i=1;i<16;i++)
    {
        var e1 = document.getElementById("bib"+i);
        var e2 = document.getElementById("box"+i);
        e1.className = e1.className.replace(/\sactive1\b/,"");
        e2.className = e2.className.replace(/\sactive2\b/,"");      
    }
    var d1 = document.getElementById("bib"+n);
    var d2 = document.getElementById("box"+n);
    d1.className += " active1";
    d2.className += " active2";
}
function init()
{
    var cs = new CoordSys();
    var d = document.getElementById("map");
    cs.apply(d);
    cs.addBox(-80.625,25.5,0.625,0.5,1);
    cs.addBox(-81,25.5,0.375,0.5,2);
    cs.addBox(-83,27.5,0.75,0.5,3);
    cs.addBox(-82.25,27.5,0.629,0.5,4);
    cs.addBox(-81.625,27.5,0.625,0.5,5);
    cs.addBox(-87,31,1.01,0.25,6);
    cs.addBox(-87,30.75,1.01,0.25,7);
    cs.addBox(-86,31,0.5,0.5,8);
    cs.addBox(-85.51,31,0.5,0.5,9);
    cs.addBox(-83,30,0.5,0.5,10);
    cs.addBox(-82.5,30,0.5,0.5,11);
    cs.addBox(-83,30.5,0.5,0.5,12);
    cs.addBox(-83.49,30.5,0.5,0.5,13);
    cs.addBox(-84,30.5,.51,.5,14);
    cs.addBox(-82.5,29.5,0.5,0.5,15); // OcalaEast area
    cs.addBox(-83,29.5,.5,.5,16);  	  // current area - Ocala West
    cs.addBox(-82.5,29,.5,.5,17);     // proposed area - Inverness east

    
}
_Event.addDOMEvent(window,"load",init);


//  Additional functions

function getStyle(element,attribute,options)
{
	var a;
	if(window.getComputedStyle)
		a = window.getComputedStyle(element,attribute)[attribute];
	else
		a = eval("element.currentStyle."+attribute);
	return (options==1)?parseInt(a):a	
}
function rf(){return false;}

Array.prototype.index = function(s) {
   
    var l = this.length;
    for(var i=0;i<l;i++)
       if(this[i] == s)
          return i;
    return -1;
}