/**
 * Google Analytics Tracking for Fred Beans Parts (fredbeansparts.com)
 *
 * @version 1.2 20090728T1254-05:00
 * @uses jQuery 1.3.2, GA.js
 * @author Chris Strosser
 */
var category_accordion  = 'Left Navigation (Accordion)';
var category_topsellers = 'Top Sellers';

var goal1_1 = '/funnel-goal-1/1-checkout-options.html';
var goal1_2 = '/funnel-goal-1/2-shipping-info.html';
var goal1_3 = '/funnel-goal-1/3-shipping-method.html';
var goal1_4 = '/funnel-goal-1/4-save-payment.html';
var goal1_5 = '/funnel-goal-1/5-confirm.html';

/**
 * Google Analytics _trackEvent() Reference
 * @param  String	cat		Event category name
 * @param  String	label	Event label
 * @return void
 */
function track_click(cat, label) {
	pageTracker._trackEvent(cat, 'Click', label);
}

/**
 * Debug the event information before using track_click()
 * @param  String	cat		Event category name
 * @param  String	label	Event label
 * @return bool
 */
function debug_click(cat, label) {
	alert(cat + ' :: ' + label);
	return true;
}

(function($) {

	$(document).ready(function(){
		// Top navigation below header
		$('.navBar .nav ul li a').bind("click", function(e){
			track_click('Top Navigation', $(this).text());
		});
		// Price match image in header
		$('#header .headerContent .priceMatch a').bind("click", function(e){
			track_click('Header', 'Price Match Image (Center)');
		});
		
		/**
		 * Accordion navigation
		 */
		 
		$('#acc1 li span a').bind("click", function(e){
			if (($(this).parent("span").hasClass("level2-header")) || ($(this).parent("span").hasClass("level2-nochildren"))) {
				// Second level of navigation
				var parentCategory = $(this).parent("span").parent("li").parent("ul").parent("div").parent("li").children(".level1-header:first").text();
				track_click(category_accordion, parentCategory + ' > ' + $(this).text());
			} else {
				// First level of navigation
				track_click(category_accordion, $(this).text());
			}
		});
		
		// Third level of navigation
		$('#acc1 li div ul li div a').bind("click", function(e){
			// Set category paths for simple reference
			var parentCategoryPath = $(this).parent("div").parent("li");
			var rootCategoryPath   = parentCategoryPath.parent("ul").parent("div").parent("li");
			// Define categories
			var parentCategory	   = parentCategoryPath.children(".level2-header:first").text();
			var rootCategory	   = rootCategoryPath.children(".level1-header:first").text();
			// Track event
			track_click(category_accordion, rootCategory + ' > ' + parentCategory + ' > ' + $(this).text());
		});
		
		$('#acc1-home-button').bind("click", function(e){
			track_click(category_accordion, 'Home');
		});
		
		// Top Sellers
		$('.topSellers ul li a').bind("click", function(e){
			// Get top seller product name
			var product = $(this).text();
			// Replace illegal characters
			var illegal = new Array("'", /"/, "\\", "&");
			for (var i = 0; i < product.length; i++) {
				for (var j = 0; j < illegal.length; j++) {
					product = product.replace(illegal[j], '');
				}
			}
			// Track event
			track_click(category_topsellers, product);
		});
	});

})(jQuery);

/**
 * Google Analytics Goals
 *
 * This had to be done with JavaScript because the current Google
 * Analytics goal/funnel system depends on page views, while the
 * ProStores checkout system is based on JavaScript.
 */

/**
 * Track a pageview for the given page.
 *
 * @uses pageTracker Google Analytics tracking object
 *
 * @param string page The page URL.
 */
function goal_page(page) {
	pageTracker._trackPageview(page);
}

(function($) {

	$(document).ready(function() {
		/**
		 * Step 1 - Login
		 */
		
		// Sign In
		$('#logincustomer').click(function() {
			goal_page(goal1_1);
		});
		
		// Register
		$('#savecustomer').click(function() {
			goal_page(goal1_1);
		});
		
		// PayPal
		$('#paypallink').click(function() {
			goal_page(goal1_1);
		});
		
		/**
		 * Step 2
		 */
		$('#saveshippaddress').click(function() {
			goal_page(goal1_2);
		});
		
		/**
		 * Step 3
		 */
		$('#saveshipmethod').click(function() {
			goal_page(goal1_3);
		});
		
		/**
		 * Step 4
		 */
		$('#savepayment').click(function() {
			goal_page(goal1_4);
		});
		
		/**
		 * Step 5 - Confirm
		 */
		$('#confirm').click(function() {
			goal_page(goal1_5);
		});
	});
	
})(jQuery);