var sky={};sky.api={};sky.api.version=0.6;sky.utils={};sky.utils.shuffle=function(d){for(var b,a,c=d.length;c;b=parseInt(Math.random()*c),a=d[--c],d[c]=d[b],d[b]=a){}return d};sky.utils.array=function(a){return Array.prototype.slice.call(a,0)};sky.utils.delegate=function(b,a,c){return function(){b.apply(a,[arguments[0],arguments[1]].concat(c))}};sky.utils.replaceAll=function(b,c,a){return b.replace(new RegExp(c,"g"),a)};sky.utils.getJSON=function(b,d,a,c,e){d.rnd=(new Date()).getTime();a=sky.utils.delegate(a,c,e);$.ajax({url:b,data:d,success:a,error:sky.utils.delegate(sky.utils.errorCallback,window,e),dataType:"json"})};sky.utils.errorCallback=function(c,a,b){b.callback.apply(b.scope||window,[false,a])};sky.api.PollManager=function(a){if(a){this.pollId=a.pollId;this.questionId=a.questionId}this.pollCache};sky.api.PollManager.prototype.getPollList=function(c){var a=this;var b={section:c.section};sky.utils.getJSON(sky.api.PollManager.READ_ALL,b,this._getPollListCallback,this,c)};sky.api.PollManager.prototype.getPoll=function(a){this._doRequest(this._getPollCallback,a)};sky.api.PollManager.prototype.getQuestionList=function(a){this._doRequest(this._getQuestionListCallback,a)};sky.api.PollManager.prototype.getQuestionById=function(a){this._doRequest(this._getQuestionByIdCallback,a)};sky.api.PollManager.prototype.getAnswerListByQuestionId=function(a){this._doRequest(this._getAnswerListCallback,a)};sky.api.PollManager.prototype.getAnswerList=function(a){return this.getAnswerListByQuestionId(a)};sky.api.PollManager.prototype._doRequest=function(a,d){if(!this.pollCache){var b=this;var c={id:d.pollId||this.pollId};sky.utils.getJSON(sky.api.PollManager.READ,c,function(f,e,g){this.pollCache=this.parsePoll(f,g.randomize);a.apply(this,[g])},this,d)}else{a(d)}};sky.api.PollManager.prototype._getPollListCallback=function(d,a,e){var c=[];for(var b=0;b<d.length;b++){c.push(this.parsePoll(d[b],e.randomize))}e.callback.apply(e.scope||window,[true,c])};sky.api.PollManager.prototype._getPollCallback=function(a){a.callback.apply(a.scope||window,[true,this.pollCache])};sky.api.PollManager.prototype._getQuestionListCallback=function(a){a.callback.apply(a.scope||window,[true,this.pollCache.questionList])};sky.api.PollManager.prototype._getQuestionByIdCallback=function(d){var a=d.questionId||this.questionId;var b;for(var c=0;c<this.pollCache.questionList.length;c++){if(this.pollCache.questionList[c].id==a){b=this.pollCache.questionList[c]}}d.callback.apply(d.scope||window,[true,b])};sky.api.PollManager.prototype._getAnswerListCallback=function(d){var a=d.questionId||this.questionId;var b;for(var c=0;c<this.pollCache.questionList.length;c++){if(this.pollCache.questionList[c].id==a){b=this.pollCache.questionList[c]}}d.callback.apply(d.scope||window,[true,b.answerList])};sky.api.PollManager.prototype.parsePoll=function(e,b){var f={"abstract":e.POLL.ABSTRACT,id:e.POLL.ID,description:e.POLL.DESCRIPTION,disabled:e.POLL.DISABLED,endPage:e.POLL.PAGEEND,subSection:e.POLL.SUBSECTION,section:e.POLL.SECTION,title:e.POLL.TITLE,startDate:new Date(e.POLL.STARTDATE.time),endDate:new Date(e.POLL.ENDDATE.time),questionList:[]};for(var a=0;a<e.QUESTIONS.length;a++){var d=this.parseQuestion(e.QUESTIONS[a]);for(var g=0;g<e.ANSWERS[d.id].length;g++){d.answerList.push(this.parseAnswer(e.ANSWERS[d.id][g]))}if(b){d.answerList=sky.utils.shuffle(d.answerList)}f.questionList.push(d)}return f};sky.api.PollManager.prototype.parseQuestion=function(a){return{description:a.DESCRIPTION,id:a.ID,image:a.IMAGE,notes:a.NOTE,order:a.ORDERPOS,thumbnail:a.THUMB,total:a.TOTA,video:a.VIDEO,totalQuestion:a.TOTQ,typeQuestion:a.TYPEQ,answerList:[]}};sky.api.PollManager.prototype.parseAnswer=function(a){return{description:a.DESCRIPTION,id:a.ID,image:a.IMAGE,notes:a.NOTE,order:a.ORDERPOS,percent:a.PERC,thumbnail:a.THUMB,total:a.TOTA,video:a.VIDEO}};sky.api.PollManager.prototype.voteQuestion=function(c){var b={pollid:c.pollId||this.pollId};var a=c.questionId||this.questionId;b[b.pollid+"_"+a]=c.answer;sky.utils.getJSON(sky.api.PollManager.WRITE,b,this._voteQuestionCallback,this,c)};sky.api.PollManager.prototype._voteQuestionCallback=function(b,a,c){c.callback.apply(c.scope||window,[b.SUCCESS])};sky.api.PollManager.READ_ALL="/EtSkyItWebapp/getResultPollListCache.do";sky.api.PollManager.READ="/EtSkyItWebapp/getCountDataPollCache.do";sky.api.PollManager.WRITE="/EtSkyItWebapp/writePoll.do";sky.api.TimeManager=function(a){};sky.api.TimeManager.prototype.getCurrentDate=function(a){sky.utils.getJSON(sky.api.TimeManager.READ,{},this._getCurrentDateCallback,this,a)};sky.api.TimeManager.prototype._getCurrentDateCallback=function(f,b,j){try{var c=f.TIMESTAMP.split(" ");var a=c[0].split("/");var i=c[1].split(":");var h=new Date();h.setDate(a[0],a[1],a[2]);h.setHours(i[0],i[1],i[2])}catch(g){j.callback.apply(j.scope||window,[false,"dateparsing"])}j.callback.apply(j.scope||window,[true,h])};sky.api.TimeManager.READ="/EtSkyIt/getServerDate.do";sky.api.LoginManager=function(a){sky.utils.getJSON(sky.api.LoginManager.READ,{},this._constructorCallback,this,{callback:function(){}})};sky.api.LoginManager._constructorCallback=function(b,a,c){if(b.clientcode!=undefined&&b.userid!=undefined){this.currentUser=b}};sky.api.LoginManager.prototype.isLogged=function(){return this.currentUser!=undefined};sky.api.LoginManager.prototype.getCurrentUser=function(){return this.isLogged()?this.currentUser:false};sky.api.LoginManager.READ="/EtSkyIt/data.do";sky.api.TemplateManager=function(a){this.container=a.container;this.template=a.template;this.isArray=a.isArray;this.listeners=a.listeners||[]};sky.api.TemplateManager.prototype.apply=function(c){var a="";if(this.isArray){for(var b=0;b<c.length;b++){var d=this.template;a+=this._processTemplate(this.template,c[b])}}else{a=this._processTemplate(this.template,c)}$(this.container).append(a);for(var b=0;b<this.listeners.length;b++){$(this.listeners[b].target,this.container).bind(this.listeners[b].event,this.listeners[b].callback)}};sky.api.TemplateManager.prototype._processTemplate=function(a,b){return $.createTemplate(a).get(b,undefined,undefined,0)};
