if (typeof LowPro == "undefined") {
  LowPro = {};
  LowPro.Version = '0.5';
  LowPro.CompatibleWithPrototype = '1.6';

  if (Prototype.Version.indexOf(LowPro.CompatibleWithPrototype) != 0 && console && console.warn) {
    console.warn("This version of Low Pro is tested with Prototype " + LowPro.CompatibleWithPrototype + " it may not work as expected with this version (" + Prototype.Version + ")");
  }

  if (!Element.addMethods) {
    Element.addMethods = function(o) { Object.extend(Element.Methods, o) };
  }

  // Simple utility methods for working with the DOM
  DOM = {};

  // DOMBuilder for prototype
  DOM.Builder = {
    tagFunc : function(tag) {
      return function() {
        var attrs, children; 
        if (arguments.length>0) { 
          if (arguments[0].nodeName || typeof arguments[0] == "string") 
            children = arguments;
          else { 
            attrs = arguments[0]; 
            children = Array.prototype.slice.call(arguments, 1); 
          };
        }
        return DOM.Builder.create(tag, attrs, children);
      };
    },
    create : function(tag, attrs, children) {
      attrs = attrs || {}; children = children || []; tag = tag.toLowerCase();
      var el = new Element(tag, attrs);
      for (var i=0; i<children.length; i++) {
        if (typeof children[i] == 'string')  {
          children[i] = document.createTextNode(children[i]);
        }
        el.appendChild(children[i]);
      }
      return $(el);
    }
  };

  // Automatically create node builders as $tagName.
  (function() { 
    var els = ("p|div|span|strong|em|img|table|tr|td|th|thead|tbody|tfoot|pre|code|h1|h2|h3|h4|h5|h6|ul|ol|li|form|input|textarea|legend|fieldset|select|option|blockquote|cite|br|hr|dd|dl|dt|address|a|button|abbr|acronym|script|link|style|bdo|ins|del|object|param|col|colgroup|optgroup|caption|label|dfn|kbd|samp|var").split("|");
    var el, i=0;
    while (el = els[i++]) {
      window['$' + el] = DOM.Builder.tagFunc(el);
    }
  })();

  DOM.Builder.fromHTML = function(html) {
    var root;
    if (!(root = arguments.callee._root)) {
      root = arguments.callee._root = document.createElement('div');
    }
    root.innerHTML = html;
    return root.childNodes[0];
  };

  // Wraps the 1.6 contentloaded event for backwards compatibility
  //
  // Usage:
  //
  // Event.onReady(callbackFunction);
  Object.extend(Event, {
    onReady : function(f) {
      if (document.body) f();
      else document.observe('dom:loaded', f);
    }
  });
}

if (typeof VideoPlaza == "undefined") {
  var VideoPlaza = {};
}
VideoPlaza.namespace = function() {
  var a=arguments, o=null, i, j, d;
  for (i=0; i<a .length; i=i+1) {
    d = a[i].split(".");
    o = window;
    for (j=0; j<d.length; j=j+1) {
      o[d[j]]=o[d[j]] || {};
      o=o[d[j]];
    }
  }
  return o;
};

VideoPlaza.Tabs = Class.create({
  initialize: function(container, options) {
    if (!(this.container = $(container))) { return; }
    this.container.identify();
    this.links      = $A([]);
    this.tabs = $H({});
    this.activeTab  = false;
    this.activeLink = false;
    this.options = Object.extend({
      activeClassName:  'active',
      defaultTab:       'first',
      showFunction:     Element.show,
      hideFunction:     Element.hide,
      setClassOnContainer: false,
      appendBrowseLinks:   false,
      useLinkValuesForBrowseLinks: true
    }, options || {});

    $(container).select('div.tab').each(function(tab) {
      var link = $a({
        'href' : '#' + tab.identify()
      }, (tab.readAttribute('title') != null ? tab.readAttribute('title') : (tab.select('h1, h2, h3, h4, h5, h6').first().innerHTML ? tab.select('h1, h2, h3, h4, h5, h6').first().innerHTML : "Nothing")));
      tab.writeAttribute('title', '');
      this.addTab(link);
    }.bind(this));

    this._setup();

    if (this.options.defaultTab == 'first') {
      this.setActiveTab(this.links.first());
    } else if (this.options.defaultTab == 'last') {
      this.setActiveTab(this.links.last());
    } else {
      this.setActiveTab(this.options.defaultTab);
    }
  },
  
  addTab: function(link, container) {
    link.key = container ? container.identify() : link.getAttribute('href').replace(window.location.href.split('#')[0],'').split('/').last().replace(/#/,'');
    this.links.push(link);
    this.tabs.set(link.key, $(link.key));
    Event.observe(link, 'click', function(event) {
      this.setActiveTab(event.element('a'));
      event.stop();
    }.bind(this));
  },
  
  setActiveTab: function(link) {
    if (!link && typeof(link) == 'undefined')
      return;
    if (typeof(link) == 'string') {
      this.setActiveTab(this.links.find(function(_link) {
        return _link.key == link;
      }));
    } else if (typeof(link) == 'number') {
      this.setActiveTab(this.links[link]);
    } else {
      if (this.activeContainer) {
        this.options.hideFunction(this.activeContainer);
      }
      this.links.each(function(item) {
        (this.options.setClassOnContainer ? $(item.parentNode) : item).removeClassName(this.options.activeClassName);
      }.bind(this));
      (this.options.setClassOnContainer ? $(link.parentNode) : link).addClassName(this.options.activeClassName);
      this.activeContainer = this.tabs.get(link.key);
      this.activeLink = link;
      this.options.showFunction(this.activeContainer);
    }
  },
  
  next: function(event) {
    this.links.each(function(link, i) {
      if (this.activeLink == link && this.links[i + 1]) {
        this.setActiveTab(this.links[i + 1]);
        throw $break;
      }
    }.bind(this));
    event.stop();
  },
  
  previous: function(event) {
    this.links.each(function(link, i) {
      if (this.activeLink == link && this.links[i - 1]) {
        this.setActiveTab(this.links[i - 1]);
        throw $break;
      }
    }.bind(this));
    event.stop();
  },  
  
  _setup: function() {
    this.tabs.values().invoke('hide');

    var list = $ul({});

    tabs_navigation = $div({
      'class' : 'navigation'
    }, list);

    var header = $$('#'+this.container.identify() + ' > h1:first-child, #' + this.container.identify() + ' > h2:first-child').first();
    if (header) { Element.insert(tabs_navigation, { 'top' : header }); }
    Element.insert(this.container, {
      'top' : tabs_navigation
    });
    
    this.links.each(function(link, index) {
      Element.insert(list, {
        'bottom' : $li({
          'class' : 'tab_nav_' + (index + 1)
        }, link)
      });
      if (this.options.appendBrowseLinks) {
        var container = this.tabs.get(link.key);

        var next_link = $a({ 'href' : '#', 'class' : 'next' }, 'Next »');
        var prev_link = $a({ 'href' : '#', 'class' : 'previous' }, '« Previous');
        next_link.observe('click', this.next.bindAsEventListener(this));
        prev_link.observe('click', this.previous.bindAsEventListener(this));

        if (link == this.links.last()) {
          var appendment = $p({
            'class' : 'browse_links'
          }, (this.options.useLinkValuesForBrowseLinks ? prev_link.update(this.links[index - 1].innerHTML) : prev_link));
        } else if (link != this.links.first()) {
          var appendment = $p({
            'class' : 'browse_links'
          }, (this.options.useLinkValuesForBrowseLinks ? prev_link.update(this.links[index - 1].innerHTML) : prev_link), ' ', (this.options.useLinkValuesForBrowseLinks ? next_link.update(this.links[index + 1].innerHTML) : next_link));
        } else {
          var appendment = $p({
            'class' : 'browse_links'
          }, (this.options.useLinkValuesForBrowseLinks ? next_link.update(this.links[index + 1].innerHTML) : next_link));
        }

        Element.insert(container, { 'bottom' : appendment });
      }
    }.bind(this));

    this.container.addClassName('tabbed');
  }
});

if (Prototype.Browser.IE) {
   Event.observe(window, 'load', function() {
      $$('#intro.tabable').each(function(tabable) {
         new VideoPlaza.Tabs(tabable, { setClassOnContainer : true });
      });
      $$('#features.tabable').each(function(tabable) {
         new VideoPlaza.Tabs(tabable, { setClassOnContainer : true, appendBrowseLinks : true });
      });
   });
} else {
   $(document).observe('dom:loaded', function() {
      $$('#intro.tabable').each(function(tabable) {
         new VideoPlaza.Tabs(tabable, { setClassOnContainer : true });
      });
      $$('#features.tabable').each(function(tabable) {
         new VideoPlaza.Tabs(tabable, { setClassOnContainer : true, appendBrowseLinks : true });
      });
   });
}