/* Singleton collection of static functions. */
var Page = new Function();

/* This is NOT working at the moment - was trying to change the titlebar title associated with the page.  */
Page.setTitle = function(text) {
  if (text) {
    var head = document.getElementsByTagName("head")[0];
    var title = head.getElementsByTagName("title");
    if (title) {
	  title = title[0];
	  while (title.firstChild) {
	    title.removeChild(title.firstChild);
	  }
	  title.appendChild(document.createTextNode(text));
    }
  }
}

/* Pick a "random" index from a associative array. */
Page.randomIndex = function(a) {
  var n = 0;
  for (var idx in a) {
    n++;
  }
  
  var choice = Math.floor(Math.random() * n);
  for (var idx in a) {
    if (choice == 0) {
	  return idx;
	}
	choice--;
  }
  
  return null;
}


function Gallery(id) {
  /* ID associated with gallery. */
  this._Id = id;

  /* Collections in Gallery */
  this._Collection = new Array();
  
  /* Email address to contact. */
  this._Email = "info@paulapearlart.com";
  
  /* Optional title/description. */
  this._Title = null;
  this._Description = null;
}

Gallery.prototype.setTitle = function(text) {
  this._Title = text;
}

Gallery.prototype.setDescription = function(text) {
  this._Description = text;
}

Gallery.prototype.createSiteHeader = function() {
  var sh = document.createElement("div");
  sh.className = "siteHeader";
 
  /* Set of buttons to add to header [ bid, burl, btitle ] */

  var buttonInfo = [
    [ "about", "about.html", "About" ],
	[ "home", "index.html", "Home Page" ],
	[ "exhibits", "exhibits.html", "Upcoming and past Exhibits" ],
	[ "gallery", "gallery.html", "Browse the Galleries" ],
	[ "links", "links.html", "Related Links" ],
	[ "contact", "contactus.php", "Contact the Studio" ]
  ];

  for (var i = 0; i < buttonInfo.length; i++) {
    var bid = buttonInfo[i][0];
	var burl = buttonInfo[i][1];
	var btitle = buttonInfo[i][2];
	
	var bdiv = document.createElement("div");
	bdiv.className = bid + "Button";
	sh.appendChild(bdiv);

	var ba = document.createElement("a");
	ba.href = burl;
	bdiv.appendChild(ba);
	
	var bimg = document.createElement("img");
	bimg.src = "css/images/" + bid + "button.gif";
	bimg.title = bimg.alt = ""; // btitle;
	bimg.width = 100;
	bimg.height = 34;
	bimg.border = 0;
	ba.appendChild(bimg);
  }
  
  // Return the newly created component
  return sh;
}

Gallery.prototype.updatePage = function() {

  /* Insert standard site header/footer (if necessary) */
  var nodeId = "site-header";
  var node = document.getElementById(nodeId);
  if (node) {
	var parent = node.parentNode;
	parent.replaceChild(this.createSiteHeader(), node);
  }

  /* Currently, we aren't replacing footers.
  var nodeId = "site-footer";
  var node = document.getElementById(nodeId);
  if (node) {
	var parent = node.parentNode;
	parent.replaceChild(this.createSiteHeader(), node);
  }
*/

  /* Insert collection table (if necessary). */
  for (var cid in this._Collection) {
    var collection = this._Collection[cid];

	// Look for entity with: id="GID-CID" (something like: <div id="gallery-smallworks" />)
	var node = document.getElementById(this._Id + '-' + collection._Id);

	if (node) {
	  var parent = node.parentNode;
	  parent.replaceChild(collection.createElement(), node);

	  // Reset title for page
	  // Page.setTitle(collection._Title);
	}
  }

  /* Insert gallery table (if necessary). */  
  var nodeId = this._Id + "-collections-table";
  var node = document.getElementById(nodeId);
  if (node) {
	// Reset title for page
	// Page.setTitle(this._Title);
	
	var parent = node.parentNode;
	parent.replaceChild(this.createCollectionTable(), node);
  }
  
  /* Insert random floating piece of art. */
  var nodeId = this._Id + "-float-random-piece";
  var node = document.getElementById(nodeId);
  if (node) {
    var collection = this._Collection[Page.randomIndex(this._Collection)];
	var art = collection._Art[Page.randomIndex(collection._Art)];
 
    var t = document.createElement("div");
    t.className = this._Id + "ImageTitle";
    t.appendChild(document.createTextNode(art._Title));
  
    var img = art.createImage();
	img._Collection = collection;
    img.onclick = function() {
	  var collection = this._Collection;
      document.location = collection.getUrl();
    }

    node.appendChild(t);
    node.appendChild(img);
  }
}

Gallery.prototype.getCollection = function(id) {
  return _Collection[id];
}

Gallery.prototype.addCollection = function(collection) {
  collection._Gallery = this;
  this._Collection[collection.getId()] = collection;
}

Gallery.prototype.createCollectionTable = function() {
  var gid = this._Id;

  var e = document.createElement("div");
  e.className = gid;

  if (this._Title != null) {  
    var h = document.createElement("div");
    h.appendChild(document.createTextNode(this._Title));
    h.className = gid + "Title";
    e.appendChild(h);
  }
  
  if (this._Description != null) {
    var d = document.createElement("div");
	d.className = gid + "Description";
	d.appendChild(document.createTextNode(this._Description));
	e.appendChild(d);
  }

  var table = document.createElement("table");
  table.className = gid + "Collection";
  e.appendChild(table);
  
  var tbody = document.createElement("tbody");
  table.appendChild(tbody);
  
  var i = 0;
  var tr = null;

  for (var cid in this._Collection) {
    if ((i % 3) == 0) {
	  tr = document.createElement("tr");
	  tr.className = gid + "Collection";
	  tbody.appendChild(tr);
	}
	
    var collection = this._Collection[cid];
	
	var td = document.createElement("td");
	td.className = gid + "Image";
	collection.appendElement(td);
	tr.appendChild(td);

	i++;
  }
  
  while ((i % 3) != 0) {
    i++;
	tr.appendChild(document.createElement("td"));
  }

  return e;  
}


function Collection(id, title) {
  // ID associate with the collection.
  this._Id = id;

  // Title associated with the collection.
  this._Title = title;

  // Optional description associated with the collection.
  this._Description = null;

  // Artwork in the collection.
  this._Art = new Array();
  
  // Gallery which owns collection.
  this._Gallery = null;
}

Collection.prototype.getId = function() {
  return this._Id;
}

Collection.prototype.getTitle = function() {
  return this._Title;
}

Collection.prototype.getDescription = function() {
  return this._Description;
}

Collection.prototype.setDescription = function(text) {
  this._Description = text;
}

// Get gallery ID associated with Collection
Collection.prototype.getGid = function() {
  var gid = "gallery";
  if (this._Gallery) {
    gid = this._Gallery._Id;
  }
  return gid;
}

// Get URL for displaying the collection

Collection.prototype.getUrl = function() {
  return this._Id + '-' + this.getGid() + ".html";
}

Collection.prototype.addArt = function(art) {
  art._Collection = this;
  this._Art[art.getId()] = art;
}

Collection.prototype.createElement = function() {
  var gid = "gallery";
  if (this._Gallery) {
    gid = this._Gallery._Id;
  }

  var e = document.createElement("div");
  e.className = gid;
  
  var h = document.createElement("div");
  h.appendChild(document.createTextNode(this._Title + " Gallery"));
  h.className = gid + "Title";
  e.appendChild(h);
  
  if (this._Description != null) {
    var d = document.createElement("div");
	d.className = gid + "Description";
	d.appendChild(document.createTextNode(this._Description));
	e.appendChild(d);
  }

  var table = document.createElement("table");
  table.className = gid + "Collection";
  e.appendChild(table);
  
  var tbody = document.createElement("tbody");
  table.appendChild(tbody);
  
  var i = 0;
  var tr = null;

  for (var aid in this._Art) {
    if ((i % 3) == 0) {
	  tr = document.createElement("tr");
	  tr.className = gid + "Collection";
	  tbody.appendChild(tr);
	}
	
    var art = this._Art[aid];
	
	var td = document.createElement("td");
	td.className = gid + "Image";
	art.appendElement(td);
	tr.appendChild(td);

	i++;
  }
  
  while ((i % 3) != 0) {
    i++;
	tr.appendChild(document.createElement("td"));
  }

  return e;  
}


Collection.prototype.appendElement = function(a) {
  var gid = "gallery";
  if (this._Gallery) {
    gid = this._Gallery._Id;
  }
 
  var t = document.createElement("div");
  t.className = gid + "ImageTitle";
  t.appendChild(document.createTextNode(this._Title));
  a.appendChild(t);
  
  var aidx = Page.randomIndex(this._Art);
  if (aidx) {
    var art = this._Art[aidx];
    var img = art.createImage();
	img._Collection = this;
	
	img.onclick = function() {
	  var collection = this._Collection;
	  
      document.location = collection.getUrl();
	}
	a.appendChild(img);
  }
  
  return a;

}


function Art(id, title, thumbWidth, thumbHeight) {
  this._Id = id;
  this._Title = title;
  this._ThumbWidth = thumbWidth;
  this._ThumbHeight = thumbHeight;
  this._WidthInches = null;
  this._HeightInches = null;
  this._Collection = null;
  this._Sold = false;
}

Art.prototype.hasSizeInches = function() {
  return (this._WidthInches != null) && (this._HeightInches != null);
}

Art.prototype.setSold = function(value) {
  this._Sold = value;
}

Art.prototype.setSizeInches = function(width, height) {
  this._WidthInches = width;
  this._HeightInches = height;
}

Art.prototype.getWidthInches = function() {
  return this._WidthInches;
}

Art.prototype.getHeightInches = function() {
  return this._HeightInches;
}

Art.prototype.getId = function() {
  return this._Id;
}

Art.prototype.getTitle = function() {
  return this._Title;
}

// Get gallery ID associated with Art
Art.prototype.getGid = function() {
  var gid = "gallery";
  if (this._Collection && this._Collection._Gallery) {
    gid = this._Collection._Gallery._Id;
  }
  return gid;
}

// Get collection ID associated with Art

Art.prototype.getCid = function() {
  return this._Collection._Id;
}

// Get URL for full image

Art.prototype.getUrl = function() {
  return this.getGid() + '/' + this._Id + ".jpg";
}

// Get URL for thumbnail image

Art.prototype.getUrlThumb = function() {
  return this.getGid() + '/' + this._Id + "_low.jpg";
}

Art.prototype.createImage = function() {
  var gid = this.getGid();
  var title = '"' + this._Title + '"';
  if (this._Collection && this._Collection._Gallery) {
    var collection = this._Collection;
	title += " from the \"" + collection._Title + "\" collection";
  }

  var img = document.createElement("img");
  img.className = gid + "Image";
  img.title = title;
  img.alt = title;
  img.src = this.getUrlThumb();
  img.width = this._ThumbWidth;
  img.height = this._ThumbHeight;
  img._Art = this;

  return img;

}

Art.prototype.appendElement = function(a) {
  var gid = this.getGid();
 
  var t = document.createElement("div");
  t.className = gid + "ImageTitle";
  t.appendChild(document.createTextNode(this._Title));
  a.appendChild(t);
  
  var img = this.createImage();
  img.onclick = function() {
    document.location = this._Art.getUrl();
  }
  a.appendChild(img);
  
  if (this.hasSizeInches()) {
    var d = document.createElement("div");
	d.className = gid + "ImageDimensions";
	d.appendChild(document.createTextNode(this.getWidthInches() + '" x '
		+ this.getHeightInches() + '"'));
	a.appendChild(d);
  }
  
  if (this._Sold) {
    var d = document.createElement("div");
	d.className = "soldTag";
	d.appendChild(document.createTextNode("Sold"));
	a.appendChild(d);
  }
  
  return a;

}

/** Makes sure that enough information is entered on the
    contact page before enabling/disabling the "Send" button. */

Page.updateSendButton = function() {
  var sendButton = document.getElementById("sendButton");
  if (!sendButton) {
    return false;
  }

  // Add list of fields which must not be blank
  var nonblanks = [ "comments" ];
  for (var i = 0; i < nonblanks.length; i++) {
    var node = document.getElementById(nonblanks[i]);
    // If node is blank
    if (node && (node.value == "")) {
      // De-activate send button
      sendButton.disabled = true;
      return false;
    }
  }

  // Activate send button
  sendButton.disabled = false;
  return true;
}