
if(typeof(SK)=='undefined')SK={};if(typeof(SK.Util)=='undefined')SK.Util={};if(typeof(SK.Util.DateTime)=='undefined')SK.Util.DateTime={};if(typeof(SK.LocationFormats)=='undefined')SK.LocationFormats={};SK.LocationFormats.DateTime=new Class({months:['January','February','March','April','May','June','July','August','September','October','November','December'],days:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],months_short:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],days_short:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],env:null,default_format:'$YEAR$-$0M$-$0D$',regexp_subs:null,initialize:function(){this.getEnv();this.createRegExpSubs();},getEnv:function(){this.env=SK.Singletons&&SK.Singletons.env?SK.Singletons.env:null;if(!this.env||!this.env.get('locale')){var win=window;try{do{win=win.opener||win.parent;if(win.SK.Singletons&&win.SK.Singletons.env&&win.SK.Singletons.env.get('locale')){if(this.env){this.env.set('locale',win.SK.Singletons.env.get('locale'));}else{this.env=$extend({},win.SK.Singletons.env);}}}while((win.opener&&win!==win.opener)||win!==win.parent);}catch(e){}}},createRegExpSubs:function(){this.regexp_subs={$WEEKDAY$:'('+this.days.join('|')+')',$WD$:'('+this.days_short.join('|')+')',$YEAR$:'\\d{4}',$YR$:'\\d{2}',$MONTH$:'('+this.months.join('|')+')',$MNT$:'('+this.months_short.join('|')+')',$MN$:'(0\\d|1[0-2]|[1-9])',$0M$:'(0\\d|1[0-2])',$D$:'([0-2]\\d|3[0-1]|[1-9])',$0D$:'([0-2]\\d|3[0-1])',$H$:'(1[0-2]|[0-9])',$H24$:'([1-2]\\d|[0-9])',$M$:'[0-5]\\d',$S$:'[0-5]\\d',$AMPM$:'(AM|PM)','$AM.PM$':'(P\\.M\\.|A\\.M\\.)',$TZ$:null,$TIMEZONE$:null,$UTC$:null};this.regexp_subs.$H12$=this.regexp_subs.$H$;},getFormat:function(format){format=format||'';if(format.indexOf('$')>=0){return format;}
format=this.env?this.env.get('locale')[format]:this.default_format;return format||this.default_format;},addZero:function(num){return num<10?"0"+num:num;},parseDate:function(input){var date_obj;if(!input){date_obj=new Date();}
else if($type(input)=='date'){return input.toString()!=='Invalid Date'?input:null;}
else{if(/^[a-zA-Z]{3}, \d{2} [a-zA-Z]{3} \d{4} \d{2}:\d{2}:\d{2} (\+|\-)\d{4}$/.test(input)){return new Date(Date.parse(input));}
input=input.replace(/\-|\./g,'/');if(input.match(/^\d{1,2}:\d{2}/)){input='01/01/1970 '+input;}
date_obj=new Date(input);}
if(date_obj.toString()=='Invalid Date'){return null;}
return date_obj;},parseDateUTC:function(input){var date_obj=this.parseDate(input);if(!date_obj){return null;}
return new Date(Date.UTC(date_obj.getFullYear(),date_obj.getMonth(),date_obj.getDate(),date_obj.getHours(),date_obj.getMinutes(),date_obj.getSeconds()));},format:function(input,format){format=this.getFormat(format);var date_obj=this.parseDate(input);if(!date_obj){return input;}
var day=date_obj.getDay();var date=date_obj.getDate();var month=date_obj.getMonth();var year=date_obj.getFullYear();var hours=date_obj.getHours();var minutes=date_obj.getMinutes();var seconds=date_obj.getSeconds();var offset=this.getOffset(date_obj);var tz_match=date_obj.toString().match(Browser.Engine.trident?/(UTC(\+|\-)\d{4}|[A-Z]{3,4})/:/\(([\(\)a-zA-Z\s\,\.]+)\)$/);var timezone=tz_match?tz_match[1]:'';var subs={$WEEKDAY$:this.days[day],$WD$:this.days_short[day],$YEAR$:year,$YR$:String(year).substr(2),$MONTH$:this.months[month],$MNT$:this.months_short[month],$MN$:month+1,$0M$:this.addZero(month+1),$D$:date,$0D$:this.addZero(date),$H$:hours>12?hours-12:hours,$H24$:hours,$M$:this.addZero(minutes),$S$:this.addZero(seconds),$AMPM$:hours>=12?'PM':'AM','$AM.PM$':hours>=12?'P.M.':'A.M.',$TZ$:timezone,$UTC$:offset<0?offset:'+'+offset};subs.$H12$=subs.$H$;subs.$TIMEZONE$=subs.$TZ$;return format.replace(/\$[A-Z0-9\.]+\$/g,function(wildcard){var return_value=typeof(this.subs[wildcard])!='undefined'?this.subs[wildcard]:'';return return_value;}.bind({subs:subs}));},formatUTC:function(input,format){return this.format(this.parseDateUTC(input),format);},cache:{},validate:function(input,format){format=this.getFormat(format);var pattern;if(this.cache[format]){pattern=this.cache[format];}else{format=format.replace(/[\(\)\|\-\.\?\!\:\/\\]/g,function(c){return'\\'+c;});pattern=new RegExp('^'+format.replace(/\$[A-Z0-9\.]+\$/g,function(wildcard){return this.regexp_subs[wildcard];}.bind(this))+'$','i');this.cache[format]=pattern;}
return pattern.test(input);},setDefaultFormat:function(fmt){this.default_format=fmt;},parseInput:function(input,format){format=this.getFormat(format);var index_year=format.indexOf('$YEAR$');var index_m=format.indexOf('$MN$');index_m=index_m<0?format.indexOf('$0M$'):index_m;var index_d=format.indexOf('$D$');index_d=index_d<0?format.indexOf('$0D$'):index_d;if(index_year>index_m&&index_d<index_m){input=input.replace(/^(\d{1,2})(.{1})(\d{1,2})/,function(all,day,sep,month){return month+sep+day;});}
return this.parseDate(input)||new Date(input);},nowUTC:function(){var today=new Date();var offset=this.getOffset(today);today.setHours(today.getHours()-offset);return today;},getOffset:function(date_obj){return-(date_obj.getTimezoneOffset())/60;}});(function(SK){var mgr=new SK.LocationFormats.DateTime();SK.Util.DateTime.format=mgr.format.bind(mgr);SK.Util.DateTime.formatUTC=mgr.formatUTC.bind(mgr);SK.Util.DateTime.validate=mgr.validate.bind(mgr);SK.Util.DateTime.parseInput=mgr.parseInput.bind(mgr);SK.Util.DateTime.setDefaultFormat=mgr.setDefaultFormat.bind(mgr);SK.Util.DateTime.nowUTC=mgr.nowUTC.bind(mgr);})(SK);


if(typeof(SK)=='undefined')SK={};SK.TemplateEngine=new Class({cache:{},code:'',initialize:function(string,data){this.code=this.processTemplate(string,data);},init:function(){return this.code;},processTemplate:function(string,data){var fn=!/\W/.test(string)?this.cache[string]=this.cache[string]||this.processTemplate($(string).innerHTML):new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};"+"with(obj){p.push('"+
string.replace(/[\r\t\n]/g," ").split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',$1,'").split("\t").join("');").split("%>").join("p.push('").split("\r").join("\\'")
+"');}return p.join('');");return data?fn(data):fn;}});


if(typeof(console)=='undefined')console={log:$empty};if(typeof(SK)=='undefined')SK={};if(typeof(SK.Applications)=='undefined')SK.Applications={};SK.Applications.NewsAndEvents=new Class({Implements:[Events,Options],options:{template_url:'/Apps/NewsAndEvents/Snippets/display.html'},elements:{},entries:[],ids:{},start_pos:0,initialize:function(options){this.setOptions(options);this.ids=JSON.decode(this.options.properties.app_config);this.ids.instance=this.options.properties.instance_id;$(window).addEvent('load',this.init.bind(this));},init:function(){this.elements.placeholder=$(this.options.placeholder);this.showLoading();this.loadData();},loadData:function(callback){new SK.Sync([this.fetchEntries.bind(this),this.prepareEntries.bind(this),this.printEntries.bind(this)],function(){if(typeof(this.callback)=='function'){this.callback();}}.bind({callback:callback}),function(){alert('Error');}).run();},fetchEntries:function(sync){var data_source=this.upperCamelCase(this.options.properties.data_source);if(typeof(this['fetch'+data_source])=='function'){this['fetch'+data_source](sync);}else{this.fireEvent('fetchEntries',[sync]);}},fetchManualEntries:function(sync){var where=[[this.ids.column_ids.instance_id,'=',[this.ids.instance]],[this.ids.column_ids.active,'=',[1]]];var operator={future:'>=',past:'<='};var filter=this.options.properties.entries_filter;if(filter!='all'){where.push([this.ids.column_ids.date,operator[filter],[SK.Util.DateTime.format(null,'$YEAR$$0M$$0D$')+'0000']]);}
new SK.API(this.ids.token,{encoding:SK.Util.getEncoding()}).execute('db.query.get_filtered_rows',{db_id:this.ids.db_id,index_only:0,filter:JSON.encode({where:where,sort:{column:this.ids.column_ids.date,order:this.options.properties.entries_sorting},limit:{from:this.start_pos,count:Number(this.options.properties.entries_number)+1}})},function(success,message,result){if(!success){alert(message);return;}
if(result&&result.rows){if(result.rows.length<=this.mgr.options.properties.entries_number){this.mgr.options.properties.show_more=false;}else{result.rows.pop();}
this.mgr.entries=result.rows;if(result.rows.length){this.mgr.hideZeroFootprint();}}
this.sync.ready();}.bind({mgr:this,sync:sync}))},fetchRssFeed:function(sync){new Request.JSON({url:'/Tools/rss2json.html?url='+escape(this.options.properties.rss)
+'&limit='+(Number(this.options.properties.entries_number)+1)
+'&start_pos='+this.start_pos,method:'get',onSuccess:function(response){if(response&&response.length){if(response.length<=this.mgr.options.properties.entries_number){this.mgr.options.properties.show_more=false;}else{response.pop();}
this.mgr.entries=response;if(response.length){this.mgr.hideZeroFootprint();}}
this.sync.ready();}.bind({mgr:this,sync:sync})}).send();},prepareEntries:function(sync){this.fireEvent('beforePrepareEntries',[this.entries]);var data_source=this.upperCamelCase(this.options.properties.data_source);if(typeof(this['prepare'+data_source])=='function'){this['prepare'+data_source]();}
for(var i=0,l=this.entries.length;i<l;i++){this.entries[i].description=this.crop(this.entries[i].description);this.entries[i].date=this.prepareDate(this.entries[i].date);}
this.fireEvent('afterPrepareEntries',[this.entries]);sync.ready();},prepareManualEntries:function(){var prepared_entries=[];for(var i=0,l=this.entries.length;i<l;i++){var entry={title:this.entries[i].cells[this.ids.column_ids.title],description:this.entries[i].cells[this.ids.column_ids.description],date:this.entries[i].cells[this.ids.column_ids.date],link:this.entries[i].cells[this.ids.column_ids.link]};prepared_entries.push(entry);}
this.entries=prepared_entries;},prepareRssFeed:function(){var prepared_entries=[];for(var i=0,l=this.entries.length;i<l;i++){if(this.entries[i].date&&this.entries[i].title){prepared_entries.push(this.entries[i]);}}
this.entries=prepared_entries;if(!this.entries.length){this.showZeroFootprint();}
this.entries.sort(function(a,b){if(a.date===b.date){return 0;}
var date_a=this.parseDate(a.date);var date_b=this.parseDate(b.date);return date_a<date_b?(this.options.properties.entries_sorting=='asc'?-1:1):(this.options.properties.entries_sorting=='asc'?1:-1);}.bind(this));},parseDate:function(date_str){if(!isNaN(date_str)){date_str=date_str.substr(0,4)+'/'+date_str.substr(4,2)+'/'
+date_str.substr(6,2);}else{date_str=date_str.replace(/([\+\-]{1}\d\d)\:(\d\d)$/gi,function(a,b,c){return b+c;});}
return new Date(Date.parse(date_str));},prepareDate:function(date_str){var date=this.parseDate(date_str);return{date:SK.Util.DateTime.format(date,'date_fmt'),time:SK.Util.DateTime.format(date,'time_fmt').toLowerCase(),month:SK.Util.DateTime.format(date,'$MNT$'),day:SK.Util.DateTime.format(date,'$D$')};},printEntries:function(){this.hideLoading();if(this.entries.length){new Request.Iframe({url:this.options.template_url,method:'get',encoding:SK.Util.getEncoding(),onSuccess:function(response){var html_code=new SK.TemplateEngine(response,{data:this.entries,properties:this.options.properties}).init();this.elements.placeholder.innerHTML+=html_code;this.bindShowMoreLink(this.elements.placeholder);}.bind(this)}).send();}},showMore:function(){var show_more=this.elements.placeholder.getElement('.sk-ne-show-more');var hidden=show_more.getPrevious('div.hidden');show_more.dispose();if(hidden){hidden.dispose();}
this.start_pos+=Number(this.options.properties.entries_number);this.showLoading().loadData();},bindShowMoreLink:function(wrapper){var show_more=wrapper.getElement('.sk-ne-show-more a');if(show_more){show_more.addEvent('click',function(e){e.preventDefault();this.showMore();}.bind(this));}},upperCamelCase:function(string){var newstring=string.charAt(0).toUpperCase()+string.substr(1);newstring=newstring.replace(/_(\w)/g,function(all,s){return s.toUpperCase();});return newstring;},crop:function(string,limit){limit=limit||this.options.properties.crop_after;if(limit&&string.length>limit){while(string.charAt(limit).match(/\w/)){limit--;}
string=string.substr(0,limit)+'...';}
return string;},showZeroFootprint:function(){this.elements.placeholder.getParent().removeClass('sk-remove-zero-footprint');},hideZeroFootprint:function(){this.elements.placeholder.getParent().addClass('sk-remove-zero-footprint');},showLoading:function(){this.elements.loading=new Element('p',{'class':'heading2 sk-ne-loading',text:'Loading data...'}).inject(this.elements.placeholder);return this;},hideLoading:function(){if(this.elements.loading){this.elements.loading.dispose();}
return this;}});


