$(document).ready(function() {
  BackgroundSizer.init(
    $($('#background_image')),
    $($(window))
  );

  $('#popup_links a, .open_in_popup').click(function(event) {
    event.preventDefault();
    var popup_url = $(this).attr('href');
    var popup_content = $('#popup_content');
    popup_content.html('');
    popup_content.load(popup_url, function() {
      popup_content.css('height', $('#popup_window').height() - 110);
    });
    $('#popup_wall').fadeIn('fast');
    $('#popup_window').fadeIn('fast');
  });

  $('#popup_close a').click(function(event) {
    event.preventDefault();
    $(this).closest('.popup_container').hide();
    $('#popup_wall').fadeOut('fast');
  });

  //show items after a delay
  /* The initial delay is "500", seen below */
  setTimeout(function() {
    Slider.showSponsors();
  },
  2200);
});

$(window).load(function() {
  BackgroundSizer.resize();

  Slider.init(
    $('#control'),
    $('#items')
  );
});

Slider = {
  control: null,
  content: null,
  hidden_margin: null,
  init: function(control_el, content_el) {
    this.control = control_el;
    this.content = content_el;
    this.hidden_margin = $(content_el).css('marginRight');

    this._bindActions();
  },

  _bindActions: function() {
    var parentObj = this;
    this.control.click(function() {
      if(parentObj.content.css('marginRight') === '0px') {
        parentObj.hideSponsors();
      } else {
        parentObj.showSponsors();
      }
    });
  },

  showSponsors: function() {
    /* 
    * Change the value below margin (default 1000)
    * to change the animation speed
    */
    var parentObj = this;
    $(this.content).animate(
      {'margin-right': '0px'}, 
      1000,
      null,
      function() {
        parentObj.control.html('&gt;');
      }
    );
  },
  hideSponsors: function() {
    var parentObj = this;
    $(this.content).animate(
      {'margin-right': parentObj.hidden_margin},
      300,
      null,
      function() {
        parentObj.control.html('&lt;');
      }
    );
  }
};

BackgroundSizer = {
  image: null,
  window: null,
  interval: null,
  last_win_width: 0,
  target_width: null,
  target_height: null,

  init: function(image_jq, window_jq) {
    this.image = image_jq;
    this.window = window_jq;


    this.bindResize();
  },
  bindResize: function(duration) {
    var parentObj = this;
    this.window.resize(function() {
      if(parentObj.last_win_width !== parentObj.window.width()) {
        parentObj.resize();
      }
      $('#popup_content').css('height', $('#popup_window').height() - 110);
      parentObj.last_win_width = parentObj.window.width();
    });
  },
  resize: function() {

    this.win_width = this.window.width();
    this.win_height = this.window.height();

    this.image.css('width', this.win_width);

    var img_width = this.image.width();
    var img_height = this.image.height();

    var width = this.win_width;

    while(img_width < this.win_width ||
          img_height < this.win_height) {

      width ++;
      this.image.css('width', width);
      img_width = this.image.width();
      img_height = this.image.height();
    }
  }
};

