﻿/**
 * @author mh
 */

var WinDetails = null;

var aktPhotoIndex = -1;
var oldPhotoX = -1;
var oldPhotoY = -1;
var newPhotoX = -1;
var newPhotoY = -1;


function getPhotoInfos(hddPhotosID)
{
   var photoInfos = null;
   if (document.getElementById(hddPhotosID)!=null)
   {
      photoInfos = document.getElementById(hddPhotosID).value.split("|");
   }
   return photoInfos;
}

function getPhotoLink(photoInfos, index)
{
   if (photoInfos!=null) {
     return  photoInfos[index].split(";")[0];
   }
   return "";
}

function getPhotoSizeX(photoInfos, index)
{
   if (photoInfos!=null) {
     return  photoInfos[index].split(";")[1];
   }
   return -1;
}

function getPhotoSizeY(photoInfos, index)
{
   if (photoInfos!=null) {
     return  photoInfos[index].split(";")[2];
   }
   return -1;
}

function getPhotoText(photoInfos, index)
{
   if (photoInfos!=null) {
     return  photoInfos[index].split(";")[3];
   }
   return '';
}

var myDelegate = {

  canClose: function(window) {
    HideDetails();
   return true;
  }

};

function photoDlgDestroy()
{
  WinDetails = null;
}

function HideDetails()
{
  if (WinDetails!=null) {
    WinDetails.hide();
    WinDetails.destroy();
  }
  WinDetails = null;
}

var sContent = '';
var sBottom = '';

function afterHide()
{
  if (newPhotoX/newPhotoY!=oldPhotoX/oldPhotoY) {
    WinDetails.setSize(newPhotoX, newPhotoY);
  }
  WinDetails.getContent().innerHTML = sContent + sBottom;
  $('detailPhoto').style.display = 'none';
  new Effect.Appear('detailPhoto', {duration:0.01});
  oldPhotoX = newPhotoX;
  oldPhotoY = newPhotoY;
  WinDetails.showCenter(true);
}

function showPhoto(hddPhotosID, fileName, index, x, y)
{

  var sNext = 'Nächstes';
  var sPrev = 'Vorheriges';

  var imgX = x;
  var imgY = y;

  if (imgX>640)
  {
    imgX = 640;
  }

  if (imgY>480)
  {
    imgX = imgX * 480 / imgY;
    imgY = 480;
  }
  
  if (imgX<480) 
  {
    sNext = ' ';
    sPrev = ' ';
  }

  photoInfos = getPhotoInfos(hddPhotosID);
  var sText = getPhotoText(photoInfos, index);

  sContent =
    '<img id="detailPhoto" ' +
     'width="' + imgX + '" ' +
     'heigth="' + imgY + '" ' +
     'src="' + fileName + '" />';

  sBottom = '<div id="detailBottom">';
  if (photoInfos!=null && index>0)
  {
    var newIndex = index-1;
    var newLink = getPhotoLink(photoInfos, index-1);
    var newX    = getPhotoSizeX(photoInfos, index-1);
    var newY    = getPhotoSizeY(photoInfos, index-1);
    sBottom += '<input type="button" id="PrevPhoto" value="' + sPrev + '"' +
                ' onclick="showPhoto(\'' + hddPhotosID + '\', \'' + newLink + '\',' + newIndex + ',' + newX + ',' + newY + ');"  />';
  } else  {
    sBottom += '<input type="button" id="PrevPhotoDis" value="' + sPrev + '" disabled="disabled" />';
  }
  sBottom += '<label>' + sText + '</label>';

  if (photoInfos!=null && index<photoInfos.length-2)
  {
    var newIndex = index+1;
    var newLink = getPhotoLink(photoInfos, index+1);
    var newX    = getPhotoSizeX(photoInfos, index+1);
    var newY    = getPhotoSizeY(photoInfos, index+1);
    sBottom += '<input type="button" id="NextPhoto" value="' + sNext + '"' +
                ' onclick="showPhoto(\'' + hddPhotosID + '\', \'' + newLink + '\',' + newIndex + ',' + newX + ',' + newY + ');"  />';
  } else  {
    sBottom += '<input type="button" id="NextPhotoDis" value="' + sNext + '" disabled="disabled" />';
  }

  sBottom += '</div>';

  if (WinDetails!=null)
  {
    newPhotoX = imgX;
    newPhotoY = imgY+36;
    new Effect.Fade('detailPhoto', {duration: 0.5, afterFinish: afterHide});
    WinDetails.setTitle(fileName);
  } else {
    WinDetails = new Window('PhotoDetailWnd',
      {
      className: "dialog",
      width: imgX,
      height: imgY+36,
      zIndex: 100,
      draggable : true,
      destroyOnClose: true,
      showEffect: Element.show,
      hideEffect: Element.hide,
      onDestroy: photoDlgDestroy
      }
    );
    WinDetails.getContent().innerHTML = sContent + sBottom;
    WinDetails.showCenter(true);
    //WinDetails.setDelegate(myDelegate);
    WinDetails.setTitle(fileName);
  }
  aktPhotoIndex = index;
  return true;

}

function clearHRefs ()
{
  var divThumbs = document.getElementById('cmscontent');
  var theAs = document.getElementsByClassName('cmsthumb', divThumbs);
  for (var index=0; index<theAs.length; index++)
  {
    theAs[index].onclick = theAs[index].childNodes[0].onclick;
    theAs[index].href = '#';
    theAs[index].target = '_self';
    theAs[index].childNodes[0].onclick = null;
  }
}


Event.observe(window, 'load', clearHRefs);

