/*global $, window, _gaq, gaObj  */
/*jslint white: true, browser: true, undef: true, eqeqeq: true, newcap: true */
/**
 * Google analytics setup file. Includes the standard Google Analytics tracking code which can be used on any page
 * and custom event tracking methods inside the gaEvent object. Custom event can be tracked by defining a method to
 * capture them, then wanging the values to gaEvent.push
 * 
 * @author grockett
 * @version 1
 * @requires jQuery
 */

/** 
 * gaPage is used to hold methods related to the standard Google Analytics tracking code
 * takes no parameters returns nothing, downloads ga.js from Google
 * @constructor
 */
var gaPage = {};

var virtualPageView = {};

virtualPageView.track = function() {
    var pageIs = $('#pageIdentifier').attr('class');
    _gaq.push(['_trackPageview', pageIs]);
};

/**
 * init downloads the ga.js file from google-analytics.com
 */
gaPage.init = function () {
    (function () {
        var ga = document.createElement('script');
        ga.type = 'text/javascript';
        ga.async = true;
        ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(ga, s);
    })();
};

/** 
 * gaEvent is used to hold methods for tracking user events
 * @constructor
 */
var gaEvent = {};

/** 
 * push sends the event to GA
 * @param {Object} category The category to list the event as, e.g. Signup Error Messages
 * @param {Object} action The action is used to specify a particular page, got from pageIdentifier
 * @param {Object} label The label can be any text, e.g. the error message displayed, the name of the last filled in field etc
 * @member gaEvent
 */
gaEvent.push = function (category, action, label) {
    _gaq.push(['_trackEvent', category, action, label]);
};

/** 
 * getErrorValues gets any displayed error messages. Is only run if error messages are displayed
 * @member gaEvent
 */
gaEvent.getErrorValues = function () {
    if ($('#signupCenter').length > 0) {
        var category = "Signup Error Messages";
        var action = $('#pageIdentifier').attr('class');
        /* Select SIGNUP validation errors from UL and H2 elements*/
        $('ul.errorMessages li,h2.errorMessages').each(function () {
            var label = $(this).html();
            gaEvent.push(category, action, label);
        });
    }
	if ($('#faultTool').length > 0) {
		var category = "KBD Faults Error Messages";
		var action = "Faults Errors";
		$('div.genericMessage.error p').each(function(){
			var label = $(this).html();
			gaEvent.push(category, action, label);
		});
	}
};

/**
 * registerEditClicks registers the click event for KBD Faults edit buttons
 * @member gaEvent
 */
gaEvent.registerEditClicks = function () {
	$('div.questionEdit a.button').click(function(){
		var category = "KBD Edit Click";
		var label = $(this).parents('li').children('div.answerText').text();
		var action = $(this).parents('li').children('div.questionText').text();
		gaEvent.push(category, action, label);
	});
}

/**
 * The init function runs the various event getters according to whats displayed on the current page
 * @member gaEvent
 */
gaEvent.init = function () {
    /* if there are error messages displayed */
    if ($('ul.errorMessages').length > 0 || $('h2.errorMessages').length > 0 || $('.genericMessage.error p').length > 0) {
        gaEvent.getErrorValues();
    }
	/* KBD faults event tracking */
	if ($('#faultTool').length > 0) {
		gaEvent.registerEditClicks();
	}
	
};

/**
 * the standard jQuery ready whatsit
 * @ignore
 */
$(document).ready(function () {
    gaPage.init();
    gaEvent.init();
    if($('#pageIdentifier').length > 0) {
        virtualPageView.track();
    }
    if('live' in $()) {
    	
	    $('.event-click').live('click', function() {
	        var category = 'Banner / Button Tracking';
	        var label = $(this).data('label');
	        var action = 'Click';
	        
	        // Ensure that "push" is a function.
	        if ("function" == typeof(_gaq.push)) {
	            if (label) {
	                // If the value is undefined, make it empty.
	                if(typeof(label) == 'undefined' || typeof(label) != 'string') {
	                    label = '';
	                }
	                _gaq.push(['_trackEvent', category, label, action]);
	            } else {
	                _gaq.push(['_trackEvent', 'click', 'portal', $(this).attr('href')]);
	            }
	        }
	    });
	    
	    if ($('.event-impression').length > 0) {
	        
	        var element = $('.event-impression');
	        var category = 'Banner / Button Tracking';
	        var action = 'Impression';
	        
	        // Ensure that "push" is a function.
	        if ("function" == typeof(_gaq.push) && element.length > 0) {
	            element.each(function () {
	                var label = $(this).data('label');
	                _gaq.push(['_trackEvent', category, label, action]);
	            });
	        }
	    };
	    
    }
    if ($('.thistleError').length > 0) {
        $('.thistleError').each(function(){
            var category = 'Signup payment errors';
            var label = $(this).data('errorType');
            var action = $(this).data('errorCode');
            if ("function" == typeof(_gaq.push)) {
                _gaq.push(['_trackEvent', category, action, label]);
            }
        });   
    };
});

