// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* swaps the detail image on the products page */
function swapDetailImage(image_url) {
    $('detail_image').src = image_url;
}

/* Wrapper to call ajax simply from flash */
function callAjax(url) {
    new Ajax.Request(url.replace('&amp;', '&'), {asynchronous:true, evalScripts:true});
    
    // Horrible hack for IE 6: sometimes onload doesn't get called if callAjax
    // is called too early.
    if ( self.body_onload ) {
    	body_onload();
    }
}

/* */
function openWindow(url, name, width, height) {
    opt = 'toolbar=no,menubar=no,scrollbars,width='+width+',height='+height
    popupWin = window.open(url, name, opt)
	if (popupWin.opener == null) popupWin.opener = window;
	popupWin.focus();
}


function setFormValue(id, value) {
    $(id).value = value;
}

if( ! (/flash_override=true/.test(document.cookie)) && !DetectFlashVer(8, 0, 0)) {
	location.href = "/store/compatibility_notice";
}

/*
	Used by the shopping cart to populate the shipping info with the billing
*/	
function populate_shipping_with_billing(toggle) {
	
		if(toggle) {
			setFormValue('shipping_address_first_name', $('billing_address_first_name').value);
			setFormValue('shipping_address_last_name', $('billing_address_last_name').value);
			setFormValue('shipping_address_street1', $('billing_address_street1').value);
			setFormValue('shipping_address_street2', $('billing_address_street2').value);
			setFormValue('shipping_address_city', $('billing_address_city').value);
			setFormValue('shipping_address_state', $('billing_address_state').value);
			setFormValue('shipping_address_postal_code', $('billing_address_postal_code').value);
			setFormValue('shipping_address_country', $('billing_address_country').value);
			setFormValue('shipping_address_phone_number', $('billing_address_phone_number').value);
		}
		
		$('shipping_address_first_name').disabled = toggle;
		$('shipping_address_last_name').disabled = toggle;
		$('shipping_address_street1').disabled = toggle;
		$('shipping_address_street2').disabled = toggle;
		$('shipping_address_city').disabled = toggle;
		$('shipping_address_state').disabled = toggle;
		$('shipping_address_postal_code').disabled = toggle;
		$('shipping_address_country').disabled = toggle;
		$('shipping_address_phone_number').disabled = toggle;
		
}

function get_shipping_quote() {
	$('shipping_cost').innerHTML='calculating...'; 
	new Ajax.Request('/cart/shipping_quote/?shipping_method_id=' + $('cart_shipping_method_id').value + '&postal_code=' + $('shipping_address_postal_code').value + 
		'&country=' + $('shipping_address_country').value +
		'&state=' + $('shipping_address_state').value, {asynchronous:true, evalScripts:true});
}



/* Product page */
var selected_detail_image = null;
var detail_image_thumb_width = 66+28;
var detail_images_list_current = 0;
var detail_images_list_length = 0;
var detail_images_list_effect = null;

function initialize_detail_images () {
  var detail_image = $('detail_image');
  var magnify = $('detail_image_magnify'),
    huge_img = magnify.down('img');
  var offset = detail_image.cumulativeOffset();
  var over_header = false, enter, leave;
  
  $('detail_images_list_left').onclick = function () {
    detail_images_list_move(-1)
  }
  
  $('detail_images_list_right').onclick = function () {
    detail_images_list_move(1)
  }
  
  var thumbs = $$('#detail_images_slider .detail_images_thumb');
  if ( thumbs.length ) {
    detail_image_thumb_width = thumbs[0].offsetWidth;
    $('detail_images_slider').style.width
      = detail_image_thumb_width*thumbs.length + "px";
    detail_images_list_length = thumbs.length;
  }
  
  switch_detail_image($('detail_images_slider').down());
  detail_images_list_move(0);
  
  function hide () {
    magnify.style.visibility = "hidden";
    $('detail_image_note').style.visibility = "hidden";
  }
  
  if ( Prototype.Browser.IE ) {
    enter = "mouseenter";
    leave = "mouseleave";
  } else {
    enter = "mouseover";
    leave = "mouseout";
  }
  
  $('header').observe(enter, function () { over_header = true; hide(); });
  $('header').observe(leave, function () { over_header = false; });
  
  Event.observe(document, 'mousemove', function ( event ) {
    var width = detail_image.offsetWidth,
      height = detail_image.offsetHeight,
      x = event.pointerX() - offset.left,
      y = event.pointerY() - offset.top,
      max_x_range = huge_img.offsetWidth - magnify.offsetWidth,
      max_y_range = huge_img.offsetHeight - magnify.offsetHeight;
    
    if ( over_header
        || ( huge_img.offsetWidth <= width + 10
          && huge_img.offsetHeight <= height + 10 ) ) {
      hide();
      return;
    }
    
    $('detail_image_note').style.visibility = "visible";
    
    if ( x < -20 || x > width + 20 ) {
      magnify.style.visibility = "hidden";
      return;
    } else if ( x < 0 ) {
      x = 0;
    } else if ( x > width ) {
      x = width;
    }
    
    if ( y < -20 || y > height + 20 ) {
      magnify.style.visibility = "hidden";
      return;
    } else if ( y < 0 ) {
      y = 0;
    } else if ( y > height ) {
      y = height;
    }
    
    magnify.style.visibility = "visible";
    
    huge_img.style.left = Math.floor(-max_x_range*x/width) + "px";
    huge_img.style.top = Math.floor(-max_y_range*y/height) + "px";
    
    magnify.style.left = (width-magnify.offsetWidth)*x/width + "px";
    magnify.style.top = (height-magnify.offsetHeight)*y/height + "px";
  });
}

function switch_detail_image ( thumb ) {
  if ( selected_detail_image ) {
    selected_detail_image.removeClassName("selected");
  }
  
  selected_detail_image = thumb;
  selected_detail_image.addClassName("selected");
  
  $('detail_image').down("img").src = thumb.getAttribute("big_url");
  $('detail_image_magnify').down("img").src = thumb.getAttribute("huge_url");
}

function switch_detail_image_item ( item_id ) {
  var thumb = $('detail_images_thumb_' + item_id + '_1'), node, i = 0;
  
  node = thumb;
  while ( node = node.previous() ) {
    i++;
  }
  
  switch_detail_image(thumb);
  
  if ( detail_images_list_current > i ) {
    detail_images_list_move(i - detail_images_list_current);
  } else if ( detail_images_list_current + 3 < i ) {
    detail_images_list_move(i - detail_images_list_current - 3);
  }
}

function detail_images_list_move ( delta ) {
  var slider = $('detail_images_slider');
  
  detail_images_list_current += delta;
  
  if ( detail_images_list_current <= 0 ) {
    detail_images_list_current = 0;
    $('detail_images_list_left').style.visibility = "hidden";
  } else {
    $('detail_images_list_left').style.visibility = "visible";
  }
  
  if ( detail_images_list_current >= detail_images_list_length - 4 ) {
    detail_images_list_current = Math.max(detail_images_list_length - 4, 0);
    $('detail_images_list_right').style.visibility = "hidden";
  } else {
    $('detail_images_list_right').style.visibility = "visible";
  }
  
  if ( detail_images_list_effect ) {
    detail_images_list_effect.cancel();
  }
  
  detail_images_list_effect = new Effect.Move(slider, {
    x: - detail_images_list_current*detail_image_thumb_width
      - slider.offsetLeft,
    duration: 0.2,
    afterFinish: function () {
      detail_images_list_effect = null;
    }
  });
}

function show_in_place_of_tabs ( name ) {
  $('product_info').hide();
  $('tell_friend').hide();
  $('backorder').hide();
  new Effect.Appear(name || 'product_info');
}

function show_reviews ( show ) {
  var review_form = $('review'), reviews = $('product_reviews');
  
  if ( ! review_form || ! reviews ) {
    return;
  }
  
  if ( show ) {
    review_form.hide();
    new Effect.Appear(reviews);
  } else {
    reviews.hide();
    new Effect.Appear(review_form);
  }
}
