/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

$.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};

ppl.product = {

	loadedHistograms: [],
	loadedVariations: [],
	
	init: function() {
	
		$('.pplShop-list-price-tag > .all-prices-trigger').show();
		
		if($("#tabs").length) {
			$("#tabs").tabs();
		}
		
		$('.pplShop-add-to-cart > form').submit(function(e) {
			e.preventDefault();
			ppl.product.addToCart(this);
		});
		
		if($('#pplShop-recommendation-items').length) {
			$('#pplShop-recommendation-items').jcarousel({
				scroll: 4
			});
		}
		
		$('.pplShop-product-histogram-trigger').hoverIntent({
			over: ppl.product.showHistogram,
			timeout: 500,
			out: ppl.product.hideHistogram
		});
		
		$('.pplShop-list-price-tag > .all-prices-trigger').hoverIntent({
			over: ppl.product.showVariations,
			timeout: 500,
			out: ppl.product.hideVariations
		});
	
	},
	
	addToCart: function(element) {
		var product = $(element).children('input[name=product]');
		var bundle = $(element).children('input[name=bundle]');
		var quantity = $(element).children('input[name=quantity]');
		var attributes = $(element).children('input[name*=attributes]');
		if(bundle.val() !== undefined) {
			$.post('/pplCart/addBundle?bundle=' + bundle.val(), function(data) {
				$('#pplShop-cartSummary-wrap').html(data);
				ppl.product.animateCartSummary(element);
			});
		} else if((product.val() !== undefined) && (attributes.val() === undefined)) {
			$.post('/pplCart/addItem?product=' + product.val() + '&quantity=' + quantity.val(), function(data) {
				$('#pplShop-cartSummary-wrap').html(data);
				ppl.product.animateCartSummary(element);
			});
		} else {
			$(element).submit();
		}
	},
	
	animateCartSummary: function(element) {
	
		$('#pplCart-compact').stop().css('background', '#FFFF9C').animate({ backgroundColor: '#FFFFFF'}, 1500);
		$(element).children('.pplShop-add-to-cart-button').stop().css('background-position', '0 -100px').css('background-color', '#FFFF9C').animate({ backgroundColor: '#FBFAF9'}, 1000).animate({ backgroundPosition: '0 100%'}, 100);
		var target = $('#pplShop-cartSummary-wrap');
		$('html,body').wait(450).animate({scrollTop: target.offset().top}, 500);
		
		$('#store-head-user-cart').load('/partial/header-cart');
	
	},
	
	showHistogram: function() {
		element = $(this).children('.pplShop-rating-histogram-wrap');
		elementId = $(element).attr('id');
		$(element).show();
		if($.inArray(elementId, ppl.product.loadedHistograms) == -1) {	
			productId = elementId.match(/\d+/);
			if(productId.length) {
				$(element).load('/pplReview/reviewHistogram?product=' + productId);
				ppl.product.loadedHistograms.push(elementId);
			} else {
				$(element.hide());
			}
		}
	},
	
	hideHistogram: function() {
		$(this).children('.pplShop-rating-histogram-wrap').hide();
	},
	
	showVariations: function() {
		element = $(this).children('.all-prices');
		elementId = $(element).attr('id');
		$(element).show();
		if($.inArray(elementId, ppl.product.loadedVariations) == -1) {
			productId = elementId.substr(11);
			$(element).load('/pplShop/showProductVariations?product=' + productId);
			ppl.product.loadedVariations.push(elementId);
		}
	},
	
	hideVariations: function() {
		$(this).children('.all-prices').hide();
	}

}

jQuery(document).ready(function() {
    ppl.product.init();
});
