lazyLoad.lazyRunJs(function(){
(function($,_){
var json = itemJSON;
var selector = '.portal-nav ul li';
var activeClass = 'portal-button-active';
var itemAlertCounter = 0;
var activeSalesPortalConnection = false;
function loaderOff(){
$("#portal-content #sp-ajaxLoaderContainer").css({"display":"none"});
};
function error(){
//if(json.status && json.status == 'error'){
loaderOff();
$('.'+activeClass).removeClass(activeClass);
console.log('Connection to the database has failed. Please try to refresh the page');
var str = "
Sales portal unavailable. Error getting data from the server. Please try again later
";
$('#portal-content').append(str);
$('#sales-portal-databaseError').css({"display":"block"});
// $('.portal-button').css({"display":"none"});
return;
//}
};
var menuNames = {"price":"Price","inventory":"Inventory","location":"Location","contact":"Contact","returns":"Returns","item":"Item"};
var menuButtons = "";
$.each(json.data, function(index,item){
var alert = item.data ? (item.data.priceLocked ? "(1)" : item.data.alerts ? "(" + item.data.alerts + ")" : "") : "";
menuButtons += "" + menuNames[item.type] + " " + alert + "";
});
$(".portal-nav ul").html(menuButtons);
$('body').off('click','.portal-nav ul li');
$('body').on('click','.portal-nav ul li', function(){
$('#portal-content').find('.spNoCache').remove(); //Remove all that should always be reloaded
$('#portal-content').find(".sales-portal-loaded-content:visible").css({"display":"none"}); //hide visible content that is already loaded
$("#portal-content #sp-ajaxLoaderContainer").css({"display":"block"});
var clicked = $(this).attr("data-spButton");
var templateData = "";
var o = $(this); //putting the clicked object in variable for easy reference
$(selector).removeClass(activeClass); //remove class so no other li is active
o.addClass(activeClass); //setting active class
function spContinue(clicked){
if($("#sales-portal-"+clicked).length){
$("#sales-portal-"+clicked).css({"display":"block"});
loaderOff();
return false;
}else{
return true;
}
};
function inject(data,container){
var template = _.template($( "script.sales-portal-template-"+container ).html(), data, { interpolate: /\{\{=(.+?)\}\}/g, evaluate: /\{\{(.+?)\}\}/g, variable: "template" } );
loaderOff();
$("#portal-content").append(template);
activeSalesPortalConnection = false;
};
function get(url,type){
if(!activeSalesPortalConnection){
activeSalesPortalConnection = true;
var promise = $.getJSON(url, function(html){
if(html.status && html.status=='error'){
error();
return;
}
inject(html.data,type);
}).fail(function(){
error();
});
}else{
console.log("ajax request already in progress please try later");
return false;
}
};
if(!spContinue(clicked)) return; //Check if element exists then we should not do anything because we already removed NoCache elements
switch(clicked){
case "price":
$.each(json.data, function(index,item){
if(item.type=="price"){
inject(item.data,item.type);
$('#portal-price-lower-rebate-tooltip').hover(function(){
$('#portal-price-tooltip').css({"display":"block"});
$('#portal-price-lower-rebate-tooltip-status').html('-');
},function(){
$('#portal-price-tooltip').css({"display":"none"});
$('#portal-price-lower-rebate-tooltip-status').html('+');
});
}
});
break;
case "inventory":
$.each(json.data, function(index,item){
if(item.type=="inventory"){
get(item.url, item.type);
}
});
break;
case "location":
$.each(json.data, function(index,item){
if(item.type=="location"){
get(item.url, item.type);
}
});
break;
case "contact":
$.each(json.data, function(index,item){
if(item.type=="contact"){
get(item.url, item.type);
}
});
break;
case "returns":
$.each(json.data, function(index,item){
if(item.type=="returns"){
get(item.url, item.type);
}
});
break;
case "item":
$.each(json.data, function(index,item){
if(item.type=="item"){
inject(item.data,item.type);
}
});
break;
}
});
})(jQuery,_);
});