var Site = Class.create();
Site.base = function() {
  if (document.location.protocol == "file:") {
    // dev only
    return ''
  } else {
    return '/'
  }
}

Site.imageBase = function() {
  return Site.base() + 'images/';
}

var Production = Class.create();
Production.prototype = {
  initialize: function() {
  },

  newImage: function() {
    var img = new Production.Image();
    img.production = this;
    return img;
  },

  readImages: function(xml, tagName) {
    var images = new Array();
    var elements = xml.getElementsByTagName(tagName);
    for (var i = 0; i < elements.length; i++) {
      var imageX = elements.item(i);
      var image = this.newImage();
      image.populateFromElement(imageX);
      images.push(image);
    }
    return images;
  },

  loadDetails: function() {
    var req = new Ajax.Request('productions/' + this.id + '.xml',
        {method: 'get', asynchronous: false}
      );
    var xml = req.transport.responseXML;

    var prodElements = xml.getElementsByTagName('production').item(0);
    var imageSubDir = prodElements.getAttribute('image-sub-dir');
    if (imageSubDir != null) {
      this.imageSubDir = imageSubDir;
    } else {
      this.imageSubDir = this.id;
    }

    var imgList = this.readImages(xml, 'title-image');
    if (imgList.length == 1)
      this.titleImage = imgList[0];

    var imgList = this.readImages(xml, 'head-image');
    if (imgList.length == 1)
      this.headImage = imgList[0];

    this.sections = new Array();
    var elements = xml.getElementsByTagName('section');
    for (var i = 0; i < elements.length; i++) {
      var sectionX = elements.item(i);
      var section = new Production.Section();
      section.title = sectionX.getAttribute('title');
      section.images = this.readImages(sectionX, 'image');
      this.sections.push(section);
    }

    this.images = this.readImages(xml, 'image');

  },

  getDetails: function() {
    if (this.detailsLoaded == false) {
      this.loadDetails();
      this.detailsLoaded = true;
    }
  }

}

Production.Section = Class.create();
Production.Section.prototype = {
  initialize: function() {
  }
}

Production.Image = Class.create();
Production.Image.prototype = {
  initialize: function() {
  },

  populateFromElement: function(element) {
    this.full = element.getAttribute('src');
    if (this.full == null)
      this.full = element.getAttribute('full');
    this.width = element.getAttribute('width');
    this.height = element.getAttribute('height');

    this.thumb = element.getAttribute('thumb');
    this.thumbwidth = element.getAttribute('thumbwidth');
    this.thumbheight = element.getAttribute('thumbheight');
  },

  hasThumb: function() {
    return (this.thumb != null);
  },

  getFullImgPath: function() {
    return Site.imageBase() + this.production.imageSubDir + '/' + this.full;
  },

  getFullImgTagString: function() {
    var str = '<img src="' + this.getFullImgPath().escapeHTML() + '"';
    if (this.width != null && this.height != null) {
      str += ' width="' + this.width + '" height="' + this.height + '"';
    }
    str += ' alt="" />';
    return str;
  },

  getThumbImgPath: function() {
    return Site.imageBase() + this.production.imageSubDir + '/' + this.thumb;
  },

  getThumbImgTagString: function() {
    var str = '<img src="' + this.getThumbImgPath().escapeHTML() + '"';
    if (this.thumbwidth != null && this.thumbheight != null) {
      str += ' width="' + this.thumbwidth + '" height="' + this.thumbheight + '"';
    }
    str += ' class="thumb" alt="" />';
    return str;
  },

  getHTMLImage: function() {
    var htmlImage = new Image();
    htmlImage.src = this.full;
    return htmlImage;
  }
}

var allProductions = null;

function loadProductions() {
  var req = new Ajax.Request('productions.xml',
      {method: 'get', asynchronous: false}
    );
  var xml = req.transport.responseXML;
  var elements = xml.getElementsByTagName('production');
  var productions = new Array();
  for (var x = 0; x < elements.length; x++) {
    var prodX = elements.item(x);
    var prod = new Production();
    prod.title = prodX.getAttribute('title');
    prod.date = prodX.getAttribute('date');
    prod.director = prodX.getAttribute('director');
    prod.asstdir = prodX.getAttribute('asstdir');
    prod.writer = prodX.getAttribute('writer');
    prod.id = prodX.getAttribute('id');
    prod.thumb = prodX.getAttribute('thumb');
    prod.detailsLoaded = false;
    productions.push(prod);
  }
  return productions;
}

function getProductions() {
  if (allProductions == null) {
    allProductions = loadProductions();
  }
  return allProductions;
}

function getProductionById(id) {
  var all = getProductions();
  for (var i = 0; i < all.length; i++) {
    if (all[i].id == id)
      return all[i];
  }
  return null;
}

var Slideshow = Class.create();
Slideshow.counter = 0;
Slideshow.array = [];
Slideshow.prototype = {
  initialize: function(images) {
    this.id = Slideshow.counter++;
    this.name = 'slideshow_' + this.id;

    this.images = images;
    this.current = 0;
  },

  write: function() {
    var out = '<div class="slideshow" id="' + this.name + '">\n';
    out += this.getThumbsDivHTML();

    out += '<div class="screen" id="' + this.name + '_screen">\n';
    out += '<a href="" class="prevButton">&lt; Prev</a>\n';
    out += '||\n';
    out += '<a href="" class="nextButton">Next &gt;</a>\n';
//  out += '||\n';
//  out += '<a href="" class="closeButton">[X] Close</a>\n';
    out += '<div class="view" id="' + this.name + '_screen_view"></div>\n';
    out += '<br class="clear" />\n';
    out += '</div>\n';
    document.write(out);

    Slideshow.array.push(this);
  },

  getThumbsDivHTML: function() {
    var out = '';
    out += '<div class="thumbs">\n';
    for (var i = 0; i < this.images.length; i++) {
      var slideName = '' + this.name + '_thumb_' + i;
      var image = this.images[i];
      image.slideNum = i;
      if (image.hasThumb()) {
        out += '<a href="' + image.getFullImgPath().escapeHTML()
             + '" class="imgLink" id="' + slideName + '">';
        out += image.getThumbImgTagString();
        out += '</a>\n';
      } else {
        out += image.getFullImgTagString();
      }
    }
    out += '<br class="clear" />\n';
    out += '</div>\n';
    return out;
  },

  wire: function() {
      this.tree = $(this.name);
      this.viewDiv = $(this.name + '_screen_view');

      var prev = document.getElementsByClassName('prevButton', this.tree)[0];
      prev.slideshow = this;
      prev.onclick = function() { this.slideshow.previous(); return false; };
      var next = document.getElementsByClassName('nextButton', this.tree)[0];
      next.slideshow = this;
      next.onclick = function() { this.slideshow.next(); return false; };
//    var close = document.getElementsByClassName('closeButton', this.tree)[0];
//    close.slideshow = this;
//    close.onclick = function() { this.slideshow.next(); return false; };

      for (var i = 0; i < this.images.length; i++) {
        var image = this.images[i];
        var element = document.getElementById(this.name + '_thumb_' + image.slideNum);
        element.slideshow = this;
        element.slideshowImageNum = i;
        element.onclick = function() { this.slideshow.go(this.slideshowImageNum); return false; };
      }

      this.go(this.current);
  },

  previous: function() {
    var prev = this.current - 1;
    if (prev < 0)
      prev = this.images.length - 1;
    this.go(prev);
  },

  next: function() {
    var next = this.current + 1;
    if (next >= this.images.length)
      next = 0;
    this.go(next);
  },

  go: function(index) {
    this.current = index;
    var image = this.images[index];
    this.currentImage = image;

    var img = new Image();
    img.src = image.getFullImgPath();

//  alert(img.width + 'x' + img.height);
    var slideshowHeight = xGetComputedStyle(this.tree, 'height', true);
    var thumb0 = $(this.name + '_thumb_0').getElementsByTagName('img')[0];
    var thumb0Top = thumb0.offsetTop;

    this.viewDiv.visibility = 'hidden';
    var thumb = $(this.name + '_thumb_' + index).getElementsByTagName('img')[0];
    var thumbCenter = thumb.offsetTop + thumb.height / 2;
    var y = (thumbCenter - img.height / 2 + thumb0Top);
    if (y < 0) y = 0;
    if (y + img.height > slideshowHeight) y = slideshowHeight - img.height;
    this.viewDiv.style.top = y + 'px';
//  alert(this.viewDiv.style.top);

    this.viewDiv.innerHTML = '<img src="' + image.getFullImgPath().escapeHTML()
        + '" />';
    this.viewDiv.visibility = 'visible';
  }
}
Slideshow.wireAll = function() {
  for (var i = 0; i < Slideshow.array.length; i++) {
    Slideshow.array[i].wire();
  }
}

// functions

function writeHeader(title, caption) {
  var out =
      '<div id="header">'
    + '<a href="' + Site.base() + 'irp/" class="imgLink">'
    + '<img id="headerLogo" src="' + Site.imageBase()
       + 'logo/color_187x150.png" width="187" height="150" />'
    + '</a>'
    + '<h1>' + title + '</h1>'
    + '<hr class="horizon" />'
    + '<p>' + caption + '</p>'
    + '</div>';
  document.write(out);
}

var menuItems = new Array(
  { url: 'irp/',		text: 'News' },
  { url: 'about.html',		text: 'About Us' },
  { url: 'feature.html',	text: 'Featured Volunteer' },
  { url: 'past.html',		text: 'Past Productions' },
//{ url: 'attractions.html',	text: 'Coming Attractions' },
//{ url: 'tickets.html',	text: 'Tickets' },
//{ url: 'auditions.html',	text: 'Upcoming Auditions' },
  { url: 'contribute.html',	text: 'Contribute' },
  { url: 'guestbook.html',	text: 'Guest Book' }
//{ separator: true },
//{ url: 'registration.html',	text: 'IACT Registration' }
);

function writeMenu(selected) {
  var out = '<div id="menu">\n<ul>\n';
  menuItems.each(function(menuItem) {
    if (menuItem.separator) {
      out += '</ul>\n<hr /><ul>\n';
      return;
    }
    if (menuItem.text == selected) {
      out += '<li class="selected">';
    } else {
      out += '<li>';
    }
    out += '<a href="' + Site.base() + menuItem.url + '">'
           + menuItem.text.escapeHTML() + '</a>';
    out += '</li>\n';
  });
  out += '</ul>\n'
  out += '</div>\n';
  document.write(out);
}

function writeFooter() {
  var out =
      '<div id="footer">'
    + 'Copyright &copy;2000-2006 Iowa River Players.'
    + '</div>';
  document.write(out);
}



// xGetComputedStyle, Copyright 2001-2005 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xGetComputedStyle(oEle, sProp, bInt)
{
  var s, p = 'undefined';
  var dv = document.defaultView;
  if(dv && dv.getComputedStyle){
    s = dv.getComputedStyle(oEle,'');
    if (s) p = s.getPropertyValue(sProp);
  }
  else if(oEle.currentStyle) {
    // convert css property name to object property name for IE
    var a = sProp.split('-');
    sProp = a[0];
    for (var i=1; i<a.length; ++i) {
      c = a[i].charAt(0);
      sProp += a[i].replace(c, c.toUpperCase());
    }   
    p = oEle.currentStyle[sProp];
  }
  else return null;
  return bInt ? (parseInt(p) || 0) : p;
}
