(function($){
	$.analytics.services.doubleClick = {
			isready: false,
			settings: {},
			init: function(s, callback){
				var _this = this;
				_this.settings = s;
				_this.isready = true;
				$.log("tracking:double click:init called.");
				if($.isFunction(callback)) callback();
			},
			track: function(){
				var _this = this;
				if(!_this.isready){
					$.log("tracking:double click:not ready.");
					return;
				}
				
				if(arguments.length == 0){
					$.log("tracking:double click:must send parameters, this is a function after all!");
					return;
				}
				if(typeof(arguments[0]) === "undefined"){
	            	$.log("tracking:double click:invalid definition, nothing to track.");
	            	return;					
				}
				
				var definition = $.extend({source:"",cat:"",type:""}, arguments[0]);
				var callback = arguments[arguments.length-1];
				
				var contextValues = {};
				
				if(arguments.length > 2) {
					var sender = arguments[2];
					contextValues = arguments[1];
				}
				$.log("tracking:double click:tracking",definition.id, definition, contextValues);
				
				
				if( (typeof(definition) === "undefined") ||
					(typeof(definition.source) === "undefined" || definition.source === "") ||
					(typeof(definition.type) === "undefined" || definition.type === "") ||
					(typeof(definition.cat) === "undefined" || definition.cat === "") ) 
				{
					$.log("tracking:double click:invalid definition, nothing to track.");
					return;
				}

				var ord = Math.floor(Math.random() * 999999);
				var extraValues = "";
				if(window["goodyear"]){
					if(typeof(definition.session) !== "undefined"){
						var sessionIn = definition.session.split(",");
						for(index in sessionIn){
							if(sessionIn[index] == "ord") {
								ord = goodyear.session.id;
							} else {
								sessionIn[index] = sessionIn[index] + "=" + goodyear.session.id;
							}
						}
						
						var ordIndex = $.inArray("ord", sessionIn);
						if(ordIndex != -1) sessionIn.splice(ordIndex, 1);
						
						if(sessionIn.length > 0) {
							extraValues = ";" + sessionIn.join(";");
						}
					}
				}

                if(contextHasSearchByVechileInformation()){
                	var allVehicleVariables = String.format("{0}_{1}_{2}",
                			contextValues.year,
                			contextValues.make,
                			contextValues.model
                	).toUpperCase();
                	extraValues += ";u2=" + allVehicleVariables.replace(/ /g, "_");
                }
				
                if(contextHasSearchBySizeInformation()){
                	var allSizeVariables = String.format("{0}_{1}_{2}_{3}",
                			contextValues.metric,
                			contextValues.width,
                			contextValues.aspect,
                			contextValues.rim
                	).toUpperCase();
                	extraValues += ";u3=" + allSizeVariables;
                }
                
                if(typeof(contextValues.location) !== "undefined"){
                	extraValues += ";u4=" + contextValues.location.replace(/ /g, "_");
                }
	            
	            var sourceTag = String.format("{0};src={1};type={2};cat={3};ord={4}", 
	            		_this.settings.dartUrl, 
	            		definition.source, 
	            		definition.type, 
	            		definition.cat, 
	            		ord);

	            var container = $("<div>").css({ width: 0, height: 0, display: "none" }).appendTo("body");
	            $("<iframe>").attr("src", sourceTag + extraValues + "?").attr("height", 1).attr("width", 1).attr("frameborder", 0).appendTo(container);
	            
	            $.log("tracking:double click:",definition.id,"done.");
	            if($.isFunction(callback)) callback();
	            
	            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);
