var resources={},global={},titleFormat="{0} | Ryoushin";resources.validation={};function navigateTo(a){showLoading();$("#articleContentFrm").hide().height(0).attr("src",String.format(resources.iframe,escape(a.key),a.view,a.page));a.view.toLowerCase()=="article"&&$("#articleContentFrm").show()}function onNavigateTo(a){setCurrentView(a)}var columnWidths,svc,blogSvc,culture,article,animationEasing="linear",animationDuration=Sys.Browser.agent==Sys.Browser.InternetExplorer?0:330,hideDuration=Sys.Browser.agent==Sys.Browser.InternetExplorer?0:10,showDuration=Sys.Browser.agent==Sys.Browser.InternetExplorer?0:10;$(function(){setupLayout(global.currentView);setListItemCanvases();trimSummaries();global.channelKey&&global.channelKey!=""&&reconfigureCarousel(global.channelKey);$("#feedback button").click(sendFeedback);setupFeedbackValidation();$("a[id^=nav_]").each(function(){var a=$(this),c=a.attr("id"),b=c.split("_")[1].toLowerCase();switch(b){case"home":case"blog":a.click(listClick);break;case"archive":a.click(archiveClick);break;case"author":a.click(authorClick);break;case"article":a.click(articleClick)}});$("#search input:image").click(searchClick);$("#search input:text,#search input[type=search]").keypress(function(a){a.which==13&&searchClick(a)})});function setupLayout(a){switch(a){case"home":case"archive":case"channel":case"search":case"blog":$("#author,#article,#feedback").hide();$("#list,#pager").show();$("#column0").animate({width:columnWidths[2][0]+"px"},animationDuration);$("#column1").animate({width:columnWidths[2][1]+"px"},animationDuration);$("#column2").animate({width:columnWidths[2][2]+"px"},animationDuration);$("#column3").animate({width:columnWidths[2][3]+"px"},animationDuration);break;case"author":$("#list,#article,#pager,#feedback").hide();$("#author").show();$("#column0").animate({width:columnWidths[1][0]+"px"},animationDuration);$("#column1").animate({width:columnWidths[1][1]+"px"},animationDuration);$("#column2").animate({width:columnWidths[1][2]+"px"},animationDuration);$("#column3").animate({width:columnWidths[1][3]+"px"},animationDuration);break;case"article":$("#author,#list,#pager").hide();$("#article,#feedback").show();$("#column0").animate({width:columnWidths[0][0]+"px"},animationDuration);$("#column1").animate({width:columnWidths[0][1]+"px"},animationDuration);$("#column2").animate({width:columnWidths[0][2]+"px"},animationDuration);$("#column3").animate({width:columnWidths[0][3]+"px"},animationDuration)}switch(a){case"archive":case"search":case"blog":$("#syndicate_section,#topRated_section,#archive_section,#author_section,#books_section").show();$("#map_section,#share_section,#author2_section,#share_section,#latest_section").hide();break;case"author":$("#syndicate_section,#topRated_section,#archive_section,#author_section,#books_section,#share_section,#author2_section,#share_section,#latest_section").hide();$("#map_section").show();break;case"channel":$("#syndicate_section,#topRated_section,#archive_section,#author_section,#books_section").show();$("#map_section,#share_section,#author2_section,#share_section,#latest_section").hide();break;case"article":$("#map_section,#topRated_section,#syndicate_section,#archive_section,#author_section,#books_section").hide();$("#author2_section,#share_section,#articleContainer,#latest_section").show()}$("#column2 h2:visible").each(function(a){a>0&&$(this).css("marginTop","1em")});$("#column3 h2:visible").each(function(a){a>0&&$(this).css("marginTop","1em")})}function setCurrentView(a){var e=a.view||global.currentView,h=a.url,d=a.page,c=null,f=null;document.title=String.format(titleFormat,a.title);var g="Ryoushin";a.breadcrumb&&a.breadcrumb.length&&$.each(a.breadcrumb,function(b,a){g+=" \\ "+a});$("#header h3").html(g);substituteState(h);setupLayout(e);switch(e){case"blog":callArticlePage(d*pageSize);break;case"archive":f=a.key;callArticlePage(d*pageSize,pageSize,{month:f});break;case"search":callArticlePage(d*pageSize,pageSize,{query:a.key});break;case"author":showLoading();$.ajax(svc+"/GetAuthor",{type:"POST",contentType:"application/json; chartset=utf-8",dataType:"json",data:JSON.stringify({c:culture}),success:function(b){hideLoading();var a=b.d;$("#author h1").html(a.Name);$("#author p").html(a.Bio);document.title=String.format(titleFormat,a.Name);substituteState(h)},error:function(){hideLoading()}});loadMap&&typeof loadMap=="function"&&loadMap();break;case"channel":c=a.obj.Key;callArticlePage(d*pageSize,pageSize,{channel:c});break;case"article":var i=a.obj.ID;c=a.obj.ChannelKey;showLoading();if(isNaN(i)){$("#articleContent,#articleFooter").show();$("#articleContentFrm").hide();hideLoading()}else{hideLoading();var b=a.obj;$("#articleHeader > h1").html(b.Title);$("#articleHeader span").html(b.Author);$("#articleHeader summary").html(b.Description);$("#rating").attr("src",String.format(resources.rating+"&m=2",b.Rating)).attr("alt",String.localeFormat("{0:n2}",b.Rating)).attr("title",String.localeFormat("{0:n2}",b.Rating));$("#articleHeader time").html(getHowMuchAgo(getJsonDate(b.PublishDate))).attr("datetime",String.localeFormat("{0:d} {0:t}",b.PublishDate));document.title=String.format(titleFormat,b.Title);$("#feedback .ErrorContainer").remove();$("#feedback_id,#feedback_name,#feedback_email,#feedback_website").val("");$("#feedback_comment").text("");$("#feedback_title").val(String.format("Re: {0}",b.Title));$("#feedback_id").val(b.ID);$("#feedback_rating").val("0").change();$("#articleContent,#articleFooter,#feedback .Success,#feedback .Loader,#feedback .Fail").hide();var j=getJsonDate(a.obj.ExpireDate);if(j>new Date)$("#feedback fieldset").show();else $("#feedback fieldset").hide()}}reconfigureCarousel(c);global.currentView=e}function getJsonDate(c){var b=/([\d]+)/i,a=b.exec(c);return a!=null?new Date(parseInt(a[0])):null}function authorClick(a){a.stopPropagation();a.preventDefault();navigateTo({view:"author"})}function listClick(a){a.stopPropagation();a.preventDefault();navigateTo({view:"blog",page:0})}function archiveClick(a){a.stopPropagation();a.preventDefault();var c=$(this).attr("id").split("_")[2],b=Date.parseInvariant(c+"01","yyyyMMdd");navigateTo({view:"archive",page:0,key:String.format("{0:yyyy-MM}",b)})}function articleClick(a){a.stopPropagation();a.preventDefault();var b=$(this).attr("id").split("_")[2];navigateTo({view:"article",key:b})}function searchClick(b){b.stopPropagation();b.preventDefault();var a=$("#search input:text,#search input[type=search]").val();a&&a!=""&&a.length>1&&a!=resources.watermarkText&&navigateTo({view:"search",key:a,page:0})}function showLoading(){rloader.showLoader()}function hideLoading(){setTimeout(function(){rloader.hideLoader.apply(rloader)},500)}function trimSummaries(){$("#list summary").each(function(){var a=$(this).text();a.length>200&&$(this).text(a.substring(0,197)+"...").attr("title",a)})}function setListItemCanvases(){$("*[class^=Ch] canvas").each(function(){try{var g=$(this)[0];g.width=$(this).width();var c=parseInt($(this).parent().parent()[0].className.substring(2)),a=new pacem3D.engine.viewport3D(g,40);a.backfaceCulling=false;a.stretch=pacem3D.engine.viewport3DStretch.uniformToFill;a.camera.eye=new pacem3D.maths.point3D(0,0,4.5);a.camera.aspectRatio=1.5;a.light=new pacem3D.engine.light(1,new pacem3D.maths.vector3D(0,-1,-1));var b=null;switch(c){case 4:b=new pacem3D.primitives.tetrahedron;break;case 8:b=new pacem3D.primitives.octahedron;break;case 6:b=new pacem3D.primitives.hexahedron(1,true);break;case 20:b=new pacem3D.primitives.icosahedron;break;case 12:b=new pacem3D.primitives.dodecahedron(1,true)}var h=new pacem3D.engine.rotateTransform(pacem3D.maths.point3D.empty(),pacem3D.maths.vector3D.j(),0);h.angle=0;b.transforms=[h,new pacem3D.engine.translateTransform(.5,0,0)];a.children.push(b);a.add_rendered(oneTimeDraw);a.startRenderer();var d=$(this).parents("li").first()[0],f=function(){a.stopRenderer();var b=$(".Canvas",this);b.removeClass(String.format("Ch{0}Canvas",c))};$(d).mouseenter(function(){var b=$(".Canvas",this);b.addClass(String.format("Ch{0}Canvas",c));a.startRenderer()});$(d).mouseleave(f);$(d).click(function(a){f();a.preventDefault();a.stopPropagation();var b=parseInt($(this).attr("id").split("_")[1]);navigateTo({view:"article",key:b})})}catch(i){for(var e in i)alert(e+": "+i[e])}})}function oneTimeDraw(){var a=this;a.remove_rendered(oneTimeDraw);a.stopRenderer();a.add_rendered(modelTransform)}function modelTransform(){var d=this,c=d.children[0],a=c.transforms[0],b=a.angle+10;a=new pacem3D.engine.rotateTransform(pacem3D.maths.point3D.empty(),pacem3D.maths.vector3D.j(),b);a.angle=b;c.transforms[0]=a}function setupFeedbackValidation(){$("form").validate({rules:{feedback_title:{required:true},feedback_email:{required:true,email:true},feedback_comment:{required:true},feedback_website:{url:true},feedback_name:{required:true}},errorClass:"Error",errorElement:"span",messages:{feedback_title:{required:resources.validation.titleRequired},feedback_email:{required:resources.validation.emailRequired,email:resources.validation.emailFormat},feedback_comment:{required:resources.validation.commentRequired},feedback_website:{url:resources.validation.urlFormat},feedback_name:{required:resources.validation.nameRequired}},errorPlacement:function(e,b){var d=e.clone(),c=$(b).offset();c.left+=$(b).width();var a=$(b).data("floatPanel");if(!a){a=$("<div>").addClass("ErrorContainer").appendTo("body");$(b).data("floatPanel",a);$(b).change(function(){a.empty()})}a.css("position","absolute").offset({top:c.top,left:c.left+6});a.append(d)}})}function sendFeedback(a){a.preventDefault();a.stopPropagation();var b=$("form").valid();if(!b)return;$("#feedback button").attr("disabled","disabled");$("#feedback .Loader").show();var d=blogSvc+"/RegisterFeedback",c={articleID:parseInt($("#feedback_id").val()),culture:culture,title:$("#feedback_title").val(),name:$("#feedback_name").val(),email:$("#feedback_email").val(),url:$("#feedback_website").val(),comment:$("#feedback_comment").text(),rating:parseInt($("#feedback_rating").val()),nobot:$("#feedback_nobot").val()};$.ajax({url:svc+"/RegisterFeedback",type:"POST",data:JSON.stringify(c),contentType:"application/json; chartset=utf-8",dataType:"json",success:function(a){$("#feedback button").removeAttr("disabled");$("#feedback fieldset").hide();if(a.d>0)$("#feedback .Success").show();else $("#feedback .Fail").show()},error:function(){$("#feedback button").removeAttr("disabled");$("#feedback fieldset").hide();$("#feedback .Fail").show()}})}function notifyIframeContentResized(a){a=Math.max(a,1);$("#articleContentFrm").show().animate({height:a},250,animationEasing,function(){$("#articleContentFrm").height(a)})}function getHowMuchAgo(c){var b=new Date;b=Date.UTC(b.getFullYear(),b.getMonth(),b.getDate(),b.getHours(),b.getMinutes(),b.getSeconds(),b.getMilliseconds());var d=b.valueOf(),e=c.valueOf(),a=(d-e)/1e3;if(a<120)return String.localeFormat(resources.seconds,a);a/=60;if(a<=120)return String.localeFormat(resources.minutes,a);a/=60;if(a<=48)return String.localeFormat(resources.hours,a);a/=24;return a<=31?String.localeFormat(resources.days,a):String.localeFormat("{0:D}",c)}function substituteState(a){window.history&&window.history.replaceState&&window.history.replaceState(window.location.hash,document.title,a)}
