/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights" & "EqualWidths"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

// just in case you need it...
$.fn.equalWidths = function(px) {
	$(this).each(function(){
		var currentWidest = 0;
		$(this).children().each(function(i){
				if($(this).width() > currentWidest) { currentWidest = $(this).width(); }
		});
		if(!px || !Number.prototype.pxToEm) currentWidest = currentWidest.pxToEm(); //use ems unless px is specified
		// for ie6, set width since min-width isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'width': currentWidest}); }
		$(this).children().css({'min-width': currentWidest}); 
	});
	return this;
};


/*-------------------------------------------------------------------- 
 * javascript method: "pxToEm"
   http://www.filamentgroup.com
--------------------------------------------------------------------*/

Number.prototype.pxToEm = String.prototype.pxToEm = function(settings){
	//set defaults
	settings = jQuery.extend({
		scope: 'body',
		reverse: false
	}, settings);
	
	var pxVal = (this == '') ? 0 : parseFloat(this);
	var scopeVal;
	var getWindowWidth = function(){
		var de = document.documentElement;
		return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
	};	
				
	if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
		var calcFontSize = function(){		
			return (parseFloat($('body').css('font-size'))/getWindowWidth()).toFixed(3) * 16;
		};
		scopeVal = calcFontSize();
	}
	else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };
			
	var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
	return result;
};






$().ready(function(){

  // bigTarget for featured products on homepage
  $("#homeFeaturedProducts ul h3 a").bigTarget({
    hoverClass: 'over', // CSS class applied to the click zone onHover
    clickZone : 'li:eq(0)' // jQuery parent selector
  });

  // bigTarget for news on homepage
  $("#homeLatestNews ul h3 a").bigTarget({
    hoverClass: 'over', // CSS class applied to the click zone onHover
    clickZone : 'li:eq(0)' // jQuery parent selector
  });
  
  // bigTarget for sections
  $("#sectionlist h3 a").bigTarget({
    hoverClass: 'over', // CSS class applied to the click zone onHover
    clickZone : 'li:eq(0)' // jQuery parent selector
  });  

  // bigTarget for related products
  $("#related h3 a").bigTarget({
    hoverClass: 'over', // CSS class applied to the click zone onHover
    clickZone : 'li:eq(0)' // jQuery parent selector
  }); 

  // small presentational hack for homepage.
  $('#homeFeaturedProducts hr:last').remove();

  // equal heights for home featured products
  $('#homeFeaturedProducts ul').equalHeights(true);
  $('ul#sectionlist').equalHeights(true);

  //extend the manufacturers list on hover
  $('#footer .manufacturers li:gt(8)').hide();
  
  $('#footer .manufacturers').hover(
    function(){
      $('#footer .manufacturers li:gt(8)').fadeIn();  
    }, 
    function(){
      $('#footer .manufacturers li:gt(8)').fadeOut();
    }
  );  



  //**** plus and minus next to quantity buttons
  $('.buybox .quantity input[type=text]').attr("value", 1); // On page load reset all values to 1

  $(".buybox .quantity input[type=text]")
    .before('<a href="#" class="minus"><img src="minus.gif" /></a>')
    .after(' <a href="#" class="plus"><img src="plus.gif" /></a>');

  $('.singleproductform input[type=text]').attr("value", 1);

  $('.plus').click(function() {
    quantity = ( $(this).siblings("input[type=text]").attr("value") * 1 ) + 1;
    $(this).siblings("input[type=text]").attr("value", quantity);
    return false;
  });

  $('.minus').click(function() {
    quantity = ($(this).siblings("input[type=text]").attr("value") * 1);
    if(quantity > 1) {$(this).siblings("input[type=text]").attr("value", quantity  - 1);}
    return false;
  });




  //**** enhanced add to cart button

  var options = { 
      beforeSubmit: SendForm, 
      success: CallBack,
      resetForm: true
  }; 
 
    
  $('.oneproduct form').ajaxForm(options);

  
});





function SendForm(data, $theform, options) {
  
  if($("ul.onlyone").length == 1) {
    name = $('h1').text();
  } else {
    name = $theform.find("h2").text();  
  }
  
  quantity = $theform.find(".quantity input").attr('value');

  $($theform).append('<div class="current_notice"><h4><strong>' +quantity+ '</strong> of <em>&quot;' +name+ '&quot;</em> has been added to your shopping cart</h4></div>');
  return true;
}


function CallBack(responseText, statusText, xhr, $form){
    
  $(".current_notice").fadeIn("slow", function() {
    $(this).delay(2000).fadeOut("slow", function() {
        $(this).remove();
        
        quantity   = getCartItem(3);
        total      = getCartItem(1);
        
        $('#miniCart span.quantity').text(quantity);
        $('#miniCart span.total').html(total);
        
        $('#miniCart p').animate({ backgroundColor: "yellow", 'font-weight' : "bold" }, 'fast', function() {
          $(this).delay(3000).animate({ backgroundColor: "#ffffff", 'font-weight' : "normal" }, 'fast');
        });
        
      });
  });
  
}


$().ready(function(){
	$('a[rel=lightbox]').lightBox();
});


