﻿// JScript File

    var map;
    var sunicon = new GIcon();
    var markers = new Array();
    sunicon.image = "/images/sun.png";
    sunicon.shadow = "/images/sun.png";
    sunicon.iconSize = new GSize(22,22);
    sunicon.shadowSize = new GSize(0,0);
    sunicon.iconAnchor = new GPoint(11,11);
    sunicon.infoWindowAnchor = new GPoint(5,1);
    
    var regionList = {
        "caribbean": {"swlat":8.494, "swlon":-92.109, "nelat":31.5785, "nelon":-56.777},
        "mexico": {"swlat":11.178, "swlon":-117.6855, "nelat":33.870, "nelon":-82.529},
        "canada": {"swlat":34.452, "swlon":-125.683, "nelat":64.997, "nelon":-54.492},
        "western": {"swlat":29.152, "swlon":-127.968, "nelat":48.224, "nelon":-92.460},
        "northeast": {"swlat":36.17335, "swlon":-81.3427, "nelat":45.429, "nelon":-63.588},
        "hawaii": {"swlat":17.957, "swlon":-162.158, "nelat":23.684, "nelon":-153.237},
        "alaska": {"swlat":51.399, "swlon":-163.388, "nelat":64.244, "nelon":-127.880}
    };
    
    window.onload = function() {
        Nifty("div.feature", "big transparent");
        Nifty("div#mapcontainer","big bottom tr transparent fixed-height");
    }
        
    function load() {
        if(GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("googlemap"));
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.setCenter(new GLatLng(42.03297, -110.37109), 2);
            GEvent.addListener(map, "click", handleClick);
            GEvent.addListener(map, "zoomend", handleZoom);

            for(var i in locationList) {
                var point = new GLatLng(locationList[i].latitude, locationList[i].longitude);

                var region = getSubRegion(locationList[i]);
                if(region == null) {
                    markLocation(locationList[i].storeid, locationList[i].storename, point);
                } else {
                    markLocation(locationList[i].storeid, region.name, point);
                }
            }
        } else {
            document.getElementById("googlemap").innerHTML = "Sorry, but your browser does not support Google maps.";
        }
    }
    
    function getSubRegion(location) {
        var maxRegion = null;
        var zoom = map.getZoom();
        for(var j=0; j<location.regions.length; j++) {
            var region = subRegionList[location.regions[j]];
            if(region.zoomlevel != null) {
                if(!(zoom >= region.zoomlevel)) {
                    if(maxRegion == null) {
                        maxRegion = region;
                    }
                    else if(maxRegion.zoomlevel > region.zoomlevel) {
                        maxRegion = region;
                    }
                }
            }
        }

        if(maxRegion == null) {
            return null;
        }

        return (maxRegion.zoomlevel > zoom ? maxRegion : null);
    }

    function handleClick(overlay, point) {
	var zoom = map.getZoom();
        if(overlay && overlay.id) {
            var maxRegion = getSubRegion(locationList[overlay.id]);
            
            if(maxRegion ==  null) {
                loadStoreDetail(overlay.id);
            } else {
                map.setCenter(new GLatLng(maxRegion.latitude, maxRegion.longitude), maxRegion.zoomlevel);
            }
        }
    }
    
    function handleZoom(oldLevel, newLevel) {
        for(var i=0; i<markers.length; i++) {
            var marker = markers[i];
            var region = getSubRegion(locationList[marker.id]);
            
            if(region == null) {
                document.getElementById("mtgt_"+marker.id).title = locationList[marker.id].storename;
            } else {
                document.getElementById("mtgt_"+marker.id).title = region.name;
            }
        }
    }

    function markLocation(id, name, point) {
        if(!point) {
            return false;
        }
        
        var marker = new GMarker(point, {icon: sunicon, title: name});
        marker.id = id;
        map.addOverlay(marker);
        markers.push(marker);
    }

    function zoomRegion(region) {
        var regionBound = new GLatLngBounds(new GLatLng(regionList[region].swlat,regionList[region].swlon), new GLatLng(regionList[region].nelat, regionList[region].nelon));
        map.setCenter(regionBound.getCenter());
        map.setZoom(map.getBoundsZoomLevel(regionBound));
    } 
    
    function initializeAjax() {
        var xmlHttp;
        try
        {
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
        }
        catch (e)
        {
            // Internet Explorer
            try
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e)
                {
                    return null;
                }
            }
        }
        return xmlHttp;
    }
    
    function loadStoreDetail(id) {
        var storeDetail = document.getElementById('storedetail');
        
        if(!locationList[id]) {
            alert("Store ID " + id + " does not exist");
            return false;
        }
        
        if(locationList[id].detail == null) {
            var xmlHttp = initializeAjax();
            var url="/Locations/storedetail.aspx?id=" + id;
            xmlHttp.onreadystatechange = function() {
                if(xmlHttp.readyState == 4) {
                    if(xmlHttp.status == 200) {
                        storeDetail.innerHTML = xmlHttp.responseText;
                        locationList[id].detail = xmlHttp.responseText;
                        document.title = "Del Sol - Store Locations - " + locationList[id].storename;
                    } else {
                        storeDetail.innerHTML = "A fatal error occurred while retreiving the store information. Our support team has been notified.";
                    }
                } else {
                    storeDetail.innerHTML = "Loading store information. Please wait";
                }
            }
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
        } else {
            storeDetail.innerHTML = locationList[id].detail;
            document.title = "Del Sol - Store Locations - " + locationList[id].storename;
            Nifty("div#mapcontainer","big bottom tr transparent");
        }
        loadStoreInfo(id);
    }
    
    function loadStoreInfo(id) {
        var storeInfo = document.getElementById('storeinfo');
        if(locationList[id].info == null) {
            var xmlHttp = initializeAjax();
            var url="/Locations/storeinfo.aspx?id=" + id;
            xmlHttp.onreadystatechange = function() {
                if(xmlHttp.readyState == 4) {
                    if(xmlHttp.status == 200) {
                        storeInfo.innerHTML = xmlHttp.responseText;
                        locationList[id].info = xmlHttp.responseText;
                        Nifty("div#mapcontainer","big bottom tr transparent");
                    } else {
                        storeInfo.innerHTML = "A fatal error occurred while retreiving the store information. Our support team has been notified.";
                    }
                } else {
                    storeInfo.innerHTML = "Loading store information. Please wait";
                }
            }
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
        } else {
            storeInfo.innerHTML = locationList[id].info;
            Nifty("div#mapcontainer","big bottom tr transparent");
        }
    }