var rootFolder = "/";

function blockChars( my_event, regExp )
{
  var key;
  var keychar;
  var reg;

  if ( !regExp ) var regExp = /\d|\./; // only integer values allowed, eh. ( and . )

  if ( window.event )
  { // for IE, e.keyCode or window.event.keyCode can be used
    key = my_event.keyCode;
  } else if ( my_event.which )
  { // netscape
    key = my_event.which;
  } else // no event, so pass through
  {
    return true;
  }
  if ( key < 32 ) // non-char key-presses
    return true;
  keychar = String.fromCharCode( key );
  return regExp.test( keychar );
} // blockChars

slider = {
  slide : function( id, fps, duration, startValue, endValue, whichAttribute )
  {
    if ( !fps ) var fps = 30;
    if ( !duration ) var duration = 3000;  // 3 seconds
    if( !startOpacity ) var startOpacity = 100;
    if ( !endOpacity ) var endOpacity = 0;
//    if ( closeFadedEl == null ) var closeFadedEl = false;
    var frames = Math.round( fps * ( duration / 1000 ) );
    var interval = duration / frames;
    var delay    = interval;
    var frame    = 0;
  
    var slideStep = ( endValue - startValue ) / frames;
    var stepSlide = startValue; 
    
    while ( frame < frames )
    {
      stepSlide += slideStep;
    
      setTimeout( "slider.setAttribute( '" + id + "', '" + stepSlide + "', '" + whichAttribute + "' );", delay );

      frame++;
      delay = interval * frame; 
    }
    setTimeout( "slider.setAttribute( '" + id + "', '" + stepSlide + "', '" + whichAttribute + "' );", delay );
  }, // slide
  setAttribute : function( id, value, attribute )
  {
    if ( value <= 0 ) return;
    var slideEl = document.getElementById( id );
    if ( !slideEl ) return;
    switch( attribute )
    {
      case 'width':
        slideEl.style.width = value + 'px';
      case 'left':
        slideEl.style.left = value + 'px';
        break;
      case 'top':
        slideEl.style.top = value + 'px';
        break;
      case 'height':
        slideEl.style.height = value + 'px';
        break;
      default:
        break;
    }
    return;
  } // setOpacity
}; // slider object


function addLoadEvent( func ) 
{
  var oldonload = window.onload;
  if ( typeof window.onload != 'function' ) 
  {
    window.onload = func;
  } else 
  {
    window.onload = function() {
      oldonload();
      func();
    }
  }
} // addLoadEvent

function insertAfter( newElement,targetElement ) 
{
  var parent = targetElement.parentNode;
  if ( parent.lastChild == targetElement ) 
  {
    parent.appendChild( newElement );
  } else 
  {
    parent.insertBefore( newElement, targetElement.nextSibling );
  }
} // insertAfter

function addClass( element, value )  
{
  if ( !element.className ) 
  {
    element.className = value;
  } else 
  {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
} // addClass

function prepareDeletion()
{
  if ( !document.getElementsByTagName ) return false;
  if ( !document.getElementById ) return false;
  var allLinks = document.getElementsByTagName( 'a' );
  var numLinks = allLinks.length;
  var deleteLink;
  for ( var i = 0; i < numLinks; i++ )
  {
    if ( allLinks[ i ].className != 'delete' )
      continue;
    deleteLink = allLinks[ i ];
    deleteLink.onclick = function() { return confirm( 'Are you sure you want to delete this item?\nDeletion cannot be undone.' ); };
  }
} // prepareDeletion

function showEditDelete( spanEl )
{
  spanEl.className = 'editDelete';
} // showEditDelete

function hideEditDelete( spanEl )
{
  spanEl.className = 'hidden';
} // hideEditDelete

function prepareEditDelete()
{
  if ( !document.getElementsByTagName ) return false;
  if ( !document.getElementById ) return false;
  var allTables = document.getElementsByTagName( 'table' );
  var numTables = allTables.length;
  var prepTable, prepRows, allSpans, numSpans, prepSpan, prepTD;
  for ( var i = 0; i < numTables; i++ )
  {
    prepTable = allTables[ i ];
    allSpans = prepTable.getElementsByTagName( 'span' );
    numSpans = allSpans.length;
    for ( var j = 0; j < numSpans; j++ )
    {
      if ( allSpans[ j ].className != 'editDelete' )
        continue;
      prepSpan = allSpans[ j ];
      hideEditDelete( prepSpan );
      prepTD = prepSpan.parentNode.parentNode; // span -> p -> td
      prepTD.onmouseover = function() 
      { 
        var theSpans = this.getElementsByTagName( 'span' ); 
        var numSpans = theSpans.length, showSpan;
        for ( var q = 0; q < numSpans; q++ )
        {
          showSpan = theSpans[ q ];
          if ( showSpan.className == 'editDelete' )
            break;
        }
        showEditDelete( showSpan );
      };
      prepTD.onmouseout = function() 
      { 
        var theSpans = this.getElementsByTagName( 'span' ); 
        var numSpans = theSpans.length, hideSpan;
        for ( var q = 0; q < numSpans; q++ )
        {
          hideSpan = theSpans[ q ];
          if ( hideSpan.className == 'editDelete' )
            break;
        }
        hideEditDelete( hideSpan );
      };
    }
  } 
} // prepareEditDelete

function closeMe( closeEl )
{
  if ( closeEl != null ) { closeEl.parentNode.removeChild( closeEl ); }
} // closeMe

var editableText = {
  initEditableAnchors : function ()
  {
    if ( !document.getElementsByTagName ) return false;
    if ( !document.getElementById ) return false;
    var allEdits = document.getElementsByTagName( 'p' );
    var numEdits = allEdits.length;
    var curEdit, curPara, allAnchors, curAnchor, totAnchors;
    for ( var i = 0; i < numEdits; i++ )
    {
      if ( allEdits[ i ].className != 'edit' )
        continue;
      curPara = allEdits[ i ];
      allAnchors = curPara.getElementsByTagName( 'a' ); // we want to edit the anchor text only
      totAnchors = allAnchors.length;
      for ( var j = 0; j < totAnchors; j++ )
      {
        editableText.prepareEdit( allAnchors[ j ] );
      }
    } // all edits
  }, // initEditableAnchors
  getKeyCode : function ( e, parentEl )
  {
    if ( parentEl.layers ) { return e.which; }
    else if ( parentEl.all ) { return event.keyCode; }
    else if ( parentEl.getElementById ) { return e.keyCode; }
    return 0;
  }, // getKeyCode
  prepareEdit : function ( el, parentEl )
  {
    var newInput, paraText;
    if ( !parentEl ) var parentEl = document;
    newInput = parentEl.createElement( 'input' );
    newInput.setAttribute( 'type', 'text' );
    newInput.className = 'hidden';
    newInput.id = 'ph_title_' + el.id;
    if ( el.hasChildNodes() )
    {
      paraText = el.firstChild.nodeValue; //editableText.getEditableText( el );
      newInput.setAttribute( 'value', paraText );
    }
    newInput.onblur = function() {
       var editedA = parentEl.getElementById( this.id.replace( 'ph_title_', '' ) );
       var newTitle = parentEl.getElementById( this.id ).value;
       var showLoad = parentEl.createElement( 'img' );
       showLoad.setAttribute( 'src', rootFolder + 'loading01.gif' );
       showLoad.setAttribute( 'id', 'loading_' + this.id );
       insertAfter( showLoad, editedA );
       parentEl.getElementById( this.id ).className = 'hidden';
       
       var request = new requestAgent( 'post', rootFolder + 'edit/ajax.php', function( RA, timeoutID ) {
         if ( RA.readyState == 4 ) 
         { // only if reqestAgent shows "complete"
           clearTimeout( timeoutID ); // we're ok, do not complain.
           if ( RA.status == 200 ) 
           { // only if "OK"
             var response = RA.responseXML.documentElement;
             if ( response )
             {
               var error = response.getElementsByTagName( 'error' )[ 0 ];
               if( error.getElementsByTagName( 'has_error' )[ 0 ].firstChild.data == 1 )
               {  // handle the errors
                 return false;
               } else
               { // do the good stuff
                 showLoad.parentNode.removeChild( showLoad );
       editedA.firstChild.nodeValue = newTitle;
       editedA.className = 'edit';
                  // shouldn't notice if it's all good, eh.
               } // has_error == 1 
             } // ra.responsexml
           } else 
           {
             alert( RA.statusText );
             return false;
           } // status = 200
         } // readystate = 4
         return false;
        }, 'id=' +this.id.replace( 'ph_title_', '' ) + '&ajax=1&ph_title=' + parentEl.getElementById( this.id ).value, true );
      };
    el.parentNode.appendChild( newInput );
    el.onclick = function() {
        parentEl.getElementById( this.id ).className = 'hidden';
        var myInput = parentEl.getElementById( 'ph_title_' + this.id );
        myInput.className = '';
        if ( myInput.focus ) myInput.focus();
        return false;
      };
  } // prepareEdit
}; // editableText

document.getElementsByClass = function (needle)
{
  function _GetElementsByClass(outArray, seed, needle)
  {
    var c;
    while (seed) {
      if (seed.nodeType == 1 ) { //Node.ELEMENT_NODE) {
        if ( seed.className ) { //seed.hasAttribute("class")) {
            
          c = " " + seed.className + " ";
          
          if (c.indexOf(" " + needle + " ") != -1)
            outArray.push(seed);
        }
        _GetElementsByClass(outArray, seed.firstChild, needle)
      }
      seed = seed.nextSibling;
    }
  }

  var outArray = new Array();
  _GetElementsByClass(outArray, document.documentElement, needle);
  return outArray;
}

var imageZoom = {
  prepareImages : function ( )
  {
    var allZooms = document.getElementsByClass( 'zoom' );
    if ( !allZooms )
      return false;
    var totZooms = allZooms.length;
    var zoomImg;
    for ( var i = 0; i < totZooms; i++ )
    {
      imageZoom.doImageZoom( allZooms[ i ] );
    }
  }, // prepareImages
  makeCurtain : function ( id, parentEl )
  {
    var curtain = parentEl.createElement( 'div' );
    curtain.style.zIndex = 200;
    curtain.id = 'curtain_' + id;
//    curtain.className = loadingStyleClass;
    if ( parentEl.all || parentEl.getElementById )
    {  //if the user is using IE 4+ or Firefox/ NS6+
      curtain.style.position = 'absolute';
      curtain.style.left = '0px';
      curtain.style.top = '0px';
    }
    var newWidth, newHeight;
    if ( window.innerWidth ) //if browser supports window.innerWidth
    {
      newWidth = window.innerWidth;
      newHeight = window.innerHeight;
    } else if ( parentEl.all ) //else if browser supports parentEl.all (IE 4+)
    {
      newWidth = parentEl.body.clientWidth;
      newHeight = parentEl.body.clientHeight;
    }
    curtain.style.backgroundColor = '#000';
    curtain.style.width = ( parentEl.body.scrollWidth - 1 ) + "px";
    curtain.style.height = ( parentEl.body.scrollHeight - 1 ) + "px";
//    curtain.style.background = '#000 url(' + rootFolder + 'indicator.gif) no-repeat center';
    curtain.style.opacity = '0.45';
    curtain.style.filter = 'alpha(opacity=45)';
    parentEl.getElementById( 'mainContent' ).appendChild( curtain );
//    hideWithinBox( 'select', curtain.id );
    }, // makeCurtain
  doImageZoom : function( el, parentEl )
  {
    if ( !parentEl ) var parentEl = document;
    el.onmouseover = function() { this.parentNode.onclick = function() { return false; }; this.style.backgroundColor = '#bbcbff'; };
    el.onmouseout = function() { this.parentNode.onclick = function() {}; this.style.backgroundColor = ''; };
    zoomImg = el.getElementsByTagName( 'img' )[ 0 ];
    zoomImg.onclick = function( e ) {
	  window.scrollTo( 0, 0 );
      var requestID = this.id.replace( 'zoom_', '' );
      imageZoom.makeCurtain( requestID, parentEl );

      var loading = parentEl.createElement( 'img' );
	  loading.setAttribute( 'id', 'loading_' + requestID );
      loading.style.position = 'absolute';
	  loading.style.display = 'block';
	  loading.style.padding = '5px';
      loading.style.left = '50%';
      loading.style.top = '50%';
      loading.style.zIndex = 150;
        loading.setAttribute( 'src', rootFolder + 'images/loading03.gif' );
        loading.style.width = '100px';
        loading.style.height = '9px';
        loading.style.marginLeft = '-50px';
        loading.style.marginTop = '-4px';
		loading.style.backgroundColor = '#ffffff';
/*      loading.style.border = '3px solid #00f';
      loading.style.cursor = 'pointer';*/
      parentEl.getElementById( 'mainContent' ).appendChild( loading );

	  var maxDim = ( parentEl.body.scrollWidth > parentEl.body.scrollHeight ) ? parentEl.body.scrollWidth : parentEl.body.scrollHeight;
      var request = new requestAgent( 'get', rootFolder + 'gallery/resize/index.php', function( RA, timeoutID ) {
         if ( RA.readyState == 4 ) 
         { // only if reqestAgent shows "complete"
           clearTimeout( timeoutID ); // we're ok, do not complain.
           if ( RA.status == 200 ) 
           { // only if "OK"
             var response = RA.responseText;
             if ( response )
             {
               if ( response.length <= 0 )
               {  // handle the errors
                 return false;
               } else
               { // do the good stuff
                  var dimensions = response.split( 'x' ); // width x height
                  var zoomed = parentEl.createElement( 'img' );
                  zoomed.style.position = 'absolute';
                  zoomed.style.left = '50%';
				  zoomed.style.top = '50%';
                  zoomed.style.zIndex = 200;
                    zoomed.setAttribute( 'src', rootFolder + 'gallery/resize/?pid=' + requestID + '&size=full' );
                    zoomed.style.width = dimensions[ 0 ] + 'px';
                    zoomed.style.height = dimensions[ 1 ] + 'px';
                    zoomed.style.marginLeft = '-' + ( dimensions[ 0 ] / 2 ) + 'px';
                    zoomed.style.marginTop = '-' + ( dimensions[ 1 ] / 2 ) + 'px';
                  zoomed.style.border = '3px solid #00f';
                  zoomed.style.cursor = 'pointer';
                  zoomed.onclick = function() { 
				    this.parentNode.removeChild( this ); 
					parentEl.getElementById( 'mainContent' ).removeChild( parentEl.getElementById( 'curtain_' + requestID ) ); 
				    parentEl.getElementById( 'mainContent' ).removeChild( parentEl.getElementById( 'loading_' + requestID ) );
				  };
                  parentEl.getElementById( 'mainContent' ).appendChild( zoomed );
               
               } // has_error == 1 
             } // ra.responsexml
           } else 
           {
             alert( RA.statusText );
             return false;
           } // status = 200
         } // readystate = 4
         return false;
        }, 'pid=' +requestID + '&getXY=1&size=' + maxDim, true );
    };
  } // doImageZoom
};

function prepareAddPhoto()
{
  if ( !document.getElementsByTagName ) return false;
  if ( !document.getElementById ) return false;
  var p_iframe = document.getElementById( 'iframe' );
  if ( !p_iframe )
    return false;
  p_iframe.style.display = 'none';
  p_iframe.style.height = '1px';
  var link = document.getElementById( 'addPhoto' );
  if ( !link )
    return false;
  link.onclick = function() { 
    document.getElementById( 'photoList' ).className = '';
    this.className = 'current';
    if ( this.blur )
      this.blur();
    var iframe = document.getElementById( 'iframe' ); 
    iframe.className = ''; // initialized to 'hidden' on-page.
    iframe.style.display = '';
    slider.slide( 'iframe', 30, 500, 1, 150, 'height' );
    return false;
  };
} // prepareDeletion

function prepareToolTips()
{
  if ( !document.getElementsByTagName ) return false;
  if ( !document.getElementById ) return false;
  var allLinks = document.getElementsByTagName( 'a' );
  var numLinks = allLinks.length;
  var toolTipLink, aSpans;
  for ( var i = 0; i < numLinks; i++ )
  {
    aSpans = allLinks[ i ].getElementsByTagName( 'span' );
    if ( aSpans.length == 0 )
      continue;
    if ( allLinks[ i ].className )
      continue;
    addClass( allLinks[ i ], 'tooltip' );
  }
} // prepareTooltips


addLoadEvent( prepareAddPhoto );
addLoadEvent( editableText.initEditableAnchors );
addLoadEvent( prepareDeletion );
addLoadEvent( prepareEditDelete );
addLoadEvent( imageZoom.prepareImages );
//addLoadEvent( prepareToolTips );
