/*#REV:LizardCMS-664af563d6-1361ca-e2d5a60feaea-437837b4c6c17429ba9e995b41193d7f 7 2011-04-08 23:53:13 843a04c24147715a67e9611064d0571d */

var LDMain = new LDMainFunc();
function LDMainFunc()
{
  this.pageload = function()
  {

  }

  this.getElements = function(parent, list)
  {
    for (var i in parent.childNodes)
    {
      if (parent.childNodes[i].tagName != null) {list.push(parent.childNodes[i]);}
      if (parent.childNodes[i].hasChildNodes) {list = LDMain.getElements(parent.childNodes[i], list);}
    }
    return list;
  }

  this.utf8_encode = function(plain)
  {
    plain = plain.replace(/\r\n/g,"\n");
    var utftext = "";
    for (var n=0; n<plain.length; n++)
    {
      var c = plain.charCodeAt(n);
      if (c < 128)
      {
        utftext += String.fromCharCode(c);
      } else
      if ((c>127) && (c<2048))
      {
        utftext += String.fromCharCode((c>>6)|192);
        utftext += String.fromCharCode((c&63)|128);
      } else
      {
        utftext += String.fromCharCode((c>>12)|224);
        utftext += String.fromCharCode(((c>>6)&63)|128);
        utftext += String.fromCharCode((c&63)|128);
      }
    }
    return utftext;
  }

  this.maillink = function(obj, name, domain, tld)
  {
    if ((obj != null) && (typeof(obj) != 'undefined'))
    {
      var link = name + '@' + domain + '.' + tld;
      obj.setAttribute('href', 'mailto:'+link);
      obj.setAttribute('title', link);
      return link;
    }
    return '';
  }

  this.imgPopup = function(imgsrc)
  {
    var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
    var windowWidth  = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;

    var body = document.getElementsByTagName('body')[0];

    var div = document.createElement('div');

    var img = new Image();
    img.onload = imgloaded;
    img.onerror = loadingError;
    img.src = imgsrc;

    div.id = 'LDImagePopupDiv';
    div.style.position = 'absolute';
    div.style.border = '3px outset #888888';
    div.style.background = '#FFFFFF';
    div.style.padding = '4px';
    div.style.zIndex = '100001';
    var loadingimg = document.createElement('img');
    loadingimg.src = 'cmsimages/loadingsmall2.gif';
    loadingimg.style.verticalAlign = 'middle';
    loadingimg.style.marginRight = '3px';
    div.appendChild(loadingimg);
    div.appendChild(document.createTextNode('loading...'));
    div.style.left = ((windowWidth - div.offsetWidth) / 2) + 'px';
    div.style.top = ((windowHeight - div.offsetHeight) / 2) + 'px';

    var selectPreventionDiv = document.createElement('div');
    selectPreventionDiv.style.position = 'absolute';
    selectPreventionDiv.style.left = '0px';
    selectPreventionDiv.style.top = '0px';
    selectPreventionDiv.style.width = '100%';
    selectPreventionDiv.style.height = '100%';
    selectPreventionDiv.style.backgroundColor = 'gray';
    selectPreventionDiv.style.opacity = '0.6';
    selectPreventionDiv.style.filter = 'Alpha(opacity=60)';
    selectPreventionDiv.style.zIndex = '100000';
    body.appendChild(div);
    body.appendChild(selectPreventionDiv);

    function imgloaded()
    {
      var div = document.getElementById('LDImagePopupDiv');
      if (!div)
      {
        window.setTimeout(imgloaded, 10);
        return;
      }
      var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
      var windowWidth  = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;

      var imgTag = document.createElement('img');
      imgTag.src = img.src;
      imgTag.style.cursor = 'pointer';
      imgTag.onclick = closePopup;
      var imgTagWidth = img.width;
      var imgTagHeight = img.height;
      if ((img.height > (windowHeight - 20)) || (img.width > (windowWidth - 20)))
      {
        if ((img.height / windowHeight) > (img.width / windowWidth))
        {
          imgTagHeight = windowHeight - 20;
          imgTagWidth  = Math.floor(img.width * (windowHeight / img.height));
        }
        else
        {
          imgTagWidth  = windowWidth - 20;
          imgTagHeight = Math.floor(img.height * (windowWidth / img.width));
        }
      }
      div.style.left = ((windowWidth - imgTagWidth - 20) / 2) + 'px';
      div.style.top = ((windowHeight - imgTagHeight - 20) / 2) + 'px';
      imgTag.style.width = imgTagWidth + 'px';
      imgTag.style.height = imgTagHeight + 'px';
      while (div.hasChildNodes())
        div.removeChild(div.firstChild);
      window.setTimeout(function() {div.appendChild(imgTag);}, 0);
      return;
    }
    function closePopup()
    {
      var body = document.getElementsByTagName('body')[0];
      var div = document.getElementById('LDImagePopupDiv');
      body.removeChild(div);
      body.removeChild(selectPreventionDiv);
    }
    function loadingError()
    {
      var body = document.getElementsByTagName('body')[0];
      var div = document.getElementById('LDImagePopupDiv');
      alert('An error occured while loading.');
      body.removeChild(div);
      body.removeChild(selectPreventionDiv);
    }
  }

  //find absolute position of an element
  this.findPos = function(obj)
  {
    if (obj == null)
      return false;
    var x = 0;
    var y = 0;
    while (obj)
    {
      x += obj.offsetLeft;
      y += obj.offsetTop;
      obj = obj.offsetParent;
    }
    return {'x':x, 'y':y};
  }

  //object fade out
  var fadeOutQueue = new Array();
  var fadeOutOpacity;
  var fadeOutInterval;
  var fadeOutHeight;
  var fadeOutActive = false;
  this.fadeOut = function(objectid, delay, manual)
  {
    if (delay == null) {delay = 0;}
    if (manual == null) {manual = false;}
    setTimeout("LDMain.fadeOutEnqueue('"+objectid+"', "+manual+")", delay);
  }

  this.fadeOutQueueCheck = function()
  {
    if (fadeOutActive)
    {
      setTimeout("LDMain.fadeOutQueueCheck()", 200);
    } else
    {
      if (fadeOutQueue.length) {this.fadeOutStart(fadeOutQueue.shift());}
    }
  }


  this.fadeOutEnqueue = function(objectid, manual)
  {
    if (manual) {fadeOutQueue.unshift(objectid);} else {fadeOutQueue.push(objectid);}
    this.fadeOutQueueCheck();
  }

  this.fadeOutStart = function(objectid)
  {
    var obj = document.getElementById(objectid);
    if ((obj != null) && (obj.style.display != 'none'))
    {
      fadeOutActive = true;
      fadeOutOpacity = 1;
      fadeOutInterval = setInterval("LDMain.fadeOutWorkerFade('"+objectid+"')", 50);
    } else
    {
      this.fadeOutQueueCheck();
    }
  }

  this.fadeOutWorkerFade = function(objectid)
  {
    var obj = document.getElementById(objectid);
    fadeOutOpacity -= 0.05;
    if (fadeOutOpacity < 0) {fadeOutOpacity = 0;}
    fadeOutOpacity = Math.round(fadeOutOpacity*100)/100;
    obj.style.opacity = fadeOutOpacity;
    if (fadeOutOpacity <= 0)
    {
      clearInterval(fadeOutInterval);
      fadeOutHeight = obj.offsetHeight;
      obj.style.display = 'none';
      fadeOutActive = false;
      this.fadeOutQueueCheck();
      //fadeOutInterval = setInterval("LDMain.fadeOutWorkerMinimize('"+objectid+"')", 50);
    }
  }

  this.fadeOutWorkerMinimize = function(objectid)
  {
    fadeOutHeight -= 5000;
    if (fadeOutHeight < 0) {fadeOutHeight = 0;}
    //document.getElementById(objectid).style.height = fadeOutHeight+"px";
    if (fadeOutHeight <= 0)
    {
      clearInterval(fadeOutInterval);
      document.getElementById(objectid).style.display = "none";
      fadeOutActive = false;
      this.fadeOutQueueCheck();
    }
  }



  //implement the indexOf-Method for Arrays
  Array.prototype.indexOf = function(obj)
  {
    var len = this.length;
    var id = -1;
    for (var i=0; i<len; i++)
    {
      if (this[i] === obj)
      {
        id = i;
        break;
      }
    }
    return id;
  }

  this.getElementsByClassName = function(oElm, strTagName, strClassName)
  {
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for (var i=0; i<arrElements.length; i++)
    {
      oElement = arrElements[i];
      if(oRegExp.test(oElement.className)) {arrReturnElements.push(oElement);}
    }
    return (arrReturnElements)
  }

  //append function to object, e.g. LDMain.addEvent(window, 'load', myFunc);
  this.addEvent = function(obj, evType, fn)
  { 
    if (obj.addEventListener) {obj.addEventListener(evType, fn, false); return true;} else
    if (obj.attachEvent) {var r = obj.attachEvent("on"+evType, fn); return r;}
      else {return false;}
  }

  //document.import node also for msie
  //thanks to Xavier Amado - http://xavierdeveloper.blogspot.com/
  this.importNodeEx = function(oNode, bImportChildren)
  {
    if (bImportChildren == null) {bImportChildren = true;}
    if (document.importNode) {return document.importNode(oNode, bImportChildren);} else
    {
      var oNew;
      if( oNode.nodeType == 1)
      {
        oNew = document.createElement(oNode.nodeName);

        for(var i = 0; i < oNode.attributes.length; i++)
        {
          if (oNode.attributes[i].nodeValue != null && oNode.attributes[i].nodeValue != '')
          {
            var attrName = oNode.attributes[i].name;
            if (attrName == "class") {oNew.setAttribute("className", oNode.attributes[i].value);}
                                else {oNew.setAttribute(attrName, oNode.attributes[i].value);}
          }
        }

        if (oNode.style != null && oNode.style.cssText != null) {oNew.style.cssText = oNode.style.cssText;}
      } else
      if (oNode.nodeType == 3)
      {
        oNew = document.createTextNode(oNode.nodeValue);
      }

      if (bImportChildren && oNode.hasChildNodes())
      {
        for (var oChild = oNode.firstChild; oChild; oChild = oChild.nextSibling)
        {
          oNew.appendChild(LDMain.importNodeEx(oChild, true));
        }
      }

      return oNew;
    }
  }


  this.rand = function(min, max)
  {
    if (min > max) {return( -1 );}
    if (min == max) {return( min );}
    return (min + parseInt(Math.random() * (max-min+1)));
  }

  this.checkCapsLock = function(e)
  {
    if (e == null) {return false;}
    var myKeyCode=0;
    var myShiftKey=false;
    if ((e.keyCode == null) || (e.keyCode == 0)) {myKeyCode = e.which;} else {myKeyCode = e.keyCode;}
    if (e.shiftKey == null) {return false;} else {myShiftKey = e.shiftKey;}

    if (((myKeyCode >= 65) && (myKeyCode <= 90)) && !myShiftKey) {return true;} // Upper case letters are seen without depressing the Shift key
    if (((myKeyCode >= 97) && (myKeyCode <= 122)) && myShiftKey) {return true;} // Lower case letters are seen while depressing the Shift key
    return false;
  }


}
