/*


Dynamic Application of Functions to DOM
*/
var _debugMode = false;

var Triptych = Class.create({
  initialize: function(){
    document.observe('dom:loaded', this.setup.bind(this));
  },
  setup: function(){
    this.root = $('triptych');
    this.panels = [$('freeWifi'), $('affordableRates'), $('greatLocation')]; // $$('#triptych div div') fails in ie6
    this.top = '36px'; // this.panels.first().getStyle('top');
    this.bottom = 230; // this.root.getHeight() + ';

    if (!this.shouldRun()) return;
    
    this.panels.invoke('setStyle', { top: this.bottom/4 + 'px', opacity: '0' });

    this.fixPanelsForIE();
    
    // this.panels.invoke('hide');
  },
  fixPanelsForIE: function(){
    if (Prototype.Browser.IE)
      this.panels.invoke('setStyle', { backgroundImage: 'url(../images/triptych.jpg)', backgroundPosition: '-5px -40px' });
  },
  unFixPanelForIE: function(panel){
    if (Prototype.Browser.IE)
      panel.setStyle({ backgroundImage: 'none' });
  },
  go: function(){
    cl('window.load: triptych go');
    if (this.shouldRun())
      this.panels.each(function(panel, i){
        panel.morph(
          { top: this.top, opacity: '1' },
          { delay: .5 + (i/3), duration: .75, transition: Effect.Transitions.spring,
            afterFinish: function(){
              createCookie('sole', 'true');
              this.unFixPanelForIE.delay(.1, panel);
            }.bind(this)
          }
        );
      }.bind(this));
    // this.panels.each(function(panel, i){
    //   panel.appear({ delay: i/3, duration: .75 });
    // }.bind(this));
  },
  shouldRun: function(){
    if ($('triptych') && ($(document.body).hasClassName('home')) && (readCookie('sole') != 'true')) {
      return true;
    } else {
      return false;
    }
  }
});



var PageAppear = Class.create({
  initialize: function(){
    document.observe('dom:loaded', this.setup.bind(this));
  },
  setup: function(){
    this.body = $(document.body);
    this.shell = $('shell');
    this.feature = $('feature').setStyle({ opacity: 0 });
  },
  go: function(){
    cl('window.load: PageAppear newGo');
    $('feature').appear({
      duration: .5,
      afterFinish: panAndScan.go.bind(panAndScan)
    });
    tripTych.go();
  }
});

var PanAndScan = Class.create({
  initialize: function(duration, options){
    this.duration = duration;
    this.defaultOptions = $H({ repeat: true, reverse: false });
    this.options = this.defaultOptions.merge(options);
    document.observe('dom:loaded', this.setup.bind(this));
  },
  setup: function(){
    if (this.requiresEffect()){
      this.root = $('featureImagery');
      this.content = $('featureContent');
      this.image = this.root.down();
      [this.image, this.content].invoke('hide').invoke('setStyle', { position: 'absolute' });
    }
  },
  requiresEffect: function(){
    if ($('featureImagery') && $('feature').hasClassName('interior')) return true; else return false;
  },
  go: function(){
    cl('window.load: PanAndScan go');

    if (this.requiresEffect()){
      this.getDimensions();
      this.tryToReverseImage();
      this.effectOptions = { duration: this.duration };
      if (this.options.get('repeat'))
        this.effectOptions.afterFinish = this.alternate.bind(this);
    
      new Effect.Parallel([ Effect.Appear(this.image), Effect.Appear(this.content) ], {
        afterFinish: this.alternate.bind(this)
      });
    }
  },
  tryToReverseImage: function(){
    if (this.options.get('reverse')) this.image.setStyle(this.upperLimitMorph);
  },
  getDimensions: function(){
    var px = 'px';
    this.width = Number(this.image.getStyle('width').strip(px));
    this.height = Number(this.image.getStyle('height').strip(px));
    this.property = (this.width > this.height) ? 'left' : 'top';
    this.lowerLimit = 0;
    if (this.property == 'left')
      this.upperLimit = Number(this.root.getStyle('width').strip(px)) - this.width;
    else if (this.property == 'top')
      this.upperLimit = Number(this.root.getStyle('height').strip(px)) - this.height;

    this.upperLimitMorph = this.property + ': ' + this.upperLimit + px;
    this.lowerLimitMorph = this.property + ': ' + this.lowerLimit + px;
  },
  alternate: function(){
    var morphTo = (this.atLowerLimit()) ? this.upperLimitMorph : this.lowerLimitMorph;
    this.image.morph(morphTo, this.effectOptions );
  },
  atLowerLimit: function(){
    var p = this.image.getStyle(this.property) || '0px';
    if (p == this.lowerLimit + 'px') return true; else return false;
  }
});

var PageFade = Class.create({
  initialize: function(){
    document.observe('dom:loaded', this.setup.bind(this));
    this.linkSelector = '#navPrimary li a';
    this.fadeDuration = .3;
  },
  setup: function(){
    this.body = $(document.body);
    this.shell = $('shell');
    this.feature = $('feature');
    this.shell.observe('click', function(e){
      if (this.realLink($(e.target))){
        this.go(String(e.target.href));
        e.stop();
      }
    }.bind(this));
  },
  go: function(href){
    this.feature.morph({ 'opacity': '0' }, {
      afterFinish: function(){ location.href = href; }.bind(this),
      duration: this.fadeDuration
    });
    this.tryToFadeTriptych(href);
  },
  tryToFadeTriptych: function(href){
    if ((href.split('/').without('').size() == 2) || (href.include('/_template.html')) && (readCookie('sole') != 'true')) {
      tripTych.fixPanelsForIE();
      tripTych.panels.first().up().fade({ duration: this.fadeDuration });
    }
  },
  realLink: function(link){
    // (link.match(this.linkSelector)
    if (
      (link.hasAttribute('href'))
      && (!String(link.href).include('#'))
    ) return true; else return false;
  }
});

// from: http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}



var pageFade = new PageFade();
var pageAppear = new PageAppear();
var tripTych = new Triptych();

if (String(document.location).include('ocation'))
  var panAndScan = new PanAndScan(10, { reverse: true, repeat: false });
else
  var panAndScan = new PanAndScan(20);

// panAndScanGo = function () {
//   this.go()
// }.bind(panAndScan);


Event.observe(window, 'load', function(){
  cl('window.load');
  pageAppear.go();

    // alert($$('#shell', '#navPrimary'));
  
});


Event.observe(window, 'unload', function(){});




