/**
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 *
 * @author ?
 * @author Robert Wagler <rw@computer-leipzig.com>
 */

// Wird derzeit in search_query.js überprüft um festzustellen ob thickbox.js also diese Datei eingebunden wurde und die Funktionen verfügbar sind
var _existsTb = true;

var tb_pathToImage = "/images/loadingAnimation.gif";
var closeCaption = '<img src="/images/close.png" alt="SchlieÃŸen" width="16" height="16">';
var alternateCloseCaption = '';
var imageString = 'Ansicht ';
var ofString = ' von ';
var nextImg = '<img src="/images/right_dg.gif" alt="weiter" width="5" height="8">';
var prevImg = '<img src="/images/left_dg.gif" alt="zurÃ¼ck" width="5" height="8">';
var nextString = 'weiter';
var prevString = 'zurÃ¼ck';
//var defaultAjaxWidth = 550;
//var defaultAjaxHeight = 450;
var defaultAjaxWidth = $(window).width () - ($(window).width () / 5);
var defaultAjaxHeight = $(window).height () - ($(window).height () / 5);


/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call tb_initBox
$(document).ready (tb_initTb);

function tb_initTb ()
 {
  //pass where to apply thickbox
  tb_initBox ('a.thickbox, area.thickbox, input.thickbox');

  // preload image
  imgLoader = new Image ();
  imgLoader.src = tb_pathToImage;

  // store current settings for restoring for IE
  TB_PREVSETTINGS_CSS_HTML_WIDTH = $("html").css ('width');
  TB_PREVSETTINGS_CSS_HTML_HEIGHT = $("html").css ('height');

  TB_PREVSETTINGS_CSS_BODY_WIDTH = $("body").css ('width');
  TB_PREVSETTINGS_CSS_BODY_HEIGHT = $("body").css ('height');

  TB_PREVSETTINGS_CSS_HTML_OVERFLOW = $("html").css ('overflow');
  // IE fehalteure (fehler/feature?) korrigieren, liefert 'scroll' wenn nur eine Scrollleiste existiert, setzt aber zwei Scrollleisten wenn man 'scroll' setzt
  if (TB_PREVSETTINGS_CSS_HTML_OVERFLOW == 'scroll')
   {
    TB_PREVSETTINGS_CSS_HTML_OVERFLOW = '';
   }
 }

function fixIEbugUrlencoding (b)
 {
  fn = b.split ('/');
  a = '';
  for (i = 0; i < fn.length; ++i)
   {
    if (i < fn.length - 1)
     {
      a += fn[i] + '/';
     }
    else
     {
      a += escape (unescape (fn[i]));
     }
   }

  if (a == '')
   {
    return b;
   }

  return a;
 }

//add thickbox to href & area elements that have a class of .thickbox
function tb_initBox (domChunk)
 {
  $(domChunk).click
   (
    function ()
     {
      var t = this.title || this.name || null;
      var a = this.href || this.alt;
      var g = this.rel || false;
      var gn = $("img", this).attr ('alt');

      tb_showBox (t, a, g, gn);
      this.blur ();
      return false;
     }
   );
 }



/**
 * Hier werden die angzeigten Seitenzahlen des Pagers generiert.
 * Bsp.: < 1 2 3 ... 9 10 >
 *
 * Wichtig ist, dass der Pager beim Blättern immer die gleiche Breite aufweist um zu vermeiden, dass der rechte Pfeil "wandert".
 * D.h. also die Anzahl der Elemente "Zahl" und "..." (als Auslassungszeichen) müssen gleich sein solange first_page und last_page gleich sind.
 * Auch sollte sich die Anzahl der ein und zwei-stelligen Zahlen nicht verändern.
 * Angezeigt werden sollten immer die aktuelle Seite, mindenst eine Seite davor und eine Seite danach. Ebenso die erste und die letzte Zeile.
 * Ist die Reihe nicht fortlaufenend sollte das Auslassungszeichen verwendet werden.
 * Ist die Auslassung im Verhältnis sehr groß sollten Zwischenschritte angeboten werden.
 *
 * @author Robert Wagler <rw@computer-leipzig.com>
 */
function tb_pagerGetLinks (page, first_page, last_page, max_slots)
 {
  function countPages ()
   {
    var count = 0;
    for (var i in pages)
     {
      if (pages[i] > 0)
       {
        ++count;
       }
     }
    return count;
   }

  function countFrees ()
   {
    var count = 0;
    var last = -1;
    for (var i in pages)
     {
      if (pages[i] == 0)
       {
        if (last != 0)
         {
          ++count;
         }
       }
      last = pages[i];
     }
    return count;
   }

  function countFreeSlots ()
   {
    var free = max_slots - countPages () - countFrees ();
    if (free < 0) free = 0;
    return free;
   }

  function getFreesArea (start, end)
   {
    if (typeof start == 'undefined') start = first_page;
    if (typeof end == 'undefined') end = last_page;

    var free_start = 0;
    var free_end = 0;
    var last = 0;
    for (var i in pages)
     {
      if (i < start) continue;
      if (i > end) break;

      last = Number (i);

      if (free_start == 0)
       {
        if (pages[i] == 0)
         {
          free_start = Number (i);
         }
       }
      else if (free_end == 0)
       {
        if (pages[i] != 0)
         {
          free_end = Number (i) - 1;
         }
       }
      else
       {
        break;
       }
     }
    if (free_end == 0)
     {
      free_end = last;
     }

    var free_count = free_end - free_start + 1;

    var free_slot = new Object ();
    if (free_start == 0)
     {
      free_end = 0;
      free_count = 0;
     }
    free_slot['start'] = free_start;
    free_slot['end'] = free_end;
    free_slot['count'] = free_count;

    return free_slot;
   }

  function getMaxFrees ()
   {
    var free_slots = new Array ();
    var offset = first_page;
    while (true)
     {
      var free_slot = getFreesArea (offset);
      offset = free_slot['end'] + 1;

      if (free_slot['count'] > 0)
       {
        free_slots.push (free_slot);
        continue;
       }
      break;
     }

    return free_slots;
   }

  function fillFrees ()
   {
    if (countFreeSlots () >= 2)
     {
      var frees = getMaxFrees ();

      var count_pages = countPages ();

      for (var i in frees)
       {
        frees[i]['count_parts'] = Math.round ((countFreeSlots () - 2) / frees.length);
        frees[i]['used_slots'] = frees[i]['count_parts'] * 2 - 1;
       }

      var is_init = true;
      while (count_pages + count_used_slots < max_slots - 1)
       {
        if (! is_init)
         {
          var max = 0;
          var index_of_max = -1;
          for (var i in frees)
           {
            if (frees[i]['count'] / frees[i]['count_parts'] > max)
             {
              max = frees[i]['count'] / frees[i]['count_parts'];
              index_of_max = i;
             }
           }
          if (index_of_max == -1) break;

          frees[index_of_max]['count_parts'] += 1;
          if (frees[index_of_max]['count_parts'] == 1)
           {
            frees[index_of_max]['used_slots'] = 1;
           }
          else
           {
            frees[index_of_max]['used_slots'] = frees[index_of_max]['count_parts'] * 2 - 1;
           }
         }
        is_init = false;

        var count_used_slots = 0;
        for (var i in frees)
         {
          count_used_slots += frees[i]['used_slots'];
         }
       }

      for (var i in frees)
       {
        frees[i]['add'] = frees[i]['count'] / frees[i]['count_parts'];

        var m = 1;

        while (countFreeSlots () > 1)
         {
          p = (frees[i]['start'] - 1) + Math.round (frees[i]['add'] * m + 0.5);
          if (p > frees[i]['end']) break;

          if (typeof pages[p] == 'undefined')
           {
            pages[p] = -1;
           }
          else
           {
            pages[p] = 1;
           }

          ++m;
         }
       }
     }

    while (countFreeSlots () > 0)
     {
      var frees = getMaxFrees ();

      var max = 0;
      var index_of_max = -1;
      for (var i in frees)
       {
        if (frees[i]['count'] > max)
         {
          max = frees[i]['count'];
          index_of_max = i;
         }
       }
      if (index_of_max == -1) break;

      if (Math.abs (page - frees[index_of_max]['start']) < Math.abs (page - frees[index_of_max]['end']))
       {
        var p = frees[index_of_max]['start'];
       }
      else
       {
        var p = frees[index_of_max]['end'];
       }
      pages[p] = 1;
     }
   }

  var result = new Array ();

  if (max_slots >= 1)
   {
    var pages = new Object ();
    for (var i = first_page; i <= last_page; ++i)
     {
      pages[Number (i)] = 0;
     }

    pages[page] = 1;

    if (max_slots >= 3)
     {
      if (countFreeSlots ())
       {
        pages[first_page] = 1;
       }
      if (countFreeSlots ())
       {
        pages[last_page] = 1;
       }

      for (var i = 1; i <= 2; ++i)
       {
        if (countFreeSlots () && (page + i <= last_page))
         {
          pages[page + i] = 1;
         }
        if (countFreeSlots () && (page - i >= first_page))
         {
          pages[page - i] = 1;
         }
       }

      fillFrees ();

      var last_frees = new Array ();
      for (var i in pages)
       {
        if (pages[i] > 0)
         {
          if (last_frees.length > 0)
           {
            if (last_frees.length == 1)
             {
              result.push (last_frees[0]);
             }
            last_frees = new Array ();
           }
          result.push (i);
         }
        else
         {
          if (last_frees.length == 1)
           {
            result.push ('...');
           }
          last_frees.push (i);
         }
       }
     }
    else
     {
      result.push (page);
     }
   }

  return result;
 }



//function called when the user clicks on a thickbox link
function tb_showBox (caption, url, imageGroup, groupName)
 {
  try
   {
    if (tb_detectIE6 ())
     {
      // set height/width to a value else Microsoft Filters will not work
      $("html").css ('width', '100%');
      $("html").css ('height', '100%');
      $("body").css ('width', '100%');
      $("body").css ('height', '100%');

      // remove vertical and horizontal scrollbars
      $("html").css ("overflow", "hidden");

      //iframe to hide select elements in ie6
      if (document.getElementById ("TB_HideSelect") === null)
       {
        $("body").append ("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
        $("#TB_overlay").click (tb_removeBox);
       }
     }
    //all others
    else
     {
      if (document.getElementById ("TB_overlay") === null)
       {
        $("body").append ("<div id='TB_overlay'></div><div id='TB_window'></div>");
        $("#TB_overlay").click (tb_removeBox);
       }
     }

    if (tb_detectMacXFF ())
     {
      //use png overlay so hide flash
      $("#TB_overlay").addClass ("TB_overlayMacFFBGHack");
     }
    else
     {
      //use background and opacity, Microsoft Filters
      $("#TB_overlay").addClass ("TB_overlayBG");
     }

    if (caption === null)
     {
      caption = "";
     }

    //add loader to the page
    $("body").append ("<div id='TB_load'><img src='" + imgLoader.src + "' /></div>");

    //show loader
    $('#TB_load').show ();

    var baseURL;
    //ff there is a query string involved
    if (url.indexOf ("?") !== -1)
     {
      baseURL = url.substr (0, url.indexOf ("?"));
     }
    else
     {
      baseURL = url;
     }

    var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
    var urlType = baseURL.toLowerCase ().match (urlString);

    //code to show images
    if (urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp')
     {
      TB_PrevCaption = "";
      TB_PrevURL = "";
      TB_PrevHTML = "";
      TB_NextCaption = "";
      TB_NextURL = "";
      TB_NextHTML = "";
      var TB_PagesHTML = new Array ();
      var TB_PagesURL = new Array ();
      var TB_PagesCaption = new Array ();
      var TB_PageCurrent = 0;
      var TB_PageFirst = 0;
      var TB_PageLast = 0;
      TB_imageCount = "";
      TB_FoundURL = false;

      if (imageGroup)
       {
        TB_TempArray = $('a[rel="' + imageGroup + '"]').get ();

        TB_PageFirst = (TB_TempArray.length >= 1 ? 1 : 0);
        TB_PageLast = TB_TempArray.length;

        for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++)
         {
          var urlTypeTemp = TB_TempArray[TB_Counter].href.toLowerCase ().match (urlString);

          if (! (TB_TempArray[TB_Counter].href == url))
           {
            if (TB_FoundURL)
             {
              TB_NextCaption = TB_TempArray[TB_Counter].title;
              TB_NextURL = TB_TempArray[TB_Counter].href;
              TB_NextHTML = "<span><a href='#' id='TB_next'>" + nextImg + "</a></span>";
             }
            else
             {
              TB_PrevCaption = TB_TempArray[TB_Counter].title;
              TB_PrevURL = TB_TempArray[TB_Counter].href;
              TB_PrevHTML = "<span><a href='#' id='TB_prev'>" + prevImg + "</a></span>";
             }
           }
          else
           {
            TB_FoundURL = true;
            TB_imageCount = imageString + (TB_Counter + 1) + ofString + (TB_TempArray.length);

            TB_PageCurrent = TB_Counter + 1;
           }
         }

        for (TB_Counter = 0; ((TB_Counter < TB_TempArray.length)); TB_Counter++)
         {
          TB_PagesHTML[TB_Counter] = "<span><a href='#' id='TB_pager_" + (TB_Counter) + "' class='TB_pager" + (TB_TempArray[TB_Counter].href == url ? " current" : "") + "'>" + (TB_Counter + 1) + "</a></span>";
          TB_PagesURL[TB_Counter] = TB_TempArray[TB_Counter].href;
          TB_PagesCaption[TB_Counter] = TB_TempArray[TB_Counter].title;
         }
       }

      imgPreloader = new Image ();
      imgPreloader.onload = function ()
       {
        imgPreloader.onload = null;

        // Resizing large images - orginal by Christian Montoya edited by me.
        var pagesize = tb_getPageSize ();
        var x = pagesize[0] - 150;
        var y = pagesize[1] - 150;
        var imageWidth = imgPreloader.width;
        var imageHeight = imgPreloader.height;

        if (imageWidth > x)
         {
          imageHeight = imageHeight * (x / imageWidth);
          imageWidth = x;
          if (imageHeight > y)
           {
            imageWidth = imageWidth * (y / imageHeight);
            imageHeight = y;
           }
         }
        else if (imageHeight > y)
         {
          imageWidth = imageWidth * (y / imageHeight);
          imageHeight = y;
          if (imageWidth > x)
           {
            imageHeight = imageHeight * (x / imageWidth);
            imageWidth = x;
           }
         }
        // End Resizing

//        TB_WIDTH = imageWidth + 30;
//        TB_HEIGHT = imageHeight + 60;
        TB_WIDTH = imageWidth + 24;
        TB_HEIGHT = imageHeight + 60;

        TB__HTML = "<div class='margin'><div id='main-nav'><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='SchlieÃŸen'>" + closeCaption + "</a>" + alternateCloseCaption + "</div></div>";

//        TB__HTML += "<div id='TB_caption'>" + caption + "<div id='TB_secondLine'>" + TB_imageCount + TB_PrevHTML + TB_NextHTML + "</div></div>";

        TB__HTML += '<div class="pager"><p><span class="pager-summary">' + groupName + '</span><span class="pager-pages">';
        if (TB_PageLast > TB_PageFirst)
         {
          if (TB_PrevHTML != '')
           {
            TB__HTML += TB_PrevHTML;
           }
          else
           {
            TB__HTML += '<b>&nbsp;&nbsp;&nbsp;</b>';
           }

          var _TB_PageMax = Math.round (((imageWidth - groupName.length * 5 - 2 * 16 - 16 - 7) / 16) - 0.5); // 11 bei Standardbreite
          var _TB_Pages = tb_pagerGetLinks (TB_PageCurrent, TB_PageFirst, TB_PageLast, _TB_PageMax);
          var _TB_Page;
          for (_TB_Page in _TB_Pages)
           {
            if (isNaN (Number (_TB_Pages[_TB_Page])))
             {
              TB__HTML += '<b style="font-weight:normal">...</b>';
             }
            else
             {
              TB__HTML += TB_PagesHTML[_TB_Pages[_TB_Page] - 1];
             }
           }
          if (TB_NextHTML != '')
           {
            TB__HTML += TB_NextHTML;
           }
         }
        TB__HTML += '</span></p></div>';

        var tb_fix_gcmapnameproblem_t1070 = new Date ().getTime ();

        if (tb_detectIE6 ())
         {
          TB__HTML += "<img id='TB_Image' src='" + fixIEbugUrlencoding (url) + "' width='" + imageWidth + "' height='" + imageHeight + "' alt='" + caption + "' usemap='#thickbox" + tb_fix_gcmapnameproblem_t1070 + "' />";
         }
        else
         {
          TB__HTML += "<img id='TB_Image' src='" + url + "' width='" + imageWidth + "' height='" + imageHeight + "' alt='" + caption + "' usemap='#thickbox" + tb_fix_gcmapnameproblem_t1070 + "' />";
         }

        TB__HTML += "<map name='thickbox" + tb_fix_gcmapnameproblem_t1070 + "'>";

        if (TB_PrevURL + TB_NextURL == '')
         {
          TB__HTML += "<area shape='rect' coords='0,0," + imageWidth + "," + imageHeight + "' href='#' id='TB_closeWindowImageBacklink' alt='" + closeCaption + "'>";
         }
        else
         {
          if (TB_PrevURL == '')
           {
            TB__HTML += "<area shape='rect' coords='0,0," + imageWidth / 2 + "," + imageHeight + "' href='#' id='TB_closeWindowImageBacklink' alt='" + closeCaption + "'>";
           }
          else
           {
            TB__HTML += "<area shape='rect' coords='0,0," + imageWidth / 2 + "," + imageHeight + "' href='#' id='TB_prevImageButton' alt='" + prevString + "'>";
           }
          if (TB_NextURL == '')
           {
            TB__HTML += "<area shape='rect' coords='" + imageWidth / 2 + ",0," + imageWidth + "," + imageHeight + "' href='#' id='TB_closeWindowImageForlink' alt='" + closeCaption + "'>";
           }
          else
           {
            TB__HTML += "<area shape='rect' coords='" + imageWidth / 2 + ",0," + imageWidth + "," + imageHeight + "' href='#' id='TB_nextImageButton' alt='" + nextString + "'>";
           }
         }
        TB__HTML += "</map>";
        TB__HTML += '<div><p>' + caption + '</p></div>';
        TB__HTML += "</div>";

        $("#TB_window").append (TB__HTML);

        $("#TB_closeWindowButton").click (tb_removeBox);
        $("#TB_closeWindowImageBacklink").click (tb_removeBox);
        $("#TB_closeWindowImageForlink").click (tb_removeBox);

        if (! (TB_PrevHTML === ""))
         {
          function goPrev ()
           {
            if ($(document).unbind ("click", goPrev))
             {
              $(document).unbind ("click", goPrev);
             }
            $("#TB_window").remove ();
            $("body").append ("<div id='TB_window'></div>");
            tb_showBox (TB_PrevCaption, TB_PrevURL, imageGroup, groupName);
            return false;
           }
          $("#TB_prev").click (goPrev);
          $("#TB_prevImageButton").click (goPrev);
         }

        if (! (TB_NextHTML === ""))
         {
          function goNext ()
           {
            $("#TB_window").remove ();
            $("body").append ("<div id='TB_window'></div>");
            tb_showBox (TB_NextCaption, TB_NextURL, imageGroup, groupName);
            return false;
           }
          $("#TB_next").click (goNext);
          $("#TB_nextImageButton").click (goNext);
         }

        function goPage ()
         {
          var page = Number ($(this).attr ('id').substr (9));
          if (isNaN (page))
           {
            return false;
           }

          if ($(document).unbind ("click", goPage))
           {
            $(document).unbind ("click", goPage);
           }
          $("#TB_window").remove ();
          $("body").append ("<div id='TB_window'></div>");
          tb_showBox (TB_PagesCaption[page], TB_PagesURL[page], imageGroup, groupName);
          return false;
         }
        $(".TB_pager").click (goPage);

        document.onkeydown = function (e)
         {
          var keyCode = tb_getKeyCode (e);

          // close
          if (keyCode == 27)
           {
            tb_removeBox ();
           }
          // display previous image ?? 190?
          else if ((keyCode == 190) || (keyCode == 39))
           {
            if (! (TB_NextHTML == ""))
             {
              document.onkeydown = "";
              goNext ();
             }
           }
          // display next image ?? 188?
          else if ((keyCode == 188) || (keyCode == 37))
           {
            if (! (TB_PrevHTML == ""))
             {
              document.onkeydown = "";
              goPrev ();
             }
           }
         };

        tb_positionBox ();
        $("#TB_load").remove ();
        $("#TB_ImageOff").click (tb_removeBox);

        //for safari using css instead of show
        $("#TB_window").css ({display:"block"});
       };

      if (tb_detectIE6 ())
       {
        imgPreloader.src = fixIEbugUrlencoding (url);
       }
      else
       {
        imgPreloader.src = url;
       }
     }
    //code to show html
    else
     {
      var queryString = url.replace (/^[^\?]+\??/, '');
      var params = tb_parseQuery (queryString);

      //defaults to 630 if no paramaters were added to URL
      TB_WIDTH = (params['width'] * 1) + 30 || defaultAjaxWidth;
      ajaxContentW = TB_WIDTH - 30;

      //defaults to 440 if no paramaters were added to URL
      TB_HEIGHT = (params['height'] * 1) + 40 || defaultAjaxHeight;
      ajaxContentH = TB_HEIGHT - 45;

      // either iframe or ajax window
      if (url.indexOf ('TB_iframe') != -1)
       {
        urlNoQuery = url.split ('TB_');
        $("#TB_iframeContent").remove ();

        //iframe no modal
        if (params['modal'] != "true")
         {
          $("#TB_window").append ("<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + caption + "</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='SchlieÃŸen'>" + closeCaption + "</a>" + alternateCloseCaption + "</div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent" + Math.round (Math.random ()*1000) + "' onload='tb_showIframe ()' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' > </iframe>");
         }
        //iframe modal
        else
         {
          $("#TB_overlay").unbind ();
          $("#TB_window").append ("<iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent" + Math.round (Math.random ()*1000) + "' onload='tb_showIframe ()' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;'> </iframe>");
         }
       }
      // not an iframe, ajax
      else
       {
        if ($("#TB_window").css ("display") != "block")
         {
          //ajax no modal
          if (params['modal'] != "true")
           {
            $("#TB_window").append ("<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + caption + "</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'>" + closeCaption + "</a>" + alternateCloseCaption + "</div></div><div id='TB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px'></div>");
           }
          //ajax modal
          else
           {
            $("#TB_overlay").unbind ();
            $("#TB_window").append ("<div id='TB_ajaxContent' class='TB_modal' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>");
           }
         }
        //this means the window is already up, we are just loading new content via ajax
        else
         {
          $("#TB_ajaxContent")[0].style.width = ajaxContentW + "px";
          $("#TB_ajaxContent")[0].style.height = ajaxContentH + "px";
          $("#TB_ajaxContent")[0].scrollTop = 0;
          $("#TB_ajaxWindowTitle").html (caption);
         }
       }

      $("#TB_closeWindowButton").click (tb_removeBox);

      if (url.indexOf ('TB_inline') != -1)
       {
        $("#TB_ajaxContent").append ($('#' + params['inlineId']).children ());
        $("#TB_window").unload
         (
          function ()
           {
            // move elements back when you're finished
            $('#' + params['inlineId']).append ($("#TB_ajaxContent").children ());
           }
         );
        tb_positionBox ();
        $("#TB_load").remove ();
        $("#TB_window").css ({display:"block"});
       }
      else if (url.indexOf ('TB_iframe') != -1)
       {
        tb_positionBox ();
        //safari needs help because it will not fire iframe onload
        if ($.browser.safari)
         {
          $("#TB_load").remove ();
          $("#TB_window").css ({display:"block"});
         }

        /** 
         * display:none bewirkt im IE und FF bei Google-Maps ein Abschneiden des Kartenmaterials. 
         * Deswegen muss hier diese Eigenschaft von ursprünglich none auf block gesetzt werden. 
         */
        $("#TB_window").css ({display:"block"});
       }
      else
       {
        $("#TB_ajaxContent").load
         (
          url,
          function ()
           {// += "&random=" + (new Date ().getTime ()), function (){//to do a post change this load method
            tb_positionBox ();
            $("#TB_load").remove ();
            //tb_initBox ("#TB_ajaxContent a.thickbox");
            $("#TB_window").css ({display:"block"});
           }
         );
       }
     }

    if (! params['modal'])
     {
      document.onkeyup = function (e)
       {
        // close
        if (tb_isKeyCode (e, 27))
         {
          tb_removeBox ();
         }
       };
     }
   }
  catch (e)
   {
    //nothing here
   }
 }

//helper functions below
function tb_showIframe ()
 {
  $("#TB_load").remove ();
  $("#TB_window").css ({display:"block"});
 }

function tb_removeBox ()
 {
  object_id = tb_getObjectId ();

  $("#TB_imageOff").unbind ("click");
  $("#TB_closeWindowButton").unbind ("click");
  $("#TB_closeWindowImageBacklink").unbind ("click");
  $("#TB_closeWindowImageForlink").unbind ("click");

  $("#TB_window").fadeOut
   (
    "fast",
    function ()
     {
      $('#TB_window, #TB_overlay, #TB_HideSelect').trigger ("unload").unbind ().remove ();

      if (tb_detectIE6 ())
       {
        $("html").css ('width', TB_PREVSETTINGS_CSS_HTML_WIDTH);
        $("html").css ('height', TB_PREVSETTINGS_CSS_HTML_HEIGHT);
        $("body").css ('width', TB_PREVSETTINGS_CSS_BODY_WIDTH);
        $("body").css ('height', TB_PREVSETTINGS_CSS_BODY_HEIGHT);
  
        $("html").css ('overflow', TB_PREVSETTINGS_CSS_HTML_OVERFLOW);
       }
     }
   );
  $("#TB_load").remove ();

  document.onkeydown = "";
  document.onkeyup = "";

  /* Hack: zuletzt hervorgehobene Zeile wieder hervorheben, leider nur anhand der Objekt-Id
   */
  if (object_id && $('#offer-row-' + object_id + '_1').size () && $('#offer-row-' + object_id + '_2').size () && $('#offer-row-' + object_id + '_3').size ())
   {
    // Zeile hervorheben
    if ($('#offer-row-' + object_id + '_1').attr ('class').indexOf ('offer-row-hover') == -1) 
     {
      $('#offer-row-' + object_id + '_1').attr ('class', $('#offer-row-' + object_id + '_1').attr ('class').replace ('offer-row', 'offer-row-hover')); 
     }
    if ($('#offer-row-' + object_id + '_2').attr ('class').indexOf ('offer-row-hover') == -1) 
     {
      $('#offer-row-' + object_id + '_2').attr ('class', $('#offer-row-' + object_id + '_2').attr ('class').replace ('offer-row', 'offer-row-hover')); 
     }
    if ($('#offer-row-' + object_id + '_3').attr ('class').indexOf ('offer-row-hover') == -1) 
     {
      $('#offer-row-' + object_id + '_3').attr ('class', $('#offer-row-' + object_id + '_3').attr ('class').replace ('offer-row', 'offer-row-hover')); 
     }
   }

  return false;
 }

function tb_getObjectId ()
 {
  try
   {
    if (scr = $('#TB_Image').attr ('src'))
     {
      if ($('a[href=' + scr + ']').size ())
       {
        id = ($('a[href=' + scr + ']').parent ().parent ().parent ().attr ('id'));

        id = id.split ('_');
        id = id.shift ();

        id = id.split ('-');
        id = id.pop ();

        return id;
       }
     }
   }
  catch (exc)
   {
    return false;
   }
 }

function tb_positionBox ()
 {
  if (! tb_detectIE6 ())
   {
    $("#TB_window").css ({marginTop: '-' + parseInt (TB_HEIGHT / 2, 10) + 'px'});
   }

  $("#TB_window").css ({marginLeft: '-' + parseInt (TB_WIDTH / 2, 10) + 'px', width: TB_WIDTH + 'px'});
 }

function tb_parseQuery (query)
 {
  var Params = {};

  if (! query)
   {
    // return empty object
    return Params;
   }

  var Pairs = query.split (/[;&]/);

  for (var i = 0; i < Pairs.length; i++)
   {
    var KeyVal = Pairs[i].split ('=');

    if (! KeyVal || KeyVal.length != 2)
     {
      continue;
     }

    var key = unescape (KeyVal[0]);
    var val = unescape (KeyVal[1]);
    val = val.replace (/\+/g, ' ');
    Params[key] = val;
   }

  return Params;
 }

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 = [w, h];
  return arrayPageSize;
 }

function tb_getKeyCode (e)
 {
  // ie
  if (e == null)
   {
    return event.keyCode;
   }
  // mozilla
  else
   {
    return e.which;
   }
 }

function tb_isKeyCode (e, c)
 {
  return c == tb_getKeyCode (e);
 }

function tb_detectMacXFF ()
 {
  var userAgent = navigator.userAgent.toLowerCase ();

  return (userAgent.indexOf ('mac') != -1) && (userAgent.indexOf ('firefox') != -1);
 }

function tb_detectIE6 ()
 {
  // //if (! (jQuery.browser.msie && jQuery.browser.version < 7))

  return typeof document.body.style.maxHeight === "undefined";
 }


