function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function storeQueryString() {
    var querystring = new Array;
    var q = String(document.location).split('?')[1];
    if (!q) return false;
    q = q.split('&');
    for (var i = 0; i < q.length; i++) {
        var o = q[i].split('=');
        querystring[o[0]] = o[1];
    }
    return querystring;
}


function SuperCookie() {
    this.source = " ";
    this.medium = " ";
    this.campaign = " ";
    this.keyword = " ";
    this.timestamp = " ";
    this.adgroup = " ";
    this.custom = " ";

	this._getValue = function(){ return this.source + '|' + this.medium + '|' + this.campaign + '|' + this.keyword + '|' + this.timestamp + '|' + this.adgroup + '|' + this.custom;
	};

	this._setCookie = function(name, value, expires, path, domain, overwrite) {
			// set time, it's in milliseconds
			var today = new Date();
			today.setTime(today.getTime());
			/*
				if the expires variable is set, make the correct 
				expires time, the current script below will set 
				it for x number of days, to make it for hours, 
				delete * 24, for minutes, delete * 60 * 24
			*/
			if (expires) {
				expires = expires * 1000 * 60 * 60 * 24;
			}
			else {
				expires = 365 * 24 * 60 * 60 * 1000;
			}
			var expires_date = new Date(today.getTime() + (expires));
			if (!value) {
				value = this.source + '|' + this.medium + '|' + this.campaign + '|' + this.keyword + '|' + this.timestamp + '|' + this.adgroup + '|' + this.custom;
			}
			if(overwrite == true || !document.cookie.match('(^|;\\s)' + name)){
			document.cookie = name + "=" + value + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path: ";path=/") + ((domain) ? ";domain=" + domain: "");
			}
	};
	
	this._initData = function(type) {
		var cancel_campaign = false;
		var cancel_medium = false;
		var cancel_keyword = false;
		var cancel_source = false;
		var cancel_content = false;
		if (type && document.cookie.match('(^|;\\s)' + type) && type != '__utmz') {
			var c2 = readCookie(type);
			var ca2 = c2.split('|');
			this.medium = ca2[0];
			this.source = ca2[1];
			this.campaign = ca2[2];
			this.keyword = ca2[3];
			this.timestamp = ca2[4];
			this.adgroup = ca2[5];
			this.custom = ca2[6];
		}
		if (!type || type == 'both' || type == 'query') {
			var q = storeQueryString();
			if (q['utm_source']) {
				this.source = q['utm_source'];
				cancel_source = true;
			}
			if (q['utm_medium']) {
				this.medium = q['utm_medium'];
				cancel_medium = true;
			}
			if (q['utm_campaign']) {
				this.campaign = q['utm_campaign'];
				cancel_campaign = true;
			}
			if (q['utm_term']) {
				this.keyword = q['utm_term'];
				cancel_keyword = true;
			}
			if (q['utm_content']) {
				this.adgroup = q['utm_content'];
				cancel_content = true;
			}
			if (q['ga_campaign']) {
				this.campaign = q['ga_campaign'];
				cancel_campaign = true;
			}
			if (q['ga_keyword']) {
				this.keyword = q['ga_keyword'];
				cancel_keyword = true;
			}
			if (q['ga_ad_content']) {
				this.custom = q['ga_ad_content'];
			}
			if (q['ga_adgroup']) {
				this.adgroup = q['ga_adgroup'];
				cancel_content = true;
			}
		}
		if (!type || type == 'both' || type == '__utmz') {
			if (document.cookie.indexOf('__utmz') != -1) {
				var c2 = readCookie('__utmz'); //This gets the cookie
				var ca2 = c2.split('|'); //This splits the cookie into parts
				temp2 = ca2[0].substring(ca2[0].indexOf('utmcsr'));
				temp2 = temp2.substring(temp2.indexOf('utmgclid'));
				ca2[0] = temp2; // We then replace the item in the array with just the variable data									
				for (i = 0; i < ca2.length; i++) {
					temp3 = ca2[i]; //First, take each variable (ex. utmcsr=sourcename)
					temp4 = temp3.split('='); //Splits into an array, with temp4[0] = 'utmcsr, and temp4[1] = 'sourcename' using our above example
					if (temp4[0] == 'utmgclid') //Identifies the varaible and replaces appropriate items for Google Adwords Campaigns
					{
						if (!cancel_source) {
							this.source = 'google';
							cancel_source = true;
						}
						if (!cancel_medium) {
							this.medium = 'cpc';
							cancel_medium = true;
						}
						if (!cancel_campaign) {
							this.campaign = 'google';
							cancel_campaign = true;
						}
					}
					if (temp4[0] == 'utmcsr' && !cancel_source) {
						this.source = temp4[1];
					}
					if (temp4[0] == 'utmccn' && !cancel_campaign) {
						this.campaign = temp4[1];
					}
					if (temp4[0] == 'utmcmd' && !cancel_medium) {
						this.medium = temp4[1];
					}
					if (temp4[0] == 'utmctr' && !cancel_keyword) {
						this.keyword = temp4[1];
					}
					if (temp4[0] == 'utmcct' && !cancel_content) {
						this.adgroup = temp4[1];
					}
				}
			}
		}
	};

	this._setAll = function(src,med,camp,key,time,adgr,cust){
		var today = new Date();
		today.setTime(today.getTime()); 
		(src) ? this.source = src : this.source = " ";
		(med) ? this.medium = med : this.medium = " ";
		(camp) ? this.campaign = camp : this.campaign = " ";
		(key) ? this.keyword = key : this.keyword = " ";
		(time) ? this.timestamp = time : this.timestamp = today.getUTCFullYear() + '/' + (today.getUTCMonth() + 1) + '/' + today.getUTCDate();
		(adgr) ? this.adgroup = adgr : this.adgroup = " ";
		(cust) ? this.custom = cust : this.custom = " ";
	};

    this._setSource = function(x) {
        this.source = x;
    };
    this._setMedium = function(x) {
        this.medium = x;
    };
    this._setCampaign = function(x) {
        this.campaign = x;
    };
    this._setKeyword = function(x) {
        this.keyword = x;
    };
    this._setAdgroup = function(x) {
        this.adgroup = x;
    };
    this._setTimestamp = function(x) {
       if(x){ 
		this.timestamp = x;
	   }
	   else{
		var today = new Date();
		today.setTime(today.getTime()); 
	   	this.timestamp = today.getUTCFullYear() + '/' + (today.getUTCMonth() + 1) + '/' + today.getUTCDate();
	   }
    };
    this._setCustom = function(x) {
        this.custom = x;
    };
}

