if(typeof Mendeley==='undefined'){Mendeley={};}
if(typeof Mendeley.Stats==='undefined'){Mendeley.Stats={};}
Mendeley.Stats.Publication={category:null,divId:null,canvasWidth:null,canvasHeight:null,showViews:false,showReaders:true,showDownloads:false,dataViewsObj:null,countViews:null,dataReadersObj:null,countReaders:null,dataDownloadsObj:null,countDownloads:null,dataXAxisCount:null,colors:["#ed7501","#336699","#049217"],lineColor:"#dedede",lineNumberColor:"#6f6f6f",profileName:null,showPastWeek:function(divId,canvasWidth,canvasHeight,profileName){this.profileName=profileName;this.paint("week",divId,canvasWidth,canvasHeight,true);},showPastMonth:function(divId,canvasWidth,canvasHeight,profileName){this.profileName=profileName;this.paint("month",divId,canvasWidth,canvasHeight,true);},showPastYear:function(divId,canvasWidth,canvasHeight,profileName){this.profileName=profileName;this.paint("year",divId,canvasWidth,canvasHeight,true);},showAll:function(divId,canvasWidth,canvasHeight,profileName){this.profileName=profileName;this.paint("all",divId,canvasWidth,canvasHeight,true);},paint:function(category,divId,canvasWidth,canvasHeight,updateStats){this.category=category;this.divId=divId;this.canvasWidth=canvasWidth;this.canvasHeight=canvasHeight;$("div.pub-stats-text div.pub-stats-hide").hide();$("div.pub-stats-text div#pub-stats-"+this.category+"-text").show();if(updateStats){if(!this.updateStatsData(category)){return;}}
var xAxisArr=new Array();for(var i=1;i<=this.dataXAxisCount;i++){xAxisArr.push(i);}
this.emptyContainer();var canvas=Raphael(divId,this.canvasWidth,this.canvasHeight+10);var colorsArr=new Array();var displayArr=new Array(),displayIndex=0,displayStats=false;displayArr[0]=[];displayArr[1]=[];displayArr[2]=[];if(this.showViews){displayArr[displayIndex++]=this.dataViewsObj;colorsArr.push(this.colors[0]);displayStats=true;}
if(this.showReaders){displayArr[displayIndex++]=this.dataReadersObj;colorsArr.push(this.colors[1]);displayStats=true;}
if(this.showDownloads){displayArr[displayIndex++]=this.dataDownloadsObj;colorsArr.push(this.colors[2]);displayStats=true;}
$("span#"+divId+"-views-count").text(this.countViews);$("span#"+divId+"-readers-count").text(this.countReaders);$("span#"+divId+"-downloads-count").text(this.countDownloads);if(displayStats){this.resetVisibility();var yAxisMaxValue=Math.max(Math.max.apply(Math,this.dataViewsObj),Math.max.apply(Math,this.dataReadersObj),Math.max.apply(Math,this.dataDownloadsObj));var yAxisMinValue=Math.min(Math.min.apply(Math,this.dataViewsObj),Math.min.apply(Math,this.dataReadersObj),Math.min.apply(Math,this.dataDownloadsObj));if(yAxisMinValue==yAxisMaxValue){yAxisMaxValue++;}
var marginLeft=-4,marginRight=5;if(this.category=="week"){marginLeft=-22;marginRight=23;}else if(this.category=="year"){marginLeft=-12;marginRight=13;}else if(this.category=="all"){marginLeft=-6;marginRight=7;}
var lineChartYAxisMinValue=3;for(var i=0;i<=2;i++){if(displayArr[i].length==0){lineChartYAxisMinValue--;}}
if(lineChartYAxisMinValue<=1){lineChartYAxisMinValue=0;}else{lineChartYAxisMinValue=null;}
var lineChart=canvas.g.linechart(marginLeft,0,this.canvasWidth+marginRight,this.canvasHeight,[xAxisArr],[displayArr[0],displayArr[1],displayArr[2]],{axis:"0 0 0 0",gutter:3,colors:colorsArr,axisymax:yAxisMaxValue,axisymin:lineChartYAxisMinValue,symbol:"o",symbolwidth:1.2});var that=this;lineChart.hover(function(){var value=this.value;var rectangleWidth=20;var textOffset=0;if(value==0){value="0";}else{var tempValue=Math.floor(value/100);if(tempValue>999){rectangleWidth+=10;textOffset+=4;}
while(tempValue>0){rectangleWidth+=4;textOffset+=2;tempValue=Math.floor(tempValue/10);}}
var popupX=-10,popupY=-25;if(this.x<rectangleWidth+10){popupX=0;}else if(this.x>that.canvasWidth-rectangleWidth){popupX=-rectangleWidth;}
if(this.y<25){popupY=0;if(this.x<rectangleWidth+10){popupX=10;}else{popupX=-10-rectangleWidth;}}
this.tags=canvas.set();this.tags.push(canvas.rect(this.x+popupX,this.y+popupY,rectangleWidth,20,2).attr({"fill":this.symbol.attr("fill"),"stroke":"none"}));this.tags.push(canvas.text(this.x+popupX+10+textOffset,this.y+popupY+10,value).attr({fill:"#ffffff","font-family":"Arial","font-size":"10px"}));this.symbol.animate({"r":"4"},0);},function(){this.tags.animate({opacity:0},200,function(){this.remove();});this.symbol.animate({"r":"2.4"},0);});}
var lineAttributes={"stroke-width":1,"stroke":this.lineColor,"stroke-dasharray":"-"};var x1=0,y1=this.canvasHeight;var x2=this.canvasWidth,y2=y1;canvas.path("M "+x1+" "+y1+" L "+x2+" "+y2).attr(lineAttributes).toBack();x1=0,y1=this.canvasHeight/2;x2=this.canvasWidth,y2=y1;canvas.path("M "+x1+" "+y1+" L "+x2+" "+y2).attr(lineAttributes).toBack();var yAxisMiddleValue=Math.round((yAxisMaxValue+yAxisMinValue)/2);if(yAxisMiddleValue!=yAxisMaxValue){canvas.text(x2-12,y1+7,yAxisMiddleValue).attr({fill:this.lineNumberColor,"font-weight":"bold"}).toBack();}
x1=0,y1=3;x2=this.canvasWidth,y2=y1;canvas.path("M "+x1+" "+y1+" L "+x2+" "+y2).attr(lineAttributes).toBack();canvas.text(x2-12,y1+7,yAxisMaxValue).attr({fill:this.lineNumberColor,"font-weight":"bold"}).toBack();var step=0.25,limit=0.75;if(this.category=="week"){step=0.1667;limit=step*5;}
for(var i=step;i<=limit;i+=step){x1=this.canvasWidth*i;canvas.path("M "+x1+" "+y1+" L "+x1+" "+this.canvasHeight).attr(lineAttributes).toBack();}},updateStatsData:function(category){var that=this;var response=false;$.ajax({async:false,url:"/profiles/"+that.profileName,data:{"category":category},dataType:"json",type:"get",beforeSend:function(){that.emptyContainer();$("div#"+that.divId).addClass("mypub-stats-loading");},success:function(data){if(!data.success){that.showError(data.stats.error);return;}
var myPubStatsObj=data;that.dataXAxisCount=myPubStatsObj.stats.xAxisCount;that.dataViewsObj=myPubStatsObj.stats.views;that.dataReadersObj=myPubStatsObj.stats.readers;that.dataDownloadsObj=myPubStatsObj.stats.downloads;that.countViews=myPubStatsObj.stats.count.views;that.countReaders=myPubStatsObj.stats.count.readers;that.countDownloads=myPubStatsObj.stats.count.downloads;response=true;},complete:function(){$("div#"+that.divId).removeClass("mypub-stats-loading");}});return response;},switchTo:function(category){this.paint(category,this.divId,this.canvasWidth,this.canvasHeight,true);},repaint:function(){if(!this.hasError()){this.paint(this.category,this.divId,this.canvasWidth,this.canvasHeight,false);}},emptyContainer:function(){$("div#"+this.divId).empty();},setContainer:function(data){$("div#"+this.divId).text(data);},showError:function(errorCode){this.resetVisibility();$("div."+this.divId+" div#"+this.divId+"-error"+errorCode).show();$("div."+this.divId+" div."+this.divId+"-errors").show();$("div."+this.divId+" div."+this.divId+"-show").hide();},resetVisibility:function(){$("div."+this.divId+" div."+this.divId+"-errors").hide();$("div."+this.divId+" div#"+this.divId+"-error1").hide();$("div."+this.divId+" div#"+this.divId+"-error2").hide();$("div."+this.divId+" div."+this.divId+"-show").show();},hasError:function(){return $("div."+this.divId+" div."+this.divId+"-errors").is(":visible")}};
