document.observe("dom:loaded", function() {
  // print this page
  if ($('aside-print')) {
    $('aside-print').observe('click', function(event) {
      window.print();
      Event.stop(event);
    });
  }

  // email to a friend
  if ($('aside-email')) {
    $('aside-email').observe('click', function(event) {
      var width  = 580;
      var height = 680;
			// var left = (screen.width - width)/2;
			// var top = (screen.height - height)/2;
      newwin = window.open(this.href, "emailafriend", "status=no,toolbar=no,resizable=yes,scrollbars=1,width="+width+",height="+height);
      if (window.focus) { newwin.focus(); }
      Event.stop(event);
    });
  }

  /* [ product detail pages ]
  -------------------------------------------- */
  if( $('product-info') ){

  	// tab navigation
  	// -----------------------------
  	var productTabs = $('product-info-tabs');
  	var productTabContent = $('product-info-content');
  	var TAB_IS_ANIMATING = false;
    var p = 0;

    $$('#product-info-content li').each(function(e){ if (p++) e.hide(); });
  	productTabs.down(':first-child a').addClassName('on');
  	//productTabContent.down(':first-child').show();
//  	productTabContent.setStyle({height:productTabContent.down().getHeight()+'px'});

  	$$('#product-info-tabs a').each(function(e){
  		e.observe('click', function(event) {
        var element = event.element();
        if (!element.hasClassName('on')) {
          $$('#product-info-content li').each(function(e){ e.hide(); });
//          productTabContent.down().hide();
          $(element.readAttribute('href').substr(1)).show();
          productTabs.down('.on').removeClassName('on');
          element.addClassName('on');
        }
  			Event.stop(event);
	    });
  	});


  	// product scroller
  	// -----------------------------
  	var productScrollerWrap = $('product-scroller');
  	var productScrollerNav = productScrollerWrap.down('ul.nav');
  	var productScrollerContent = productScrollerWrap.down('ul#scroll');
  	var numProductPanels = productScrollerContent.childElements().length;
  	var PANEL_IS_ANIMATING = false;

  	if ($$('#product-scroller ul.nav :first-child a').length) {
      productScrollerNav.down(':first-child a').addClassName('on');
      productScrollerContent.select('li:nth-child(1n+2)').each(function(element){
        element.hide();
      });
    }

  	$$('#product-scroller .nav a').each(function(e){
  		e.observe('click', function(event) {
  			if( ! PANEL_IS_ANIMATING ){
  				var element = event.element();
	  			if( ! element.hasClassName('on') ){
						PANEL_IS_ANIMATING = true;
	  				var index = $$('#product-scroller .nav li').indexOf(element.up());
	  				var targetPanel = productScrollerContent.select('li:nth-child('+(index+1)+')')[0];

	  				productScrollerContent.select('li.on').each(function(element){
	  					element.removeClassName('on');
	  				});
	  				targetPanel.addClassName('on');
	  				new Effect.Appear(targetPanel, { duration: 0.4 });
	  				productScrollerContent.select('li:not(.on)').each(function(element){
	  					new Effect.Fade(element, { duration: 0.4, afterFinish: function(){
			  				PANEL_IS_ANIMATING = false;
			  				element.hide();
			  			}});
	  				});

	  				productScrollerNav.down('.on').removeClassName('on');
		  			element.addClassName('on');
	  			};
	  		};
	  		Event.stop(event);
	  	});
	  });

  };

});

