//
// Event tracking namespace, contains methods to attach
// event handlers to clicks and other things we want to track
//

var Base64 = {
    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) +
            Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);

        }

        return output;
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }
};

Tracker = function (props) {
    this.debug  = (props.debug) ? true : false;
    this.events = (props.events) ? props.events : [];
    
    // XXX - KLN - Pass these values in as properties!
    this.partnerCode = (props.partnerCode) ? props.partnerCode : 'undefined';
    this.source      = (props.source)      ? props.source      : 'undefined';
    this.instanceId  = (props.instanceId)  ? props.instanceId  : 'undefined';
    this.campaignId  = (props.campaignId)  ? props.campaignId  : 'undefined';
    this.providerId  = (props.providerId)  ? props.providerId  : 'undefined';
    this.trackingHost  = (props.trackingHost)  ? props.trackingHost  : 'undefined';
	this.sessionId   = (props.sessionId)   ? props.sessionId   : 'undefined';

    // Declare tracking variables
    this.userAgent        = 'undefined';
    this.ipAddress        = 'undefined';
    this.referer          = 'undefined';
    this.requestUrl       = 'undefined';
    this.loadTime         = 'undefined';
    this.startTime        = 'undefined';
    this.incomingKeywords = 'undefined';

	this.initRP();
};
 
Tracker.prototype = {
    initRP : function () {
        this.parsePage();
    },
	// Capture the event and send it to the handler to construct the JSON
    fireEvent : function (data) {
        this.logMessage("fireEvent");
        this.logMessage(data);

        if (this.debug) {
            this.logMessage(data);
        } else {
            if (data.data == undefined || data.data === undefined) {
                data.data = '{}';
            }
            var message = { 
                wvLogMsg : {
                    e: {
                        name: data.event_name,
                        details: data.data
                    },
                    p: this.partnerCode,
                    r: this.source,
                    c: this.campaignId,
                    d: (new Date()).getTime()
             }  
            };
            
            this.logMessage(message);
            this.sendMessage(this.trackingHost + 'api/log-rp', message);
        }
    },
	toJSON : function(obj) { 
		switch (typeof obj) {  
			case 'object':   
				if (obj) {    
					var list = [];    
					if (obj instanceof Array) {     
						for (var i=0;i < obj.length;i++) {      
							list.push(this.toJSON(obj[i]));     
						}     
						return '[' + list.join(',') + ']';    
					} 
					else {     
						for (var prop in obj) {      
							list.push('"' + prop + '":' + this.toJSON(obj[prop]));     
						}     
						return '{' + list.join(',') + '}';    
					}   
				} 
				else {    
					return 'null';   
				}  
			case 'string':   
				return '"' + obj.replace(/(["'])/g, '\\$1') + '"';  
			case 'number':  
			case 'boolean':   
				return new String(obj); 
		}
	},
    // Send the message that is passed in
    sendMessage : function (url, msg) {
        var json = null;
		try {
			json = $.toJSON(msg);
		}
		catch(err) {
			json = this.toJSON(msg);
		}
        var img = new Image(1,1);
        img.onLoad = function () { };
        url = url + "?json=" + encodeURIComponent(json);
        if(this.sessionId)
            url = url + "&sessionid=" + this.sessionId;
        img.src = url
    },
    // Calculate the page load time
    getLoadTime : function () {
        this.loadTime = (new Date()).getTime() - this.startTime;
    },
    // Log the session information, set cookie
    startSession : function () {
        // Construct the message to send to session service
        var message = {
            instance: this.instanceId,
            campaign: this.campaignId,
            url     : document.location.href,
            referer : document.referrer,
            provider: this.providerId
        };
        
        // Get this from the document
        this.requestUrl = document.URL;
        
        // Send the message to get more session data
        this.sendMessage(this.trackingHost + '/api/session', message);
    },
    // Log the start of the page load
    startPageLoad : function () {
        this.logMessage("startPageLoad");

        this.startTime = (new Date()).getTime();
    },
    // Handle page load event
    postPageLoad : function () {
        this.logMessage("postPageLoad");

        var message = {
            event_name : 'page_load',
            data : {
                load_time: this.loadTime
            }
        };
        
        this.sendMessage(message);
    },

    logMessage : function (msg) {
        if (window.console && window.console.log) {
          window.console.log(msg);
        } else {
          // alert( msg);
        }
    },    
   
   	logClick: function (url, linkId, linkClass) {
		this.fireEvent({event_name : "Click",
						 data: { url: url}
		});
    },
    logVideoView: function () {
        this.fireEvent({event_name : "VideoView", 
						data:{}
		});
    },
    logPrint: function () {
        this.fireEvent({event_name : "PagePrint", 
						data:{}
		});
    },
    logBookmark: function (url) {
        this.fireEvent({event_name : "Bookmark", 
						data: { url: url}
        });
    },
    logDirectionsTo: function (url) {
        this.fireEvent({event_name : "DirectionsTo", 
						data: {url: url}
        });
    },
    logDirectionsFrom: function (url) {
        this.fireEvent({event_name : "DirectionsFrom", 
						data: {url: url}
        });
    },
    logEmailForm: function (to, from, subject, body) {
        this.fireEvent({event_name : "EmailForm", 
						data: {target: Base64.encode(to),
							   sender_name: from,
							   sender_email: from,
							   sender_message: body,
							   brand: ""}
        });
    },
    logPageLoad: function (url, loadTime) {
        this.fireEvent({event_name : "PageLoad", 
						data: { }
        });
    },
    logFormSubmit: function (formVariables) {
        this.fireEvent({event_name : "FormSubmit", 
						data: formVariables
		});
    },
	parsePage: function () {
        if (this.isSessionCreated() == true) {
            var lastHtml = "#%&(@#&%^#@";
            for (var linkIndex = 0; linkIndex < document.links.length; linkIndex++) {
                if (document.links[linkIndex].href.substring(0, 7).toLowerCase() != "mailto:" && document.links[linkIndex].href.substring(0, 11).toLowerCase() != "javascript:") {
                    if (lastHtml == document.links[linkIndex].innerHTML) {
                        continue;
                    }
                    lastHtml = document.links[linkIndex].innerHTML;
                    var childObjects = document.createElement('div');
                    for (var i = document.links[linkIndex].childNodes.length - 1; i >= 0; i--) {
                        var o = document.links[linkIndex].childNodes[i];
                        childObjects.appendChild(o);
                    }
                    document.links[linkIndex].href = this.prepareLink(document.links[linkIndex].href);
                    for (var i = childObjects.childNodes.length - 1; i >= 0; i--) {
                        var child = childObjects.childNodes[i];
                        document.links[linkIndex].appendChild(child);
                    }
                }
            }
            for (var formIndex = 0; formIndex < document.forms.length; formIndex++) {
                if (document.forms[formIndex].action != null && document.forms[formIndex].action != "" && document.forms[formIndex].action.substring(0, 11).toLowerCase() != "javascript:") {
                    document.forms[formIndex].action = this.prepareLink(document.forms[formIndex].action);
					document.forms[formIndex]._initialAction = document.forms[formIndex].action;
                }
            }
        }
    },
	prepareLink: function (uri) {
        var tmp = uri.split('#');
        if (tmp[0].indexOf('?') < 0) {
            tmp[0] = tmp[0] + '?wvsessionid=' + this.sessionId;
        } else {
            var index = tmp[0].indexOf('?');
            var base = tmp[0].substring(0, index + 1);
            var qs = tmp[0].substring(index + 1);
            qs = qs.replace(/\+/g, ' ');
            var args = qs.split('&');
            for (var qsIndex = 0; qsIndex < args.length; qsIndex++) {
                var pair = args[qsIndex].split('=');
                if (decodeURIComponent(pair[0]).toLowerCase() != 'wvsessionid') {
                    base += args[qsIndex] + '&';
                }
            }
            tmp[0] = base + 'wvsessionid=' + this.sessionId;
        }
        return tmp.join('#');
    },
	isSessionCreated: function () {
        return (this.sessionId != null && this.sessionId != "");
    }
};

