/* TOG Javascript framework
 *
 * NOTE:
 *  Requires Prototype JavaScript framework version 1.4.0 or later to work.
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *  For details, see the Prototype web site: http://prototype.conio.net/
 *
 */

// Set the frameworks version. 
var Tog = {
  Version: '1.0.3',
  prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
}

// Test if the correct version of Prototype is available.
if ((typeof Prototype == 'undefined') || Tog.prototypeVersion < 1.4)
  throw("Tog requires the Prototype JavaScript framework >= 1.4");


// Definition of the Billboard class.
Tog.Billboard = Class.create();

Tog.Billboard.prototype = {

  initialize: function(container, options) {

    // Did the caller give us the ID of an element, if not quit.
    if (!container)
      return;

    this.container = $(container);
    // Does the element exist, if not quit.
    if (!this.container)
      return;
  
    this.setOptions(options);

    this.panels = this._getDirectChildrenByTag(this.container, 'DIV');
    this.currentPanel = 0;

    // Did we find any panels to be displayed, if not quit.
    if (this.panels.length == 0)
      return

    // Hide all the panels except for the first.  
    this.panels.each(function(item, index){if (index != 0) Element.hide(item);});

    // Start up the timer.
    this.timer = new PeriodicalExecuter(this.changePanel.bind(this), this.options.delay);  
  },
  
  setOptions: function(options) {
    // Setup some default options for the Billboard.
    this.options = {
        delay: 2
    }

    // Override the defaults with the users values.
    Object.extend(this.options, options || {});
  },

  changePanel: function() {

    Element.hide(this.panels[this.currentPanel]);
  
    this.currentPanel = (this.currentPanel + 1 == this.panels.length) ? 0 : this.currentPanel + 1;
    
    Element.show(this.panels[this.currentPanel]);
  },

  _getDirectChildrenByTag: function(element, tagName) {

    var kids = new Array();
    var allKids = element.childNodes;

    for (var i = 0; i < allKids.length; i++)
      if (allKids[i] && allKids[i].tagName && allKids[i].tagName == tagName)
        kids.push(allKids[i]);

    return kids;
  }
}

function imageWindow(window_name, image_url, image_width, image_height, title, caption) {
  width = image_width + 50;
  height = image_height + 80;

  imgwin = window.open('', window_name, 'resizable=no,toolbar=no,menubar=no,width=' + width + ',height=' + height);
  imgwin.document.writeln('<html><head><title>' + title + '</title><link href="/css/imgwin.css" type="text/css" rel="stylesheet"></head>');
  imgwin.document.writeln('<body><div align="center"><img src="' + image_url + '" width="' + image_width + '" height="' + image_height + '" border="1" alt="' + title + '"><div class="caption">' + caption + '</div>');
  imgwin.document.writeln('<div class="popupnav"><a href="javascript:window.parent.close();" id="close-link">Close Image</a></div></div></body></html>');

  imgwin.document.close();
  if (window.focus) {imgwin.focus();}
}


