/*
 * Copyright 2009 Smilehouse Oy.
 */
function copyArray(c){if(c==undefined){return c
}var b=new Array(c.length);for(var a=0;a<c.length;a++){b[a]=c[a]}return b}function copyArray2(a){if(a==undefined){return a
}return a.slice()}var WSAjaxLib={Events:{},Core:{},ServerLogging:{}};WSAjaxLib.Event=function(a,b,c){this.name=a;
this.source=b;this.params=c};WSAjaxLib.Events=new function(){var c={};function a(e,g){var h=c[e];
if(h==undefined){h=new Array();c[e]=h}for(var f=0;f<h.length;f++){if(h[f]==g){return
}}h.push(g)}function d(e,g){var h=c[e];if(h==undefined){return}for(var f=0;f<h.length;
){if(h[f]==g){h.splice(f,1)}else{f++}}}function b(e,h,k){var j=c[e];if(j==undefined){return
}var g=new WSAjaxLib.Event(e,h,k);for(var f=0;f<j.length;f++){j[f](g)}}return{registerEventHandler:function(e,f){a(e,f)
},unregisterEventHandler:function(e,f){a(e,f)},makeEvent:function(f,e,g){b(e,f,g)
}}};WSAjaxLib.AjaxAction=function(e,f,g,c){this["actionid"]=e;this["params"]=f;var a=g;
var d=c;var b=new Object();this.getCallback=function(){return a};this.isBlocking=function(){return d
};this.getLocalParams=function(){return b};this.setLocalParam=function(i,h){b[i]=h
};this.getLocalParam=function(h){return b[h]}};WSAjaxLib.CombinedAjaxAction=function(d,c){this["actions"]=new Array();
this.add=function(e){var f;if(e instanceof WSAjaxLib.AjaxAction){this["actions"].push(e)
}else{if(jQuery.isArray(e)){this["actions"]=this["actions"].concat(e)}else{return
}}};this.add(d);var a=c;var b=new Object();this.setCallback=function(e){a=e};this.getCallback=function(){return a
};this.isBlocking=function(){for(var e=0;e<this.actions.length;e++){if(this.actions[e].isBlocking()){return true
}}};this.getLocalParams=function(){return b};this.setLocalParam=function(f,e){b[f]=e
};this.getLocalParam=function(e){return b[e]}};WSAjaxLib.Core=new function(){var l="ClientJSON";
var y="workspace_client_ajaxToken";var x="workspace_client_ajaxFlags";var i="workspace_client_ajaxPayload";
var s="transactionid";var r="actions";var d="actionid";var u="params";this.AJAX_RESPONSE_COMPLETE="COMPLETE";
function z(){return true}function q(){return WSContext.token}var e=0;function h(){e++;
return""+e}var f=new Array();var b=null;var a=0;var m=new Object();var v=null;function k(C,D,B,A){this.transactionid=C;
this.actions=D;this.requestParams=B;this.reqJSONobject=A;this.reqTime=new Date().getTime();
this.resCode=null;this.resTime=null;this.resText=null;this.resData=null;this.setResponseTime=function(E){this.resTime=E
};this.isCombinedAction=function(){return this.actions instanceof WSAjaxLib.CombinedAjaxAction
};this.timeoutCallId=null}function c(){window.clearTimeout(v);v=null;if(a<=0){return
}var F=false;var D=new Date().getTime();var A=D;for(var E in m){var C=m[E];if(C.resCode!=null){continue
}if((C.reqTime+10000)>A){A=C.reqTime+10000}var B=D-C.reqTime;if(B>=10000){if(C.timeoutCallId!=null){window.clearTimeout(C.timeoutCallId);
C.timeoutCallId=null}delete m[E];a--;F=true}}if(F){n()}if(a>0){v=window.setTimeout(c,A+10000)
}}function t(B){var A=JSON.stringify(B.reqJSONobject,null,4);m[B.transactionid]=B;
a++;$.post(l,B.requestParams,o);B.timeoutCallId=window.setTimeout(c,10000)}function j(A){if(A.actions.isBlocking()&&(a>0)){return true
}else{if(a>=1){for(var C in m){var B=m[C];if(B.actions.isBlocking()){return true}}}}return false
}function w(A){if((f.length<=0)&&(a<=0)){t(A);return}if(f.length>0){f.push(A);n();
return}if(j(A)){f.push(A);b=window.setTimeout(n,100);return}t(A)}function n(){window.clearTimeout(b);
b=null;while(f.length>0){var A=f[0];if(j(A)){break}f.shift();t(A)}if(f.length>0){b=window.setTimeout(n,100)
}}function p(F){var C=q();if((C==undefined)||(C=="")){return}var G=h();var E=new Object();
E[y]=C;var A=new Object();A[s]=G;if(F instanceof WSAjaxLib.CombinedAjaxAction){A[r]=F.actions
}else{A[r]=F}var B=JSON.stringify(A,null);E[i]=B;var D=new k(G,F,E,A);w(D)}function o(D){var B=new Date().getTime();
if(D==undefined){return}if(typeof D!="string"){return}if(D==""){return}var F=JSON.parse(D);
var E=F[s];var C=m[E];if(C==undefined){return}window.clearTimeout(C.timeoutCallId);
C.setResponseTime(B);C.resCode=this.AJAX_RESPONSE_COMPLETE;C.resText=D;C.resData=F;
if(C.resData[r][d]!="logger.log"){WSAjaxLib.ServerLogging.log("DEBUG","ajaxlib.privaResponseHandler()",'Got ajax response: "'+D+'".')
}delete m[E];a--;n();if(C.actions.getCallback()!=undefined){var A=F[r];C.actions.getCallback()(A,C)
}else{if(!C.isCombinedAction()){return}g(C)}}function g(C){if(!C.isCombinedAction()){return
}var F=C.actions.actions;for(var B=0;B<F.length;B++){var D=F[B];var E=D.actionid;
var A=C.resData[r][B];if(A[d]!=D[d]){if(A[d]!="logger.log"){WSAjaxLib.ServerLogging.log("WARN","ajaxlib.privaCallbackSplitter()","response actionid does not match request actionid.")
}continue}D.getCallback()(A,C)}}return{callbackSplitter:function(A){g(A)},isAjaxSupported:function(){return z()
},executeAjaxAction:function(A){p(A)},executeCombinedAjaxAction:function(A){p(A)},sendJSONAjaxRequest:function(A){privSendJSONAjaxRequest(A)
},doAjaxAction:function(C,D,E,B){var A=new WSAjaxLib.AjaxAction(C,D,E,B);p(A)}}};
WSAjaxLib.ServerLogging=new function(){return{log:function(c,b,a){WSAjaxLib.Core.doAjaxAction("logger.log",{level:c,source:b,message:a},null,false)
}}};
