// JavaScript Document

var IE = false;
var IE6 = false;
var NS = false;
var browser_version = parseInt(navigator.appVersion);
var browser_type = navigator.appName;
if (browser_type == "Microsoft Internet Explorer" && (browser_version >= 4)) { 
  IE = true;
} 
if (browser_type == "Microsoft Internet Explorer" && (browser_version <= 6)) { 
  IE6 = true;
} 
else if (browser_type == "Netscape" && (browser_version >= 4) && (browser_version < 5)) { 
  NS = true;
}
else if (browser_type == "Netscape" && (browser_version >= 5)) { 
  NS = true;
}
// -----------------------------------------------------------------------------

/*
 * Erstellt eine statische Sprechblase, Elemente müssen in HTML erstellt werden (->SEO)
 * @author: DKL
 */
function createBigStaticBubble(el, id, bgid, cX, cY)
{
  var bsb = document.getElementById(id);

  bsb.style.position = 'absolute';
  bsb.style.zIndex = '9999';

  x = getX(el) + (el.offsetWidth/2) - 214 + cX;
  y = getY(el) + cY;

  bsb.style.left = x + 'px';
  bsb.style.top = y + 'px';

  makeBGLayer(bgid, id, 6, 7777, true, false);

  bsb.style.display = 'none';

  $(bsb).fadeIn('slow');
}
// -----------------------------------------------------------------------------

/*
 * Blendet die Photos einer Gallery ein (als Stack)
 * @author: DKL
 */
function viewStackGallery(id, folder)
{
  var el = document.getElementById(id);
  var H = getHeightWithoutPadding(el);
  var W = getWidthWithoutPadding(el);

  makeBGLayer('stackbg', id, 7.5, 8888, true, false);

  var top = getElementTOP(H);
  var left = getElementLEFT(W);

  left = Math.floor(left)+'px';
  top = Math.floor(top)+'px';

  container = el.getElementsByClassName('photo');

  if(!container[0])
  {
    imgsstring = loadImagesOfFolder(folder);
    imgs = imgsstring.split(';');
    zi = 9000 + imgs.length;
    for(j = 0; j < (imgs.length - 1); j++)
    {
      img = new Image();
      img.src = imgs[j];
      img.className = 'photo';
      img.style.position = 'absolute';
      img.style.zIndex = zi;
      img.style.filter = "AlphaImageLoader(src='"+imgs[j].src+"', sizingMethod='crop')";
      stack = el.getElementsByClassName('stackpictures');
      stack[0].appendChild(img);
      zi--;
    }
  }

  el.style.position = 'absolute';
  el.style.zIndex = '9000';
  el.style.left = left;
  el.style.top = top;

  el.style.display = 'none';

  $(el).fadeIn('slow');
}
// --------------------------------------------------------------------------------

function switchPhoto(id, dir)
{
  var el = document.getElementById(id);
  var imgs = el.getElementsByClassName('photo');

  for(i = 0; i < imgs.length; i++)
  {
    if(!dir)
    {
      imgs[i].style.zIndex = parseInt(imgs[i].style.zIndex) - 1;
    }
    else
    {
      imgs[i].style.zIndex = parseInt(imgs[i].style.zIndex) + 1;
    }
  }

  if(dir)
  {
    biggest = 0;
    check = 0;
    for(i = 0; i < imgs.length; i++)
    {
      if(imgs[i].style.zIndex >= check)
      {
        check = imgs[i].style.zIndex;
        biggest = i;
      }
    }
    
    imgs[biggest].style.zIndex = parseInt(imgs[biggest].style.zIndex) - (imgs.length);
  }
  else
  {
    smallest = 0;
    check = 99999;
    for(i = 0; i < imgs.length; i++)
    {
      if(imgs[i].style.zIndex <= check)
      {
        check = imgs[i].style.zIndex;
        smallest = i;
      }
    }
    
    imgs[smallest].style.zIndex = parseInt(imgs[smallest].style.zIndex) + (imgs.length);
  }

}
// -----------------------------------------------------------------------------

/*
 * Blendet die Photos einer Gallery ein (als Popup mit Thumbs)
 * @author: DKL
 */
function viewGallery(W, H, id, header, autor, footer, tags)
{
  viewLoading();

  W += 320;

  var top = getElementTOP(calculatePopupHeight(H, true, header, footer, tags));
  var left = getElementLEFT(W);

  left = Math.floor(left)+'px';
  top = Math.floor(top)+'px';

  makeBGLayer('containerbg', 'roundedcontainer_container', 6.5, 8888, true, true);

  var copy = $('<div></div>').html( $('#'+id).html() );

  var container = createRoundedCornerContainer(W, H, copy.get(0), true, header, footer, tags, autor);

  $(container).css('display', 'none');
  $(container).css('position', 'absolute');
  $(container).css('z-index', '9000');
  $(container).css('left', left);
  $(container).css('top', top);

  $('body').append(container);

  adjustThumbs();
  if($('.galleryleft').length) $('.galleryleft').css('display', 'none');

  $(container).fadeIn('slow');
  
  hideLoading();
}

function viewGalleryPic(url)
{
  $('#roundedcontainer_container .gallerypic:first').fadeOut('fast', function() {
    $('#roundedcontainer_container .gallerypic:first').attr('src', url);
  });
  $('#roundedcontainer_container .gallerypic:first').fadeIn('fast');
}

function moveThumbs(dir)
{
  var left = parseInt( $('#roundedcontainer_container .thumbs').css('left') );
  max = parseInt( $('#roundedcontainer_container .thumbs').css('width') ) - parseInt( $('#roundedcontainer_container .thumbcontainer').css('width') );

  rest = max + left;
  max *= -1;

  if(dir == 'left')
  {
    if(left > max && rest > 60)
    {
      $('.galleryleft').fadeIn(500);
      left = (left - 60) + 'px';
      $('#roundedcontainer_container .thumbs').animate({
        left: left
      }, 250, "swing");
    }
    else
    {
      $('.galleryright').fadeOut(500);
      left = (left - rest) + 'px';
      $('#roundedcontainer_container .thumbs').animate({
        left: left
      }, 250, "swing");
    }
  }

  if(dir == 'right')
  {
    if(left < 0 && left <= -60)
    {
      $('.galleryright').fadeIn(500);
      left = (left + 60) + 'px';
      $('#roundedcontainer_container .thumbs').animate({
        left: left
      }, 250, "swing");
    }
    else
    {
      $('.galleryleft').fadeOut(500);
      $('#roundedcontainer_container .thumbs').animate({
        left: '20px'
      }, 250, "swing");
    }
  }
}
// --------------------------------------------------------------------------------

/*
 * Blendet ein Photo ein!
 * @author: DKL
 */
function viewPicture(src, ext, close, header, footer, tags, autor)
{
  var loader;

  if(!$('#roundedcontainer_container').length && !$('#dkxloader').length)
  {
    if(!ext)
    {
      src = './images/' + src;
      
      if($(window).height() < 650)
      {
        newsrc = src.substring(0, (src.length - 4));
        newsrc += '_small' + src.substring((src.length - 4), src.length);

        src = newsrc;
      }
    }
  
    loader = new ImageLoader(src);
    viewLoading();
    loader.loadEvent = function(src, image)
    {
      makeBGLayer('containerbg', 'roundedcontainer_container', 6.5, 8888, true, true);

      var H = image.height;
      var W = image.width;

      if((image.height + 125) > ($(window).height() - 100))
      {
        var factor = (image.height + 125) / ($(window).height() - 100);
        H = H/factor;
        W = W/factor;
        image.height = H;
        image.width = W;
      }

      var top = getElementTOP(calculatePopupHeight(H, close, header, footer, tags));
      var left = getElementLEFT(W);

      left = Math.floor(left)+'px';
      top = (Math.floor(top)-15)+'px';

      var container = createRoundedCornerContainer(W, H, image, close, header, footer, tags, autor);

      container.style.display = 'none';
      container.style.position = 'absolute';
      container.style.zIndex = '9000';
      container.style.left = left;
      container.style.top = top;

      document.body.appendChild(container);

      $(container).fadeIn('slow');

      hideLoading();
    }
    loader.load();
  }
}
// --------------------------------------------------------------------------------

/*
 * Blendet HTML Content ein!
 * @author: DKL
 */
function viewContent(close, header, subheader, content, footer, tags)
{
  makeBGLayer('containerbg', 'roundedcontainer_container', 6.5, 8888, true, true);

  var H = parseInt($('#'+content).css('height'));
  var W = parseInt($('#'+content).css('width'));

  var top = getElementTOP(calculatePopupHeight(H, close, header, footer, tags));
  var left = getElementLEFT(W);

  left = Math.floor(left)+'px';
  top = (Math.floor(top)-15)+'px';

  var container = createRoundedCornerContainer(W, H, $('#'+content).get(0), close, header, footer, tags, subheader);

  container.style.display = 'none';
  container.style.position = 'absolute';
  container.style.zIndex = '9000';
  container.style.left = left;
  container.style.top = top;

  document.body.appendChild(container);

  $(container).fadeIn('slow');
}
// --------------------------------------------------------------------------------

function calculatePopupHeight(H, close, header, footer, tags)
{
  if(close || $('#'+header).html().length > 0) H += 40;
  if($('#'+footer).html().length > 0) H += 75;
  if($('#'+tags).html().length > 0) H += 20;

  return H;
}

function createRoundedCornerContainer(W, H, content, close, header, footer, tags, autor)
{ 
  var padding = 5;
  var borderLR = 15;
  var borderTB = 25;
  var RH = H;
  var RW = W;

  if(close || $('#'+header).html().length > 0) H += 40;
  if($('#'+footer).html().length > 0) H += 75;
  if($('#'+tags).html().length > 0) H += 20;

  var container = document.createElement('div');
  container.id = 'roundedcontainer_container';
  container.style.width = ( W + ( borderLR * 2 + padding * 2 ) )+'px';
  container.style.height = ( H + ( borderTB * 2 ) )+'px';

  var stage = document.createElement('div');
  stage.id = 'roundedcontainer_stage';
  stage.style.width = ( W + ( borderLR * 2 + padding * 2 ) )+'px';
  stage.style.height = ( H + ( borderTB * 2 ) )+'px';

  var tlc = document.createElement('div');
  tlc.id = 'roundedcontainer_tlc';
  stage.appendChild(tlc);

  var tb = document.createElement('div');
  tb.id = 'roundedcontainer_tb';
  tb.style.width = ( W + ( padding * 2 ) )+'px';
  stage.appendChild(tb);

  var trc = document.createElement('div');
  trc.id = 'roundedcontainer_trc';
  stage.appendChild(trc);

  var lb = document.createElement('div');
  lb.id = 'roundedcontainer_lb';
  lb.style.height = H+'px';
  stage.appendChild(lb);

  var center = document.createElement('div');
  center.id = 'roundedcontainer_center';
  center.style.width = W+'px';
  center.style.height = H+'px';

  if(close || $('#'+header).html().length > 0)
  {
    var headerbox = document.createElement('div');
    headerbox.className = 'header';
    if(header)
    {
      headerbox.innerHTML = '<div style="float:left; position:absolute; left:5px; top:0;">'+
      '<h1>'+document.getElementById(header).innerHTML+'</h1>';
      if(autor) {
        headerbox.innerHTML += '<br /><small>'+document.getElementById(autor).innerHTML+'</small>'
      }
      headerbox.innerHTML += '</div>';
    }
    headerbox.innerHTML += '<div style="float:right; position:absolute; top:0px; right:5px; background:#FFFFFF;">'+
    '<a class="closelink" href="javascript:closePopup(\'roundedcontainer_container\', \'containerbg\', true, true)" title="close this layer!">close <img src="./images/BTN_Bubble_Close.gif" align="top" alt="close this layer!" title="close this layer!" border="0" /></a>'+
    '</div>';
    center.appendChild(headerbox);
  }

  var contentbox = document.createElement('div');
  contentbox.className = 'content';
  if($(content).html())
  {
    contentbox.innerHTML = $(content).html();
    contentbox.style.width = RW + 'px';
    contentbox.style.height = RH + 'px';
    contentbox.style.overflow = 'auto';
  }
  else
  {
    contentbox.appendChild(content);
  }
  center.appendChild(contentbox);

  if($('#'+footer).html().length > 0)
  {
    var maxchars = calcuateMaxChars($('#'+footer).text().length, RW, 70);
    var footerhtml = truncateHTML($('#'+footer).html(), $('#'+footer).text(), maxchars, '...', true, true, false);

    var footerbox = document.createElement('div');
    footerbox.className = 'footer';
    footerbox.innerHTML = '<p>'+footerhtml+'</p>';
    footerbox.style.width = RW + 'px';
    center.appendChild(footerbox);
  }

  if($('#'+tags).text().length > 0)
  {
    var tagsbox = document.createElement('div');
    tagsbox.className = 'tagsbox';
    tagsbox.innerHTML = document.getElementById(tags).innerHTML;
    tagsbox.style.width = RW + 'px';
    center.appendChild(tagsbox);
  }

  stage.appendChild(center);

  var rb = document.createElement('div');
  rb.id = 'roundedcontainer_rb';
  rb.style.height = H+'px';
  stage.appendChild(rb);

  var blc = document.createElement('div');
  blc.id = 'roundedcontainer_blc';
  stage.appendChild(blc);

  var bb = document.createElement('div');
  bb.id = 'roundedcontainer_bb';
  bb.style.width = (W - 20 + ( padding * 2 ) )+'px';
  stage.appendChild(bb);

  var brc = document.createElement('div');
  brc.id = 'roundedcontainer_brc';
  stage.appendChild(brc);

  container.appendChild(stage);

  return container;
  
}
// -----------------------------------------------------------------------------

/*
 * Fadet das Popup & den BG aus
 * @author: DKL
 */
function closePopup(id, bgid, effect, del)
{
  hideLoading();
  if(effect)
  {
    $('#'+id).fadeOut('slow');
    $('#'+bgid).fadeOut('slow');

    if(del)
      setTimeout("deleteLayer('"+id+"', false)", 500);

    setTimeout("deleteLayer('"+bgid+"', false)", 500);
  }
  else
  {
    if(del)
      deleteLayer(id, false);
    else
      document.getElementById(id).style.display = 'none';

    deleteLayer(bgid, false);
  }
}
// --------------------------------------------------------------------------------

/*
 * Erstellt den Layer fuer den Hintergrund
 * @author: DKL
 */
function makeBGLayer(id, pid, opacity, zIndex, effect, del)
{
  if (!document.createElement) return;
  if (document.getElementById(id)) return;
  
  var d = document.createElement('div');
  d.id = id;
  if(effect)
    d.style.display = 'none';
  else
    d.style.display = 'block';
  d.style.left = '0px';
  d.style.top = '0px';
  d.style.width = $(document).width() + 'px';
  d.style.height = $(document).height() + 'px';
  d.style.backgroundColor = '#000000';
  d.style.position = 'absolute';
  d.style.zIndex = zIndex;
  d.style.opacity = opacity/10;
  d.style.filter = 'alpha(opacity=' + opacity*10 + ')';
  d.onclick = function(){
    closePopup(pid, id, effect, del);
  }
  document.body.appendChild(d);
  if(effect)
    $(d).fadeIn('fast');
}
// --------------------------------------------------------------------------------

/*
 * Loescht einen Layer aus dem DOM
 * @author: DKL
 */
function deleteLayer(id, scroll)
{
  if(scroll)
  {
    if(NS)
      document.body.style.overflow = 'auto';
    if(IE)
    {
      if(!document.documentElement.clientHeight)
      {
        document.body.style.overflow = 'auto';
      }
      else
      {
        document.documentElement.style.overflow = 'auto';
      }
    }
  }
  if (!document.createElement) return;
  d = document.getElementById(id);
  if (!d) return;
  document.body.removeChild(d);
}
// --------------------------------------------------------------------------------
