Neaux = {
  Name: "Neauxscript",
  Version: "0.2-1",
  Date: "28-Jan-2008",
  Author: "H.G.Laffler",
  Organization: "Neauxware Technologies",
  Home: "http://www.neauxware.com",
  SWVersion : function() { return this.Name+" V"+this.Version+" ("+this.Date+") "; },
  Copyright: function() { return " \xA92007-2008 "+this.Organization+" ("+this.Home+") All Rights Reserved."; },
  toString : function() { return this.SWVersion()+"; "+this.Copyright(); },
  ___end : null
}

Neaux.Api = {
  _ObjectRegistry : [],
  DoSlideShow : function( aTarget) {
    var theGallery = arguments[1];
    var theSlideShow = new Neaux.SlideShow( aTarget, theGallery);
    Neaux.Api._ObjectRegistry.push(theSlideShow);
    if ( arguments.length > 2)
      theSlideShow.Interval = arguments[2];
    theSlideShow.SetCrossfade();
    theSlideShow.Start();
  },
  DoRollover: function( aTarget, aID) {
    var theSuffix = typeof(arguments[2]) == "function" ? arguments[3] : arguments[2] ;
    var theCallback = typeof(arguments[2]) == "function" ? arguments[2] : null;
    var theRollover = new Neaux.Rollover( aTarget, aID, theSuffix);  
    if (theCallback != null)
      theRollover.Callback = theCallback;
    Neaux.Api._ObjectRegistry.push(theRollover);
  },
  DoIEFavorites: function( aID, aFavURL, aTitleText) {
    var theUserAgent = navigator.userAgent.toLowerCase();
    if ( theUserAgent.indexOf("msie") >= 0) {
      var theTag = document.getElementById(aID);
      var theAnchor = theTag.getElementsByTagName("a")[0];
      theAnchor.innerText = aTitleText;
      theAnchor.href = 'javascript:window.external.AddFavorite("'+aFavURL+'","'+document.title+'")';
    }
  },
  ___end : null
}

Neaux.Browser = { 
  _Opacity : undefined,
  Opacity : function() {  
    if ( typeof this._Opacity == 'undefined') {
      if ( document.all && document.body.style.filter != undefined)
        this._Opacity = "ie";
      else if ( document.body.style.opacity != undefined)
        this._Opacity = "w3c";
      else if ( document.body.style.MozOpacity != undefined)
        this._Opacity = "mozilla";
      else if ( document.body.style.KhtmlOpacity != undefined)
        this._Opacity = "khtml";
      else
        this._Opacity = "unsupported"; 
    }
    return this._Opacity;  
  },
  ___end : null
}


Neaux.Event = {
  Registry : [],
  Register : function( aTag, aThis) {
    this.Registry[aTag] = aThis;
  },
  Unregister : function( aTag) { /* Not Implemented */ },
  SetTimeout : function( aObj, aHandler, aDuration) {
  	return setTimeout( function(){Neaux.Event._Callback(aObj,aHandler);}, aDuration);
  },
  ClearTimeout : function( aTimerID) { 
    return clearTimeout(aTimerID); 
  },
  SetInterval : function( aObj, aHandler, aDuration) {
  	return setInterval( function(){Neaux.Event._Callback(aObj,aHandler);}, aDuration);
  },
  ClearInterval : function( aTimerID) { 
    return clearInterval(aTimerID); 
  },
  Attach : function( aElement, aEvent, aHandler) {
    if ( document.attachEvent)
      aElement.attachEvent("on"+aEvent,aHandler);
    else if (window.addEventListener)
      aElement.addEventListener(aEvent,aHandler,false);
    else
      aElement["on"+aEvent] = aHandler;
  },
  Detach : function( aElement, aEvent, aHandler) {
    if ( document.detachEvent)
      aElement.detachEvent("on"+aEvent,aHandler);
    else if (window.addEventListener)
      aElement.removeEventListener(aEvent,aHandler,false);
    else
      aElement["on"+aEvent] = null;
  },
  GetEvent : function() {
    if ( window.event)
      return this._IETweak(window.event);
    else
      return this.GetEvent.caller.arguments[0];
  },
  _IETweak : function ( aEvent) {
    if ( window.event) {
      aEvent.target = aEvent.srcElement;
      aEvent.pageX = aEvent.clientX + document.body.scrollTop;
      aEvent.pageY = aEvent.clientY + document.body.ScrollLeft;
      aEvent.charCode = (aEvent.type == "keypress") ? aEvent.keyCode : 0;
      switch ( aEvent.type) {
        case "mouseover":
          aEvent.relatedTarget = aEvent.fromElement; break;
        case "mouseout":
          aEvent.relatedTarget = aEvent.toElement; break;
      }
    }
    return aEvent;
  },
  _Callback : function( aObj, aFn) { aFn.call(aObj); },
  ___end : null
}

Neaux._Ware = {
  _Rollover : null,
  _Floater : null,
  ___end : null
}

// Gallery Object Methods
//    Constructor
//    Load
//    Append
//    Reset
//    First
//    Next
Neaux.Gallery = function() {
  this.Images = [];
  this._Img = null;
  if ( arguments.length > 0)
    this.Load(arguments[0]);
}
Neaux.Gallery.prototype.Load = function() {
  while (this.Images.length > 0) {
    var img = this.Images.Pop();
    delete img;
  }
  this.Append(arguments[0]);
}
Neaux.Gallery.prototype.Append = function() {
  var theGallery = arguments[0];
  var theContainer = (typeof theGallery == "string") ? document.getElementById(theGallery) : theGallery;
  var theLI = theContainer.getElementsByTagName("li");

  var theImage,theAnchor;
  for ( var i=0; i < theLI.length; i++) { 
    theAnchor = theLI[i].getElementsByTagName("a");
    if ( theAnchor && theAnchor.length > 0 && theAnchor[0].href) {
      theImage = new Image();
      theImage.src = theAnchor[0].href;
      this.Images.push(theImage);
    }
  } 
}
Neaux.Gallery.prototype.Reset = function() {
  this._Img = null;
}
Neaux.Gallery.prototype.First = function() {
  this._Img = 0;
  return this.Images[0];
}
Neaux.Gallery.prototype.Next = function() {
  if (this._Img == null || ++this._Img == this.Images.length) 
    this._Img = 0;
  return this.Images[this._Img];
}
// END OF Gallery Object


// SlideShow Object
//    Constructor
//    Start
//    Stop
//    Show
//    SetCrossfade
Neaux.SlideShow = function( aTarget, aGallery, aInterval) {
  this._Target = typeof(aTarget) == "string" ? document.getElementById(aTarget) : aTarget;
  this._Gallery = new Neaux.Gallery(aGallery);
  this.Interval = (aInterval == undefined) ? 2000 : aInterval;
  this.Manual = false;
  this._Crossfade = null;
}
Neaux.SlideShow.prototype.Start = function() {
  var bResume = arguments.length > 0 && arguments[0] == true;
  if ( !bResume)
    this._Gallery.Reset();
  this.Show();
}
Neaux.SlideShow.prototype.Stop = function() {
}
Neaux.SlideShow.prototype.Show = function() {
  var theTarget = this._Gallery.Next();
  if ( this._Crossfade) {
    this._Crossfade.Start( theTarget.src);
  }
  else {
    this._Target.src = theTarget.src;
    if (!this.Manual)
      Neaux.Event.SetTimeout(this, this.Show, this.Interval);
  }
}
Neaux.SlideShow.prototype._EndCrossfade = function() {
  if (!this.Manual)
    Neaux.Event.SetTimeout(this, this.Show, this.Interval);
}
Neaux.SlideShow.prototype.SetCrossfade = function() {
  var bSet = arguments.length > 0 ? arguments[0] : true;
  if ( bSet && this._Crossfade == null) {
    this._Crossfade = new Neaux.Crossfade(this._Target);
    this._Crossfade.PersistClone = true;
    this._Crossfade.SetCallback( this, this._EndCrossfade);
  }
  else if ( !bSet && this._Crossfade) {
    delete this._Crossfade;
    this._Crossfade = null;
  }
}
// END OF SlideShow object


// Crossfade Object Methods
//    Constructor
//    SetCallback
//    Start
//    _CloneTarget
//    _CloneInit
//    _SetOpacity
//    _Next
Neaux.Crossfade = function( aTarget, aDuration, aRate) {
  this._Target = typeof(aTarget) == "string" ? document.getElementById(aTarget) : aTarget;
  this.Duration = aDuration ? aDuration : 2000;   // Duration of Crossfade
  this.Rate = aRate ? aRate : 40;                 // # transitions during cf
  this.PersistClone = false;

  this._Support = Neaux.Browser.Opacity();
  this._nTransition = 0;
  this._TimerID = null;
  this._Clone = null;
  this._CallbackObj = null;
  this._Callback = null;                           // End of Transition Callback
}
Neaux.Crossfade.prototype.Start = function( aNewImage) {
  if ( this._Support != "unsupported") {
    this._CloneInit();

    var IsFadeInProgress = (this._TimerID != null);
    if ( !IsFadeInProgress) {
      this._Clone.src = aNewImage;
      this._nTransition = 0;
      this._SetOpacity();
      this._Clone.style.visibility = "visible";
      var theFadeSlice = this.Duration/this.Rate;
      this._TimerID = Neaux.Event.SetInterval(this, this._Next, theFadeSlice);
    }
    else {
      this._Target.src = this._Clone.src;
      this._Clone.src = aNewImage;
    }
  }
  else
    this._Target.src = aNewImage;
}
Neaux.Crossfade.prototype._CloneTarget = function() {
  if ( this._Clone == null) {
    this._Clone = document.createElement("img");
    var theBody = document.getElementsByTagName("body")[0];
    theBody.appendChild(this._Clone);
  }
  this._Clone.style.visibility = "hidden";
  this._Clone.style.position = "absolute";
  this._CloneInit();
  this._Clone.style.width = this._Target.width + "px";
  this._Clone.style.height = this._Target.height + "px";
}
Neaux.Crossfade.prototype._CloneInit = function() {
  if ( !this._Clone)
    this._CloneTarget();
  var thePtr = this._Target; 
  var x = y = 0;
  while (thePtr != null) {
    x += thePtr.offsetLeft;
    y += thePtr.offsetTop;
    thePtr = thePtr.offsetParent;
  } 
  this._Clone.style.left = x + "px";
  this._Clone.style.top = y + "px";
}
Neaux.Crossfade.prototype._SetOpacity = function() {
  var theOpacity = this._nTransition/this.Rate;
  var theOtherOpacity = 1.0 - theOpacity;
  switch ( this._Support) {
    case "ie":
      theOpacity *= 100;
      this._Clone.style.filter = "alpha(opacity:"+theOpacity+")"; break;
      this._Target.style.filter = "alpha(opacity:"+theOtherOpacity+")"; break;
    case "w3c":
      this._Clone.style.opacity = theOpacity; break;
      this._Target.style.opacity = theOtherOpacity; break;
    case "mozilla":
      this._Clone.style.MozOpacity = theOpacity; break;
      this._Target.style.MozOpacity = theOtherOpacity; break;
    case "khtml":
      this._Clone.style.KhtmlOpacity = theOpacity; break;
      this._Target.style.KhtmlOpacity = theOtherOpacity; break;
  }
}
Neaux.Crossfade.prototype._Next = function() {
  ++this._nTransition;
  this._SetOpacity();
  if ( this._nTransition == this.Rate) {
    Neaux.Event.ClearInterval(this._TimerID);
    this._TimerID = null;
    this._Target.src = this._Clone.src;
    this._Clone.style.visibility = "hidden";
    if ( !this.PersistClone) {
      this._Clone.parentNode.removeChild(this._Clone);
      this._Clone = null;
    }
    if (this._Callback != null) {
      var theFN = this._Callback;
      theFN.call(this._CallbackObj);
    }
  }
}
Neaux.Crossfade.prototype.SetCallback = function( aObj, aFn) {
  this._CallbackObj = aObj;
  this._Callback = aFn;
}
// END OF Crossfade object

// Rollover Object Methods
//    Constructor
//    SwapHandler
//    Swap
Neaux.Rollover = function( aTarget, aID, aSuffix) {
  Neaux._Ware._Rollover = this;
  this._Target = document.getElementById(aTarget);
  var theContainer = document.getElementById(aID);
  var theImages = theContainer.getElementsByTagName("img");
  this.Suffix = aSuffix == undefined ? "_thumb" : aSuffix;
  this.Callback = null;
  this._Gallery = [];

  var j, theSrc, theStr, theImage;
  for ( var i=0; i < theImages.length; i++) {
    j = theImages[i].src.lastIndexOf(this.Suffix);
    if ( j > 0) { 
      theSrc = theImages[i].src;
      theStr = theSrc.substring(theSrc.lastIndexOf('/') + 1, theSrc.lastIndexOf('.')) ;
      theImage = new Image();
      theImage.src = theSrc.substring(0,j) + ".jpg";  // LATER: NO ASSUMPTIONS ABOUT FILE NAMES/TYPES
      this._Gallery[theStr] = theImage;
      Neaux.Event.Attach( theImages[i], "mouseover", this.SwapHandler);  
      Neaux.Event.Attach( theImages[i], "mouseout", this.SwapHandler);  
    }
  }
}
Neaux.Rollover.prototype.SwapHandler = function( aEvent) {
  var theEvent = Neaux.Event.GetEvent();
  Neaux._Ware._Rollover.Swap( theEvent.target, theEvent);
}
Neaux.Rollover.prototype.Swap = function( aImage, aEvent) {
  if ( aEvent.type == "mouseover") {
    var theStr = aImage.src.substring(aImage.src.lastIndexOf('/') + 1, aImage.src.lastIndexOf('.')) ;
    this._Target.src = this._Gallery[theStr].src;
  }
  if ( this.Callback)
    this.Callback(aImage,aEvent.type,aEvent);
}
// END OF Rollover object
