var curGBpage = 1;
function checkLinks(){
var submenus = document.getElementsByClassName('divSubmenu');
if (submenus.length > 0) {
  var i = 0;
  var offset = $('naviMenu').offsetLeft;
  do {
    submenus[i].id.match(/(\d+)$/);
    var id = RegExp.$1;
    $('td_navi_'+id).setStyle({paddingLeft: offset+"px"});
    i++;
  } while (i < submenus.length);
}
onloading.init();
var aLinks=document.getElementsByTagName('a');if(aLinks.length>0){var href = new Array(); var cnt = 0; for(var i=0;i<aLinks.length;i++){if(!aLinks[i].onclick){if(aLinks[i].href.match(/file\.php/)){href.push(aLinks[i].href);aLinks[i].href="javascript:void(0)";aLinks[i].onclick=function(){window.open(href[cnt],'Files','resizable=yes,height=600,width=500');cnt++;}}}
if(aLinks[i].className=="class_intern"&&!aLinks[i].onclick&&!aLinks[i].href.match(/pid/)&&!aLinks[i].href.match(/\.html/)){var link=aLinks[i].href;var sLink=link+"/pid/"+aCurPageId;sLink+=(aCurSubpageId)?"/subpage/"+aCurSubpageId:"";aLinks[i].href=sLink;}
if(aLinks[i].href.match(/image\.php/)){aLinks[i].href=aLinks[i].href.replace(/&y=\d+/,""); aLinks[i].href=aLinks[i].href.replace(/x=\d+/,"x="+((document.viewport.getWidth())-50)+"&y="+((document.viewport.getHeight())-150));}}}
if($('previewLogin')){document.getElementsByTagName('body')[0].style.overflow="hidden";new Effect.Appear($('previewLogin'));}}
function displayGBpage(el){var id = parseInt($(el).innerHTML);if (!id) { id = 1; }if (curGBpage) {$('content').getElementsByClassName('gb_page_no_active')[0].className = "gb_page_no";new Effect.Fade('gbPage_'+curGBpage,{duration: 0.4, queue: 'front'});}$(el).className = "gb_page_no_active";new Effect.Appear('gbPage_'+id,{duration: 0.4, queue: 'end'});curGBpage = id;}
function showGB() {new Effect.Appear('gb',{duration: 0.7});$('gbPageNo').show();}
if(window.addEventListener)window.addEventListener("load",checkLinks,false);else if(window.attachEvent)window.attachEvent("onload",checkLinks);

var onloading = {
  init: function() {
    log.info('loading');
    if (loading && loading.length > 0) {
      var i = 0;
      do {
        switch(loading[i].fkt) {
          case 'events':
            this.loadEvents(loading[i].id);
          break;
        }
        i++;
      } while (i < loading.length);
    }
  },
  
  loadEvents: function(id) {
    log.info("events");
    var url = new URL({selfMade: "modId/"+id,modfkt: "read", admin: false});
    log.info(url.getURL());
    new Ajax.Request(url.getURL(),{
      onCreate: function() {
        log.info("yes make something");
      },
      onSuccess: function(req) {
        try {
        log.info(req.responseText);
        if (req.responseText.isJSON()) {
          var data = req.responseText.evalJSON();
          log.info("Data.length " + data.length);
          if (data.length > 0) {
            var div = Builder.node('div',{
              style: "position: absolute; top: 25%; left: 25%; z-index: 3000; width: auto; height: auto; display: none;",
              className: "popup"
            });
            var head = Builder.node('div',{
              className: "phead"
            });
            var cl = Builder.node('span',{
              className: "pClose"
            });
            cl.onclick = function() {
              new Effect.Fade(div);
            }
            head.appendChild(cl);
            var content = Builder.node('div',{
              className: "pcontent"
            });
            var i = 0;
            do {
              try {
                var con = Builder.node('div',{
                  className: "pcontent_inner"
                },[
                  Builder.node('div',{
                    className: "pcontent_time"
                  },data[i].event_time_start),
                  Builder.node('div',{
                    className: "pcontent_title"
                  },[
                    Builder.node('a',{
                      href: data[i].event_link
                    },data[i].event_title)
                  ])
                ]);
                content.appendChild(con);
              }
              catch(e) {
                log.error(e.message);
              }
              i++;
            } while (i < data.length);
            var footer = Builder.node('div',{
              className: "pfooter"
            })
            div.appendChild(head);
            div.appendChild(content);
            div.appendChild(footer);
            Insertion.Top(document.body,div);
            new Effect.Appear(div);
          }
        }
        }
        catch(e) {
          if (logging)
            log.error(e.message+" at line " +e.lineNumber + " " + e.fileName);
        }
      }
    })
  }
};


var displayedNavi = null;

function toggleMenuDiv(id, out) {
  if (!out) {
    if ($('navi_'+id)) {
      var offset = $('naviMenu').offsetLeft;
      $('td_navi_'+id).setStyle({paddingLeft: offset+"px"});
      $('navi_'+ id).show();
      $('li_'+id).className = 'menulink_hover';
    }
  }
  else {
    if ($('navi_'+id)) {
      $('navi_'+id).hide();
      $('li_'+id).className = 'menulink';
    }
  }
}






var general = {
  emailfavData : {
    divElement: null,
    divElementContent: null,
    divElementIfr: null,
    overlayDuration: 0.2
  },
  makeWindow: function(url) {
    try {
      var pageSize = this.getPageSize();
      var width  = Math.ceil(pageSize[0] / 2);
      var height = pageSize[1];
      var pHeight = document.viewport.getHeight();
      var arrayPageScroll = document.viewport.getScrollOffsets();
      general.emailfavData.divElement = Builder.node('div',{
        id: "emailfavDiv",
        className: "outer_email_fav",
        style: "position: absolute; top: " + Math.ceil(arrayPageScroll[1] + (pHeight/2) - 102)+"px;"
      });
      general.emailfavData.divElementIfr = Builder.node('iframe',{
        frameborder: "0",
        className: "outer_email_fav_ifr",
        scrolling: "no",
        allowtransparency: "true",
        src: url
      });
      general.emailfavData.divElement.appendChild(general.emailfavData.divElementIfr);
      (document.getElementsByTagName('body')[0] || document.body).appendChild(general.emailfavData.divElement);
      if ($('overlay')) {
        try {
          $('overlay').setStyle({height: (height + (document.all?arrayPageScroll[1]:0))+"px", width: "100%"});
          new Effect.Appear($('overlay'), { duration: general.emailfavData.overlayDuration, from: 0.0, to: 0.72 , afterFinish: function() { log.info("done"); }});
        }
        catch(e) {
          if (logging)
            log.error(e.message + " at line " + e.lineNumber+ " " + e.fileName);
        }
      }
      else {
        if (logging)
          log.error("no overlay found");
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hide' });
        var _overlay = Builder.node('div',{
          id: 'overlay',
          style: "height: "+(height + (document.all?arrayPageScroll[1]:0))+"px; width: 100%;"
        });
        (document.getElementsByTagName('body')[0] || document.body).appendChild(_overlay);
        var t = this;
        (function() {
          $('overlay').hide().observe('click', (function() { this.emailfavClose(); }).bind(t));
          new Effect.Appear($('overlay'), { duration: general.emailfavData.overlayDuration, from: 0.0, to: 0.72 , afterFinish: function() { log.info("done"); }});
        }).defer();
      }
      new Effect.Appear(general.emailfavData.divElement,{ duration: general.emailfavData.overlayDuration, from: 0.0, to: 1.0 });
    }
    catch(e) {
      if (logging)
        log.error(e.message + " at line " + e.lineNumber + " in file " + e.fileName);
    }
  },
  emailfav: function(e) {
    this.makeWindow("/inc/wp_emailfav.php?url=" + encodeURI(window.location.href));
  },
  emailfavClose: function() {
    if ($('overlay')) {
      new Effect.Fade($('overlay'), { duration: general.emailfavData.overlayDuration, afterFinish: function() {
        (function() {
          Element.remove($('overlay'));
          log.info("removed");
        }).defer();
      } });
      $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
      if (general.emailfavData.divElement) {
        Element.remove(general.emailfavData.divElement);
        general.emailfavData.divElement = null;
      }
    }
  },
  printData: {
    divElement: null,
    divElementContent: null,
    divElementIfr: null,
    queryString: null
  },
  print: function() {
    var pageSize = this.getPageSize();
    var width  = 486; //Math.ceil(pageSize[0] / 2);
    var height = pageSize[1];
    var vHeight = document.viewport.getHeight();
    var arrayPageScroll = document.viewport.getScrollOffsets();
    general.printData.divElement = Builder.node('div',{
      id: "emailfavDiv",
      className: "outer_print",
      style: "position: absolute; top: "+ arrayPageScroll[1]+"px; left: 50%; width: "+ width+"px; height: " + vHeight+"px; margin-left: -"+Math.ceil(width/2)+"px; z-index: 30000; display: none;"
    });
    general.printData.divElementContent = Builder.node('div',{
      style: "margin: 2px; padding: 20px 10px 10px; width: " + (width-24)+"px; height: "+ (vHeight-35)+"px; background-color: #fff;"
    });
    general.printData.divElement.appendChild(general.printData.divElementContent);
    var _close = Builder.node('div',{
      style: "position: absolute; top: 4px; left: 14px; width: "+ (width-24)+"px; height: 20px;"
    },[
      Builder.node('span',{
        style: "float: left;"
      },[
        Builder.node('a',{
          href: "javascript:;",
          onclick: "$('printIfr').contentWindow.print(); return false;"
        },"Drucken")
      ]),
      Builder.node('span',{
        style: "float: right;"
      },[
        Builder.node('a',{
          href: "javascript:;",
          onclick: "general.printClose(); return false;"
        },"Schließen")
      ])
    ]);
    general.printData.divElement.appendChild(_close);
    general.printData.divElementIfr = Builder.node('iframe',{
      id: "printIfr",
      frameborder: "0",
      style: "border: 0px; width: " + (width-24)+"px; height: "+ (vHeight-35)+"px; background-color: #fff;",
      src: "/inc/wp_print.php?" + general.printData.queryString
    });
    general.printData.divElementContent.appendChild(general.printData.divElementIfr);
    (document.getElementsByTagName('body')[0] || document.body).appendChild(general.printData.divElement);
    if ($('overlay')) {
      $('overlay').setStyle({height: height+"px", width: "100%"});
      new Effect.Appear($('overlay'), { duration: general.emailfavData.overlayDuration, from: 0.0, to: 0.72 });
    }
    else {
      $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hide' });
      var _overlay = Builder.node('div',{
        id: 'overlay',
        style: "height: "+(height + (document.all?arrayPageScroll[1]:0))+"px; width: 100%;"
      });
      (document.getElementsByTagName('body')[0] || document.body).appendChild(_overlay);
      var t = this;
      (function() {
        $('overlay').hide().observe('click', (function() { this.printClose(); }).bind(t));
        new Effect.Appear($('overlay'), { duration: general.emailfavData.overlayDuration, from: 0.0, to: 0.72 , afterFinish: function() { log.info("done"); }});
      }).defer();
    }
    new Effect.Appear(general.printData.divElement,{ duration: general.emailfavData.overlayDuration, from: 0.0, to: 1.0 });
  },
  printClose: function() {
    if ($('overlay')) {
      new Effect.Fade($('overlay'), { duration: general.emailfavData.overlayDuration, afterFinish: function() {
        (function() {
          Element.remove($('overlay'));
          log.info("removed");
        }).defer();
      }});
      $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
      if (general.printData.divElement) {
        Element.remove(general.printData.divElement);
        general.printData.divElement = null;
      }
    }
  },
  newsletter: function() {
    this.makeWindow("/inc/wp_newsletter.php");
  },
  getPageSize: function() {
	     var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		return [pageWidth,pageHeight];
	}
};


