var isIE = false
var offsetCenter = 320; // spacing in px from window-center to the right
var fixedBannerHeight; // need to store the value because firefox can't handle height-detection after changing position:absolute via .css() method

if ($.browser.msie && ($.browser.version == "6.0"))
{
  isIE = true;
}



function positionPotatoe() {
  
  fixedBannerHeight = $('.overlay_banner2').height();
  $('.overlay_banner1,.overlay_banner2').css('right','auto');
  $('.overlay_banner1,.overlay_banner2').css('left',offsetCenter + $(window).width()/2);

  if(isIE)
  {
    $('.overlay_banner1,.overlay_banner2').show();
    return;
  }
  
  if ($(window).height() - ($('.overlay_banner1').position().top + $('.overlay_banner1').height()) < $('.overlay_banner2').outerHeight()) {
    
    $('.overlay_banner2').css('position','absolute');
    $('.overlay_banner2').css('bottom','auto');
    $('.overlay_banner2').css('top', $('.overlay_banner1').position().top + $('.overlay_banner1').height() );
  }
  else
  {
    // do nothing, keep CSS Default Settings
  }
  $('.overlay_banner1,.overlay_banner2').show();
};



$(window).resize(function () {
  
  
  // maintain default offset for absolute and fixed elements
  $('.overlay_banner1,.overlay_banner2').css('left',offsetCenter + $(window).width()/2);
  
  if(isIE)
  {
    return;
  }
  
  
  // collision detection for upper and lower banner box while resize
  if ($(window).height() - ($('.overlay_banner1').position().top + $('.overlay_banner1').height()) + f_scrollTop() > fixedBannerHeight) {
    $('.overlay_banner2').css('position','fixed');
    $('.overlay_banner2').css('top', 'auto');
    $('.overlay_banner2').css('bottom', '0');
  } else {
    $('.overlay_banner2').css('position','absolute');
    $('.overlay_banner2').css('bottom','auto');
    $('.overlay_banner2').css('top', $('.overlay_banner1').position().top + $('.overlay_banner1').height());
  }
})


$(window).scroll(function () {

  // collision detection for upper and lower banner box while scrolling

  if(isIE)
  {
    return;
  }
  
  if ($(window).height() - ($('.overlay_banner1').position().top + $('.overlay_banner1').height()) + f_scrollTop() > fixedBannerHeight) {

    $('.overlay_banner2').css('position','fixed');
    $('.overlay_banner2').css('top', 'auto');
    $('.overlay_banner2').css('bottom', '0');
  } else {
    $('.overlay_banner2').css('position','absolute');
    $('.overlay_banner2').css('bottom','auto');
    $('.overlay_banner2').css('top', $('.overlay_banner1').position().top + $('.overlay_banner1').height());
  }
});


// jQuery scrollOffset is not working reliable. following lines are used to calculate offset-top
function f_scrollTop() { 
  return f_filterResults (
    window.pageYOffset ? window.pageYOffset : 0,
    document.documentElement ? document.documentElement.scrollTop : 0,
    document.body ? document.body.scrollTop : 0
  );
}
function f_filterResults(n_win, n_docel, n_body) {
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel)))
    n_result = n_docel;
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

