// original window dimensions, we need these for when the user resizes the browser to a bigger size
// but then back down again. Because the new min dimesions for the window will be the current large
// resized size as the overlay's new size will force the scroll dimensions to that, and not back to its
// orginal size
var oWinHeight = 0;

var iframe = '';
var opacityLayer = '';
var containerLayer = '';

// fade overlay and container 
var overlayOpacity = '';
var containerOpacity = '';

function hideCart() {
  overlayOpacity.start('0.8', 0);  
  hideContainer();
}

function hideContainer() {
  containerOpacity.start('0.8', 0);
}

function showOverlay() {
  if (opacityLayer.getStyle('opacity') == 0) {
    overlayOpacity.start(0, '0.8');  
  }
}

function showContainer() {  
  var scroll = new Fx.Scroll(window).toTop();
  containerOpacity.start(0, 1); 
}

function iframeLoadPage(page) {
  showOverlay();
  $('ps-cart_iframe').src=carturl+'?page='+page+'&print=1&execShowContainer=1';
  (function() {showContainer();}).delay('400'); // ie needs a little time to pull the styles in
}

window.addEvent('domready', function() {
  // initialise important elements
  opacityLayer = $('ps-cart_overlay');
  containerLayer = $('ps-cart_container');
  iframe = $('ps-cart_iframe');
  
  // initialise opacity sliders, note: hide them to start off
  overlayOpacity = new Fx.Style('ps-cart_overlay', 'opacity', {duration:500}).hide();
  containerOpacity = new Fx.Style('ps-cart_container', 'opacity', {duration:500}).hide();
  
  // now change their visibility
  opacityLayer.setStyle('display','block');
  containerLayer.setStyle('display','block');
  
  // set original window dimensions
  oWinHeight = window.getScrollHeight();
  opacityLayer.setStyle('height', oWinHeight+'px');
  
  // add button actions
  $ES('.ps-cart_hide').each(function(e){
    e.addEvent('click',function(event){
      event = new Event(event).stop(); 
      hideCart();
    });
  });
  $ES('.ps-cart_goCart').each(function(e){
    e.addEvent('click',function(event){
      event = new Event(event).stop();
      iframeLoadPage('shop/cart');
    });
  });
  $ES('.ps-cart_toCheckout').each(function(e){
    e.addEvent('click',function(event){
      event = new Event(event).stop();
      document.location=carturl+'?page=shop/complete_quote';
    });
  });
});

// resize the overlay everytime the window is resized, allow for the windows max scroll to shrink down to
// its original
window.addEvent('resize',function(){
  opacityLayer.setStyle('height',(window.getHeight() > oWinHeight) ? window.getHeight() : oWinHeight); 
});