﻿/*
 CM Progress wrapper

 Author: Info-Connect A/S - Sondre Kjøniksen
 Date: Mon 22/10 2012
*/
(function(e){cmProgress=function(c){return new cmProgress.fn.init(c)};cmProgress.fn=cmProgress.prototype={init:function(c){var b=this;b.id=0;b.settings=e.extend({interval:1E3,useBar:!1,conferenceID:0,clientID:0,title:"",manualUpdate:!1,dialogWidth:330,dialogHeight:110,closeDelay:0,animate:!1},c);if(!b.settings.manualUpdate&&!b.settings.hasOwnProperty("ajax"))throw{name:"Ajax error",message:"Ajax object must be present"};b.settings.manualUpdate||(b.ajax=b.settings.ajax,b.ajax("staticVars",{iConferenceID:b.settings.conferenceID,
iClientID:b.settings.clientID}),b.async=b.ajax("async"));b.inProgress=!1;b.settings.useBar&&(b.boxDialog=e("<div />").css("display","none").appendTo("body"),b.progressBox=e('<div style="width:300px; margin-top: 5px; float: left;" />').progressbar({value:0}).appendTo(b.boxDialog));b.currentRow=0;b.totalRows=0;return function(){var a=Array.prototype.slice.call(arguments);if(a.length)if(1<a.length)cmProgress.fn.start.call(b,a[0],a[1]);else if(b.inProgress&&b.settings.manualUpdate)if("function"===typeof a[0]||
"object"===typeof a[0])cmProgress.fn.update.call(b,a[0]);else if("string"===typeof a[0]){if(b.hasOwnProperty(a[0]))return b[a[0]]}else cmProgress.fn.start.call(b,a[0]);else if("function"===typeof a[0])cmProgress.fn.start.call(b,{},a[0]);else if("string"===typeof a[0])if(cmProgress.fn.hasOwnProperty(a[0]))cmProgress.fn[a[0]].apply(b,a);else{if(b.hasOwnProperty(a[0]))return b[a[0]]}else cmProgress.fn.start.call(b,a[0]);else cmProgress.fn.start.call(b);return b}},start:function(c,b){var a=this,d=e.extend({interval:a.settings.interval,
onCallback:function(f){},title:a.settings.title,total:a.totalRows,current:a.currentRow},c);a.inProgress=!0;a.settings.manualUpdate?(a.totalRows=d.total,a.currentRow=d.current,a.settings.useBar&&(a.progressBox.progressbar("option","value",a.currentRow/d.total*100),a.boxDialog.dialog({width:a.settings.dialogWidth,height:a.settings.dialogHeight,resizable:!1,modal:!0,title:d.title}),a.settings.animate&&cmProgress.fn.animate.call(a)),b&&"undefined"!==typeof b&&b.call(a)):a.ajax({method:"create",data:{iTotalRows:d.total,
iCurrentRows:d.current},success:function(f){a.id=f;a.timeout=setTimeout(function(){cmProgress.fn.update.call(a,d.onCallback,d.interval)},d.interval);a.settings.useBar&&(a.progressBox.progressbar("option","value",0),a.boxDialog.dialog({width:a.settings.dialogWidth,height:a.settings.dialogHeight,resizable:!1,modal:!0,title:d.title}),a.settings.animate&&cmProgress.fn.animate.call(a));b&&"undefined"!==typeof b&&b.call(a)}})},update:function(){var c=this,b=Array.prototype.slice.call(arguments),a=!c.settings.manualUpdate&&
0<b.length&&"function"===typeof b[0];if(c.settings.manualUpdate){var d=b[0];d.hasOwnProperty("total")&&(c.totalRows=d.total);0<c.totalRows||(c.totalRows=1);c.currentRow=d.current;var f=c.currentRow/c.totalRows*100;c.settings.useBar&&c.progressBox.progressbar({value:f});if(d.hasOwnProperty("finish")&&d.finish||100<=f)c.inProgress=!1,c.settings.useBar&&setTimeout(function(){c.progressBox.progressbar("option","value",0);c.boxDialog.dialog("destroy")},c.settings.closeDelay)}else c.ajax({method:"get",
data:{iProgressId:c.id},success:function(g){c.settings.useBar&&c.progressBox.progressbar("option","value",g.pct);a&&b[0].call(c,g);g.finish?(c.progressBox.progressbar("option","value",100),clearTimeout(c.timeout),c.inProgress=!1,c.settings.useBar&&setTimeout(function(){c.boxDialog.dialog("destroy");c.progressBox.progressbar("option","value",0)},c.settings.closeDelay)):c.timeout=setTimeout(function(){cmProgress.fn.update.call(c,b[0],b[1])},b[1])}})},animate:function(){var c=this;e(".ui-progressbar-value",
this.progressBox).animate({"background-position":"+=20"},500,"linear",function(){c.inProgress&&cmProgress.fn.animate.call(c)})},stop:function(){clearTimeout(this.timeout)},remove:function(){var c=this;c.boxDialog&&setTimeout(function(){c.boxDialog.dialog("destroy")},c.settings.closeDelay)}}})(jQuery);