﻿var app={_ratesSeries:[],_displayCurrency:"",_moduleUrls:{fixturesUrl:"",requirementsUrl:"",availabilityUrl:"",lastUpdateUrl:"",ratesUrl:""},_region:"",_ajaxRequestsInProgress:[],enableFixtureRatesLabels:false,setRegion:function(a){app._region=a},setRatesDisplayCurrency:function(a){app._displayCurrency=a},setFixtureRates:function(a){app._ratesSeries=a},setModuleUrls:function(b,e,a,c,d){app._moduleUrls.fixturesUrl=b;app._moduleUrls.requirementsUrl=e;app._moduleUrls.availabilityUrl=a;app._moduleUrls.lastUpdateUrl=c;app._moduleUrls.ratesUrl=d},initHome:function(){app.initGrids("#fixtures-module");app.initTabs("#fixtures-module","#requirements-module","#availability-module");app.initScrollers();app.initPageScroll();function a(){app.reloadGrid(app._moduleUrls.availabilityUrl,"#availability-module",false)}$.subscribe("/broadcast/fixture",function(c,b){app.displayGrowl(b.Message);app.reloadGrid(app._moduleUrls.fixturesUrl,"#fixtures-module",true);a()});$.subscribe("/broadcast/fixtureupdatedelete",function(c,b){app.reloadGrid(app._moduleUrls.fixturesUrl,"#fixtures-module",true);a()});$.subscribe("/broadcast/requirement",function(c,b){app.displayGrowl(b.Message);app.reloadGrid(app._moduleUrls.requirementsUrl,"#requirements-module",false)});$.subscribe("/broadcast/requirementupdatedelete",function(c,b){app.reloadGrid(app._moduleUrls.requirementsUrl,"#requirements-module",false)});$.subscribe("/broadcast/availabilityupdate",function(c,b){a()})},displayGrowl:function(a){if(a&&a!=""){$.jGrowl(a,{theme:"ws"})}},initScrollers:function(){var b,a=false;$("table.availability-table td.scope").hover(function(){var d=$(this);if(d.find("div span").width()>175){b=setInterval(function(){c(d)},25);a=true}},function(){if(a){clearInterval(b);$(this).find("div > span").css({left:0})}});var c=function(d){var e=d.find("div > span").position().left-1;e=-e>d.find("div > span").width()?d.find("div").width():e;d.find("div span").css({left:e})}},initColorbox:function(){$("a.popup").colorbox({iframe:true,innerWidth:760,innerHeight:1010});$("a.vesselLink").colorbox();$("a.chartererLink").colorbox()},initGrids:function(a){$(a).find(".content-table").dataTable({bFilter:false,bSort:false,bLengthChange:false,bPaginate:true,sPaginationType:"full_numbers",iDisplayLength:15,bRetrieve:true,fnDrawCallback:function(){var d=$(this).parent();var c=d.find("div.dataTables_paginate").children("span:eq(2)").children("span:first-child");var b=d.find("div.dataTables_paginate").children("span:eq(0)");var e=d.find("div.dataTables_paginate").children("span:eq(1)");if(c.attr("class")=="paginate_active"){b.hide();e.hide()}else{b.show();e.show()}}})},initTabs:function(){for(var b=0;b<arguments.length;b++){var a=$(arguments[b]).find(".content-block");jQuery.each(a,function(d,c){$(c).find(".tabs .tab-content").hide();var e=$(c).find("ul.tab-list");e.each(function(){var h=$(this).attr("id");var i=$.cookie(h);var f;if(i){f=i;$(this).children("li").children('a[href="'+i+'"]').parent().addClass("active")}else{f=$(this).children("li").children("a:first").attr("href");$(this).children("li").children("a:first").parent().addClass("active")}$(f).css({display:"block"});var g=$(this).children("li").children("a");g.click(function(k){var j=$(this).attr("href");if(j.charAt(0)=="#"){k.preventDefault();g.parent("li").removeClass("active");$(this).parent("li").addClass("active");$(j).parent(".tabs").children("div.tab-content").css({display:"none"});$(j).css({display:"block"});$.cookie(h,j)}})})})}},blockui:function(b,a){$(b).block({message:"<h1>"+a+"</h1>",css:{border:"3px solid #a00"}})},reloadGrid:function(f,c,d){var a=$(c);var b=a.find(".content-block");b.block({message:'<img src="./Content/Global/img/loader.png" /><h1 style="color:white;">Refreshing data...</h1>',css:{padding:"10px",border:"none",color:"#fff",background:"none"}});$.ajax({type:"GET",url:f,dataType:"html",cache:false,beforeSend:function(){if(app._ajaxRequestsInProgress[c]){return false}app._ajaxRequestsInProgress[c]=true},error:function(){b.unblock();app._ajaxRequestsInProgress[c]=false},success:function(i){if(!i||i.length==0){return}var h=$(i);var g=h.find(".content-block");jQuery.each(g,function(j,k){$(b[j]).html($(k).html());app.initTabs(c);if(d){app.initGrids(c)}});app.initColorbox();app.initScrollers();app.getLastUpdate();setTimeout(function(){b.unblock()},1000);app._ajaxRequestsInProgress[c]=false}});function e(h,g){}},getLastUpdate:function(){$.ajax({type:"GET",url:app._moduleUrls.lastUpdateUrl,dataType:"html",cache:false,success:function(a){$("div#last-update > p").html("LAST UPDATED ON: "+a)}})},initPageScroll:function(){var a=$.cookie("scrollTop");if(a){$("html,body").animate({scrollTop:a},500)}$.cookie("scrollTop",null)},initBroadcast:function(b){var a=$.connection.broadcaster;a.broadcast=function(c){switch(c.Type){case 1:case 2:$.publish("/broadcast/fixture",c);break;case 3:case 4:$.publish("/broadcast/fixtureupdatedelete",c);break;case 5:case 6:$.publish("/broadcast/requirement",c);break;case 7:case 8:$.publish("/broadcast/requirementupdatedelete",c);break;case 9:app.displayGrowl(c.Message);break;case 10:$.publish("/broadcast/availabilityupdate",c);break;default:}};$.connection.hub.start(function(){a.join(b)})},initAvergeRateChart:function(b,a){app.buildAverageRatesChart(a+"?option="+$("#ahts-options>select").val());$("#chart-update").click(function(){switch($("#vessel-type").val()){case"1":app.buildAverageRatesChart(a+"?option="+$("#ahts-options>select").val());break;case"2":app.buildAverageRatesChart(b+"?option="+$("#psv-options>select").val());break;default:}});$("#vessel-type").change(function(){switch($(this).val()){case"1":$("#ahts-options").show();$("#psv-options").hide();break;case"2":$("#psv-options").show();$("#ahts-options").hide();break;default:}})},getRateChartsData:function(){$.getJSON(app._moduleUrls.ratesUrl,null,function(a){})},initCharts:function(){var a=new Highcharts.Chart({chart:{renderTo:"ahts-rates-chart",defaultSeriesType:"spline",marginRight:20,marginBottom:20,height:165,title:""},credits:{text:"Westshore Shipbrokers",href:""},title:"",xAxis:{labels:{enabled:false},categories:["","","","","","","","","",""]},yAxis:{title:{text:"",enabled:false},plotLines:[{value:0,width:1,color:"#808080"}]},tooltip:{enabled:app.enableFixtureRatesLabels,formatter:function(){return"<b>"+this.point.vessel+"</b><br/>"+this.point.charterer+"<br/>"+this.y+" "+app._displayCurrency}},legend:{enabled:app._ratesSeries.length/2==1?false:true,borderWidth:0,x:0,y:-137},series:[]},function(c){var d=0;jQuery.each(app._ratesSeries,function(e,f){var g={type:"spline",name:f.name,data:[]};if(f.vesselType=="1"){d++;jQuery.each(f.rates,function(h,j){g.data.push({name:j.vesselName,charterer:j.charterer,vessel:j.vesselName,y:parseFloat(j.displayRate>0?j.displayRate:j.rate)})});c.addSeries(g)}})});var b=new Highcharts.Chart({chart:{renderTo:"psv-rates-chart",defaultSeriesType:"spline",marginRight:20,marginBottom:20,height:165,title:""},credits:{text:"Westshore Shipbrokers",href:""},title:"",xAxis:{labels:{enabled:false},categories:["","","","","","","","","",""]},yAxis:{title:{text:"",enabled:false},plotLines:[{value:0,width:1,color:"#808080"}]},tooltip:{enabled:app.enableFixtureRatesLabels,formatter:function(){return"<b>"+this.point.vessel+"</b><br/>"+this.point.charterer+"<br/>"+this.y+" "+app._displayCurrency}},legend:{enabled:app._ratesSeries.length/2==1?false:true,borderWidth:0,x:0,y:-137},series:[]},function(c){var d=0;jQuery.each(app._ratesSeries,function(e,f){var g={type:"spline",name:f.name,data:[]};if(f.vesselType=="2"){d++;jQuery.each(f.rates,function(h,j){g.data.push({name:j.vesselName,charterer:j.charterer,vessel:j.vesselName,y:parseFloat(j.displayRate>0?j.displayRate:j.rate)})});c.addSeries(g)}})})},getPageScroll:function(){var a=0;if(self.pageYOffset){a=self.pageYOffset}else{if(document.documentElement&&document.documentElement.scrollTop){a=document.documentElement.scrollTop}else{if(document.body){a=document.body.scrollTop}}}return a},buildAverageRatesChart:function(b){var a={chart:{renderTo:"average-rates-chart",defaultSeriesType:"line",marginRight:20,marginBottom:20,height:300,title:""},credits:{text:"",href:""},title:"",xAxis:{labels:{enabled:true},categories:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]},yAxis:{title:{text:"",enabled:false},plotLines:[{value:0,width:1,color:"#808080"}]},tooltip:{formatter:function(){return"<b>"+this.series.name+"</b><br/>"+this.x+": "+this.y}},legend:{layout:"vertical",align:"right",verticalAlign:"top",x:-10,y:20,borderWidth:0},series:[]};a.series=[];$.getJSON(b,null,function(c){var d={type:"line",name:c.currentYear,data:[]};var e={type:"line",name:c.lastYear,data:[]};jQuery.each(c.current,function(f,g){d.data.push({name:g,y:parseFloat(g)})});jQuery.each(c.last,function(f,g){e.data.push({name:g,y:parseFloat(g)})});a.series.push(d);a.series.push(e);chart=new Highcharts.Chart(a);chart.render()})},hideEmails:function(){$("a.email").each(function(){var b=$(this).text();var a=b.replace(" at ","@");$(this).attr("href","mailto:"+a);$(this).text(a)})}};(function(a){var b=a({});a.subscribe=function(){b.bind.apply(b,arguments)};a.unsubscribe=function(){b.unbind.apply(b,arguments)};a.publish=function(){b.trigger.apply(b,arguments)}})(jQuery);
