﻿if (typeof ($) == 'undefined') alert('Vehix.Web.Omniture.js requires the jquery framework');
if (typeof ($.cookie) == 'undefined') alert('Vehix.Web.Omniture.js requires the jquery cookie framework');
if (typeof (s_gi) == 'undefined') alert('Vehix.Web.Omniture.js requires the omniture s_code framework');
if (typeof (Vehix) == 'undefined') Vehix = new Object();
if (typeof (Vehix.Web) == 'undefined') Vehix.Web = new Object();
if (typeof (Vehix.Web.Omniture) == 'undefined') Vehix.Web.Omniture = new Object();
Vehix.Web.Omniture.serializePage = function()
{
	var serial = $.cookie('page-serial');
	if(serial != null) serial = serial.split('|');
	if (serial == null || serial.length < 2 || serial[0] != document.location)
	{
		var date = new Date();
		serial = parseInt(((date.getTime() / 1000) / 60)) + '' + parseInt(Math.random() * 100000000);
		$.cookie('page-serial', document.location + '|' + serial);
	}
	else serial = serial[1];
	return serial;
}
Vehix.Web.Omniture.serializeEvent = function(eventName)
{
	return eventName + ':' + Vehix.Web.Omniture.serializePage();
}
Vehix.Web.Omniture.Behavior = function()
{
	this._events = null;
	this._vars = [];
	this._products = null;
	this._trackingEvents = false;
	this._trackingProducts = false;
}
Vehix.Web.Omniture.Behavior.prototype =
{
	_appendTo: function(list, value, delimiter) {
		if (list == null || list == 'None') {
			list = '';
		}
		if (typeof (list) != 'undefined' && list != null && list.length > 0) {
			list += delimiter;
		}
		list += value;
		return list;
	},
	addEvent: function(name) {
		this._events = this._appendTo(this._events, name, ',');
		this._trackingEvents = true;
		return this;
	},
	addVar: function(id, value) {
		this._vars['eVar' + id] = value;
		return this;
	},
	addProduct: function(category, name, quantity, price, event) {
		if (typeof (category) == 'undefined' || category == null) {
			category = '';
		}
		if (typeof (name) == 'undefined' || name == null) {
			name = '';
		}
		if (typeof (quantity) == 'undefined' || quantity == null) {
			quantity = '';
		}
		if (typeof (price) == 'undefined' || price == null) {
			price = '';
		}
		if (typeof (event) == 'undefined' || event == null) {
			event = '';
		}
		var product = category + ';' + name;
		if (quantity != '' | price != '' | event != '') {
			product += ';' + quantity;
		}
		if (price != '' | event != '') {
			product += ';' + price;
		}
		if (event != '') {
			product += ';' + event;
		}
		this._products = this._appendTo(this._products, product, ',');
		this._trackingProducts = true;
		return this;
	},
	trackLink: function(element) {
		if (typeof (s_account) != 'undefined') {
			var ls = new s_gi(s_account);
			if (this._events != null) {
				ls.events = this._events;
				if (this._events.indexOf(",") != -1) {
					var arr = this._events.split(",");
					for (var i; i < arr.length(); i++) {
						if (arr[i].indexOf(":") != -1) {
							ls.linkTrackEvents += this._events.split(":")[0] + ",";
						}
						else {
							ls.linkTrackEvents += this._events + ",";
						}
					}
					ls.linkTrackEvents = ls.linkTrackEvents.substring(0, ls.linkTrackEvents.length - 1);
				}
				else if (this._events.indexOf(":") != -1) {
					ls.linkTrackEvents = this._events.split(":")[0];
				}
				else {
					ls.linkTrackEvents = this._events;
				}

			}
			if (this._vars != null) {
				for (var key in this._vars) {
					if (key.match(/eVar.+/)) {
						value = this._vars[key];
						ls.linkTrackVars = this._appendTo(ls.linkTrackVars, key, ',');
						eval('ls.' + key + '=\'' + value + '\'');
					}
				}
				if (this._trackingProducts) {
					ls.linkTrackVars = this._appendTo(ls.linkTrackVars, 'products', ',');
				}
				if (this._trackingEvents) {
					ls.linkTrackVars = this._appendTo(ls.linkTrackVars, 'events', ',');
				}
			}
			if (this._products != null) {
				ls.products = this._products;
			}

			s_objectID = element.name;
			if (element.tagName == null || element.tagName.toLowerCase() != 'a') element = null;
			ls.tl(element, 'o', this._products);
		}
		return this;
	},
	trackLink: function(element) {
		if (typeof (s_account) != 'undefined') {
			var ls = new s_gi(s_account);
			if (this._events != null) {
				ls.events = this._events;
				if (this._events.indexOf(",") != -1) {
					var arr = this._events.split(",");
					for (var i; i < arr.length(); i++) {
						if (arr[i].indexOf(":") != -1) {
							ls.linkTrackEvents += this._events.split(":")[0] + ",";
						}
						else {
							ls.linkTrackEvents += this._events + ",";
						}
					}
					ls.linkTrackEvents = ls.linkTrackEvents.substring(0, ls.linkTrackEvents.length - 1);
				}
				else if (this._events.indexOf(":") != -1) {
					ls.linkTrackEvents = this._events.split(":")[0];
				}
				else {
					ls.linkTrackEvents = this._events;
				}

			}
			if (this._vars != null) {
				for (var key in this._vars) {
					if (key.match(/eVar.+/)) {
						value = this._vars[key];
						ls.linkTrackVars = this._appendTo(ls.linkTrackVars, key, ',');
						eval('ls.' + key + '=\'' + value + '\'');
					}
				}
				if (this._trackingProducts) {
					ls.linkTrackVars = this._appendTo(ls.linkTrackVars, 'products', ',');
				}
				if (this._trackingEvents) {
					ls.linkTrackVars = this._appendTo(ls.linkTrackVars, 'events', ',');
				}
			}
			if (this._products != null) {
				ls.products = this._products;
			}

			s_objectID = element.name;
			if (element.tagName == null || element.tagName.toLowerCase() != 'a') element = null;
			ls.tl(element, 'o', this._products);
		}
		return this;
	},
	trackPage: function(element) {
		if (typeof (s_account) != 'undefined') {
			var ls = new s_gi(s_account);
			if (this._events != null) {
				ls.events = this._events;
				ls.linkTrackEvents = this._events;
			}
			if (this._vars != null) {
				for (var key in this._vars) {
					if (key.match(/eVar.+/)) {
						value = this._vars[key];
						ls.linkTrackVars = this._appendTo(ls.linkTrackVars, key, ',');
						eval('ls.' + key + '=\'' + value + '\'');
					}
				}
				if (this._trackingProducts) {
					ls.linkTrackVars = this._appendTo(ls.linkTrackVars, 'products', ',');
				}
				if (this._trackingEvents) {
					ls.linkTrackVars = this._appendTo(ls.linkTrackVars, 'events', ',');
				}
			}
			if (this._products != null) {
				ls.products = this._products;
			}
			ls.t();
		}
		return this;
	}
}
