var aRS = "";
var aStores = "";
var oLocation;
var iCurrentPage = 1;
var iNumRec = 0;
var map;
var oButton;
var oSelectedStore = null;
var selectedColor = "#f6cb76";
var highlightedColor = "#d0d0d0";
var currentMarker;
var currentOverlay;

function suggestStores(oThisButton, sLocation) {

    oButton = oThisButton;
    oButton.disabled=true;
    
    var oXmlHttp = zXmlHttp.createRequest();
    oLocation = document.getElementById(sLocation);
    oMap = document.getElementById('map');
    
    var oData = {
        location: oLocation.value
    };
    
    iNumRec = 0;
    GUnload();
    oMap.innerHTML = "";
    oMap.style.backgroundColor = '';
    
    oXmlHttp.open("POST","suggeststores.php", true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {         
                aRS = JSON.parse(oXmlHttp.responseText);
                aStores = aRS.stores;
                iNumRec = aRS.num_rec;
                var oStoreContainer = document.getElementById('storecontainer');
                if(aRS.error != null) {
                    oStoreContainer.innerHTML = aRS.error;
                    showlayer('stores');
                }
                else if(iNumRec == 0) { 
                    oStoreContainer.innerHTML = "No nearest retail store found.";
                    showlayer('stores');
                }
                else {
                    oMap.innerHTML = "Loading...<br /><img src=\"images/indicator_white.gif\" />";
                    showPage(oStoreContainer);
                    showlayer('stores');
                    setTimeout(function () {
                        showMap(0);
                    },1000);
                }
            }
            oButton.disabled = false;
        }

    };
    oXmlHttp.send(JSON.stringify(oData));
};

function suggestPromoStores(sID) {
    
    var oXmlHttp = zXmlHttp.createRequest();
    oMap = document.getElementById('map');
    
    var oData = {
        location: sID,
        promo: 1
    };
    
    iNumRec = 0;
    GUnload();
    oMap.innerHTML = "";
    oMap.style.backgroundColor = '';
    
    oXmlHttp.open("POST","suggeststores.php", true);
    oXmlHttp.onreadystatechange = function () {
        if(oXmlHttp.readyState == 4) {
            if(oXmlHttp.status == 200) {         
                aRS = JSON.parse(oXmlHttp.responseText);
                aStores = aRS.stores;
                iNumRec = aRS.num_rec;
                var oStoreContainer = document.getElementById('storecontainer');
                if(aRS.error != null) {
                    oStoreContainer.innerHTML = aRS.error;
                    showlayer('stores');
                }
                else if(iNumRec == 0) { 
                    oStoreContainer.innerHTML = "No nearest retail store found.";
                    showlayer('stores');
                }
                else {
                    oMap.innerHTML = "Loading...<br /><img src=\"images/indicator_white.gif\" />";
                    showPage(oStoreContainer);
                    showlayer('stores');
                    setTimeout(function () {
                        showMap(0);
                    },1000);
                }
            }
        }

    };
    oXmlHttp.send(JSON.stringify(oData));
};

function setBg(oStyle,color) {

    if(oStyle == oSelectedStore) {
        thisColor = oSelectedStore;
    }
    else {
        thisColor = color;
    }
    oStyle.style.backgroundColor = thisColor;
    
};

function showPage(oContainer) {

    var sContent = "";
    oContainer.innerHTML = "";
    
    for(var i=0;i<iNumRec;i++) {
        sContent += "<p id=\"store" + i + "\" class=\"storeText\" onclick=\"moveToNewLocation(" + i + ",this,'" + selectedColor + "')\" style=\"cursor:pointer;\" onmouseover=\"setBg(this,'" + highlightedColor + "');\" onmouseout=\"setBg(this,'');\"><b>" + aStores[i].name;
        if(aStores[i].distance) {
            sContent += " (" + aStores[i].distance + " miles)";
        }
        sContent += "</b><br />" + aStores[i].street + "<br />" + aStores[i].city + ", " + aStores[i].state + " " + aStores[i].zip + "<br />" + aStores[i].phone.replace(/(\d{3})(\d{3})(\d{4}).*/,"($1) $2-$3");
        if(aStores[i].website != "") {
            sContent +=  "<br />Website: <a href=\"http://" + aStores[i].website + "\" target=\"_blank\">" + aStores[i].website + "</a>";
        }
        sContent += "</p>\n";
    }
    oContainer.innerHTML = sContent;
};

function showMap(iRowID) {
    var sLatitude = aStores[iRowID].lat;
    var sLongitude = aStores[iRowID].lng;
    var oStore = document.getElementById("store" + iRowID);
    
    oStore.style.backgroundColor = selectedColor;
    oSelectedStore = oStore;
    
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        var point = new GLatLng(sLatitude,sLongitude);

        map.addControl(new GSmallMapControl());
        map.addControl(new GOverviewMapControl());
        map.setCenter(point, 14);
        currentMarker = createMarker(new GLatLng(sLatitude, sLongitude), aStores[iRowID]);
        currentOverlay = map.addOverlay(currentMarker);
        showlayer('mapContainer');
    } 
};

function moveToNewLocation(iRowID,oStore,color) {
    if(oSelectedStore != null) {
        oSelectedStore.style.backgroundColor = '';
    }
    oStore.style.backgroundColor = color;
    oSelectedStore = oStore;
    map.removeOverlay(currentMarker);
    currentMarker = createMarker(new GLatLng(aStores[iRowID].lat, aStores[iRowID].lng), aStores[iRowID]);
    map.addOverlay(currentMarker);
    map.setCenter(new GLatLng(aStores[iRowID].lat,aStores[iRowID].lng), 14);
};

function createMarker(point, aData) {
    var marker = new GMarker(point,aData.name);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindow("<b>" + aData.name + "</b><br />" + aData.street + "<br />" + aData.city + ", " + aData.state + " " + aData.zip);
    });
    
    return marker;
};