if (document.getElementById('purchase_block')) {
    document.getElementById('purchase_block').style.visibility = 'hidden';
}

window.onload = function(){
  /* Init shadowbox */
  if (typeof Shadowbox == 'undefined') {
  } else {
    Shadowbox.init({});
  }

  if (document.getElementById('purchase_block')) {
    $('purchase_block').style.top = ($('product_sidebar').clientHeight + 115) + 'px';
    $('purchase_block').style.visibility = 'visible';
  }
  
  if ($('size_unit_changer') && $('variant_prices')) {
    size_options = $$('#size_unit_changer input');
    for (var i = 0; i < size_options.length; i++) {
      input = size_options[i];
      input.onclick = function(){

        product_sizes_cm = $$('#variant_prices .sizes_cm');
        product_sizes_inches = $$('#variant_prices .sizes_inches');
        
        /* Make sure there's something to change */
        if (product_sizes_cm != '' && product_sizes_inches != '') {
          if (this.value == 'cm') {
            product_sizes_cm.each(Element.show);
            product_sizes_inches.each(Element.hide);
          } else if (this.value == 'inches') {
            product_sizes_cm.each(Element.hide);
            product_sizes_inches.each(Element.show);
          }
        }
      }
    }
  }
  
  if ($('navigation')) {
	dropdowns = $$('#navigation .dropdown');
	dropdowns.each(Element.hide);
	
	navigation_links = $$('#navigation a.section');
	navigation_links.invoke('observe', 'mouseover', function() {
      this.addClassName('highlighted');
      $(this.id + '_dropdown').show();
	});
	navigation_links.invoke('observe', 'mouseout', function() {
	  this.removeClassName('highlighted');
	  $(this.id + '_dropdown').hide();
	});
	
	dropdowns.invoke('observe', 'mouseenter', function() {
	  link_id = this.id.replace('_dropdown', '');
      if (!$(link_id).hasClassName('highlighted')) {
	    $(link_id).addClassName('highlighted');
	    this.show();
      }
	});
	dropdowns.invoke('observe', 'mouseleave', function() {
	  link_id = this.id.replace('_dropdown', '');
      if ($(link_id).hasClassName('highlighted')) {
	    $(link_id).removeClassName('highlighted');
	    this.hide();
      }
	});	
	
	dropdown_links = $$('#navigation .sub_collections a');
	dropdown_links.invoke('observe', 'mouseover', function (event) {
      if ($(this.id + '_image')) {
    	$$('#navigation .dropdown .default_image').invoke('hide');
        $(this.id + '_image').show();
      }
	});
	dropdown_links.invoke('observe', 'mouseout', function (event) {
	  if ($(this.id + '_image')) {
		$$('#navigation .dropdown .default_image').invoke('show');
		$(this.id + '_image').hide();
	  }
	});	
  }
  
}

window.onscroll = function(){
  if (document.getElementById('purchase_block')) {
	var topGap = 0;
	var scrollTop = document.documentElement.scrollTop + document.body.scrollTop;
	
    if (scrollTop > $('product_options_container').clientHeight) {
      topGap = scrollTop;
    } else {
      topGap = $('configure_intro').clientHeight + scrollTop;
    }
    
    if (topGap > ($('product_options_container').clientHeight - $('purchase_block').clientHeight)) {
      topGap = $('product_options_container').clientHeight - $('purchase_block').clientHeight;
    }
    
    $('purchase_block').style.top = topGap + 'px';
  }

  if (document.getElementById('package_purchase_container')) {
    var topGap = 0;
    var scrollTop = document.documentElement.scrollTop + document.body.scrollTop;

    if (scrollTop > $('package_configure_container').clientHeight) {
      topGap = scrollTop;
    } else {
      topGap = scrollTop;
    }

    if (topGap > ($('package_configure_container').clientHeight - $('package_purchase_container').clientHeight)) {
      topGap = $('package_configure_container').clientHeight - $('package_purchase_container').clientHeight;
    }
	    
    $('package_purchase_container').style.top = topGap + 'px';
  }
}