Prototype.Browser.IE8 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==8;

var Sitebuilder = window.Sitebuilder||{};

document.observe('dom:loaded', function(){
  if(Prototype.Browser.IE && !Prototype.Browser.IE8){
    $('primary-navigation').immediateDescendants().each(function(li){
      li.observe('mouseenter', function(e){
        li.addClassName('hover');
      });
      li.observe('mouseleave', function(e){
        li.removeClassName('hover');
      });
    });
  }

 $$('li.external a').each(function(link){
   link.observe('click', function(e){
     e.stop();
     window.open(link.href);
   });
 });

});

Sitebuilder.FlashEmbed = Class.create({
  initialize : function(element, options){
    if(!(this.element = $(element))) return;
    this.options = Object.extend({
      minVersion : 8
    }, options ||{});
    this.build();
  },
  build : function(){
    var fls = flashembed.getVersion();
    if(fls[0] && (parseInt(fls[0], 10) > parseInt(this.options.minVersion, 10))){
      var dimensions = this.element.getDimensions();
      this.options.width = this.options.width ? this.options.width : dimensions.width;
      this.options.height = this.options.height ? this.options.height : dimensions.height;
      flashembed(this.element, this.options);
    }
  }
});

Sitebuilder.MediaPlayer = Class.create({
  initialize : function(element, videos, path){
    if(!(this.element = $(element))) return;
    this.path = path;
    this.embed(videos);
  },
  embed : function(videos){
    $A(videos).find(function(video){
      if(this.supported(video.extension)){
        this.embeds[video.extension](this.element, video, this.path);
        return true;
      }else{
        return false;
      }
    }, this);
  },
  supported : function(format){
    switch(format){
      case "flv":
        var fls = flashembed.getVersion();
        return parseInt(fls.join(''), 10);
        break;
      case "wmv":
          return isWMPInstalled();
        break;
      default:
        return false;
        break;
    }
  },
  embeds : {
    flv : function(element, video, path){
      flowplayer(
        element.id,
        '/flowplayer-3.1.2.swf',
        {
          'clip' : {
             'url' : path+'/'+video.id+'.'+video.extension
          },
          canvas : {
            backgroundColor : '#000'
          },
          plugins: {
            controls: flowplayerconf.skins.site
          }
        }
      );
    },
    wmv : function(element, video, path){
      var dimensions = element.getDimensions();
      var wmv = new WMPObject(path+'/'+video.id+'.'+video.extension, "movid", dimensions.width, dimensions.height);
      wmv.write(element.id);
    }
  }
});

Sitebuilder.CollapsiblePanel = Class.create({
  initialize: function(element, options) {
    this.element = $(element);
    this.options = Object.extend({
      triggerClassName : 'module-hd',
      contentClassName : 'module-bd',
      openClassName : 'open',
      closedClassName : 'closed',
      startOpen : false
    }, options||{});

    this.trigger = this.element.down('div.'+this.options.triggerClassName);
    this.content = this.element.down('div.'+this.options.contentClassName);

    this.trigger.observe('click', this.toggle.bindAsEventListener(this));

    if(!this.options.startOpen){
      this.element.addClassName('closed');
      this.content.hide();
    }

  },
  /**
   *  Sitebuilder.CollapsiblePanel() -> undefined
   *
   *
  **/
  toggle : function(){
    this[this.content.visible() ? '_hide' : '_show']();
  },
  _show : function(){
    this.element.removeClassName(this.options.closedClassName);
    this.element.addClassName(this.options.openClassName);
    this.show();
  },
  beforeShow : function(){
    this.content.setStyle({
      'height' : '0'
    }).show();
  },
  show : function(){
    var height = this.content.getHeight();
    this.content.morph('height:'+height+'px;', {
      before : this.beforeShow.bind(this)
    });
  },
  _hide : function(){
    this.element.removeClassName(this.options.openClassName);
    this.element.addClassName(this.options.closedClassName);
    this.hide();
  },
  hide : function(){
    this.content.morph('height:0', {
      after : this.afterHide.bind(this)
    });
  },
  afterHide : function(){
    this.content.hide().setStyle({
      'height' : 'auto'
    });
  }
});

CollapsiblePanel = Class.create({
  initialize: function(element, options) {
    this.element = $(element);
    this.options = Object.extend({
      indicator : true,
      startOpen : true
    }, options||{});

    this.header = $(this.element).select('div.cp-header').first();
    this.content = $(this.element).select('div.cp-content').first();

    this.contentPaddingTop = this.content.getStyle('paddingTop').replace('px','').replace('em','');
    this.contentPaddingBot = this.content.getStyle('paddingBottom').replace('px','').replace('em','');

    this.contentHeight = this.content.getHeight() - (this.contentPaddingTop + this.contentPaddingBot);

    if(this.options.indicator) this.addIndicator();

    this.header.observe('click', this.toggleState.bindAsEventListener(this));

    if(this.element.hasClassName('closed')){
      this.element.removeClassName('open');
      this.content.setStyle({
        'height' : '0',
        'opacity' : 0
      });
    }

    if($$('.filtered').length > 0) {
      this.openPanel();
    } else if (this.options.startOpen) {
        this.openPanel();
    } else {
        this.closePanel();
    }
  },
  closePanel: function() {
    if(this.options.indicator) this.indicator.update('Open');
    this.content.morph('height: 0;').fade();
    if(this.element.hasClassName('open')) {
      this.element.removeClassName('open');
    }
  },
  openPanel: function() {
    if(this.options.indicator) this.indicator.update('Close');
    this.content.morph('height: '+this.contentHeight+'px').appear();
    if(!this.element.hasClassName('open')) {
      this.element.addClassName('open');
    }
  },
  toggleState: function(e) {
    if(this.element.hasClassName('open')) {
      this.closePanel();
    } else {
      this.openPanel();
    }
  },
  addIndicator: function() {
    this.indicator = new Element('a', {'href' : '#', 'class': 'cp-state-ind' }).update("Close");
    Element.insert(this.header, this.indicator);
  }
});

Sitebuilder.JobsTable = function(){
  var tableElement, truncateLength;

  function addListeners(){
    tableElement.observe('click', function(e) {

      if(target = e.findElement('a.more-link')) {
        target.next('span.more-text').toggle();
        target.previous('span.elipsis').toggle();
        target.next('a.less-link').toggle();
        target.toggle();
      }

      if(target = e.findElement('a.less-link')) {
        target.previous('span.more-text').toggle();
        target.previous('span.elipsis').toggle();
        target.previous('a.more-link').toggle();
        target.toggle();
      }

    });
  }

  function truncateSummaries(){
    summaries = tableElement.select('.summary');
    summaries.each(function(el) {
      cleanText = el.innerHTML.stripTags();
      textLen = cleanText.length;
      shortText = cleanText.substring(0, truncateLength)+"<span class='elipsis'>...&nbsp;</span>";
      moreLink = new Element('a', { 'class' : 'more-link' }).update('[more]');
      hiddenText = new Element('span', { 'class' : 'more-text' }).hide().update(cleanText.substring(truncateLength, (textLen-truncateLength))+'&nbsp;');
      lessLink = new Element('a', { 'class' : 'less-link' }).hide().update('[less]');

      el.update(shortText);
      el.insert(moreLink);
      el.insert(hiddenText);
      el.insert(lessLink);
    });
  }

  return {
    initialize : function(element, truncLen){
      truncateLength = truncLen ? truncLen : 100;
      tableElement = $(element);
      truncateSummaries();
      addListeners();
    }
  }
}();
