/* EventCalendar. Copyright (C) 2005-2008, Alex Tingle.
 * This file is licensed under the GNU GPL. See LICENSE file for details.
 */

// Set in HTML file:
//   var ec3.start_of_week
//   var ec3.month_of_year
//   var ec3.month_abbrev
//   var ec3.myfiles
//   var ec3.home
//   var ec3.viewpostsfor

///////////////////////////////////////////////////////////////////////////////
// namespace ec3
///////////////////////////////////////////////////////////////////////////////
var ec3={version:"3.2.beta2",today:new Date(),calendars:[],allday:"all day",ELEMENT_NODE:1,TEXT_NODE:3,init:function(){ec3.today_day_num=ec3.today.getDate();ec3.today_month_num=ec3.today.getMonth()+1;ec3.today_year_num=ec3.today.getFullYear();ec3.imgwait=new Image(14,14);ec3.imgwait.src=ec3.myfiles+"/ec_load.gif";ec3.viewpostsfor=ec3.unencode(ec3.viewpostsfor);for(var B=0;B<ec3.month_of_year.length;B++){ec3.month_of_year[B]=ec3.unencode(ec3.month_of_year[B])}for(var A=0;A<ec3.month_abbrev.length;A++){ec3.month_abbrev[A]=ec3.unencode(ec3.month_abbrev[A])}},do_onload:function(A){var B=window.onload;window.onload=function(){if(B){B()}A()}},new_calendar:function(A,B){var C=new ec3.Calendar(A);if(B&&B.hasOwnProperty("hide_logo")){C.hide_logo=B.hide_logo}ec3.do_onload(function(){C.init()});ec3.calendars[A]=C;return C},unencode:function(A){if(!ec3.unencodeDiv){ec3.unencodeDiv=document.createElement("div")}ec3.unencodeDiv.innerHTML=A;return(ec3.unencodeDiv.innerText||ec3.unencodeDiv.firstChild.nodeValue)},get_child_by_tag_name:function(C,D){var B=C.getElementsByTagName(D);if(B){for(var A=0;A<B.length;A++){if(B[A].parentNode==C){return B[A]}}}return 0},calc_day_id:function(A,B,C){if(ec3.today_day_num==A&&ec3.today_month_num==B&&ec3.today_year_num==C){return"today"}else{return"ec3_"+C+"_"+B+"_"+A}},add_class:function(E,B){if(E.className.length==0){E.className=B}else{if(-1==E.className.indexOf(B)){E.className+=" "+B}else{var D=E.className.split(" ");for(var C=0,A=D.length;C<A;C++){if(D[A-C-1]==B){return }}E.className+=" "+B}}},parse_datetime:function(B){if(B&&B.length){var C=B.split(" ");var D=C[0].split("-");var A=C[1].split(":");return new Date(parseInt(D[0],10),parseInt(D[1],10)-1,parseInt(D[2],10),parseInt(A[0],10),parseInt(A[1],10),parseInt(A[2],10))}return null},attr2bool:function(B,C){var D;if(typeof B.getAttributeNode=="function"){var E=B.getAttributeNode(C);return(E&&E.specified&&E.value=="1")?true:false}else{var A=B.getAttribute(C);return(A&&A=="1")?true:false}},extend:function(A,B){for(k in B){A[k]=B[k]}}};ec3.do_onload(function(){ec3.init()});ec3.Calendar=function(A){this.cal_id=A};ec3.Calendar.prototype={hide_logo:false,full_id:function(A){return this.cal_id+"-"+A},short_id:function(A){return A.substr(this.cal_id.length)},getElementById:function(A){return document.getElementById(this.full_id(A))},init:function(){this.reqs=new Array();this.div=document.getElementById(this.cal_id);var F=this.getElementById("ec3_prev");var E=this.getElementById("ec3_next");if(F&&E){var H=new RegExp("[&?]ec3_listing=[eE].*$");var C=H.exec(F.href);if(C){this.is_listing=true}var B=this;F.onclick=function(){B.go_prev();return false};E.onclick=function(){B.go_next();return false};F.href="#";E.href="#"}if(typeof ec3_Popup!="undefined"){var G=this.get_calendars();if(G){for(var D=0,A=G.length;D<A;D++){ec3_Popup.add_tbody(ec3.get_child_by_tag_name(G[D],"tbody"))}}}},create_calendar:function(N,C,K){var P=N.cloneNode(1);var J=C-1;var Q=ec3.get_child_by_tag_name(P,"caption");if(Q){var M=ec3.get_child_by_tag_name(Q,"a");var E=ec3.month_of_year[J]+" "+K;if(M&&M.firstChild&&M.firstChild.nodeType==ec3.TEXT_NODE){if(C<10){M.href=ec3.home+"/?m="+K+"0"+C}else{M.href=ec3.home+"/?m="+K+C}if(this.is_listing){M.href+="&ec3_listing=events"}M.title=ec3.viewpostsfor;M.title=M.title.replace(/%1\$s/,ec3.month_of_year[J]);M.title=M.title.replace(/%2\$s/,K);M.firstChild.data=E}}if(Q&&Q.firstChild&&Q.firstChild.nodeType==ec3.TEXT_NODE){Q.firstChild.data=ec3.month_of_year[J]+" "+K}var I=ec3.get_child_by_tag_name(P,"tbody");while(I.lastChild){I.removeChild(I.lastChild)}var F=new Date(K,J,1,12,0,0);var L=document.createElement("tr");var G,B;I.appendChild(L);var A=0;var D=0;while(F.getMonth()==J&&A<40){var O=(F.getDay()+7-ec3.start_of_week)%7;if(D>6){L=document.createElement("tr");I.appendChild(L);D=0}if(D<O){G=document.createElement("td");G.colSpan=O-D;G.className="pad";L.appendChild(G);D=O}G=document.createElement("td");G.ec3_daynum=F.getDate();var H=ec3.calc_day_id(F.getDate(),C,K);G.id=this.full_id(H);if(H=="today"){G.className="ec3_today"}this.new_day(G);L.appendChild(G);D++;A++;F.setDate(F.getDate()+1)}if(D<7){G=document.createElement("td");G.colSpan=7-D;G.className="pad";L.appendChild(G)}if((7-D)>1&&!this.hide_logo){a=document.createElement("a");a.href="http://blog.firetree.net/?ec3_version="+ec3.version;a.title="Event Calendar "+ec3.version;G.style.verticalAlign="bottom";G.appendChild(a);B=document.createElement("div");B.className="ec3_ec";B.align="right";a.appendChild(B)}P.id=this.full_id("ec3_"+K+"_"+C);return P},loadDates:function(C,E){var D=new XMLHttpRequest();if(D){this.reqs.push(D);var A=this;D.onreadystatechange=function(){A.process_xml()};var B=ec3.home+"/?feed=ec3xml&year="+E+"&monthnum="+C;if(this.is_listing){B+="&ec3_listing=events"}D.open("GET",B,true);this.set_spinner(1);D.send(null)}},get_calendars:function(){var A=new Array();for(var B=0;B<this.div.childNodes.length;B++){var C=this.div.childNodes[B];if(C.id&&C.id.search(this.full_id("ec3_[0-9]"))==0&&C.style.display!="none"){A.push(this.div.childNodes[B])}}if(A.length>0){return A}else{return 0}},rewrite_controls:function(C,D){var B=this.getElementById("ec3_prev");if(B&&B.firstChild&&B.firstChild.nodeType==ec3.TEXT_NODE){B.firstChild.data="\u00ab\u00a0"+ec3.month_abbrev[C%12]}var A=this.getElementById("ec3_next");if(A&&A.firstChild&&A.firstChild.nodeType==ec3.TEXT_NODE){A.firstChild.data=ec3.month_abbrev[D%12]+"\u00a0\u00bb"}},set_spinner:function(A){var C=this.getElementById("ec3_spinner");var B=this.getElementById("ec3_publish");if(C){if(A){C.style.display="inline";if(B){B.style.display="none"}}else{C.style.display="none";if(B){B.style.display="inline"}}}},go_prev:function(){var F=this.get_calendars();if(!F){return }var A=F[0].parentNode;var C=this.short_id(F[0].id).split("_");if(C.length<3){return }var E=parseInt(C[1]);var B=parseInt(C[2])-1;if(B==0){B=12;E--}var D=this.getElementById("ec3_"+E+"_"+B);if(D){D.style.display=this.calendar_display}else{D=this.create_calendar(F[0],B,E);A.insertBefore(D,F[0]);this.loadDates(B,E)}this.calendar_display=F[F.length-1].style.display;F[F.length-1].style.display="none";this.rewrite_controls(B+10,B+F.length-1)},go_next:function(){var G=this.get_calendars();if(!G){return }var B=G[0].parentNode;var A=G[G.length-1];var D=this.short_id(A.id).split("_");if(D.length<3){return }var F=parseInt(D[1]);var C=1+parseInt(D[2]);if(C==13){C=1;F++}var E=this.getElementById("ec3_"+F+"_"+C);if(E){E.style.display=this.calendar_display}else{E=this.create_calendar(G[0],C,F);if(A.nextSibling){B.insertBefore(E,A.nextSibling)}else{B.appendChild(E)}this.loadDates(C,F)}this.calendar_display=G[0].style.display;G[0].style.display="none";this.rewrite_controls(C-G.length+11,C)},process_xml:function(){var C=0;for(var A=0;A<this.reqs.length;A++){var B=this.reqs[A];if(B){if(B.readyState==4){this.reqs[A]=0;if(B.status==200){this.update_days(new ec3.xml.Calendar(B.responseXML))}}else{C=1}}}if(!C){while(this.reqs.shift&&this.reqs.length&&!this.reqs[0]){this.reqs.shift()}this.set_spinner(0)}},update_days:function(B){for(var C=0,A=B.day.length;C<A;C++){var E=this.getElementById(B.day[C].id());if(E&&E.ec3_daynum){this.update_day(E,B.day[C])}}if(typeof ec3_Popup!="undefined"){var D=this.getElementById(B.id());if(D){ec3_Popup.add_tbody(ec3.get_child_by_tag_name(D,"tbody"))}}},new_day:function(A){A.appendChild(document.createTextNode(A.ec3_daynum))},update_day:function(D,C){ec3.add_class(D,"ec3_postday");var A=D.removeChild(D.firstChild);var B=document.createElement("a");B.href=C.link();B.title=C.titles();if(C.is_event()){ec3.add_class(D,"ec3_eventday");B.className="eventday"}B.appendChild(A);D.appendChild(B)}};ec3.xml={details:[]};ec3.xml.Calendar=function(A){this.element=A;this.init()};ec3.xml.Calendar.prototype={day:[],init:function(){var C=this.element.getElementsByTagName("day");for(var B=0,A=C.length;B<A;B++){this.day[B]=new ec3.xml.Day(this,C[B])}},id:function(){var A=this.element.getElementsByTagName("month");if(A){return A[0].getAttribute("id")}else{return""}},_detail:function(C){if(!ec3.xml.details[C]){var E=this.element.getElementsByTagName("detail");for(var D=0,A=E.length;D<A;D++){var B=E[D].getAttribute("id");if(B){ec3.xml.details[B]=E[D]}}}return ec3.xml.details[C]}};ec3.xml.Day=function(B,A){this.calendar=B;this.element=A};ec3.xml.Day.prototype={id:function(){return this.element.getAttribute("id")},link:function(){return this.element.getAttribute("link")},titles:function(){return this.element.getAttribute("titles")},is_event:function(){return ec3.attr2bool(this.element,"is_event")},date:function(){var A=this.element.getAttribute("date").split("-");return new Date(parseInt(A[0],10),parseInt(A[1],10)-1,parseInt(A[2],10))},_events:function(B){var D=this.element.getElementsByTagName("event");if(D){for(var C=0,A=D.length;C<A;C++){B.push(new ec3.xml.Event(this,D[C]))}}return B},_posts:function(B){var D=this.element.getElementsByTagName("post");if(D){for(var C=0,A=D.length;C<A;C++){B.push(new ec3.xml.Post(this,D[C]))}}return B},events:function(A){return this._events([])},posts:function(A){return this._posts([])},posts_and_events:function(){var A=[];A=this._posts(A);A=this._events(A);return A}};ec3.xml.Post=function(A,B){this.day=A;this.element=B};ec3.xml.Post.prototype={kind:"post",brief:function(){return""},link:function(){return this._detail().getAttribute("link")},title:function(){return this._detail().getAttribute("title")},excerpt:function(){var A=this._detail().getElementsByTagName("excerpt");if(A){return A[0].firstChild.data}else{return""}},_detail:function(){return this.day.calendar._detail(this.element.getAttribute("post_id"))}};ec3.xml.Event=function(A,B){this.day=A;this.element=B};ec3.extend(ec3.xml.Event.prototype,ec3.xml.Post.prototype);ec3.extend(ec3.xml.Event.prototype,{kind:"event",allday:function(){return ec3.attr2bool(this.element,"allday")},brief:function(){if(this.allday()){return ec3.allday}var A="";var C=this.element.getElementsByTagName("start");var B=this.element.getElementsByTagName("end");if(C&&C.length){A=C[0].getAttribute("time");if(B&&B.length){A+=" - "+B[0].getAttribute("time")}else{A+="..."}}else{if(B&&B.length){A="..."+B[0].getAttribute("time")}else{A="..."+ec3.allday+"..."}}return A}});
