/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to commercial source code license 
 * of StoreFront Consulting, Inc.
 *
 * @copyright	(C)Copyright 2011 StoreFront Consulting, Inc (http://www.StoreFrontConsulting.com/)
 * @author		Dennis Rogers
 */

/*
 * Category - should be "Add to Cart", "Add to Wishlist" or "Review Submit"
 * Action - should be product SKU
 * Label - should be page URL where action occured
 * Value - should be numeric product ID
*/

jQuery(document).ready(function(){
	
	var category = '';
	var label = window.location.href;
	var action = '';
	var value = 0;
	
	// product grid/list
	jQuery('.item button.btn-cart,.item a.link-wishlist,.item a.link-compare').click(function(e){
		if(jQuery(this).hasClass('btn-cart')){
			category = "Add to Cart";
		} else {
			category = jQuery(this).text();
		}
		action = jQuery(this).parents('.item').find('.product-name a').text();		
		value = Number(jQuery(this).parents('.item').attr('id').replace(/addto_/, ''));
		pushEvent(category, label, action, value);
	});
	
	// product view	
	jQuery('.product-shop button.btn-cart,.product-shop a.link-wishlist,.product-shop a.link-compare, .product-shop .emailto-link a').click(function(e){
		if(jQuery(this).hasClass('btn-cart')){
			category = "Add to Cart";
		} else {
			category = jQuery(this).text();
		}
		action = jQuery(this).parents('.product-shop').find('.product-name h1').text();		
		value = Number(jQuery('#product_addtocart_form').attr('action').replace(/.*\/(\d*)\//, '$1'));
		pushEvent(category, label, action, value);
	});
	
	// product tabs		
	jQuery('.product-view .tab').click(function(e){
		category = "Tab Click";
		action = jQuery(this).find('span').text();		
		value = Number(jQuery('#product_addtocart_form').attr('action').replace(/.*\/(\d*)\//, '$1'));
		pushEvent(category, label, action, value);
	});
	
	// reviews form	
	jQuery('#review-form button.button').click(function(e){
		category = "Submit Review";
		action = jQuery(this).parents('.product-view').find('.product-shop .product-name a').text();		
		value = Number(jQuery('#review-form').attr('action').replace(/.*\/(\d*)\//, '$1'));
		pushEvent(category, label, action, value);
	});
	
	// newsletter sub	
	jQuery('#mailchimp-validate-detail button.button').click(function(e){
		category = "Newsletter";
		action = jQuery(this).parent().attr('class');
		pushEvent(category, label, action, value);
	});
	
	// leave site links	
	jQuery('.twitter a, .facebook a, .AuthorizeNetSeal a').click(function(e){
		category = "Leaving";
		action = jQuery(this).parent().attr('class');
		pushEvent(category, label, action, value);
	});

});

//	_trackEvent(category, action, opt_label, opt_value)
function pushEvent(category, label, action, value){
	console.log(category, label, action, value);
	_gaq.push(["_trackEvent", category, action, label, value]);
}
