/**************************************************************************
 * $ProjectName: Kruse-IT website $
 * $Author: Thomas Kruse $
 * $Date: 2006/01/18 $
 * ************************************************************************
 * Copyright (c) 1999-2006 Kruse-IT GmbH
 * ************************************************************************
 * Description
 *
 *  Common services and methods for the website
 *
 * ************************************************************************
 * $Log: $
 */

/**************************************************************************
 *
 * GENERAL
 *
 */

 function initScripts(strId) {
   enableWikiAcronyms ();
   activateElement(strId);
 }

/**************************************************************************
 *
 * NAVIGATION BAR
 *
 */

/**************************************************************************
 * Prints out the current date in common german format.
 *
 * @author     Thomas Kruse
 */
function printDate()
{
   var a = new Date();
   var lm_year = a.getYear();
   if (lm_year < 1000){
      if (lm_year < 70){
      lm_year = 2000 + lm_year;
      }
      else lm_year = 1900 + lm_year;
   }
   var lm_month = a.getMonth() + 1;
   if (lm_month < 10){
      lm_month = '0' + lm_month;
   }
   var lm_day = a.getDate();
   if (lm_day < 10){
      lm_day = '0' + lm_day;
   }
   var monthName = new Array(12)
   monthName[0]  = 'Januar'
   monthName[1]  = 'Februar'
   monthName[2]  = 'März'
   monthName[3]  = 'April'
   monthName[4]  = 'Mai'
   monthName[5]  = 'Juni'
   monthName[6]  = 'Juli'
   monthName[7]  = 'August'
   monthName[8]  = 'September'
   monthName[9]  = 'Oktober'
   monthName[10] = 'November'
   monthName[11] = 'Dezember'
   document.write(lm_day + '. ' + monthName[lm_month - 1] + ' ' + lm_year);
}

/**************************************************************************
 * Prints out the current date in common german format.
 *
 * @author     Thomas Kruse
 */
function printDateYear()
{
   var a = new Date();
   var lm_year = a.getYear();
   if (lm_year < 1000){
      if (lm_year < 70){
      lm_year = 2000 + lm_year;
      }
      else lm_year = 1900 + lm_year;
   }
   document.write(lm_year);
}

/**************************************************************************
 * Prints out the number of years since the companie's foundation.
 *
 * @author     Thomas Kruse
 */
function printYearsSinceFoundation()
{
   var a = new Date();
   var lm_year = a.getYear();
   if (lm_year < 1000){
      if (lm_year < 70){
      lm_year = 2000 + lm_year;
      }
      else lm_year = 1900 + lm_year;
   }
   document.write(lm_year - 1990);
}

/**************************************************************************
 *
 * NAVIGATION
 *
 */

/**************************************************************************
 * Set the given element to active.
 *
 * @author     Thomas Kruse
 *
 * @param      strId    id of the element to be high-lightend
 */
function activateElement(strId) {
   var item = document.getElementById(strId);
   if (null != item) {
      item.className = "active";
   }
}

/**************************************************************************
 *
 * HOME PAGE
 *
 */

/**************************************************************************
 * Highlights the given element.
 *
 * @author     Thomas Kruse
 *
 * @param      strId    id of the element to be high-lightend
 */
function highlightElement(strId) {
   var item = document.getElementById(strId);
   if (null != item) {
      item.style.color = "black";
   }
}

/**************************************************************************
 * Un-highlights the given element.
 *
 * @author     Thomas Kruse
 *
 * @param      strId    id of the element to be high-lightend
 */
function unhighlightElement(strId) {
   var item = document.getElementById(strId);
   if (null != item) {
      item.style.color = "gray";
   }
}

/**************************************************************************
 *
 * PROFILE AND PROJECT VISIBILTY
 *
 */

/**************************************************************************
 * Returns the current visible project list.
 *
 * @author     Thomas Kruse
 */
function getProjectVisibility() {
   var strUrl = document.URL;
   var nParam = strUrl.indexOf("?p=");

   return (nParam < 0) ? "" : strUrl.substr(nParam + 3, 1);
}

/**************************************************************************
 * Swaps the visibility of the current projekt list.
 *
 * @author     Thomas Kruse
 *
 * @param      strPrf      current activated profile
 */
function swapProjectVisibility(strPrf) {
   window.location.href = "canvasProfile.htm" + ((getProjectVisibility() != strPrf) ? ("?p=" + strPrf) : "");
}

/**************************************************************************
 * Prints out the current projekt list.
 *
 * @author     Thomas Kruse
 *
 * @param      strPrf      current activated profile
 */
function printProjectContent(strPrf, strContent) {

   var item = document.getElementById("profile");
   if (null != item) {
      var strOptions = "#view=FitH&pagemode=none&navpanes=0";
      document.write(((getProjectVisibility() != strPrf) ? "" : "<embed style='width:100%;height:100%;' src='" + strContent + strOptions + "'></embed>"));
   }
}

/**************************************************************************
 * Prints out if the current projekt list is visible or not.
 *
 * @author     Thomas Kruse
 *
 * @param      strPrf      current activated profile
 */
function printProjectVisibility(strPrf) {
   document.write(((getProjectVisibility() != strPrf) ? "&raquo;" : "&laquo;"));
}

/**************************************************************************
 *
 * SITEMAP AND TAG-CLOUD
 *
 */

/**************************************************************************
 * Lights the sitemap entries related to the hovered tag cloud word.
 *
 * @author     Thomas Kruse
 *
 * @param      strIds   comma separated tag id list of the all sitemap
 *                      entries to be lighted
 */
function lightSME(strIds)
{
   var arrIds = strIds.split(",");

   for (var i = 0; i < arrIds.length; i++) {
      var item = document.getElementById(arrIds[i]);
      if (null != item) {
         item.style.color = "#78db64";
         item.style.textDecoration = "underline";
      }
   }
}

/**************************************************************************
 * Shades the sitemap entries related to the hovered tag cloud word.
 *
 * @author     Thomas Kruse
 *
 * @param      strIds   comma separated tag id list of the all sitemap
 *                      entries to be shaded
 */
function shadeSME(strIds)
{
   var arrIds = strIds.split(",");

   for (var i = 0; i < arrIds.length; i++) {
      var item = document.getElementById(arrIds[i]);
      if (null != item) {
         item.style.color = "";
         item.style.textDecoration = "";
      }
   }
}

/**************************************************************************
 * Lights the tag cloud entries related to the hovered sitemap entry.
 *
 * @author     Thomas Kruse
 *
 * @param      strIds   comma separated tag id list of the all tag cloud
 *                      entries to be lighted
 */
function lightTCE(strIds)
{
   var arrIds = strIds.split(",");

   for (var i = 0; i < arrIds.length; i++) {
      var item = document.getElementById("lnk" + arrIds[i]);
      if (null != item) {
         item.style.color = "#78db64";
         item.style.textDecoration = "underline";
      }
   }
}

/**************************************************************************
 * Shades the tag cloud entries related to the hovered sitemap entry.
 *
 * @author     Thomas Kruse
 *
 * @param      strIds   comma separated tag id list of the all tag cloud
 *                      entries to be shaded
 */
function shadeTCE(strIds)
{
   var arrIds = strIds.split(",");

   for (var i = 0; i < arrIds.length; i++) {
      var item = document.getElementById("lnk" + arrIds[i]);
      if (null != item) {
         item.style.color = "";
         item.style.textDecoration = "";
      }
   }
}

/**************************************************************************
 * Shows the tag cloud links for an hovered tag cloud word.
 *
 * @author     Thomas Kruse
 *
 * @param      strId    tag id of the div containing the tag cloud links
 *                      and the link-tag for the hovered tag cloud word
 */
function showTCL(strId)
{
   var itemDiv = document.getElementById("div" + strId);
   var itemLnk = document.getElementById("lnk" + strId);

   if (null != itemDiv && null != itemLnk) {
      itemDiv.style.visibility = "visible";
      itemDiv.style.marginTop  = itemLnk.style.lineHeight;
   }
}

/**************************************************************************
 * Hides the tag cloud links for an hovered tag cloud word.
 *
 * @author     Thomas Kruse
 *
 * @param      element  event element
 * @param      event    triggered event
 * @param      strIdDiv tag id of the div contaiing the tag cloud links
 */
function hideTCL(element, event, strIdDiv)
{
   if (checkMouseLeave(element, event)) {

      var itemDiv = document.getElementById("div" + strIdDiv);

      if (null != itemDiv) {
         itemDiv.style.visibility = "";
      }
   }
}

/**************************************************************************
 * Checks if the mouse pointer has entered the given element. It considers
 * the inner elements also - so that the result is the "true" entering of
 * the given element and not the leaving of one of its contained elements.
 *
 * @author     Thomas Kruse
 *
 * @param      element  event element
 * @param      event    triggered event
 */
function checkMouseEnter(element, event)
{
   if (element.contains && event.fromElement) {
      return !element.contains(event.fromElement);
   }
   else if (event.relatedTarget) {
      return !containsDOM(element, event.relatedTarget);
   }
   else {
      return false;
   }
}

/**************************************************************************
 * Checks if the mouse pointer has left the given element. It considers
 * the inner elements also - so that the result is the "true" leaving of
 * the given element and not the entering of one of its contained elements.
 *
 * @author     Thomas Kruse
 *
 * @param      element  event element
 * @param      event    triggered event
 */
function checkMouseLeave(element, event)
{
   if (element.contains && event.toElement) {
      return !element.contains(event.toElement);
   }
   else if (event.relatedTarget) {
      return !containsDOM(element, event.relatedTarget);
   }
   else {
      return false;
   }
}

/**************************************************************************
 * Checks if the containee is contained by the container.
 *
 * @author     Thomas Kruse
 *
 * @param      conainer    container element
 * @param      containee   element to be checked if it is the container's child
 */
function containsDOM(container, containee)
{
   var isParent = false;
   do {
      if ((isParent = container == containee)) {
         break;
      }
      containee = containee.parentNode;
   }
   while (containee != null);

   return isParent;
}

/**************************************************************************
 * Set the opacity of the given element in a browser dependent way.
 *
 * @author     Thomas Kruse
 *
 * @param      element     element which opacity has to be set
 * @param      nOpacity    opacity level as integer between 0 and 100
 */
function setOpacity(element, nOpacity)
{
   // plausibility check
   nOpacity = nOpacity > 100 ? 100 : (nOpacity < 0 ? 0 : nOpacity);

   if(navigator.appName.indexOf("Netscape" ) != -1 && navigator.appVersion.charAt(0) >= 4) {
      element.style.opacity = "" + (nOpacity / 100.0);
   }
   else
   if(navigator.appName.indexOf("Microsoft") != -1 && navigator.appVersion.charAt(0) >= 4) {
      element.style.filter  = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + nOpacity + ")";
   }
   else
   if(navigator.appName.indexOf("Netscape" ) == -1 && navigator.appName.indexOf("Microsoft") == -1 && navigator.appVersion.charAt(0) < 4) {
      element.style.opacity = "" + (nOpacity / 100.0);
   }
}

/**************************************************************************
 * Rertieves the opacity of the given element in a browser dependent way.
 * The opcaity is returend as an integer value between 0 and 100.
 *
 * @author     Thomas Kruse
 *
 * @param      element     element which opacity has to be retrieved
 */
function getOpacity(element)
{
   var nOpacity = 0;

   if(navigator.appName.indexOf("Netscape" ) != -1 && navigator.appVersion.charAt(0) >= 4) {
      nOpacity = Math.floor(parseFloat(element.style.opacity) * 100);
   }
   else
   if(navigator.appName.indexOf("Microsoft") != -1 && navigator.appVersion.charAt(0) >= 4) {
      nOpacity = parseInt(element.style.filter.substring("progid:DXImageTransform.Microsoft.Alpha(opacity=".length, element.style.filter.length - 1));
   }
   else
   if(navigator.appName.indexOf("Netscape" ) == -1 && navigator.appName.indexOf("Microsoft") == -1 && navigator.appVersion.charAt(0) < 4) {
      nOpacity = Math.floor(parseFloat(element.style.opacity) * 100);
   }
   else {
      nOpacity = 0;
   }

   // plausibility check
   return nOpacity > 100 ? 100 : (nOpacity < 0 ? 0 : nOpacity);
}

/**************************************************************************
 *
 * CONTACT FORM
 *
 */

/**************************************************************************
 * Navigates to the last site.
 *
 * @author     Thomas Kruse
 */
function goBack()
{
   if (navigator.appName.indexOf('Netscape')) {
      history.back();
   }
   else {
      back();
   }
}

/**************************************************************************
 *
 * WIKIPEDIA
 *
 */

/**************************************************************************
 * Attaches to each acronmy element a link to the related wikipedia entry.
 *
 * @author     Thomas Kruse
 */
function enableWikiAcronyms()
{
   if(document.getElementsByTagName && document.getElementById) {

      var acronym = document.getElementsByTagName("acronym"); // get all acronyms

      for (var l = 0; l < acronym.length; l++) {
         var linkTag  = document.createElement("a");
         var linkText = document.createTextNode (acronym[l].firstChild.nodeValue);

         linkTag.href      = "http://de.wikipedia.org/wiki/?title=Spezial:Search&search=" + acronym[l].firstChild.nodeValue;
         linkTag.target    = "_blank";
         linkTag.className = "acronym";
         linkTag.appendChild(linkText);

         acronym[l].appendChild(linkTag);
         acronym[l].firstChild.nodeValue = "";
      }
   }
}

/**************************************************************************
 *
 * RSS-FEEDER
 *
 */

/**************************************************************************
 * Prints out the specified rss feed.
 *
 * @author     Thomas Kruse
 *
 * @param      strHeader      header of the feed
 * @param      strUrl         url the feed is stored on
 * @param      nFeed          number of the feed
 * @param      nMax           max items to be printed out
 */
function printFeed(strHeader, strUrl, nFeed, nMax) {

   var handler = rss2html;

   try {
      if (document.implementation && document.implementation.createDocument) {
         var xmldoc = document.implementation.createDocument("", "", null);
         xmldoc.onload = function() {
            handler(strHeader, xmldoc, nFeed, nMax);
         }
         xmldoc.load(strUrl);
      }
      else
      if (window.ActiveXObject) {
         var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
         xmldoc.onreadystatechange = function() {
            if (xmldoc.readyState == 4) {
               handler(strHeader, xmldoc, nFeed, nMax);
            }
         }
         xmldoc.load(strUrl);
      }
      else {
         rss2html(strHeader, null, nFeed, nMax);
      }
   }
   catch (e) {
      rss2html(strHeader, null, nFeed, nMax);
   }
}

/**************************************************************************
 * Transforms the xml to an html content.
 *
 * @author     Thomas Kruse
 *
 * @param      strHeader      header of the feed
 * @param      xmldoc         xml content to be transformed
 * @param      nFeed          number of the feed
 * @param      nMax           max items to be printed out
 */
function rss2html(strHeader, xmldoc, nFeed, nMax) {

   var strNewsFeed  = "";
   var strNewsItems = "";
   var arrTitle     = new Array();
   var arrLink      = new Array();
   var newsfeed0    = null;
   var newsfeed1    = null;

   // set standard header
   strNewsFeed = '<h1>' + strHeader + "</h1>" ;

   try {
      if (xmldoc != null) {
         // parse the xml feed document
         var items = xmldoc.getElementsByTagName("item");

         for(var i = 0; i < items.length && i < nMax; i++) {
            var item = items[i];
            arrTitle[i] = item.getElementsByTagName("title")[0].firstChild.data;
            arrLink [i] = item.getElementsByTagName("link" )[0].firstChild.data;
         }

         // generate html from the parsed document
         for(var i = 0; i < arrTitle.length; i++ ) {
            strNewsItems = strNewsItems + '<p class="feedItem"><a target="_blank" href="' + arrLink [i] + '">' + arrTitle[i] + '</a></p>';
         }
      }
   }
   catch (e) { /* do nothing */ }
   finally {

      // check if we have at least one item
      if (strNewsItems == null || strNewsItems == '') {
         strNewsItems = '<p class="feedItem">News zur Zeit nicht abrufbar</p>';
      }

      // fill in document
      if (nFeed == 0) {
         newsfeed0.innerHTML = strNewsFeed + strNewsItems;
      }
      else
      if (nFeed == 1) {
         newsfeed1.innerHTML = strNewsFeed + strNewsItems;
      }
   }
}

