/*______________________________________

  schedule.js #080328
  
  Script written by Mahiro Komura <mahirok@mizuyari.jp>
  http://mizuyari.jp/
______________________________________*/

var schedule;
mizuyari.addOnLoadEvent(mizGlobal_scheduleAppInit);

function mizGlobal_scheduleAppInit() {
 schedule = new scheduleApp("schedule");
}

function mizGlobal_XMLLoaded(obj) {
 schedule.xmlLoaded(obj);
}

function scheduleApp (self) {
 this.self = 'window.' + self;
 this.items  = null;
 this.itemListElmtPrefix = 'items-list-';
 this.itemSets = ['items-list-calendar','items-list-special','items-list-temporary','items-list-permanent','items-list-event']
 this.wdays = ['SUN','MON','TUE','WED','THU','FRI','SAT'];
 this.activeItemSetId = null;
 this.xmlURL = 'schedule.xml';
 this.basePxSize = 3;
 this.beginDateText = null;
 this.currentDate = new Date();
 this.currentRelMonth = 0;
 this.currentDateText = null;
 this.finalDateText = null;
 this.maxEventItems = 9999;
 this.eventItems = 0;
 this.ready = false;
 this.getXML();
}

scheduleApp.prototype = {
 
getXML : function(obj) {
 sendRequest(mizGlobal_XMLLoaded,'','GET',this.xmlURL,true,true);
},
 
xmlLoaded : function(obj) {
 this.items = obj.responseXML.getElementsByTagName("item");
 this.currentDateText = this.getCurrentDateText();
 this.beginDateText = "20110301";
 this.beginRelMonth = this.getRelMonth(this.currentDateText, this.beginDateText);
 this.finalDateText = this.getFinalDate();
 this.finalRelMonth = this.getRelMonth(this.currentDateText, this.finalDateText) -4;
 this.ready = true;
 this.printItems(0);
 this.moveRelMonth(0);
},

modifyActiveItemSet : function(itemSetId) {
 if (this.itemSetId != null) {
  mizuyari.removeClass(miz$(this.itemSets[this.itemSetId]),'active');
 }
 this.itemSetId = itemSetId;
 mizuyari.addClass(miz$(this.itemSets[this.itemSetId]),'active');
},

printItems : function(itemSetId) {
 if (!this.ready) return;
 var itemCount = 0;
 for (i=0; i<this.itemSets.length; i++) {
  this.clearItemElements(this.itemSets[0]);
 }
 for (i=0; i<this.items.length; i++) {
  this.createItemElement(i);
  itemCount++;
 }
},

moveThisMonth : function() {
 var posX = this.getPosByDate(this.currentDateText);
 for (i=0; i<this.itemSets.length; i++) {
  elmtScroll(this.itemSets[i],-posX,0);
 }
 this.currentRelMonth = 0;
 this.printCurrentDateHTML();
},

moveRelMonth : function(relMonth) {

 if (
  this.currentRelMonth == this.beginRelMonth && relMonth < 0 ||
  this.currentRelMonth >= this.finalRelMonth && relMonth > 0
 ) return;

 if (this.currentRelMonth+relMonth <= this.beginRelMonth && relMonth < 0) {
  relMonth = -(this.currentRelMonth - this.beginRelMonth);
 }

 if (this.currentRelMonth+relMonth >= this.finalRelMonth) {
  relMonth = this.finalRelMonth - this.currentRelMonth;
 }

 var mm = this.currentDate.getMonth();
 mm += relMonth;
 this.currentDate.setMonth(mm);
 var cYear = this.currentDate.getFullYear();
 var cMonth = "" + (this.currentDate.getMonth()+1);
 while(cMonth.length < 2) { cMonth = "0" + cMonth; }
 this.currentDateText = ""+cYear+cMonth+"01";

 var posX = this.getPosByDate(this.currentDateText);
 for (i=0; i<this.itemSets.length; i++) {
  elmtScroll(this.itemSets[i],-posX,0);
 }

 this.currentRelMonth += relMonth;
 this.printCurrentDateHTML();
},

printCurrentDateHTML : function() {
 var cYear  = "" + this.currentDate.getFullYear();
 var cMonth = "" + (this.currentDate.getMonth()+1);
 var tempHTML = "";
 for (i=0; i<cYear.length; i++) {
  tempHTML += '<img src="/shared/images/' + cYear.charAt(i) + '.gif" alt="' + cYear.charAt(i) + '" />';
 }
 tempHTML += '<img src="/shared/images/slash.gif" alt="/" />';
 miz$('current-date').innerHTML = tempHTML;
},

getPosByDate : function(begin) {
  var baseDate = new Date("2008","01"-1,"01");
  var eventBeginDate = new Date();
  if (begin != null) {
   var eventDate = new Array();
   eventDate = new Array();
   eventDate[0] = begin.substring(0,4);
   eventDate[1] = begin.substring(4,6);
   eventDate[2] = begin.substring(6,8);
   eventBeginDate = new Date(eventDate[0],eventDate[1]-1,eventDate[2]);
  }
  var posX = Math.ceil((eventBeginDate.getTime()-baseDate.getTime())/8.64E07) * this.basePxSize; // 8.64E07 = 60*60*24*1000
  return posX;
},

getCurrentDateText : function() {
 var cYear = this.currentDate.getFullYear();
 var cMonth = "" + (this.currentDate.getMonth()+1);
 while(cMonth.length < 2) { cMonth = "0" + cMonth; }
 return ""+cYear+cMonth+"01";
},

getWidthByDate : function(begin, end) {
  var baseDate = new Date("2008","01"-1,"01");
  var eventDate = new Array();

  eventDate[0] = new Array();
  eventDate[0][0] = begin.substring(0,4);
  eventDate[0][1] = begin.substring(4,6);
  eventDate[0][2] = begin.substring(6,8);

  eventDate[1] = new Array();
  eventDate[1][0] = end.substring(0,4);
  eventDate[1][1] = end.substring(4,6);
  eventDate[1][2] = end.substring(6,8);

  var beginDate = new Date(eventDate[0][0],eventDate[0][1]-1,eventDate[0][2]);
  var endDate   = new Date(eventDate[1][0],eventDate[1][1]-1,eventDate[1][2]);
  var width = Math.ceil((endDate.getTime()-beginDate.getTime())/8.64E07) * this.basePxSize; // 8.64E07 = 60*60*24*1000
  return width;
},

moveCalendarPosX : function(year, month, day) {
 if (!month) month = "01";
 if (!day) day = "01";
 var posX = this.getPosByDate(""+year+month+day);
},

clearItemElements : function(id) {
 miz$(id).innerHTML = "";
},

getRelMonth : function(from, to) {
 var fromDate = new Date (from.substring(0,4), from.substring(4,6)-1, from.substring(6,8));
 var toDate = new Date(to.substring(0,4), to.substring(4,6)-1, to.substring(6,8));
 var relDate = new Date(toDate - fromDate);
 var relMonth = (relDate.getFullYear()-1970) * 12 + relDate.getMonth();
 return relMonth;
},

getFinalDate : function() {
 var itemElmts = this.items;
 var itemElmt = null;
 var finalDate = "20080101";
 for (i=0; i<itemElmts.length; i++) {
  var tempDate = itemElmts[i].getElementsByTagName("dates")[0].getAttribute("end");
  if (eval(tempDate > finalDate)) finalDate = tempDate;
 }
 return finalDate;
},

createItemElement : function(itemId) {
 function e$(tagName, idx) {
  if (!idx) idx = 0;
  if (!itemElmt.getElementsByTagName(tagName)[idx]) return "";
  var content = (!mizuyari.ua.isWinIE) ?
   itemElmt.getElementsByTagName(tagName)[idx].textContent : itemElmt.getElementsByTagName(tagName)[idx].text;
  return content;
 }

 function a$(tagName, idx, attribute) {
  if (!idx) idx = 0;
  if (!itemElmt.getElementsByTagName(tagName)[idx]) return "";
  var value = itemElmt.getElementsByTagName(tagName)[idx].getAttribute(attribute);
  return value;
 }
 
 function markupHTML(tagName, attributes, content) {
  var outAttributes = (!attributes) ? "" : " " + attributes;
  if (!content) {
   return '<' + tagName + outAttributes + ' />';
  } else {
   return '<' + tagName + outAttributes + '>' + content + '<' + '/' + tagName +'>';
  }
 }

 var itemElmt = this.items[itemId];

 var newitemElement = document.createElement('li');
 newitemElement.setAttribute("id","item-" + itemId);

 var itemHref  = (e$('link') != "") ? ' href="' + e$('link') + '"' : "";

  var bd = a$("dates", 0, "begin");
  var ed = a$("dates", 0, "end");
  var itemDateText = a$("dates", 0, "text");

  var beginDate = (bd) ? new Date(
   bd.substring(0,4),
   bd.substring(4,6)-1,
   bd.substring(6,8)
  ) : "";
  var endDate = (ed) ? new Date(
   ed.substring(0,4),
   ed.substring(4,6)-1,
   ed.substring(6,8)
  ) : "";

 if (e$('category') != "event") {

  var itemDateFormated = (itemDateText != null) ? itemDateText : eval(beginDate.getMonth()+1) + '/' + beginDate.getDate() + '(' + this.wdays[beginDate.getDay()] +') - ' +
  eval(endDate.getMonth()+1) + '/' + endDate.getDate() + '(' + this.wdays[endDate.getDay()] +')';

  var itemPosX  = this.getPosByDate(bd);
  var itemWidth = this.getWidthByDate(bd, ed);

  if (!mizuyari.ua.isWinIE) {
   newitemElement.setAttribute("style","left:" + itemPosX + "px; width:" + itemWidth + "px");
  } else {
   newitemElement.style.cssText = "left:" + itemPosX + "px; width:" + itemWidth + "px";
  }

  newitemElement.innerHTML = 
  '<p class="title"><a' + itemHref + '><img src="' + a$('titleimage',0,'src') +'" class="title" alt="' + e$('title') + '" /></a><br />' + itemDateFormated + '</p>' +
  '<div class="image"><a' + itemHref + '><img src="' + a$('image',0,'src') +'" alt="' + e$('title') + '" /></a></div>';

 } else {

  if (this.maxEventItems <= this.eventItems) return;
  this.eventItems++;

  var itemDateFormated = (itemDateText != null) ? '<br />' + itemDateText : "";
  itemDateFormated = (bd && ed) ? '<br />' + eval(beginDate.getMonth()+1) + '/' + beginDate.getDate() + '(' + this.wdays[beginDate.getDay()] +')' : itemDateFormated;

  if (itemHref) {
   newitemElement.innerHTML = '<p>' + markupHTML('a',itemHref,e$('title')) + itemDateFormated + '</p>';
  } else {
   newitemElement.innerHTML = '<p>' + e$('title') + itemDateFormated + '</p>';
  }

 }

 miz$(this.itemListElmtPrefix + e$('category')).appendChild(newitemElement);

}

}

var elmtScrollTimer = [];
function elmtScroll(id,toX,toY,frms,cuX,cuY) { // elmtScroll #080328
 if (elmtScrollTimer[id]) clearTimeout(elmtScrollTimer[id]);
 if (!cuX) cuX = miz$(id).offsetLeft;
 if (!frms) frms = 4;
 cuX += (toX - miz$(id).offsetLeft) / frms;
 var posX = (cuX >= cuX) ? Math.floor(cuX) : Math.ceil(cuX);
 miz$(id).style.left = posX + "px";
 if (posX != toX) {
  elmtScrollTimer[id] = setTimeout("elmtScroll('"+id+"',"+toX+","+toY+","+frms+","+cuX+","+cuY+")",16);
 } else {
  miz$(id).style.left = toX + "px";
 }
}

