﻿YAHOO.namespace("SitNSleep.Widget");

(function() {

    YAHOO.SitNSleep.Widget.LocationMap = function(container, stores, cfg) {
        var cfg = (null == cfg) ? {} : cfg;
        this.stores = stores;
        this.container = YAHOO.util.Dom.get(container);
        this.geocoder = new google.maps.Geocoder();
        this.zipCode = this.updateZipCode();

        var myLatlng = new google.maps.LatLng(33.904616, -118.138733); //LA Basin
        this.mapOptions = {
            zoom: 8,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        this.map = new google.maps.Map(this.container, this.mapOptions);
        var iWin = new google.maps.InfoWindow({
            content: "<div><strong>Sit &lsquo;n Sleep " 
        });
        
        if (this.stores) {

            for (var i = 0; i < this.stores.length; i++) {
                //this.addMarkerForStore(this.stores[i]);
                //addMarkerForStoreLatLong(this.stores[i]);



                var store = this.stores[i];
                myLatlng = new google.maps.LatLng(store.latitude, store.longitude); //LA Basin

                var marker = new google.maps.Marker({
                    title: store.name,
                    position: myLatlng,
                    map: this.map,
                    icon: 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',
                    html: "<div><strong>Sit &lsquo;n Sleep "
            + store.name + "</strong></div>"
            + store.streetAddress + "<br/>"
            + store.city + ", " + store.state + " "
            + store.zipCode + "<br/><strong>"
            + ((store.phone) ? store.phone + "</strong>" : "")
                });



                

                var iWin = new google.maps.InfoWindow({
                    content: "<div><strong>Sit &lsquo;n Sleep "
            + store.name + "</strong></div>"
            + store.streetAddress + "<br/>"
            + store.city + ", " + store.state + " "
            + store.zipCode + "<br/><strong>"
            + ((store.phone) ? store.phone + "</strong>" : "")
                });


           

            //var f = function(marker, point, map) { return function() { alert(marker.value); map.openInfoWindowHtml(point, getResultInfoContent(marker.value)); } }  

                google.maps.event.addListener(marker, 'click', function() {
                
                iWin.setContent(this.html);
                iWin.open(this.map, this);  

                });i

            }
        }

        if (this.zipCode) {
            this.addUserZipCodeMarker(this.zipCode);
        }
    };

    function addMarkerForStoreLatLong(store) {


        //myLatlng = new google.maps.LatLng(store.latitude,store.longitude); //LA Basin

        myLatlng = new google.maps.LatLng(33.9855680, -118.1334910); //LA Basin


        var iWin = new google.maps.InfoWindow({
            content: "<div><strong>Sit &lsquo;n Sleep "
            + store.name + "</strong></div>"
            + store.streetAddress + "<br/>"
            + store.city + ", " + store.state + " "
            + store.zipCode + "<br/><strong>"
            + ((store.phone) ? store.phone + "</strong>" : "")
        });


        var marker = new google.maps.Marker({
            position: myLatlng,
            map: this.map,
            icon: 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',
            infoWindow: iWin


        });

        google.maps.event.addListener(marker, 'click', function() {
            iWin.open(this.map, marker);
        });



    }

    function sleep(delay) {

        var start = new Date().getTime();
        while (new Date().getTime() < start + delay);
    };

    YAHOO.SitNSleep.Widget.LocationMap.prototype = {

        setZipCode: function(zipCode) {
            var expirationDate = new Date();
            expirationDate.setDate(expirationDate.getDate() + 365); //expires one year from now
            YAHOO.util.Cookie.set("zipCode", zipCode, {
                expires: expirationDate
            });
        },

        updateZipCode: function() {
            var zipReg = new RegExp("[\\?&]zipCode=([^&#]*)");
            var query = zipReg.exec(window.location.href);
            var zipCode = null;

            if (query) {
                zipCode = query[1];
            }

            if (!zipCode || "" == zipCode) {
                zipCode = YAHOO.util.Cookie.get("zipCode");
            } else {
                this.setZipCode(zipCode);
            }

            return zipCode;
        },

        addMarkerForStore: function(store) {
            this.geocoder.geocode({ 'address': store.streetAddress + ", " + store.city + ", " + store.state + " " + store.zipCode },

        this.addMarker(store.name, {
            icon: 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png',
            infoWindow: new google.maps.InfoWindow({
                content: "<div><strong>Sit &lsquo;n Sleep "
            + store.name + "</strong></div>"
            + store.streetAddress + "<br/>"
            + store.city + ", " + store.state + " "
            + store.zipCode + "<br/><strong>"
            + ((store.phone) ? store.phone + "</strong>" : "")
            })
        }
        )
      );
        },

        //Creates a closure around the addMapMarker function with the necessary vars
        addMarker: function(title, cfg) {
            var cfg = (null == cfg) ? {} : cfg;
            var title = title;
            var map = this.map;
            function addMapMarker(results, status) {
                //sleep(1000);
                if (status == google.maps.GeocoderStatus.OK) {
                    if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                        //map.set_center(results[0].geometry.location);
                        var options = {
                            map: map,
                            title: title,
                            position: results[0].geometry.location
                        };
                        if (cfg.icon) {
                            options.icon = cfg.icon;
                        }


                        var marker = new google.maps.Marker(options);
                        if (cfg.infoWindow) {
                            google.maps.event.addListener(marker, 'click', function() {
                                cfg.infoWindow.open(map, marker);

                            });
                        }
                    } else {
                        alert("Zip code not found");
                    }
                } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {

                    //alert("OVER_QUERY_LIMIT");
                    //do nothing, it seems to call the callback again when the query limit is reset (flood)
                }
                else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            }

            return addMapMarker;
        },

        addUserZipCodeMarker: function(zipCode) {
            this.geocoder.geocode({ 'address': zipCode }, this.addMarker(zipCode));
        }

    };

})();






