(function($){
	$.analytics.services.omniture = {
			isready: false,
			settings: {},
			init: function(s, callback){
				var _this = this;
				_this.settings = s;
				$.log("tracking:omniture:init called.");
				$.getScript(s.siteCatalistPath, function () {
					$.log("tracking:omniture:script downloaded.");
					_this.isready = true;
					if($.isFunction(callback)) callback();
				});
			},
			track: function(){
				var _this = this;
				if(!_this.isready){
					$.log("tracking:omniture:not ready.");
					return;
				}

				if(arguments.length == 0){
					$.log("tracking:omniture:must send parameters, this is a function after all!");
					return;
				}
				if(typeof(arguments[0]) === "undefined"){
	            	$.log("tracking:omniture:invalid definition, nothing to track.");
	            	return;					
				}
				
				var definition = $.extend({ss:"",sub:"",a:"",sel:"",od:"",cta:""}, arguments[0]);
				var callback = arguments[arguments.length-1];

				var contextValues = {};
				
				if(arguments.length > 2){
					var sender = arguments[2];
					contextValues = arguments[1];
				}

				$.log("tracking:omniture:tracking", definition.id, definition, contextValues);
				
	            var trackingDate = new Date();
	            var trackingDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
	            var trackingDay = trackingDays[trackingDate.getDay()];
	            var trackingDayType = trackingDate.getDay() == 0 || trackingDate.getDay() == 6 || trackingDate.getDay() == 7 ? "Weekend" : "Weekday";
			
	            /* clear omniture variables */
	            for(property in s_gdyr){
	            	if(property.indexOf("prop") == 0 || property.indexOf("eVar") == 0){
	            		s_gdyr[property] = "";
	            	}
	            }
	            var skipList = buildSkipList();
	            /* create pagename*/
	            s_gdyr.pageName = trimParameter(String.format("{0} | {1} | {2} | {3} | {4} | {5}", 
	            		_this.settings.domain, 
	            		definition.ss, 
	            		definition.sub, 
	            		definition.a, 
	            		definition.sel === definition.a ? "" : definition.sel, 
	            		definition.od
	            ).toUpperCase());
	            
	            if(s_gdyr.pageName === _this.settings.domain){
	            	$.log("tracking:omniture:",definition.id,"invalid definition, nothing to track.");
	            	return;
	            }
	            
	            s_gdyr.events = "";
	            if (typeof (definition.events) !== "undefined") {
	                var events = definition.events.split(",");
	                if (events.length > 0) {
	                	for(eventIndex in events){
	                		s_gdyr.events = s_gdyr.apl(s_gdyr.events, String.format("event{0}", events[eventIndex]), ',', 1);
	                	}
	                }
	            }
	            
	            /* create properties */
	            if (!skipList["prop1"]) s_gdyr.prop1 = definition.ss.toUpperCase();
	            if (!skipList["prop2"]) s_gdyr.prop2 = definition.sub.toUpperCase();
	            if (!skipList["prop3"]) s_gdyr.prop3 = String.format("{0} | {1}", 
	            		definition.ss, 
	            		definition.sub
	            ).toUpperCase();
	            if (!skipList["prop4"]) s_gdyr.prop4 = definition.a.toUpperCase();
	            if (!skipList["prop5"]) s_gdyr.prop5 = String.format("{0} | {1}", 
	            		definition.sub, 
	            		definition.a
	            ).toUpperCase();
	            if (!skipList["prop6"]) s_gdyr.prop6 = String.format("{0} | {1} | {2}", 
	            		definition.ss, 
	            		definition.sub, 
	            		definition.a
	            ).toUpperCase();
	            if (!skipList["prop7"]) s_gdyr.prop7 = definition.sel.toUpperCase();
                if (!skipList["prop8"]) s_gdyr.prop8 = String.format("{0} | {1}", 
                		definition.ss, 
                		definition.a
                ).toUpperCase();
                if (!skipList["prop9"]) s_gdyr.prop9 = String.format("{0} | {1} | {2}",
                		definition.sub,
                		definition.a,
                		definition.sel
                ).toUpperCase();
                if (!skipList["prop10"]) s_gdyr.prop10 = String.format("{0} | {1} | {2} | {3}",
                		definition.ss, 
                		definition.sub,
                		definition.ss, 
                		definition.a
                ).toUpperCase();
                if (!skipList["prop11"]) s_gdyr.prop11 = definition.od.toUpperCase();
                if (!skipList["prop12"]) s_gdyr.prop12 = String.format("{0} | {1}", 
                		definition.sel,
                		definition.od
                ).toUpperCase();
                if (!skipList["prop13"]) s_gdyr.prop13 = String.format("{0} | {1} | {2}", 
                		definition.a,
                		definition.sel,
                		definition.od
                ).toUpperCase();
                if (!skipList["prop14"]) s_gdyr.prop14 = String.format("{0} | {1} | {2} | {3}",
                		definition.sub, 
                		definition.a, 
                		definition.sel, 
                		definition.od
                ).toUpperCase();
                if (!skipList["prop15"]) s_gdyr.prop15 = String.format("{0} | {1} | {2} | {3} | {4}", 
                		definition.ss, 
                		definition.sub, 
                		definition.a, 
                		definition.sel, 
                		definition.od
                ).toUpperCase();
                
                if (!skipList["prop16"]) s_gdyr.prop16 = "";
                if (!skipList["prop17"]) s_gdyr.prop17 = "";
                if (!skipList["prop18"]) s_gdyr.prop18 = "";
                if (!skipList["prop19"]) s_gdyr.prop19 = "";
                if (!skipList["prop20"]) s_gdyr.prop20 = "";
                if (!skipList["prop21"]) s_gdyr.prop21 = "";
                if (!skipList["prop22"]) s_gdyr.prop22 = "";
                if (!skipList["prop23"]) s_gdyr.prop23 = "";
                if (!skipList["prop24"]) s_gdyr.prop24 = "";
                if (!skipList["prop25"]) s_gdyr.prop25 = "";
                if (!skipList["prop26"]) s_gdyr.prop26 = "";
                if (!skipList["prop27"]) s_gdyr.prop27 = "";
                
                if (!skipList["prop28"]) s_gdyr.prop28 = trackingDate.toTimeString();
                if (!skipList["prop29"]) s_gdyr.prop29 = trackingDay;
                if (!skipList["prop30"]) s_gdyr.prop30 = trackingDayType;
                if (!skipList["prop31"]) s_gdyr.prop31 = s_gdyr.getDaysSinceLastVisit("s_lv");
                if (!skipList["prop32"]) s_gdyr.prop32 = _this.settings.domain.toUpperCase();
                s_gdyr.eVar31 = s_gdyr.getDaysSinceLastVisit("s_lv");

                if(typeof(definition.omniture) !== "undefined"){
                	for(property in definition.omniture){
                		$.log("tracking:omniture:",definition.id,":found pattern exception for", property, "with value", definition.omniture[property]);
                		s_gdyr[property === "pagename" ? "pageName" : property] = analyzePattern(definition.omniture[property]).toUpperCase();
                	}
                }
                
                s_gdyr.trackExternalLinks = false;

                if(contextHasSearchByVechileInformation()){
                	var allVehicleVariables = String.format("{0} | {1} | {2} | {3}",
                			contextValues.year,
                			contextValues.make,
                			contextValues.model,
                			contextValues.versionAndOption
                	).toUpperCase();

                	s_gdyr.eVar6 = allVehicleVariables;
                	s_gdyr.eVar9 = typeof(contextValues.year) !== "undefined" ? contextValues.year : "";
                	s_gdyr.eVar13 = allVehicleVariables;
                }
                
                if(contextHasSearchBySizeInformation()){
                	var allSizeVariables = String.format("{0} | {1} | {2} | {3}",
                			contextValues.metric,
                			contextValues.width,
                			contextValues.aspect,
                			contextValues.rim
                	).toUpperCase();
                	s_gdyr.eVar7 = allSizeVariables;
                	s_gdyr.eVar15 = typeof(contextValues.aspect) !== "undefined" ? contextValues.aspect : "";
                }
                
                if(typeof(contextValues.recentlyViewedDealers) !== "undefined"){
                	s_gdyr.eVar16 = contextValues.recentlyViewedDealers
                		.toUpperCase();
                }
                
                if(typeof(contextValues.tireTier) !== "undefined"){
                	s_gdyr.eVar17 = contextValues.tireTier
                		.toUpperCase();
                }

                if(typeof(contextValues.page) !== "undefined" && !skipList["evar18"]){
                	s_gdyr.eVar18 = ("page_" + contextValues.page).toUpperCase();
                }
                
                if(typeof(contextValues.dealerlist) !== "undefined"){
                	s_gdyr.eVar24 = contextValues.dealerlist.toUpperCase();
                }
                
                if(typeof(contextValues.location) !== "undefined"){
                	s_gdyr.eVar25 = contextValues.location.toUpperCase();
                }
                
				if(window["goodyear"]){
					s_gdyr.eVar26 = goodyear.session.id;
				}
                
                if(typeof(contextValues.tirelist) !== "undefined"){
                	s_gdyr.eVar35 = contextValues.tirelist.toUpperCase();	
                }
                
	            /* trim extra characters from the properties */
	            for(property in s_gdyr){
	            	if(property.indexOf("prop") == 0 || property.indexOf("eVar") == 0){
	            		s_gdyr[property] = trimParameter(s_gdyr[property]);
	            	}
	            }

	            s_gdyr.t();

	            $.log("tracking:omniture:",definition.id,"done.");
	            if($.isFunction(callback)) callback(s_gdyr);
	            
	            function buildSkipList(){
		            var skip = new Array();
		            for (var i = 1; i < 36; i++) { skip["prop" + i] = false; }
		            if (typeof (definition.skip) !== typeof (undefined)) {
		                $.each(definition.skip.split(","), function (i, v) {
		                	if(v.lastIndexOf("e") == 0) {
		                		skip["evar" + v.substring(1)] = true;
		                	} else {
		                		skip["prop" + v] = true;
		                	}
		                });
		            }
		            return skip;
	            }
	            function analyzePattern(patternToAnalyze){
	            	var pattern = patternToAnalyze;
	            	for(property in definition){
	            		pattern = pattern.replace(
	            				new RegExp("{" + property + "}", "g"), 
	            				definition[property]
	            		);
	            	}
	            	return pattern;
	            }
	            function trimParameter(parameter) {
	                if (typeof (parameter) !== "string") return "";
	                var trimmedParam = parameter;
	                var lastChar = trimmedParam.charAt(trimmedParam.length - 1);
	                while (lastChar === " " || lastChar === "|" || lastChar === "-") {
	                    trimmedParam = trimmedParam.substring(0, trimmedParam.length - 1);
	                    lastChar = trimmedParam.charAt(trimmedParam.length - 1);
	                }
	                trimmedParam = escape(trimmedParam)
	            		.replace(/%26TRADE%3B/g, "")
	            		.replace(/%26trade%3B/g, "")
	                	.replace(/%u2122/g, "")
	                	.replace(/%26REG%3B/g, "")
	                	.replace(/%26REG/g, "")
	                	.replace(/%26reg%3B/g, "")
	                	.replace(/%26reg/g, "")
	                	.replace(/%AE%20/g, " ")
	                	.replace(/%AE/g, "");
	                return unescape(trimmedParam)
                		.replace(/&TRADE;/g, "")
	                	.replace(/&REG;/g, "")
	                	.replace(/&REG/g, "")
	                	.replace(/&reg;/g, "")
	                	.replace(/&reg/g, "")
	                	.replace(/~/g,";")
	                	.replace(/\|  \|/g, "|");
	            }
	            function contextHasSearchByVechileInformation(){
	            	return (typeof(contextValues.year) !== "undefined" && contextValues.year !== "") &&
	            			(typeof(contextValues.make) !== "undefined" && contextValues.make !== "") &&
	            			(typeof(contextValues.model) !== "undefined" && contextValues.model !== "") &&
	            			(typeof(contextValues.versionAndOption) !== "undefined" && contextValues.versionAndOption !== "");
	            }
	            function contextHasSearchBySizeInformation(){
	            	return (typeof(contextValues.metric) !== "undefined" && contextValues.metric !== "") &&
	            			(typeof(contextValues.width) !== "undefined" && contextValues.width !== "") &&
	            			(typeof(contextValues.aspect) !== "undefined") &&
	            			(typeof(contextValues.rim) !== "undefined" && contextValues.versionAndOption !== "");
	            }
			}
	};
})(jQuery);
