/**
 * AKQA Doubleclick Floodlight Tags
 * Creates Doubleclick Floodlight tags for page landings and button clicks.
 * Landing events are created based on the path. Click events are created
 * based on the element having a fls-tag class and switched on the ID.
 * These tags were added in November 2011.
 */
$(document).ready(function() {

	//Flag to tell if we need debugging. This looks for the URL of the page
	//containing the main domain.
	var flsDebug = ($(location).attr('host').indexOf('www.cloroxprofessional.com') == -1);
	if (typeof console == "undefined" || typeof console.log == "undefined") {
		var console = { 
			log: function() {} 
		};
	}
	/**
	 * Function that implements the tag.
	 */
	function fireFlsTag(flsSrc, flsType, flsCat) {
		var axel = Math.random() + "";
		var a = axel * 10000000000000;
		$('body').append(
			'<iframe src="https://fls.doubleclick.net/' + 
			'activityi' + 
			';src=' + flsSrc + 
			';type=' + flsType +
			';cat=' + flsCat +
			';ord=' + a + '?' + 
			'" width="1" height="1" frameborder="0" ' + 
			'style="display:none"></iframe>');

		//This will fire off only on a non-production site. Use it to
		//check that tags are firing.
		if(flsDebug) { 
			console.log('QA FLS ' + flsSrc + ' ' + flsType + ' ' + flsCat);
		}
	}

	/**
	 * Function for getting url params
	 */
	function taggingGetParams() {
		var urlParams = {};
		(function () {
			var e,
				a = /\+/g,  // Regex for replacing addition symbol with a space
				r = /([^&=]+)=?([^&]*)/g,
				d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
				q = window.location.search.substring(1);
			while (e = r.exec(q)) {
				urlParams[d(e[1])] = d(e[2]);
			}
		})();
		return urlParams;
	}

	/**
	 * Tags that fire on landing
	 * 
	 */
	$('body').each(function() {
		//For debugging only. Should not do anything in production.
		if(flsDebug) { 
			console.log('Body ID ' + this.id + ' ' + $(location).attr('pathname'));
		}
		switch($(location).attr('pathname')) {
			case '/products/clorox-broad-spectrum-quaternary-disinfectant-cleaner/at-a-glance/':
				fireFlsTag('1261211', 'fy12a779', 'broad283');
				break;
			case '/industry/health/free-resources/':
				var p = taggingGetParams();
				if(p.broadPdf == 'yes') {
					fireFlsTag('1261211', 'fy12a779', 'broad940');
				}
				if(p.cdiffPdf == 'yes') {
					fireFlsTag('1261211', 'fy12a779', 'cdiff304');
				}
				if(p.trainingPdf == 'yes') {
					fireFlsTag('1261211', 'fy12a779', 'train409');
				}
				break;
			case '/industry/health/free-resources/download':
			case '/industry/health/free-resources/download/':
				var p = taggingGetParams();
				if(p.bscdQuantity > 0) {
					fireFlsTag('1261211', 'fy12a779', 'broad935');
				}
				if(p.cdiffQuantity > 0) {
					fireFlsTag('1261211', 'fy12a779', 'cdiff770');
				}
				if(p.trainingQuantity > 0) {
					fireFlsTag('1261211', 'fy12a779', 'train379');
				}
				break;
		}
	});

	/**
	 * Tags that fire on click
	 */

	// Initialize mousedown AKQA tags for all class 'akqa-tag' but MUST have
	// valid id
	$('.fls-tag').each(function() {
		$(this).mousedown(function() {
			
			//Switch on the ID of the element
			switch (this.id) { 
				//Play button on promotions page
				case 'free-resources-go':
					break;
			}
		})
	});
});

