//
// New Analytics.js
//
// copyright Compendium 2011
//

(function(){

		if(window.cpdm_ga_run_once){ 
		   return; 
		}
	 
		//
		//
		//
		if(typeof _gaq === 'undefined'){
			_gaq = [];
		}

		var meta = {},
			meta_tags = document.getElementsByTagName('meta'),
			agent_name_index = '',
			trackers = [];


		//
		// Create a tracker with the give UA tracker id
		//
		// this will also add this to the list of trackers (used by pushToAllTrackers)
		//
		function add_tracker(agent_id){
			var agent_name = "";
			if(agent_name_index === ''){
				agent_name_index = 'a';			
			}else{
				agent_name= agent_name_index+".";
				agent_name_index = String.fromCharCode(agent_name_index.charCodeAt(0)+1);
			}
			var tracker =
			{name : agent_name,
				push: function(val){

					var newVal = [];

					for(var i = 0; i < val.length; i++){
						if(i === 0){
							newVal[0] = this.name + val[0];			
						}else{	
							newVal[i] = val[i];			
						}
					}
					_gaq.push(newVal);
			}};

			trackers.push(tracker);
			tracker.push(['_setAccount', agent_id]);
			return tracker;
		}

		//read header information
		for(var mi in meta_tags){
			var m = meta_tags[mi]; 
			if(m.name){
				meta[m.name] = m.content;
			}
		}


		/*
		*
		*
		*  Set Up Trackers
		*
		*/

		if(meta.cpdm_tracker){
			add_tracker(meta.cpdm_tracker);
		}else{
			return;// don't run without the tracker
		}
	 
		if(meta.network_tracker){
			add_tracker(meta.network_tracker);
		}

		if(meta.extra_trackers){
			var ids = [meta.extra_trackers];
			if(meta.extra_trackers.indexOf(',') > -1){	
				ids = meta.extra_trackers.split(',');
			}
			for( var id in ids){
				var t=	add_tracker(ids[id]);
				if(meta.ga_host_name_override){
					t.push(['_setDomainName', meta.ga_host_name_override]);
				}
				else if(meta.host_name){
					t.push(['_setDomainName', meta.host_name.substring(meta.host_name.indexOf('.'))] );
				}
			}
		}

		window.pushToAllTrackers = function(val){
			for(var t in trackers){
				var tracker	= trackers[t];
				tracker.push(val);
				
			}	
		};

		//if a meta variable exists with the given name
		//push the content to the given slot
		function pushMetaVar(varName,slot, name){
			if(typeof meta[varName] !== 'undefined'){
				pushToAllTrackers(["_setCustomVar", slot , name, meta[varName], 3]);
			}
		}

		$(document).ready(function(){
				//set vars
				pushMetaVar('page_type', 1, 'Page Type');
				pushMetaVar('publish_date', 2, 'Publish Date');
				pushMetaVar('author', 3, 'Author');
				pushMetaVar('title', 4, 'Title');
				pushMetaVar('category', 5, 'Category');
				//track this page view
				pushToAllTrackers(['_trackPageview']);
			    
				var targetHostname = meta.target_hostname;
				$(document).on('submit', 'form', function(){

						var cta = this.name;

						if (cta && /^cta=/.test(cta)) {
							var ctaname= cta.substring(4);
							pushToAllTrackers(['_trackEvent', 'CTA', 'Click',ctaname]);
						}
 				});
				$(document).on('click', 'a', function(){
						if($(this).data('cta_name')){
							var name = $(this).data('cta_name');
							pushToAllTrackers(['_trackEvent', 'CTA', 'Click',name]);
						}else{
							var hostname, pathname, cta, ctaname;

							hostname = this.hostname;
							pathname = this.pathname;
							cta = this.name;

							if (cta && /^cta=/.test(cta)) {
								ctaname= cta.substring(4);
								pushToAllTrackers(['_trackEvent', 'CTA', 'Click',ctaname]);
							} else if (typeof targetHostname == 'string' && hostname === targetHostname) {
								pushToAllTrackers(['_trackEvent', 'Outgoing Link' , pathname]);
							}
						}
				});
				setTimeout(function(){
					$('a').each(function(){
						if($(this).is(':visible')){
							var cta = this.name;
							if (cta && /^cta=/.test(cta)) {
								var ctaname= cta.substring(4);
								pushToAllTrackers(["_trackEvent", "CTA", 'Impression', ctaname, 1, true]);
							}	
						}		
					});
				}, 1000);
							  
		});


 (function() {
    var ga = document.createElement('script');     ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:'   == document.location.protocol ? 'https://ssl'   : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
	 
	 window.cpdm_ga_run_once = true;
})();

//pull in analytics
