var blogPost={date:-1,category:"news",categoryNum:null,categoryName:"Latest News",months:["Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov","Dec"],init:function(){this.getBlogPost(),this.getBlogCats()},popPrev:function(prevData){prevData?$(".pagination.prev-next .prev-btn a").attr("href",prevData):$(".pagination.prev-next .prev-btn").addClass("disabled")},popNext:function(nextData){nextData?$(".pagination.prev-next .next-btn a").attr("href",nextData):$(".pagination.prev-next .next-btn").addClass("disabled")},getBlogPost:function(){var blogUrl=window.location.href.split("/").pop();"#"===blogUrl.charAt(blogUrl.length-1)&&(blogUrl=blogUrl.substring(0,blogUrl.length-1));let qstring=blogUrl.indexOf("?");qstring>0&&(blogUrl=blogUrl.substring(0,qstring)),$.ajax({url:"/api/async.php?t=blog&m=blog&a=get_post&post="+blogUrl,type:"GET",dataType:"json",success:this.popBlogInfo,error:function(err){console.log(err)}})},popBlogInfo:blogData=>{"blog"===blogData.source&&blogPost.getComments(blogData.id),blogPost.popNext(blogData.nextUrl),blogPost.popPrev(blogData.previousUrl),$("#blog-post-h1").html(blogData.title).text(),blogData.image&&$("#blog-post-image").attr("src",blogData.image).removeClass("hide");var date=new Date(1e3*blogData.date);$("#blog-post-date").append(blogPost.months[date.getMonth()]+" "+date.getDate()+", "+date.getFullYear()).text();var element=$(tmpl("blog-post-content",blogData));$("#blog-post-container").prepend(element),"events"===blogData.category?$("#blog-post-icon").attr("class","icon-calendar"):$("#blog-post-icon").attr("class","icon-book"),blogData.message.length<500&&$("#blog-post-container #blog-post-wysiwyg").addClass("text-center lead")},getComments:function(id){$.ajax({url:"/api/async.php?t=blog&m=blog&a=get_comments&id="+id,type:"GET",dataType:"json",success:this.popBlogComments,error:function(err){console.log(err)}})},popBlogComments:function(commentData){$("#user-blog-comments").append($(tmpl("blog-post-comments",commentData)))},getBlogCats:function(){$.ajax({url:"/api/async.php?t=widgetFeed&m=lists&a=get_blog_category_list",method:"GET",dataType:"json",context:this,success:this.onBlogCatsData,error:function(err){console.log(err)}})},onBlogCatsData:function(data){var self=this;data.list.length>0?(self.getLatestArticles("news","Latest News",null),$.each(data.list,(function(index,item){blogPost.category=item.categoryName.toLowerCase().replace(" ","-").replace(/'/g,""),blogPost.categoryNum=index,blogPost.categoryName=item.categoryName,self.getLatestArticles(blogPost.category,blogPost.categoryName,blogPost.categoryNum)})),self.getLatestArticles("events","Events",null)):(self.getLatestArticles("news","Latest News",null),self.getLatestArticles("events","Events",null))},getLatestArticles:function(category="news",categoryName="Latest News",categoryNum=null){var self=this,categoryStr="";if(""!==category)categoryStr=null!==categoryNum?"&category="+categoryNum:"&category="+category;else category="";$.ajax({url:"/api/async.php?t=blog&m=blog&a=get_top_three"+categoryStr,type:"GET",dataType:"json",success:function(data){data&&category&&categoryName&&self.popLatestArticles(data,category,categoryName)},error:function(err){console.log(err)}})},popLatestArticles:function(article,cat,catName){var tab,content,spliceIndex=null;for(var i in article)if(article[i].url==window.location.pathname)spliceIndex=i;else if(article[i].count=i,article[i].category=cat,article[i].categoryName=catName,"events"===cat){""===article[i].image&&(article[i].image="https://s3.amazonaws.com/jewelry-static-files/static/blog/event-placeholder.jpg");var day=this.timeConverter(article[i].date,"day"),month=this.timeConverter(article[i].date,"month");article[i].day=day,article[i].month=month}null!==spliceIndex&&article.splice(spliceIndex,1),article.length>0&&cat&&(tab=$(tmpl("latest-news-nav-script",article))),tab&&article.length>0&&cat&&$("#latest-news-nav").append(tab),article.length>0&&cat&&(content=$(tmpl("latest-blog-content-script",article))),content&&article.length>0&&cat&&($("#latest-blog-content").append(content),$("#latest-blog-content").find("span.post-body").each((function(i,e){$(e).find("img, table, [style]").remove()})),$('#latest-news-nav a[href="#news"]').tab("show"))},timeConverter:function(UNIX_timestamp,type){var a=new Date(1e3*UNIX_timestamp),year=a.getFullYear(),month=["Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov","Dec"][a.getMonth()],date=a.getDate(),hour=a.getHours(),min=a.getMinutes(),sec=a.getSeconds();return"day"===type?date:"month"===type?month:date+" "+month+" "+year+" "+hour+":"+min+":"+sec}};$(document).ready((function(){blogPost.init()}));