// global window handle variable
var termsWindow = null;
// new window launching function
function launchHRef(el) {
  if ($defined(el.get('href'))) {
    var url = el.get('href');
    el.erase('href');
    el.setStyle('cursor', 'pointer');
    el.addEvent('click', function(_url){
      termsWindow = window.open(_url, 'terms', 'width=410,height=500,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0');
      if ($defined(termsWindow.focus))
        termsWindow.focus();
    }.pass(url));
  }
}

// tertiary offer heading lining up fixer
var FixTertiaryHeadings = new Class ({

  initialize: function(elements) {
    this.elements = [];
    if ($type(elements) == 'string')
      this.elements = $$(elements);
    if ($type(elements) == 'array')
      this.elements = elements;
      
    this.fixHeights.delay(500, this);
  }
  
, fixHeights: function() {
    this.heights = [];
    this.elements.each(function(el,ix){
      this.push({ 'el': el, 'height': el.getStyle('height').toInt()});
    }.bind(this.heights));
    
    for (hx=0, hMax=this.heights.length; hx < hMax; hx += 2)
    {
      if ($defined(this.heights[hx+1])) {
        if (this.heights[hx].height > this.heights[hx+1].height) {
          this.heights[hx+1].el.setStyle('margin-top', (this.heights[hx].height - this.heights[hx+1].height) + 'px');
        }
        if (this.heights[hx].height < this.heights[hx+1].height)
          this.heights[hx].el.setStyle('margin-top', (this.heights[hx+1].height - this.heights[hx].height) + 'px');
      }
    }
  }
});


// pass in the id of the parent
var pSlider = new Class({
  Implements: Options,
  options: {
    elTrigger: false // the element that triggers the slider
  , tagChild: false // the child tag that we are going to look for that will be actually sliding in and out
  , tagButton: false // the child tag that we are going to look for that is the 'slideout' button (that doesn't actually do anything)
  }
, initialize: function(options) {
    this.setOptions(options);
    // console.log(this.options.elTrigger);
    if ($type(this.options.elTrigger) == 'element')
    {
      this.elSliding = this.options.elTrigger.getElement(this.options.tagChild);
      //this.objSlide = this.elSliding;
      this.objSlide = new Fx.Slide(this.elSliding, { 'duration': 200 } );
      
      // set the css diplay value for the sliding element
      this.elSliding.setStyles({'display': 'block'});
      // fix the height of the triggering element
      this.options.elTrigger.setStyles({'height': '145px'});
      // set the css display value for the button
      this.options.elTrigger.getChildren(this.options.tagButton).each(function(el,ix){ el.setStyles({'display': 'block'}); });
      
      // now set up the slider
      this.objSlide.hide();
      this.options.elTrigger.addEvent('mouseenter', function(event){
        this.objSlide.pause();
        this.objSlide.slideIn();
      }.bind(this));
      this.options.elTrigger.addEvent('mouseleave', function(){
        this.objSlide.pause();
        this.objSlide.slideOut();
      }.bind(this));
      this.elSliding.addEvent('mouseleave', function(){
        this.objSlide.pause();
        this.objSlide.slideOut();
      }.bind(this));
    }
  }
});

