function ToolbarAnalytics(toolbar_id, data){
    var toolbar_id = toolbar_id;					        	
	var analytics_array = {};
	
	(function(){
		for (var index in data)
			analytics_array[index] = new toolbar_analytics(toolbar_id, data[index]);					
	})();

	this.getAnalytics = function (index){
		return analytics_array[index].get_interface();
	};

	/**
	 *	Internal analytics
	 */				 				
	jQuery(document).ready(function() {
		for (var index in analytics_array){
			$("#" + index + " a").click(function() {
				analytics_array[index].internal("click",{'0':this.href});
			});
			$("#" + index + "_dropdown a").click(function() {
				analytics_array[index].internal("click",{'0':this.href});
			});			
		}
	});

	/***************************
	 *	Analytics Class 
	 ***************************/
	 
	/**
	 *	Constructor
	 *	@param toolbar_id
	 *	@param toolbar_widget_id
	 *	@param hash  
	 */  
	function toolbar_analytics(toolbar_id, toolbar_widget_id)
	{
	
	  	/*********************************************
	  	 *	Private variables and functions
	  	 *********************************************/
		var toolbar_id = toolbar_id;		   	     	
		var toolbar_widget_id = toolbar_widget_id;  
		var hash = Toolbar.getInternalHash();
		var hash_type = Toolbar.getController();
		var google = new GoogleAnalytics();
	
		/**
		 *	Sends details of analytic to server
		 *	@param transacion - special format (TDB)
		 *	@param action - which of the functions below were invoked 	 	 
		 */	 	
		var analyze = function(transaction, action, data){
			url = "/tb/toolbar_analytics/create";	
	
	        var has_data = false;
			var json_data = {};
			
			for (var index in data){
				has_data = true;
				json_data[index] = data[index];
			}
			
			if (has_data)
				google.track(json_data);
			
			post_data = {
		        "data[ToolbarAnalytic][toolbar_id]": toolbar_id,
				"data[ToolbarAnalytic][toolbar_widget_id]": toolbar_widget_id,
				"data[ToolbarAnalytic][hash_type]": hash_type,
				"data[ToolbarAnalytic][hash]": hash,
				"data[ToolbarAnalytic][transaction]": transaction,
				"data[ToolbarAnalytic][data]": $.param(json_data),
				"data[ToolbarAnalytic][action]": action
			};		 
		
			$.ajax({
				type: "POST",
				data: post_data,            
				url: url
			});
			
			delete post_data['data'];
	
			/**
			 *	This copies information in json_data to the post data, while keeping the order of post data intact and 
			 *	making sure that if there is a malicious attempt to overwrite an index in post data, we overwrite it
			 *	in json data first.	
			 *	
			 *	BOTH LOOPS NEEDED!!		 		 	  		 
			 */		 		
			for (var index in post_data)
				json_data[index] = post_data[index];
			
			for (var index in json_data)				
				post_data[index] = json_data[index];
			
			google.track(post_data);
		};  
		
	  	/*********************************************
	  	 *	Public variables and functions
	  	 *********************************************/	     	
		
		this.load = function(transaction){ analyze(transaction, "load", null);};
		this.click = function(transaction){ analyze(transaction, "click", null);};
		this.mouseover = function(transaction){ analyze(transaction, "mouseover", null);};
		this.custom = function(transaction, data){ analyze(transaction, "custom", data);};
		this.internal = function(transaction, data){ analyze(transaction, "internal", data);};
		
		function AnalyticsInterface (load, click, mouseover, custom){
			this.load = load;
			this.click = click;
			this.mouseover = mouseover;
			this.custom = custom;
		};
		
		var analytics_interface = new AnalyticsInterface(this.load, this.click, this.mouseover, this.custom);
		
		this.get_interface = function (){
			return analytics_interface;
		};
	}
		
					
}

