﻿var geocoder;
var map;
var anchors = " ";

$(function () {
    //
    var urlParams = getUrlParams();
    var storeIdQs = urlParams["psid"];
    var i = 0;
    if (storeIdQs != undefined) {
        $.ajax({
            type: "GET",
            url: "xml/StoresInfo.xml",
            dataType: "xml",
            success: function (xml) {
                anchors += '<div class="displayAnchor">';
                if ((storeIdQs == 'E0404046') || (storeIdQs == '6E654042') || (storeIdQs == '5D5D4049') || (storeIdQs == '30D64047') || (storeIdQs == '5D844058') || (storeIdQs == '35CE4044')) {
                    $('#Storedetails').append(anchors);
                    $(xml).find('#nellstores StoreAnchor').each(function () {
                        var StoreAnchorName = $(this).find('StoreAnchorName').text();
                        var StoreAnchorLink = $(this).find('StoreAnchorLink').text();
                        anchors += '<span><a href="#' + StoreAnchorLink + '" class="spacerLink">' + StoreAnchorName + '</a></span>';
                    });
                }
                else if ((storeIdQs == '11EE4050') || (storeIdQs == '3D594051')) {
                    $(xml).find('#shurefinestores StoreAnchor').each(function () {
                        var StoreAnchorName = $(this).find('StoreAnchorName').text();
                        var StoreAnchorLink = $(this).find('StoreAnchorLink').text();
                        anchors += '<span><a href="#' + StoreAnchorLink + '" class="spacerLink">' + StoreAnchorName + '</a></span>';
                    });
                }
                $('#Storedetails').append(anchors);
                anchors += '<div class="clear"></div></div>';

                $(xml).find('StoreInfo').each(function () {
                    var psid = $(this).find('StoreId').text();
                    var psid_array = psid.split(",");
                    var store_num = 0;

                    while (store_num < psid_array.length) {
                        if (storeIdQs == psid_array[store_num]) {
                            var stores = $(this).find('Stores');
                            stores.find('Store').each(function () {
                                var StoreUrl = $(this).find('StoreUrl').text();
                                var StoreName = $(this).find('StoreName').text();
                                var StoreL = $(this).find('StoreL').text();
                                var StoreAddress = $(this).find('StoreAddress').text();
                                var StoreHours = $(this).find('StoreHours').text();
                                var StorePhone = $(this).find('StorePhone').text();
                                var StoreFax = $(this).find('StoreFax').text();
                                var StorePharmacy = $(this).find('StorePharmacy').text();

                                if (StoreUrl != "") {
                                    document.location.href = StoreUrl;
                                    return false;
                                }
                                var deptHtml = "";
                                $(this).find('Department').each(function () {
                                    var department = $(this).text();
                                    deptHtml += '<li>' + department + '</li>';
                                }); //close each
                                if (deptHtml == "") {
                                    $('#Departments' + i).css('display', 'none');
                                }
                                var servHtml = "";
                                $(this).find('Service').each(function () {
                                    var service = $(this).text();
                                    servHtml += '<li>' + service + '</li>';
                                }); //close each    
                                var content = " ";
                                content += '<a name="' + StoreL + '" id="' + StoreL + '"></a><center><br /><h1 class="StoreName">' + StoreName + '</h1></center><br /><br /><br /><br /><div class="LeftSide StoreInfo"><h2 class="StoreAddressLabel center">Address</h2><p class="center">' + StoreAddress + '</p><h2 id="StoreHoursLabel' + i + '" class="StoreHoursLabel center">Store Hours</h2><p class="StoreHoursLabel center">' + StoreHours + '</p><h2 id="StorePhoneLabel' + i + '" class="StorePhoneLabel center">Phone</h2><p class="StoreHoursLabel center">' + StorePhone + '</p><h2 id="StorePharmacyLabel' + i + '" class="StorePharmacyLabel">Pharmacy Hours</h2><p class="StoreHoursLabel center">' + StorePharmacy + '</p><div id="Departments' + i + '" class="left Departments"><h2>Departments</h2><ul> ' + deptHtml + '<ul></div><div id="Services' + i + '"class="right Services"><h2>Available Services</h2><ul>' + servHtml + '</ul></div> <div class="clear"></div></div><div class="RightSide GoogleMap"><div id="map' + i + '"></div></div><div class="clear"></div><br /><br />';

                                geocoder = null;
                                map = null;

                                $('#Storedetails').append(content);
                                if (StoreFax == "") {
                                    $("h2#StoreFaxLabel" + i).css('display', 'none');
                                }

                                if (StorePharmacy == "") {

                                    $("h2#StorePharmacyLabel" + i).css('display', 'none');

                                }

                                if (StoreHours == "") {
                                    $("h2#StoreHoursLabel" + i).css('display', 'none');
                                }

                                if (StorePhone == "") {
                                    $("h2#StorePhoneLabel" + i).css('display', 'none');
                                }
                                if (servHtml == "") {
                                    $('#Services' + i).css('display', 'none');
                                }
                                geocoder = new google.maps.Geocoder();
                                var latlng = new google.maps.LatLng(-34.397, 150.644);
                                var myOptions = {
                                    zoom: 8,
                                    center: latlng,
                                    mapTypeId: google.maps.MapTypeId.ROADMAP
                                }
                                map = new google.maps.Map(document.getElementById("map" + i), myOptions);
                                codeAddress(StoreAddress, map);
                                i = i + 1;
                            }); //close each store
                            // }
                        }
                        store_num += 1;
                    }
                }); //close each(
            } //close success
        }); //close $.ajax
    }
});
function codeAddress(address, map) {
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
function getUrlParams() {
    var urlParams = {};
    (function () {
        var e,
        q = window.location.search.substring(1),
        r = /([^&=]+)=([^&]+)/g;
        while (e = r.exec(q))
            urlParams[decodeURIComponent(e[1])] = decodeURIComponent(e[2]);
    })();
    return urlParams;
}
