/*
 * Thickbox AJAX-Power 
 * von Olaf Bosch http://olaf-bosch.de
 * Demo: http://olaf-bosch.de/wp-content/obosch/templates/power/ajax-power/
 * Copyright (c) 2006 Olaf Bosch
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * $Date: 2006-12-30
 * $Rev: 1.0.1
 ***** beruht auf Original:
 * Thickbox 2.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2006 cody lindley
 */
//on page load call TB_init
$(document).ready(TB_init);

function TB_init(){
  $("a.thickbox").click(function(){
  var url = this.href;
  var checkURL = url.toLowerCase();
  if( /\.(xhtml|php|html)$/.test(checkURL) ) {
  var caption = this.title || null;
  TB_show(caption, checkURL, url);
  return false;
  }
  });
}
// PopUp - definieren
var DivH = 450;  // max. Hoehe
var DivW = 820;  // max. Breite

function TB_show(caption, checkURL, url) {//function called when the user clicks on a thickbox link
  try {
// Ajax im DIV
    if( /\.(xhtml|html|php)$/.test(checkURL) ) {

    if (document.getElementById("TBhideSelect") == null) {
    $("body").append("<iframe id='TBhideSelect'></iframe>\n<div id='TBoverlay'>\n</div>\n<div id='TBwindow'>\n</div>");
    $("#TBoverlay").click(TB_remove);
    }

    if(caption==null){caption=""};
    $(window).scroll(TB_position);
    TBoverlaySize();

    $("body").append("\n<div id='TBload'></div>");
    TBload_position();
    TB_resize();

      $("#TBwindow").fadeIn("fast").append("\n<div id='TBtitle'>\n<div id='TBcloseAjaxWindow'><a href='#' id='TBcloseWindowButton'>close <small>(ESC)</small></a>\n</div>\n<div id='TBajaxWindowTitle'>"+caption+"\n</div>\n</div>\n<div id='TBajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'>\n</div>");
       TB_position();

      $("#TBcloseWindowButton").click(TB_remove);

$.ajax({
   type: "GET",
   url: url,
   dataType: "html",
   success: function(html){
       $("#TBajaxContent").html(html);
       $("#TBload,legend").remove();
       $("#TBajaxContent").append("\n<div id='TBerr'></div>");

       $('#power').ajaxForm({
           target: '#TBerr',
           success: function(data) {
               $("#TBerr .feedback a#back").remove();
               $("#TBerr .feedback").addClass("jsison");
               $("#TBerr .jsison").animateClass('feed', 700);
           }
       });
   }
}); 
}
  } catch(e) {
    alert( e );
  }
}

//helper functions
function TB_resize(){
      TB_WIDTH = (DivW*1) + 30;
      TB_HEIGHT = (DivH*1) + 40;
      ajaxContentW = TB_WIDTH - 30;
      ajaxContentH = TB_HEIGHT - 45;

      var pagesize = TB_getPageSize();
      var x = pagesize[0] - 100;
      var y = pagesize[1] - 100;
      if (ajaxContentW > x) {
        ajaxContentH = ajaxContentH * (x / ajaxContentW);
        ajaxContentW = x; 
        if (ajaxContentH > y) { 
          ajaxContentW = ajaxContentW * (y / ajaxContentH);
          ajaxContentH = y; 
        }
      } else if (ajaxContentH > y) { 
        ajaxContentW = ajaxContentW * (y / ajaxContentH);
        ajaxContentH = y; 
        if (ajaxContentW > x) { 
          ajaxContentH = ajaxContentH * (x / ajaxContentW);
          ajaxContentW = x;
        }
      }
      TB_WIDTH = (ajaxContentW*1) + 30;
      TB_HEIGHT = (ajaxContentH*1) + 40;
}
function TB_remove() {
  $("#TBwindow").fadeOut("slow");
  $("#TBoverlay").fadeOut("fast",function(){
  $("#TBload,#TBhideSelect,#TBoverlay,#TBwindow,#TBajaxContent").remove();
});
  return false;
}
function TB_position() {
  var pagesize = TB_getPageSize();
  var arrayPageScroll = TB_getPageScrollTop();
  $("#TBwindow").css({width:TB_WIDTH+"px",left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)+"px" });
}
function TBoverlaySize(){
  if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {  
    yScroll = window.innerHeight + window.scrollMaxY;
    xScroll = window.innerWidth + window.scrollMaxX;
    var deff = document.documentElement;
    var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
    var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
    xScroll -= (window.innerWidth - wff);
    yScroll -= (window.innerHeight - hff);
  } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
    yScroll = document.body.scrollHeight;
    xScroll = document.body.scrollWidth;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    yScroll = document.body.offsetHeight;
    xScroll = document.body.offsetWidth;
    }
  $("#TBoverlay").css({"height":yScroll +"px"});
  $("#TBhideSelect").css({"height":yScroll +"px","width":xScroll +"px"});
}
function TBload_position() {
  var pagesize = TB_getPageSize();
  var arrayPageScroll = TB_getPageScrollTop();
  $("#TBload")
  .css({left: (arrayPageScroll[0] + (pagesize[0] - 100)/2)+"px", top: (arrayPageScroll[1] + ((pagesize[1]-100)/2))+"px" })
  .css({display:"block"});
}
function TB_getPageScrollTop(){
  var yScrolltop;
  var xScrollleft;
  if (self.pageYOffset || self.pageXOffset) {
    yScrolltop = self.pageYOffset;
    xScrollleft = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){   // Explorer 6 Strict
    yScrolltop = document.documentElement.scrollTop;
    xScrollleft = document.documentElement.scrollLeft;
  } else if (document.body) {
    yScrolltop = document.body.scrollTop;
    xScrollleft = document.body.scrollLeft;
  }
  arrayPageScroll = new Array(xScrollleft,yScrolltop) 
  return arrayPageScroll;
}
function TB_getPageSize(){
  var de = document.documentElement;
  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
  arrayPageSize = new Array(w,h) 
  return arrayPageSize;
}
document.onkeydown = function(e){
  if (e == null) {
    keycode = event.keyCode;
  } else {
    keycode = e.which;
  }
  if(keycode == 27){
    TB_remove();
  }
}