/* hafas_standard_calendar_utilties.js */
/* built: 05.11.2007 by MHEI */

// extracted from prototype framework - BEGIN
/*if(typeof $=='undefined'){
  function $() {
     return document.getElementById(arguments[0]);
  }
}
if(typeof Class=='undefined'){
  var Class = {
    create: function() {
      return function() {
        this.initialize.apply(this, arguments);
      }
    }
  }
}*/
// extracted from prototype framework - END

// some more Date object enhancements (see hafas_utilities.js):
Date.prototype.shiftD = function(value) {
   this.setDate(this.getDate()+value);
}

Date.prototype.shiftM = function(value) {
   this.setMonth(this.getMonth()+value);
}

Date.prototype.shiftY = function(value) {
   this.setYear(this.getYear()+value);
}

function parseUserDateInput(userInput,weekdaysTexts) {
   if (isString(userInput) && (userInput.length>0) && !isDate(userInput)) {

      if ((userInput.length > 2) || (userInput.indexOf("+") != -1)) {
         var matchReg = /(\d+)\D+(\d+)\D+(\d+)\D*/;
         matchReg.exec(userInput);

         var day = 1 * RegExp.$1;
         var month = 1 * RegExp.$2;
         var year = 1 * RegExp.$3;

         // attention: month is 1-12-ranged!
         // ~~~
         if (month != "") {
            month -= 1;
            if (month < 0) {
               month = 11;
            } else if (month > 11) {
               month = 0;
            }
         }
         if (year != "") {
            if (year < 100) {
              year += 2000;
            }
         }
         if ((year != 0) && (day != 0)) {
            return new Date(year, month, day);
         }

      }
      // else if (userInput.length == 2) {
      //   this.mydoWeekday(userInput);
      //}
   } else if (isDate(userInput)) {
      return userInput;
   }
   return new Date();
}

function isFunction(o) {
  return (typeof(o)=="function");
}

function isObject(a)
{
  return (typeof a == 'object' && !!a) || isFunction(a);
}

function isArray(a)
{
  return isObject(a) && a.constructor == Array;
}

function isDate(a)
{
  return isObject(a) && a.constructor == Date;
}

function isString(a)
{
  return typeof a == 'string';
}

function inherits(base, extension)
{
   for ( var property in base )
   {
      try
      {
         extension[property] = base[property];
      }
      catch( warning ){}
   }
}

function inheritsPartly(base, extension, properties)
{
   for ( var i = 0; i < properties.length; i++ )
   {
      try
      {
         extension[properties[i]] = base[properties[i]];
      }
      catch( warning ){}
   }
}

function makeObservable (obj, observer) {
   inherits(new Observable(), obj);
   if (observer != undefined) {
      obj.addObserver(observer);
   }
}

Array.prototype.forEach = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        fn.call(scope, this[i], i, this);
    }
};

Array.prototype.filter = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( !fn.call(scope, this[i], i, this) ) {
            continue;
        }
        a.push(this[i]);
    }
    return a;
};

Observer = Class.create();
Observer.prototype = {

      initialize: function() {
         // intentionally left blank
      },
      observe: function() {
      }

}

function Observable() {
    this.fns = [];
}

Observable.prototype = {
    addObserver : function(fn) {
        this.fns.push(fn);
    },
    removeObserver : function(fn) {
        this.fns = this.fns.filter(
            function(el) {
                if ( el !== fn ) {
                    return el;
                }
            }
        );
    },
    notify : function(o, thisObj) {
        var scope = thisObj || window;
        this.fns.forEach(
            function(el) {
                //el.call(scope, o);
                el.observe(o);
            }
        );
    }
};

CalUtils = Class.create();
CalUtils.prototype =
{

    initialize : function() { // intentionally left blank
    },

    getDimensions : function(el) {
       var display = el.style.display;
       var ret = new Object();
       var e = el;

       for ( ret.x = 0, ret.y = 0; e != null; ret.x += e.offsetLeft, ret.y += e.offsetTop, e = e.offsetParent)
          ;

       /* Safari bug */
       if (display != 'none' && display != null) {

          ret.w = el.offsetWidth;
          ret.h = el.offsetHeight;
       } else {

          /*
           *  All *Width and *Height properties give 0 on els with display none, so enable the el temporarily
           */
          var els = el.style;
          var orgVisibility = els.visibility;
          var orgPosition = els.position;
          var orgDisplay = els.display;
          els.visibility = 'hidden';
          els.position = 'absolute';
          els.display = 'block';

          ret.w = el.clientWidth;
          ret.h = el.clientHeight;

          els.display = orgDisplay;
          els.position = orgPosition;
          els.visibility = orgVisibility;
       }
       return ret;
    },

  getWindowSize: function (w) {
    var array = [];

    w = w ? w : window;
    array.width = array[0] = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
    array.height = array[1] = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);

    return array;
  }

}

// removes given element from position in DOM tree and appends it to the body
// (good for absolute positioning, ...)
function moveToBody(element) {
   if (isObject(element)) {
      // is object already in DOM tree?
      if (element.parentNode != null) {
         var child = element.parentNode.removeChild(element);
      } else {
         var child = element;
      }
      document.body.appendChild(child);
      return child;
   } else {
      return element;
   }
}

function safeAttachEvent(el, event, func) {
   // IE Safe
   if (el.addEventListener){
      el.addEventListener(event, func, false);
  } else if (el.attachEvent){
      el.attachEvent('on'+event, func);
   }
}

function putIFrameBehind(div, iframeid) {
   if (typeof(iframeid) == "undefined") {
       iframeid = div.id+"_iframe";
   }
   if (isObject(div)) {
     var iframe = document.getElementById(iframeid);
     if (iframe == null) {
        iframe = cEl("iframe");
        document.body.appendChild(iframe);
        iframe.id = iframeid;
     }
     iframe.className = div.className;
     iframe.style.position = "absolute";
     calUtils = new CalUtils();

     var divDim = calUtils.getDimensions(div);

     iframe.width = divDim.w + "px";
     iframe.height = (divDim.h - div.style.border)+ "px";
     iframe.style.top = divDim.y + "px";
     iframe.style.left = divDim.x + "px";
     iframe.style.border = "none";
     iframe.style.display = div.style.display;
   }
}

function modifyClassName(element, classname, activate) {
    /* de-/activates given classname in element*/
   var css = " "+element.className+" ";
   if (activate) {
      // not present -> append
      if (css.search(" "+classname+" ") > -1) {
      } else {
         css = css+classname;
      }
   } else {
      css = css.replace(" "+classname+" ", " ");
   }
   css = css.replace(/^\s+/, "").replace(/\s+$/, "");
   element.className = css;
}


