(function($) {

	$.event.special.domready = {
		setup: function() {
			// Make sure the ready event is setup
			bindDOMReady();
			return;
		},

		teardown: function() { return; }
	};

	jQuery.fn.extend({
		domready: function(f) {
			// Attach the listeners
			bindDOMReady();

			// If the DOM is already ready
			if ( jQuery.isDOMReady )
			// Execute the function immediately
			f.apply( document, [jQuery] );

			// Otherwise, remember the function for later
			else
			// Add the function to the wait list
			jQuery.domreadyList.push( function() { return f.apply(this, [jQuery]); } );

			return this;
		}
	});

	jQuery.extend({
		/*
		* All the code that makes DOM Ready work nicely.
		*/
		isDOMReady: false,
		domreadyList: [],

		// Handle when the DOM is ready
		domready: function() {
			// Make sure that the DOM is not already loaded
			if ( !jQuery.isDOMReady ) {
				// Remember that the DOM is ready
				jQuery.isDOMReady = true;

				// If there are functions bound, to execute
				if ( jQuery.domreadyList ) {
					// Execute all of them
					jQuery.each( jQuery.domreadyList, function() {
						this.apply( document );
					});

					// Reset the list of functions
					jQuery.domreadyList = null;
				}

				// Trigger any bound domready events
				jQuery(document).triggerHandler("domready");

				// Remove event listener to avoid memory leak
				if ( document.removeEventListener )
				document.removeEventListener( "DOMContentLoaded", jQuery.domready, false );

				// Remove script element used by IE hack
				if( !window.frames.length ) // don't remove if frames are present (#1187)
				jQuery(window).load(function() { jQuery("#__ie_init").remove(); });
			}
		}
	});

	var domreadyBound = false;

	function bindDOMReady() {
		if ( domreadyBound ) return;
		domreadyBound = true;

		if ( document.addEventListener )
		// Use the handy event callback
		document.addEventListener( "DOMContentLoaded", jQuery.domready, false );

		// If IE is used, use the excellent hack by Matthias Miller
		// http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
		if ( jQuery.browser.msie ) {

			// Only works if you document.write() it
			document.write("<scr" + "ipt id=__ie_init defer=true " + 
			"src=//:><\/script>");

			// Use the defer script hack
			var script = document.getElementById("__ie_init");

			// script does not exist if jQuery is loaded dynamically
			if ( script ) 
			script.onreadystatechange = function() {
				if ( this.readyState != "complete" ) return;
				jQuery.domready();
			};

			// Clear from memory
			script = null;

		// If Safari is used
		} else if ( jQuery.browser.safari )
		// Continually check to see if the document.readyState is valid
		jQuery.safariTimer = setInterval(function() {
			// loaded and complete are both valid states
			if ( document.readyState == "loaded" || 
			document.readyState == "complete" ) {

				// If either one are found, remove the timer
				clearInterval( jQuery.safariTimer );
				jQuery.safariTimer = null;

				// and execute any waiting functions
				jQuery.domready();
			}
		}, 10); 

		// A fallback to window.onload, that will always work
		jQuery.event.add( window, "load", jQuery.domready );
	}

})(jQuery);
// gaTracker: jQuery Google Analytics Integration
// A quicker, automated way to embed Google Analytics.
// (c)2007 Jason Huck/Core Five Creative
// (c)2008 Thomas Harning Jr/TrustBearer Labs
//
// Changes: Thomas Harning Jr: Uses ga.js and new action API
//          Thomas Harning Jr: Fix for Safari's delay
// Requires jQuery 1.2.x or higher (for cross-domain $.getScript)


(function($) {

	$.gaTracker = function(code, opts) {
		opts = jQuery.extend({
			extensions: [
			'pdf','doc','xls','csv','jpg','gif', 'mp3',
			'swf','txt','ppt','zip','gz','dmg','xml'		
			]
		}, opts);

		// Returns the given URL's action-type if it is:
		//		a) a link to an external site -> external
		//		b) a mailto link              -> mailto
		//		c) a downloadable file        -> download
		// ...otherwise returns nothing
		function decorateLink(u) {
			if(u.indexOf('://') == -1 && u.indexOf('mailto:') != 0) {
				// no protocol or mailto - internal link - check extension
				var ext = u.split('.')[u.split('.').length - 1];			
				var exts = opts.extensions;

				for(i = 0; i < exts.length; i++) {
					if(ext == exts[i]) {
						return "download";
					}
				}				
			} else {
				if(u.indexOf('mailto:') == 0) {
					return "mailto";
				} else {
					return "external";
				}
			}
		}

		// add tracking code to the current page
		function addTracking() {
			// Fix for Safari
			if(!window._gat) {
				var interval = setInterval(function() {
					if(!window._gat) return;
					clearInterval(interval);
					addTracking();
				}, 500);
			}
			var tracker = _gat._getTracker(code);
			tracker._initData();
			tracker._trackPageview();

			// examine every link with an href in the page
			$('a[@href]').each(function() {
				var u = $(this).attr('href');
				if(!u) return; // Just in case its a blank
				var action = decorateLink(u);
				if(!action) return;
				// if it needs to be tracked manually,
				// bind a click event to call GA with
				// the decoration
				$(this).click(function() {
					tracker._trackEvent(action, "click", newLink);
				});
			});

		}

		// include the external GA script in try/catch to play nice
		function initGA() {
			try{
				// determine whether to include the normal or SSL version
				var gaURL = (location.protocol == 'https:') ? 'https://ssl' : 'http://www';
				gaURL += '.google-analytics.com/ga.js';

				// include the script
				$.getScript(gaURL, function() {
					addTracking();
				});
			} catch(err) {
				// log any failure
				if(window.console) console.log('Failed to load Google Analytics:' + err);
			}
		}

		initGA();

	}

})(jQuery);

