﻿(function ($) {
    $.fn.storelocator = function (options, value) {
        if (typeof options === "string") {
            switch (options) {
                case 'showstore':
                    $('#searchWidget0').hide();
                    $('#selectedStore0').show();
                    $.getStoreLocatorSelectedStore(this, value);
                    break;
                case 'showsearch':
                    $('#searchWidget0').show();
                    $('#selectedStore0').hide();
                    break;
            }
        } else {
            var settings = $.extend({
                cityDefaultText: 'City',
                zipDefaultText: 'Zip Code'
            }, options || {});

            settings.targetDiv$ = this.filter('div');

            settings.targetDiv$
                .html('')
                .each(function (index, storeDiv) {
                    $(this)
				        .append('<div id="searchWidget' + index + '" class="storeLocatorWidget" >' +
				        '<a href="/store/index.aspx"><h2>Store Locator</h2>' +
				        '<img alt="shipping stores" src="/images/storeLocator-map.gif" title="shipping stores" /></a> ' +
				        '<input id="txtCity' + index + '" type="text" name="city" value="City" />' +
				        '<p>or</p>' +
				        '<input id="txtZip' + index + '" type="text" name="zip" value="Zip Code" />' +
				        '<input id="btnFindPakMail' + index + '" type="button" Text="Search" class="submitWidget"/>' +
				        '</div>');

                    $('#txtCity' + index)
                        .bind('focus', function () {
                            if (settings.cityDefaultText == this.value) { this.value = "" }
                        })
                        .bind('blur', function () {
                            if (this.value == "") { this.value = settings.cityDefaultText }
                        });

                    $('#txtZip' + index)
                        .bind('focus', function () {
                            if (settings.zipDefaultText == this.value) { this.value = "" }
                        })
                        .bind('blur', function () {
                            if (this.value == "") { this.value = settings.zipDefaultText }
                        });

                    $('#btnFindPakMail' + index).click(function () {
                        var zip = $('#txtZip' + index)[0].value;
                        var city = $('#txtCity' + index)[0].value;
                        if (zip != undefined && zip != settings.zipDefaultText) {
                            window.location = '/Store/Index.aspx?zip=' + encodeURIComponent(zip);
                        }

                        if (city != undefined && city != settings.cityDefaultText) {
                            window.location = '/Store/Index.aspx?city=' + encodeURIComponent(city);
                        }
                    })

                    $(this).append('<div id="selectedStore' + index + '" ></div>');

                })

            return this;
        }
    }


    $.getStoreLocatorSelectedStore = function (targetDiv, storeNumberCookie) {
        $(targetDiv).html('');
        $.ajax({
            type: "POST",
            url: "/WebServices/Locations.asmx/GetStoreLocation",
            data: "{'storeNumber':'" + storeNumberCookie + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $.each(data.d.Locations, function (index, location) {
                    $(targetDiv)
								.append('<p><a href="http://' + location.StoreURL + '"><i>Click here to return to your store\'s homepage</i></a></p>')
                                .append((location.StoreURL == undefined ? '<h2 class="storeName">' + location.StoreNumber + '</h2>' : '<a href="http://' + location.StoreURL + '"><h2 class="storeName">' + location.StoreNumber + '</h2></a>'))
								.append('<h3>Address:</h3><p>' + location.Address + ',<br />' + location.City + ', ' + location.State + ' ' + location.PostalCode + '</p>')
								.append('<p><a id="showMap' + index + '" href="/store/index.aspx?store=' + location.StoreNumber + '">Click here for a Map</a></p>')
								.append('<h3>Phone Number :</h3><p>' + location.Phone + '</p>')
								.append((location.Email == undefined ? '' : '<h3>Email :</h3><p><a href="mailto:' + location.Email + '">' + location.Email + '</a></p>'))
								.append('<h3>Fax :</h3><p>' + location.Fax + '</p>')
								.append('<p class="socialLinks"></p>')
				                .append((location.WeekdayHours == undefined ? '<p><br /><br />' : '<h3>Hours : </h3><p>Mon - Fri ' + location.WeekdayHours) + (location.SaturdayHours == undefined ? ' ' : ', <br />Saturday ' + location.SaturdayHours) + (location.SundayHours == undefined ? ' ' : '<br />Sunday ' + location.SundayHours) + '</p>')
								.append('<p><img id="newSearchButton' + index + '" class="locatorButton" src="/store/images/start-new-search.gif" alt="click to start new search" /></p>');

                    var storeNameUrl = 'http://' + location.StoreURL + '/svc/storename';
                    var socialSitesUrl = 'http://' + location.StoreURL + '/svc/socialsites';

                    $.get(storeNameUrl, function (data) {
                        if (data) {
                            $('h2.storeName').text(data);
                        }
                    });

                    $.get(socialSitesUrl, function (data) {
                        if (data) {
                            $('p.socialLinks').html(data);
                        }
                    });

                    $('#newSearchButton' + index).click(function () {
                        $.cookie('StoreNumber', null);
                        $('#selectedStoreContent').storelocator();
                    });

                    //                    $('#showMap' + index).click(function () {
                    //                        var _popupHandler = window.open('http://' + window.location.host + '/store/index.aspx?store=' + location.StoreNumber, 'Map');
                    //                        if (navigator.appName.indexOf("Internet Explorer") >= 0) {
                    //                            try {
                    //                                _popupHandler.location.reload();
                    //                            }
                    //                            catch (err) { }
                    //                        }
                    //                        _popupHandler.focus();
                    //                    })


                    if (location.FacebookURL == null) {
                        $('#facebook' + index).hide();
                    } else {
                        $('#facebook' + index).show().click(function () {
                            var _popupHandler = window.open('http://' + location.FacebookURL, 'Facebook', 'menubar=no,resizable=yes');
                            if (navigator.appName.indexOf("Internet Explorer") >= 0) {
                                try {
                                    _popupHandler.location.reload();
                                }
                                catch (err) { }
                            }
                            _popupHandler.focus();
                        })
                    }

                    if (location.TwitterURL == null) {
                        $('#twitter' + index).hide()
                    } else {
                        $('#twitter' + index).show().click(function () {
                            var url = 'http://twitter.com/share?url=' + encodeURIComponent('http://' + location.TwitterURL) + '&text=' + encodeURIComponent('PakMail (' + location.StoreNumber + ')');
                            var _popupHandler = window.open(url, 'Twitter', 'menubar=no,resizable=yes');
                            if (navigator.appName.indexOf("Internet Explorer") >= 0) {
                                try {
                                    _popupHandler.location.reload();
                                }
                                catch (err) { }
                            }
                            _popupHandler.focus();
                        })
                    }
                });
            }
        })
    }

    $.getStoreLocatorCookie = function (name) {
        var cookies = document.cookie.split(/;\s*/);
        var pattern = new RegExp("\\b" + name + "=(.*)");
        for (var i = 0, l = cookies.length; i < l; i++) {
            var match = cookies[i].match(pattern);
            if (match) {
                return decodeURIComponent(match[1]);
            }
        }
    }
})(jQuery);

jQuery(document).ready(function () {
    $('#selectedStoreContent').storelocator();
    var storeNumberCookie = $.cookie('StoreNumber');
    if (storeNumberCookie == undefined) {
        $('#selectedStoreContent').storelocator('showsearch');
    } else {
        $('#selectedStoreContent').storelocator('showstore', storeNumberCookie);
    }
});
