var Krillion = {
GetBrandStoreLocationsAsync: function(_zipCode, _callback) {
jQuery.ajax(
{
type: "post",
url: "krillionservice.asmx/GetBrandStoreLocations",
contentType: "application/json; charset=utf-8",
data: jQuery.toJSON(
{
zipCode: "" + _zipCode,
radius: 25,
dealersOnly: false
}
),
dataType: "json",
async: true,
cache: false,
success: function (data) {
_callback(data);
}/*,
error: function (XMLHttpRequest, textStatus, errorThrown) {}*/,
complete: function (XMLHttpRequest, textStatus) { KrillionUI.LoadingIndicator.Hide(); }
}
);
}
}
var KrillionUI = {
ShowAll: true,
JSONObject: null,
ProductId: null,
ZipCode: null,
NumRetailers: 0,
CurrentPage: 1,
PageSize: 5,
Init: function() {
GMarker.prototype.IconIndex = -1;
GMarker.prototype.location = null;
GMarker.prototype.retailerIndex = null;
GMarker.prototype.locationIndex = null;
},
AddRetailer: function(retailer, filter, itemNum) {
var location = null;
var html = "
";
var x = 0;
//do {
// if(x >= retailer.Locations.LocationList.length) return false;
//
// location = retailer.Locations.LocationList[x++]
//} while(filter == "1" && location.Availability.Status != "in_stock" && location.Availability.ProductBuyUrl.Type != "buyOnline");
//x--; //point to current location
var numLoc = retailer.Locations.LocationList.length;
//if(filter == "1") {
// numLoc = 0;
// for(var i = x; i < retailer.Locations.LocationList.length; i++)
// if(!(location.Availability.Status != "in_stock" && location.Availability.ProductBuyUrl.Type != "buyOnline")) numLoc++;
//}
for(var i = 0; i < retailer.Locations.LocationList.length; i++) {
//next
location = retailer.Locations.LocationList[i];
//if(filter == "1" && location.Availability.Status != "in_stock" && location.Availability.ProductBuyUrl.Type != "buyOnline") continue;
//var xp = "retailer[@index=" + retailer.Index + "]/locations/location[@index=" + location.Index + "]";
//var status = { imgsrc: null, row1: null, row2: null, row3: null };
//status.imgsrc = "";
//if(location.Availability.Status == "in_stock") status.row1 = "Available";
//else if(location.Availability.Status != "in_stock" && location.Availability.ProductBuyUrl.Type == "buyOnline") {
// status.row1 = "Available Online";
//
// if(location.Availability.Status == "out_of_stock" || location.Availability.Status == "ship_to_store")
// status.row2 = "Out-Of-Stock Locally";
// if(location.Availability.Status != "ship_to_store")
// status.row3 = "Ship to Store Delivery Available";
//} else {
// //location.Availability.Status == "unknown" etc.
// status.row1 = "Contact Store";
// status.imgsrc = "";
//}
//var statustxt = " | " +
// "" + status.row1 + " " +
// (status.row2 == null ? "" : "" + status.row2 + "") +
// " |
" +
// (status.row3 == null ? "" : "" + status.row3 + "
");
var hasStoreNumber = (location.Address.StoreNumber && location.Address.StoreNumber != '' && location.Address.StoreNumber != null && location.Address.StoreNumber != undefined);
var hasStoreCode = (location.StoreCode && location.StoreCode != '' && location.StoreCode != null && location.StoreCode != undefined);
var rowId = (hasStoreNumber ? location.Address.StoreNumber : (hasStoreCode ? location.StoreCode : ""));
var retailerName = (hasStoreNumber ? retailer.AttributeName : retailer.Code);
if((retailerName == null || retailerName.length == 0) && retailer.AttributeName && retailer.AttributeName.length > 0)
retailerName = retailer.AttributeName;
retailerName = retailerName.replace(/[^\w\s]+/, ""); //temp fix
html +=
" x ? " class=\"additionallocation forceprint\"" : "") + ((rowId.length > 0) ? (" id=\"rr_" + escape(rowId) + "\"") : "") + ">" +
"" + (retailer.LogoUrl == null || i > x ? "" : " ") + " | " +
" | " +
" x ? " topborder" : "") + "\" style=\"width: 595px;\">" + /*250px*/
location.Address.StoreName + ((location.Address.StoreLabel != null && location.Address.StoreLabel.length > 0) ? " - " + location.Address.StoreLabel : "") + " " +
location.Address.Street1 + " " +
location.Address.City + ", " + location.Address.State + " " + location.Address.Zip + " " +
/*" Click for phone number
" +*/
" " + location.Phone + " " +
(location.StoreUrl && location.StoreUrl.length > 0 ? " Visit the " + location.Address.StoreName + " website" : " ") +
" Distance to the location - " + location.Distance + " mi" +
" | " +
/*" x ? " topborder" : "") + "\" style=\"width: 225px;\"> | " +*/
/*" x ? " topborder" : "") + " noprint\" style=\"width: 170px;\"> | " +*/
" x ? " topborder" : "") + " noprint\" style=\"width: 165px; border-right: none;\">" +
""*/ +
" | " +
"
" +
(i == x && numLoc-1 > x ?
"" +
"| x ? " topborder" : "") + " noprint\" colspan=\"3\"> | | " +
"
"
:
""
);
//data=\"" + $.toJSON({ num : (retailer.LocationsWithAvailability.LocationAvailabilityList.length - 1), storeName: location.Address.StoreName }).replace(/\"/g,"|!|") + "\"
}
html += "
";
$("#wtb_retailers").append(html);
this.NumRetailers++;
return true;
},
UpdateRetailers: function(data) {
var num = 0;
if(data.d.Response != null && data.d.Response.Retailers != null && data.d.Response.Retailers.RetailersList != null) {
num = data.d.Response.Retailers.RetailersList.length;
}
$("#wtb_results").html("You have searched for " + KrillionUI.ZipCode + " and we have found " + num + " results.");
$("#wtb_results").show();
if(data.d.Response == null || data.d.Response.Retailers == null || data.d.Response.Retailers.Length == 0 || data.d.Response.Retailers.RetailersList == null || data.d.Response.Retailers.RetailersList.Length == 0) return;
this.JSONObject = data.d.Response;
this.LoadingIndicator.Hide();
this.NumRetailers = 0;
$("#wtb_options").show();
this.ApplyFilter($('#show_filter').attr('value'));
this.UpdateView();
},
ApplyFilter: function(filter) {
$("#wtb_retailers").html("");
this.NumRetailers = 0;
var retval = false;
var num = 0;
for(var i = (KrillionUI.CurrentPage-1)*KrillionUI.PageSize; i < this.JSONObject.Retailers.RetailersList.length; i++) {
if(this.AddRetailer(this.JSONObject.Retailers.RetailersList[i], filter, i) == true) {
retval = true;
num++;
}
if(num >= KrillionUI.PageSize) break;
}
if(retval == false) $("#wtb_retailers").html("No locations matching current filter...");
else {
var pagenav = KrillionUI.BuildPageNavigation();
//$("#wtb_retailers").prepend("" + pagenav + "
").append("" + pagenav + "
");
$("#pagenav_top_container").html(pagenav);
$("#pagenav_bottom_container").html(pagenav);
}
},
ChangePerPage: function(num) {
var _num = (num != "-1" ? parseInt(num) : 1000)
if(this.PageSize != _num) {
this.PageSize = _num;
this.CurrentPage = 1;
this.ApplyFilter($('#show_filter').attr('value'));
this.BindMarkers();
}
},
BuildPageNavigation: function() {
var html = "";
var neighbourCount = 3;
var currentPage = KrillionUI.CurrentPage-1;
var numPages = Math.ceil(this.JSONObject.Retailers.RetailersList.length/KrillionUI.PageSize);
var firstPage = (currentPage-neighbourCount > 0 ? currentPage-neighbourCount : 0);
var lastPage = (currentPage+neighbourCount < numPages-1 ? currentPage+neighbourCount : numPages-1);
//if(currentPage > 0) {
// html += "<< ";
// html += "Prev ";
//}
//for(var i = firstPage; i <= lastPage; i++) {
// html += (i == firstPage ? "" : ", ") + ((i+1) != KrillionUI.CurrentPage ? ("" + (i+1) + "") : ("" + (i+1) + ""));
//}
//if(currentPage < numPages-1) {
// html += " Next";
// html += " >> ";
//}
html += "";
return html;
},
TogglePage: function(num) {
KrillionUI.CurrentPage = num;
this.ApplyFilter($('#show_filter').attr('value'));
this.BindMarkers();
},
DoUpdate: function() {
var zipCode = $('#' + PageIDs.ZipCode1).attr("value").toLowerCase();//.replace(/\s+/g, '');
if(this.ValidateZip(zipCode) && KrillionUI.ZipCode != zipCode) {
KrillionUI.ZipCode = zipCode;
KrillionUI.ShowAll = true;
KrillionUI.SetShowAllLabel($("#wtb_expandAll").get()[0]);
$('#show_filter').attr("value", "0");
KrillionUI.ResetView();
KrillionUI.LoadingIndicator.Show();
Krillion.GetBrandStoreLocationsAsync(KrillionUI.ZipCode, function(data) { KrillionUI.UpdateRetailers(data); });
}
},
ValidateZip: function(zip) {
//$('#user_zipCode_info').hide();
if(/^[0-9]{5}([- \/]{0,1}[0-9]{4})?$/.test(zip) == true) {
//$('#user_zipCode_info').show();
$('#user_zipCode1_error').hide();
return true;
} else {
$('#user_zipCode1_error').show();
return false;
}
},
ToggleAllRetailers: function(obj) {
var al = $('.retailer a.toggleretailer');
al.each(function() {
KrillionUI.ToggleRetailer(this, KrillionUI.ShowAll);
});
KrillionUI.ShowAll = (KrillionUI.ShowAll != true);
KrillionUI.SetShowAllLabel(obj);
},
SetShowAllLabel : function(obj) {
if(!KrillionUI.ShowAll)
obj.innerHTML = "Collapse All";
else
obj.innerHTML = "Expand All";
},
ToggleRetailer: function(link, show) {
//var parameters = $.secureEvalJSON(link.getAttribute("data").replace(/\|!\|/g, "\""));
var loc = this.JSONObject.Retailers.RetailersList[parseInt(link.getAttribute("id").match(/toggleretailer\-([0-9]+)/)[1])];
var parameters = { num: (loc.Locations.LocationList.length - 1), retailerName: loc.Locations.LocationList[0].Address.StoreName };
var al = $(link).closest('table').find('tr.additionallocation');
/*if(show) al.toggle(show);
else al.toggle();*/
if(show != undefined) { if(show) { al.show(); } else { al.hide(); } }
else if(al.is(":hidden") || al.eq(0).css("display") == "none") { al.show(); } else { al.hide(); }
$(link).html((al.css("display") == "none" ? "[+] Click to view " + parameters.num + " more " + parameters.retailerName + " locations" : "[-] Click to hide all " + parameters.retailerName + " locations"));
},
GMap: null,
//GEOCache: null,
//GEOCoder: null,
CurrentView: 1,
IconArray : ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],
markers : null,
ResetView: function() {
this.GMap = null;
GUnload();
this.markers = null;
$(".retailer tr").removeClass("map-selected");
$("#map_canvas").hide();
this.CurrentPage = 1;
$("#pagenav_top_container").html("");
$("#pagenav_bottom_container").html("");
},
SetView: function(id) {
this.CurrentView = parseInt(id);
this.UpdateView();
},
UpdateView: function() {
if(this.CurrentView == 0) {
//list view
$("input[name=show_as]")[0].checked = true;
$("input[name=show_as]")[1].checked = false;
$(".retailer tr").removeClass("map-selected");
$("#map_canvas").hide();
$(".distance").children(".marker").hide();
} else if(this.CurrentView == 1) {
//map view
$("input[name=show_as]")[0].checked = false;
$("input[name=show_as]")[1].checked = true;
if(GBrowserIsCompatible() && this.ZipCode != null) {
var k = 0;
if(this.markers != null) {
for(var m in this.markers) {
this.markers[m].bindInfoWindowHtml(null);
delete this.markers[m];
}
}
this.markers = new Array();
for(var i = 0, l = this.JSONObject.Retailers.RetailersList.length; i < l; i++) {
for(var x = 0, l2 = this.JSONObject.Retailers.RetailersList[i].Locations.LocationList.length; x < l2; x++) {
var location = this.JSONObject.Retailers.RetailersList[i].Locations.LocationList[x];
//if($('#show_filter').attr('value') == "1" && location.Availability.Status != "in_stock" && location.Availability.ProductBuyUrl.Type != "buyOnline") continue;
var geo = location.GeoCode;
var lat = geo.Latitude * (180 / Math.PI), lng = geo.Longitude * (180 / Math.PI);
this.markers[k] = this.CreateMarker(lat, lng, k, location.Address.StoreName + ((location.Address.StoreLabel != null && location.Address.StoreLabel.length > 0) ? " - " + location.Address.StoreLabel : ""));
this.markers[k].location = location;
this.markers[k].retailerIndex = i;
this.markers[k].locationIndex = x;
k++;
}
}
if(this.GMap == null) {
//init map
this.GMap = new GMap2($("#map_canvas").get()[0], { size: new GSize(parseInt($("#map_canvas").css("width")), parseInt($("#map_canvas").css("height"))) });
var bz = this.GetMarkerBoundsAndZoom(this.markers);
this.GMap.setCenter(bz.bounds.getCenter(), bz.zoom);
//this.GEOCache = new GGeocodeCache();
//this.GEOCoder = new GClientGeocoder(this.GEOCache);
KrillionUI.GMap.setUIToDefault();
}
this.GMap.clearOverlays();
for(var i = 0; i < this.markers.length; i++) {
var location = this.markers[i].location;
this.GMap.addOverlay(this.markers[i]);
var hasStoreNumber = (location.Address.StoreNumber && location.Address.StoreNumber != '');
var hasStoreCode = (location.StoreCode && location.StoreCode != '');
var rowId = (hasStoreNumber ? location.Address.StoreNumber : (hasStoreCode ? location.StoreCode : ""));
var retailer = this.JSONObject.Retailers.RetailersList[this.markers[i].retailerIndex];
var retailerName = (hasStoreNumber ? retailer.AttributeName : retailer.Code);
if((retailerName == null || retailerName.length == 0) && retailer.AttributeName && retailer.AttributeName.length > 0)
retailerName = retailer.AttributeName;
retailerName = retailerName.replace(/[^\w\s]+/, ""); //temp fix
this.markers[i].bindInfoWindowHtml("" + location.Address.StoreName + ((location.Address.StoreLabel != null && location.Address.StoreLabel.length > 0) ? " - " + location.Address.StoreLabel : "") + "
" +
location.Address.Street1 + "
" +
location.Address.City + ", " + location.Address.State + " " + location.Address.Zip + "
" +
"" + location.Phone + "
" +
(rowId.length > 0 && retailerName && retailerName.length > 0 ?
"Email A Friend
Text To Cell
"
:
""
) +
"Distance to the location - " + location.Distance + " mi"
, { onOpenFn: function() { /*alert("tst");*/ }, pixelOffset : 500 });
}
this.BindMarkers();
$("#map_canvas").show();
}
}
},
BindMarkers: function() {
for(var i = 0; i < this.markers.length; i++) {
var location = this.markers[i].location;
var hasStoreNumber = (location.Address.StoreNumber && location.Address.StoreNumber != '');
var hasStoreCode = (location.StoreCode && location.StoreCode != '');
var rowId = (hasStoreNumber ? location.Address.StoreNumber : (hasStoreCode ? location.StoreCode : ""));
var retailerRow = $("#rr_" + escape(rowId));
if(retailerRow) {
KrillionUI.BindShowMarkerInfoBox(this.markers[i], retailerRow);
KrillionUI.BindShowSelectedRetailerRow(this.markers[i], retailerRow, rowId);
KrillionUI.DisplayMarkerOnRetailer(retailerRow, this.markers[i]);
}
}
},
DisplayMarkerOnRetailer: function(retailer, marker) {
if(marker.IconIndex < 0) return;
var m = $(retailer).find(".distance").children(".marker");
m.attr("src", marker.getIcon().image);
m.show();
},
CreateMarker: function(lat, lng, index, title) {
var point = new GLatLng(lat, lng, false);
var markerIcon = new GIcon(G_DEFAULT_ICON);
if(index + 1 <= this.IconArray.length)
markerIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/marker"+this.IconArray[index]+".png";
var marker = new GMarker(point, { title: title, icon: markerIcon });
if(index + 1 <= this.IconArray.length)
marker.IconIndex = index;
else
marker.IconIndex = -1;
return marker;
},
GetMarkerBoundsAndZoom: function(markers) {
var minLat = null, minLng = null, maxLat = null, maxLng = null;
for(var i = 0; i < markers.length; i++) {
var lat = markers[i].getLatLng().lat();
var lng = markers[i].getLatLng().lng();
if(lat > maxLat || i == 0) maxLat = lat;
if(lat < minLat || i == 0) minLat = lat;
if(lng > maxLng || i == 0) maxLng = lng;
if(lng < minLng || i == 0) minLng = lng;
}
var div = document.createElement("div");
var map = new GMap2(div, { size: new GSize(parseInt($("#map_canvas").css("width")), parseInt($("#map_canvas").css("height"))) });
var paddingDeg = 0.005;
var bounds = new GLatLngBounds(new GLatLng(minLat-paddingDeg, minLng-paddingDeg), new GLatLng(maxLat+paddingDeg, maxLng+paddingDeg));
var zoomFit = map.getBoundsZoomLevel(bounds);
delete map;
delete div;
return { bounds: bounds, zoom: zoomFit };
},
BindShowMarkerInfoBox: function(marker, retailerRow) {
if(retailerRow) {
retailerRow.bind("click",
function() {
/*if(KrillionUI.CurrentView == 1 && KrillionUI.GMap != null && marker) {
GEvent.trigger(marker, "click");
$("#map_canvas").get(0).scrollIntoView(true);
}*/
KrillionUI.ShowMarkerInfoBox(marker, retailerRow);
}
);
}
},
ShowMarkerInfoBox: function(marker, retailerRow) {
if(KrillionUI.CurrentView == 1 && KrillionUI.GMap != null && marker) {
GEvent.trigger(marker, "click");
$("#map_canvas").get(0).scrollIntoView(true);
}
},
BindShowSelectedRetailerRow: function(marker, retailerRow, rowId) {
if(marker && retailerRow) {
GEvent.clearListeners(marker, "click");
GEvent.addListener(marker, "click", function() { KrillionUI.ShowSelectedRetailerRow(retailerRow, marker.retailerIndex, rowId); });
}
},
ShowSelectedRetailerRow: function(retailerRow, index, rowId) {
$(".retailer tr").removeClass("map-selected");
if(index < (KrillionUI.CurrentPage-1)*KrillionUI.PageSize || index >= KrillionUI.CurrentPage*KrillionUI.PageSize) {
var page = Math.ceil((index+1)/KrillionUI.PageSize);
KrillionUI.TogglePage(page);
retailerRow = $("#rr_" + escape(rowId));
}
if(retailerRow) {
retailerRow.addClass("map-selected");
var toggle = $("#" + retailerRow.attr("id") + "_toggle");
if(toggle)
toggle.addClass("map-selected");
}
}
}
KrillionUI.LoadingIndicator = {
Id: null,
Step: 0,
Bars: 20,
Show: function() {
$("#wtb_results").hide();
$("#wtb_options").hide();
this.Animate();
this.Id = window.setInterval(function() { KrillionUI.LoadingIndicator.Animate(); }, 25);
},
Hide: function() {
if(this.Id != null) {
window.clearInterval(this.Id);
this.Id = null;
this.Step = 0;
$("#wtb_retailers").html("");
}
},
Animate: function() {
if(this.Id != null) {
var dots = "";
for(var i = 0; i < this.Bars; i++) {
if(i == this.Step%(this.Bars+1)) dots += "";
dots += "|";
}
if(this.Step%(this.Bars+1) != this.Bars) dots += "";
$("#wtb_retailers").html("" + dots + "
");
this.Step++;
}
}
}
$(document).ready(function() { KrillionUI.Init(); });