/**
 *	In an effort to be less intrusive, we move all of our tracking
 *	into this tracking file so that we don't have to manipulate
 *	code in later iterations.
 */

// GA initialization and pageview tracking
BGC.track.init("UA-5624474-1", "UA-5870535-31", "UA-25631303-1");
BGC.track(); 

$(function() {
	PT.Tracking.init();
	PT.Track.init();
});

PT.Track = {

	init: function () {
		
		if ( $('body').hasClass('home') ) {
			PT.Track.home();
		}
		
		if ( $('body').hasClass('recipes') && $('body').hasClass('search') ) {
			PT.Track.recipeSearch();
		}
		
		if ( $('body').hasClass('recipe') && $('body').hasClass('detail') ) {
			PT.Track.recipeSearch();
		}
		
	},
	
	home: function() {
		
	},
	
	recipeSearch: function() {
	
		$( 'a[href$="/print"]' ).click(function() {
			BGC.track( '/action/recipe/print-from-search' );
		});
	
	},
	
	recipeDetails: function() {
		$( 'a[href$="/print"]' ).click(function() {
			BGC.track( '/action/recipe/print' );
		});
	}

};

/**
 *	This code was originally housed in pt.js, however, it was moved into this file so
 *	that our tracking code is isolated.
 */

PT.Tracking = {
	init: function () {
		// class-based GA tracking hooks
		// link tracking (finds and tracks links with class="track category|action|label" applied)
		jQuery("a.track").live("click", function (e) {
			var ev = getEventString(this.className), target = null, callback = null, href = null;
			if (ev) {
				if (this.className.match(/action/)) { // use "action" rather than "event" tracking
					ev = "/" + ev.replace(/\|/g, "/");
				}
				target = this.target;
				if ( !target || target.match(/^_(self)|(top)$/i) ) {
					href = jQuery(e.target).closest("a")[0].href;
					callback = function () {
						if ( target.match(/^_top$/i) ) {
							top.location.href = href;
						} else {
							location.href = href;
						}
					};
					e.preventDefault();
				}
				BGC.track(ev, callback);
			}
		});
		
		// form view/submit "action" (virtual pageview) tracking
		jQuery("form.track").each(function () {
			var ev = getEventString(this.className).replace(/\|/g, "/");
			if (ev) {
				BGC.track(ev + "/view");
				jQuery(this).submit(function (e) {
					/*if (Page_IsValid) {*/ // only track the submit action if the .NET validation routine passed
						BGC.track(ev + "/submit");
					/*}*/
				});
			}
		});
		
		function getEventString (s) {
			var ev = "";
			s = s.split(" ");
			jQuery.each(s, function () {
				if (this.indexOf("|") > -1) {
					ev = this.toString();
					return;
				}
			});
			return ev;
		}
	}
};
