//**************************************************************
// Data Structures
//**************************************************************
//
// Base structure with utility methods
//
function FCD_XML_OBJECT ()
{
}
FCD_XML_OBJECT.prototype.findElement = function ( name, parent )
{
  if ( parent == null || name == null ) return null;
  for ( var i = 0; i < parent.childNodes.length; i++ )
  {
    var child = parent.childNodes[i];
    if ( child.nodeType != 1 ) continue;
    if ( child.nodeName == name ) return child;
  }
  return null;
}
FCD_XML_OBJECT.prototype.getChildValue = function ( name, parent )
{
  var kid = this.findElement ( name, parent );
  return ( kid != null && kid.childNodes != null && kid.childNodes.length > 0 ) ?
         kid.childNodes[0].data : null;
}
// create a base class to get things going
new FCD_XML_OBJECT();

//*****************************************************
// Patron object
// has attributes; name, address.
//
// param element The XML element to construct from.
//*****************************************************
function Patron ( element )
{
  this.name = this.getChildValue ( "name", element );
  this.address = this.getChildValue ( "address", element );
}
Patron.prototype = new FCD_XML_OBJECT();
Patron.prototype.getName = function () { return this.name; }
Patron.prototype.getAddress = function () { return this.address; }
Patron.prototype.toString = function ()
{
  var txt = "patron name: " + this.getName () + "<br />";
  txt += "patron address: " + this.getAddress() + "<br />";
  return txt;
}

//*****************************************************
// Artwork object
// has attributes; genre, title, description image, thumbnail, patron
//
// param element The XML element to construct from.
//*****************************************************
function Artwork ( element )
{
  this.genre = this.getChildValue ( "genre", element );
  this.description = this.getChildValue ( "description", element );
  this.title = this.getChildValue ( "title", element );
  this.content = this.getChildValue ( "content", element );
  this.thumbnail = this.getChildValue ( "thumbnail", element );
  this.patron = new Patron ( this.findElement ( "patron", element ) );
}
Artwork.prototype = new FCD_XML_OBJECT();

Artwork.prototype.getGenre = function () { return this.genre; }
Artwork.prototype.getDescription = function () { return this.description; }
Artwork.prototype.getTitle = function () { return this.title; }
Artwork.prototype.getContent = function () { return this.content; }
Artwork.prototype.getThumbnail = function () { return this.thumbnail; }
Artwork.prototype.getPatron = function () { return this.patron; }
Artwork.prototype.toString = function ()
{
  var txt = "title: " + this.getTitle() + "<br />";
  txt += "genre: " + this.getGenre() + "<br />";
  txt += "content: " + this.getContent() + "<br />";
  txt += "description: " + this.getDescription() + "<br />";
  txt += "thumbnail: " + this.getThumbnail() + "<br />";
  txt += "patron: " + this.getPatron() + "<br />";
  return txt;
}

function FCD_SITE ( CB )
{
  this.AJAX = null;
  this.ERROR = false;
  this.ERROR_MSG = null;
  this.HANDLER = CB;
  this.ROOT = null;
  this.GENRES = new Array ();
  this.PIECES = new Array ();
  this.LOAD_COMPLETE = false;
  this.DRAW = null;
  if ( window.XMLHttpRequest ) this.AJAX = new XMLHttpRequest ();
  else if ( window.ActiveXObject ) this.AJAX = new ActiveXObject ( "Microsoft.XMLHTTP" );
}
FCD_SITE.prototype.NAMESPACE = "http://fountaincitydesign.com/artwork/";
FCD_SITE.prototype.isLoadComplete = function () { return this.LOAD_COMPLETE; }
FCD_SITE.prototype.getGenres = function () { return this.GENRES; }
FCD_SITE.prototype.getPieces = function ( genre )
{
  for ( var i = 0; i < this.GENRES.length; i++ )
  {
    if ( this.GENRES[i] == genre ) return this.PIECES[i];
  }
  var pcs = new Array ();
  this.GENRES[this.GENRES.length] = genre;
  this.PIECES[this.GENRES.length-1] = pcs;
  return pcs;
}
FCD_SITE.prototype.addPiece = function ( art )
{
  var arr = this.getPieces ( art.getGenre() );
  arr[arr.length] = art;
}
FCD_SITE.prototype.getElement = function ( name, id )
{
  return this.AJAX.responseXML.getElementsByTagName(name)[id];
}
FCD_SITE.prototype.getNamespacedElement = function ( name, id )
{
  if ( this.AJAX.responseXML == null ) return null;
  var element = null;
  if ( window.ActiveXObject ) element= this.getElement ( "fcd:"+name,id );  //Stupid Internet Explorer
  else element = this.AJAX.responseXML.getElementsByTagNameNS ( this.NAMESPACE, name )[id];
  return element;
}
FCD_SITE.prototype.checkEvent = function ()
{
  if ( this.ERROR ) return true;
  else if ( this.AJAX.readyState == 4 )
  {  // response is ready
    if ( this.AJAX.status == 200 )
    {  // valid response returned
      this.ROOT = this.getNamespacedElement("artwork",0);
      if ( this.ROOT !=null )
      {
        return true;
      }
      else
      {   // Not sure what is going on
        this.ERROR = true;
        this.ERROR_MSG = "Unable to parse response:\n" + this.AJAX.responseText;
      }
    }
    else if ( this.AJAX.status!=200 )
    {  //web server returned error message
      this.ERROR=true;
      this.ERROR_MSG=this.AJAX.status+" : " + this.AJAX.statusText;
    }
    return true;
  }
  return false;  // readyState not ready yet
}
FCD_SITE.prototype.load = function ( draw, xml )
{
  this.DRAW = draw;
  this.AJAX.open ( "GET", xml, true );
  this.AJAX.onreadystatechange = this.HANDLER;
  this.AJAX.send ( null );
}
FCD_SITE.prototype.parse = function ()
{
  if ( this.ERROR ) alert ( this.ERROR_MSG );
  else if ( this.ROOT != null )
  {
    for ( var i = 0; i < this.ROOT.childNodes.length; i++ )
    {
      var child = this.ROOT.childNodes[i];
      if ( child.nodeType != 1 ) continue;
      // art node
      var art = new Artwork ( child );
      this.addPiece ( art );
    }
  }
  this.LOAD_COMPLETE = true;
  if ( this.DRAW != null ) this.DRAW();
}

function FCD_CALLBACK () { if ( SITE.checkEvent() ) SITE.parse (); }
var SITE = new FCD_SITE ( FCD_CALLBACK );
