var cPath = (window.getRelativeWebRoot ? window.getRelativeWebRoot() : '') + 'js/';
document.write ('<SCR' + 'IPT LANGUAGE="JavaScript1.2" SRC="'+ cPath +'Lib/BPage.js" TYPE="text/javascript"><\/SCR' + 'IPT>');

// Nur benötigt, wenn zusätzliche Javascript-Funktionen auf jeder Seite benötigt werden.

function getPageTags (nPageID, cParams) {
  if (cParams == null) cParams = "ajax=getTags";
  var url = window.location.pathname + "?" + cParams;
  var def = dojo.xhrGet({
    url: url,
    contentType: "text/txt",
    handleAs: "json"
  });
  def.addCallbacks(function(a) { showPageTags(nPageID, a)}, function(e){ alert ('error')});
}

function showPageTags (nPageID, aTags) {
  var parent = document.getElementById('existing-page-tags');
  while (parent.firstChild) parent.removeChild (parent.firstChild);

  for (var id in aTags) {
    var noDiv = document.createElement('a');
    noDiv.setAttribute ("id", "tagID" + id);
    noDiv.setAttribute ("class", "existingTag");
    noDiv.setAttribute ("href", "javascript:removePageTag(" + nPageID + "," + id + ")");
    noDiv.appendChild (document.createTextNode (aTags[id]));
    parent.appendChild (noDiv);
  }
}

function addPageTag (nPageID, cTagName) {
  getPageTags (nPageID, "ajax=addTag&TagName=" + cTagName);
}

function removePageTag (nPageID, nTagID) {
  getPageTags (nPageID, "ajax=removeTag&TagID=" + nTagID);
}

function editPageTags (nPageID, nTagListPageID) {
  var form = document.forms.EditPageTags;

  form.onsubmit = function() {
    var inputTag = form.elements["add-tag-name"];
    addPageTag(nPageID, inputTag.value);
    inputTag.value='';
    inputTag.focus();
    return false;
  }
  dojo.require("dijit.form.FilteringSelect");
  dojo.require("dojo.data.ItemFileReadStore");
  var tagsStore = new dojo.data.ItemFileReadStore({
      url: "Page" + nTagListPageID + (window.location.pathname.match(/\.page(\?|$)/) ? ".page" : ".html") + "?ajax=getTagStore"
  });

  var filteringSelect = new dijit.form.ComboBox({
      id: "add-tag-name",
      name: "name",
      store: tagsStore,
      searchAttr: "name"
  }, "add-tag-name");

}

function autoCompleteTags (cSearchFieldID, cSearchFieldName, nTagListPageID) {
  dojo.require("dijit.form.FilteringSelect");
  dojo.require("dojo.data.ItemFileReadStore");
  var tagsStore = new dojo.data.ItemFileReadStore({
      url: "Page" + nTagListPageID + (window.location.pathname.match(/\.page(\?|$)/) ? ".page" : ".html") + "?ajax=getTagStore"
  });

  var filteringSelect = new dijit.form.ComboBox({
      id: cSearchFieldID,
      name: cSearchFieldName,
      store: tagsStore,
      searchAttr: "name"
  }, cSearchFieldID);
}





function EventCalendar (nPageID, nTodayStamp, bOnStartPage) {
  this.nToday = nTodayStamp;
  this.nMonth = nTodayStamp;
  this.nShowDate = nTodayStamp;
  this.bOnStartPage = (bOnStartPage == true);
  this.nCalendarPageID = nPageID;
  this.oCategoryForm = null;
  this.div = null;
  this.init();
}

EventCalendar.prototype.init = function () {
  this.div = dojo.byId("event-calendar");
  this.oCategoryForm = document.forms.EventCategories;
  var self = this;
  if (!this.bOnStartPage) {
    dojo.forEach (dojo.query("table.month tr.days td a", this.div),
                  function(a) {
                    if (!a.className.match (/nolink/)) {
                      a.onclick = function() {
                        var r = /day(\d+)$/.exec (this.className);
                        if (r) self.loadDay (r[1]);
                        return (false);
                      };
                    }
                  });
  }

  if (this.oCategoryForm != null) {
    for (var i in this.oCategoryForm.elements) {
      var elem = this.oCategoryForm.elements[i];
      if (elem.type == "checkbox") {
        elem.onchange = function() {
          this.form.onsubmit();
        }
      }
    }
  }
};



EventCalendar.prototype.rpc = function (data, targetDiv) {
//  console.log ("rpc (", data , ")");
  if (targetDiv == null) targetDiv = this.div;
  var URL = window.location.pathname + "?ajax=EventCalendar"
  var def = dojo.rawXhrPost({
    url: URL,
    postData: dojo.toJson(data),
    contentType: "text/json"
  });
  var self = this;
  def.addCallbacks(function(html) {self.rpcResult(html, targetDiv);}, this.rpcError);
};


EventCalendar.prototype.rpcResult = function (html, targetDiv) {
  var node = document.createElement("DIV");
  node.innerHTML = html;
  node = node.firstChild;
  while (node.nodeType != 1) node = node.nextSibling;
  targetDiv.innerHTML = node.innerHTML;
  this.init();
};

EventCalendar.prototype.rpcError = function (a,b,c) {
  alert('rpcError: a=' + a + ', b=' + dojo.toJson(b) + ', c=' + c);
};


EventCalendar.prototype.loadMonth = function(month) {
  if (typeof month == "object") month = month.options[month.options.selectedIndex].value;
  this.nMonth = month;
  var data = {month: this.nMonth}
  if (this.nShowDate > 0) data["showdate"] = this.nShowDate;
  if (this.nToday > 0) data["today"] = this.nToday;
  if (this.oCategoryForm != null) data["CatIDs"] = this.getFormCategoryIDs();

  this.rpc (data);
  return (false);
};



EventCalendar.prototype.loadDay = function(day) {
  var data = {today: day, showdate: day, showEvents: true};
  this.nShowDate = day;
  this.nToday = day;
  if (this.oCategoryForm != null) data["CatIDs"] = this.getFormCategoryIDs();
  var targetDiv = dojo.byId("page-content");
  this.rpc (data, targetDiv);
};

EventCalendar.prototype.getFormCategoryIDs = function() {
  var cCatIDs = '';
  if (this.oCategoryForm != null) {
    for (var i in this.oCategoryForm.elements) {
      var element = this.oCategoryForm.elements[i];
      if (element.checked) cCatIDs += (cCatIDs.length == 0 ? '' : ',') + element.value;
    }
  }
  return (cCatIDs);
}

EventCalendar.prototype.onSubmitForm = function() {
  var data = {showEvents: true};
  if (this.nShowDate > 0) data["showdate"] = this.nShowDate;
  if (this.nMonth > 0) data["month"] = this.nMonth;
  if (this.nToday > 0) data["today"] = this.nToday;
  if (this.oCategoryForm != null) data["CatIDs"] = this.getFormCategoryIDs();
  var targetDiv = dojo.byId("page-content");
  this.rpc (data, targetDiv);
  return (false);
};


