function _interpolater(y, xMin, yMin, xMax, yMax) {
  return Math.min(xMax,Math.max(xMin, (xMin - xMax) / (yMin - yMax) * (y - yMin) + xMin));
}

function recordOutboundLink(category, action) {
  try {
    var pageTracker = _gat._getTracker("UA-21023741-1");
    pageTracker._trackEvent(category, action);
  }catch(err){}
}

function load() {
    var hardwareTransitions = Modernizr.csstransitions;
    
    var header = document.getElementById('header');
    var body = document.getElementById('body');
    
    if (!hardwareTransitions) {
        header.set('tween', {duration: 'long', transition: Fx.Transitions.Linear});
        body.set('tween', {duration: 'long', transition: Fx.Transitions.Linear});
    }
    
    var opacity = 1.;

    setTimeout(function() {
        if (this.iPhone)
            window.scrollTo(0, 1);
            
        if (hardwareTransitions)
            header.setStyles({'opacity': 1});
        else
            header.tween('opacity', opacity);
        
        setTimeout(function() {
            if (hardwareTransitions)
                body.setStyles({'opacity': 1});
            else
                body.tween('opacity', opacity);
               
        }, 1000);
    }, 500);
}

var PushPopPress = new Class({

  Implements: [Events],
  
  initialize: function() {
    this.iPhone = navigator.userAgent.match(/iPhone|iPod/i);
    this.iPad =  navigator.userAgent.match(/iPad/i);
    
    this.background = $("background");
    this.content = $("content");
    this.header = $("header");
    this.body = $("body");
    this.footer = $("footer");
    
    this.backgroundImageSize = { x: 1095, y: 947 };
    this.playButtonPosition = { x: 350, y: 377 };
    
    window.addEvent('resize', this.onResize.bind(this));
    this.onResize();
  },
  
  onResize: function() {
      
    var ratio, windowSize = window.getSize(),
        styles = {
          right: 0,
          bottom: 0,
          width: this.backgroundImageSize.x,
          height: this.backgroundImageSize.y
        };
    
    var width,
        portrait;
    
    if (this.iPad || this.iPhone)
        portrait = windowSize.y > windowSize.x;
    
    if (this.iPad)
        width = portrait ? 1105 : windowSize.x;
    else if (this.iPhone)
        width = portrait ? 1350 : windowSize.x;    
    else
        width = windowSize.x;
    
    ratio = width / this.backgroundImageSize.x;
    styles.width = width;
    styles.height = this.backgroundImageSize.y * ratio;
    
    if (windowSize.y > styles.height) {
      ratio = windowSize.y / this.backgroundImageSize.y;
      styles.width = this.backgroundImageSize.x * ratio;
      styles.height = windowSize.y;
    }
    
    var halfWindowHeight = windowSize.y / 2,
        halfWindowWidth = windowSize.x / 2,
        playButtonPosition = {
          x: this.playButtonPosition.x * ratio,
          y: this.playButtonPosition.y * ratio
        };
    
    if (halfWindowHeight < playButtonPosition.y) {
        styles.bottom = halfWindowHeight - playButtonPosition.y;
        if (!portrait && this.iPhone) {
            styles.bottom -= 50.;
        }
    }
    
    if (halfWindowWidth < playButtonPosition.x) {
        styles.right = halfWindowWidth - playButtonPosition.x;
    }
    
    this.background.setStyles(styles);

    var fontColor;
    
    if (this.iPad) {
        fontColor = portrait ? '#262626' : '#FFFFFF';
        if (portrait) {
            this.header.setStyles({
                'font-size': 48,
                'color': fontColor 
            });
            this.body.setStyles({
                'width': 475,
                'font-size': '',
                'color': fontColor,
                'display': ''
            });
            this.content.setStyles({
                'top': 41,
                'color' : fontColor,
                'display': ''
            });
        } else {
            this.header.setStyles({
                'font-size': 48,
                'color': fontColor 
            });
            this.body.setStyles({
                'width': 300,
                'font-size': '',
                'color': fontColor,
                'display': ''
            });
            this.content.setStyles({
                'top': 41,
                'color' : fontColor,
                'display': ''
            });
        }
    } else if (this.iPhone) {
        fontColor = portrait ? '#262626' : '#FFFFFF';
        if (portrait) {
            this.body.setStyles({
                'width': '60%',
                'color': fontColor,
                'font-size': 10
            });
            this.content.setStyles({
                'top': 41,
                'color': fontColor,
                'display': ''
            });
            this.footer.setStyles({
                'display': 'block',
                'color': fontColor,
                'bottom': 46
            });
        } else {
            this.header.setStyles({ 'font-size': 33 });
            this.content.setStyles({
                'top': 35,
                'color': fontColor,
                'display': ''
            });            
            this.body.setStyles({
                'font-size': 9,
                'width': 300,
                'color': fontColor,
                'display': ''
            });
            this.footer.setStyles({
                'display': 'block',
                'color': fontColor,
                'bottom': -80
            });
        }
    } else {        
        fontColor = (windowSize.x < 800) ? '#262626' : '#FFFFFF';
        var lockBody = (windowSize.x > 1200) || ((windowSize.x < 875) && (windowSize.y > 480));
        
        this.header.setStyles({
            'font-size': 48,
            'color': fontColor 
        });
        this.body.setStyles({
            'width': lockBody ? 360 : '34%',
            'font-size': '',
            'color': fontColor,
            'display': ''
        });
        this.content.setStyles({
            'top': 41,
            'color' : fontColor,
            'display': ''
        });
        this.footer.setStyles({ display: 'block' });
    }
    
    var links = document.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        links[i].setStyles({
            'color': fontColor 
        });
    }

    setTimeout(function() {
      window.scrollTo(0, 1);
    }, 100);
  }
});

window.addEvent('domready', function() {
  new PushPopPress();
  
  $$('a').addEvent('click', function() {
    recordOutboundLink("Outbound Links", this.href);
  });
});
