/** 
 * Manages event tracking for Google Analytics
 */
var TrackEvent = function() {
	/**
	 * Category ID
	 */
	var _category = 'en';
	
	/**
	 * Translations
	 */
  var _translate = {
    category: {
      'en': 'English',
      'it': 'Italian'
    },
    
    action: {
      'navigation': 'Navigation',
      'creds': 'Creds',
      'phones': 'Phones',
      'more': 'More',
      'footer': 'Footer',
      'art': 'Art'
    },
    
    label: {
      'it': 'Switch to Italian',
      'en': 'Switch to English',
      'iNQ': 'iNQ logo',
      'chat': 'INQ Chat 3G',
      'mini': 'INQ Mini 3G',
      'inq1': 'INQ1',
      'phones': 'Go to Phones',
      'art': 'Go to Art',
      'more': 'Go to More'
    },
    
    value: {
      'click': 'Click',
      'switch': 'Switch'
    }
  }
  
  /*
   * Translation helper
   */
  function _t(row) {
    var illegalChars = /\W/;

    if (illegalChars.test(row.category) == false) {  
      var category = (jQuery.trim(row.category) != '') ? _translate.category[row.category] : row.category;
      row.category = (typeof category != 'undefined') ? category : row.category;
    }

    if (illegalChars.test(row.action) == false) {  
      var action = (jQuery.trim(row.action) != '') ? _translate.action[row.action] : row.action;
      row.action = (typeof action != 'undefined') ? action : row.action;
    }
    
    if (illegalChars.test(row.label) == false) {  
      var label = (jQuery.trim(row.label) != '') ? _translate.label[row.label] : row.label;
      row.label = (typeof label != 'undefined') ? label : row.label;
    }
  
    if (illegalChars.test(row.value) == false) {  
      var value = (jQuery.trim(row.value) != '') ? _translate.value[row.value] : row.value;
      row.value = (typeof value != 'undefined') ? value : row.value;
    }
    
    return row;
  }
  
  /*
   * Send to Google Analytics
   * @param Object Details
   */
  function _sendLabel(row) {
    pageTracker._trackEvent(row.category, row.action, row.label, row.value);
  }

  /*
   * Bind events to be tracked
   */
  function _bindEvents() {
  	// Navigation item
  	$('a', '#navigation').click(function() { TrackEvent.addLabel({action: 'navigation', label: $(this).text()}) });
  	// $('a', '#cred-deck', '.navigation', '.thumbnails').click(function() { TrackEvent.addLabel({action: 'creds', label: $(this).children(0).attr('alt')}) });

  	// Buy phone
  	$('.phone-buy-click').click(function() {
  		TrackEvent.addLabel({action: 'phones', label: $(this).attr('title')});

  		// Prevent double recording of phone switch
  		window.location = $(this).attr('href');
  		return false;
  	});		

  	// Cred-deck thumbnails
  	$('.cred-thumbnail-click').click(function() {
  		TrackEvent.addLabel({action: 'creds', label: $(this).children(0).attr('alt')});
  	});

  	// Cred-deck Next click
  	$('.next', '#cred-deck').click(function() {
  		var index = creddeckapi.getIndex() + 1;

  		if (index == 10) index = 1;
  		if (index == 11) index = 2;

  		var tag = $('img', '.cred-thumbnail-click').parent().parent().get(index);
  		var id = $(tag).attr('id');
  	  var label = $(tag).children(0).children(0).attr('alt');

  		TrackEvent.addLabel({action: 'creds', label: label});
  	});
  	
  	// Art thumbnails
  	$('.art-thumbnail-click').click(function() {
  		TrackEvent.addLabel({action: 'art', label: $(this).children(0).attr('alt')});  		
  	});  	
  	
  	// Art Next click
  	$('.next', '#art').click(function() {
  		var index = artapi.getIndex() + 1;
  		
  		if (index == 10) index = 1;
  		if (index == 11) index = 2;  		
  		
  		var tag = $('img', '.art-thumbnail-click').parent().parent().get(index);
  		var id = $(tag).attr('id');
  	  var label = $(tag).children(0).children(0).attr('alt'); 		
  		  		
  		TrackEvent.addLabel({action: 'art', label: label});  
  	});  	   
  	
  	// Art Prev click
  	$('.prev', '#art').click(function() {
  		var index = artapi.getIndex() + 1;
  		
  		if (index == 10) index = 1;
  		if (index == 11) index = 2;  		
  		
  		var tag = $('img', '.art-thumbnail-click').parent().parent().get(index);
  		var id = $(tag).attr('id');
  	  var label = $(tag).children(0).children(0).attr('alt'); 		
  		  		
  		TrackEvent.addLabel({action: 'art', label: label});  
  	});
  	
  	// Art artists links
  	$('a', '.desc', '#art').click(function() {  	  
		  TrackEvent.addLabel({action: 'art', label: $(this).text()});
  	});  	
  	
  	// More links
  	$('a', '#more').click(function() {  	  
  	  var children = $(this).children(0);
  	  
	    if($(this).attr('title') != '') {
  		  TrackEvent.addLabel({action: 'more', label: $(this).attr('title')});
	    }
  		else {
  		  TrackEvent.addLabel({action: 'more', label: $(this).text()});
	    }
  	  
  	});     	
  	
  	// Footer links
  	$('a', '#footer').click(function() {  	  
  	  var children = $(this).children(0);
  	  
	    if($(this).attr('title') != '') {
  		  TrackEvent.addLabel({action: 'footer', label: $(this).attr('title')});
	    }
  		else {
  		  TrackEvent.addLabel({action: 'footer', label: $(this).text()});
	    }
  	  
  	});  	
  	
  	// Popup video click
  	// See templates/HomePage.ss
  } 

	var publicAPI = {
		/**
		 * Creates a TrackEvent instance
		 * @param Object Custom settings
		 */
		init : function(settings) {
			var settings = settings || {};
			var defaults = {

			};
			
			options = $.extend(defaults, settings);
			
      		var url = window.location.pathname;
      		var parts = url.split('/');

      		if (parts[1] == 'it') {
		        _category = 'it';
		      }
      
      		_bindEvents();
		},
		
		/**
		 * Prepare event tracking for sending
		 */
		addLabel: function(settings) {
			var settings = settings || {};
			var defaults = {
        		action: '',
		        label: '',
		        value: 'click'
    		};
						
			options = $.extend(defaults, settings);
			
			var action = options.action;
			var label = options.label;
			var value = options.value;
			
			if (action == 'creds' && label == '') label = 'Next';
						
			var $this = $(this);
						
			var row = {
			  category: _category,
			  action: action,
			  label: label,
			  value: value
			}
						
			row = _t(row);
			_sendLabel(row);			
		}
	}
	
	return publicAPI;
}();