/* This is the internal tracking script. 
 * It is designed to load on every page and capture mktids and store it in a cookie.
 * It also can be called to track clicks and events that are tracked outside of google
 * This file must be inlcuded in the html of any page using the campaign click tracking.
 * This file is also required to set the campaign id in the cookie for conversions tracked on 
 * signup and upgrade.
 * 
 * @author george.matthes@dailymakeover.com
 * 
 * dependencies: jquery-1.32.min.js, jquery.query-2.1.6.js, jquery.cookie.js
 * 
 * */
var iTracking = {
	url:location.search,
	cookie_name:'dmmkt_external',
	internal_cookie_name:'dmmkt_internal',
	feature_cookie_name:'dmmkt_feature',
    ext_options:{ path: '/', expires: 30, domain:'.dailymakeover.com' },
    int_options:{ path: '/', domain:'.dailymakeover.com' },
	init:function(){
		this.capture_mktid();
		this.capture_imktid();
		//iTracking_Bindings.init();
    },
    is_numeric:function(num){
    	if(isNaN(parseInt(num))){
    		return false;}
    	else{
    		return true;}
    },
	capture_mktid:function(){
		var mktid = $.query.get('mktid');

		//if mktid is not empty or null
		if(this.is_numeric(mktid)){
			//save it to a cookie.
			$.cookie(this.cookie_name, mktid, this.ext_options);
			this.capture_event();
		}
	},
	urlrecode:function(str) {
		str = str.replace('%2F/g', '-');
		str = str.replace(/\//g, '-');
		str = unescape(str);
		return str;
		},
	capture_imktid:function(){
		var imktid = $.query.get('imktid');

		//if mktid is not empty or null
		if(this.is_numeric(imktid)){
			//save it to a cookie.
			$.cookie(this.internal_cookie_name, imktid, this.ext_options);
			this.capture_event();
		}
	},
	capture_event:function(){
		$.ajax({
		      url: "../account/click-counter.php",
		      global: false,
		      type: "POST",
		      data: ({mktid : $.cookie(this.cookie_name),imktid :$.cookie(this.internal_cookie_name)}),
		      dataType: "html",
		      success: function(msg){
		        //console.log("event recorded");
		      }
		});
	},
	capture_feature:function(feature){
		var recoded_feature = this.urlrecode(feature);
		$.cookie(this.feature_cookie_name, recoded_feature, this.int_options);
	}
}
//loads after html loads
$(window).load(function(){iTracking.init();});
//loads before page loads
//$(document).ready(function() {console.log("document.ready");});
