// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// TopUp.images_path = "/assets/top_up/";

jQuery(window).load(function() {
    // Shadowbox.init();
    //function faceboxClickHandler() {
    //  jQuery.facebox.loading(true);
    //
    //  // support for rel="facebox.inline_popup" syntax, to add a class
    //  // also supports deprecated "facebox[.inline_popup]" syntax
    //  var klass = this.rel.match(/facebox\[?\.(\w+)\]?/);
    //  if (klass) klass = klass[1];
    //
    //  jQuery.facebox.fillFaceboxFromHref(this.href, klass);
    //  return false;
    //}
    // jQuery('a[rel*=facebox]').live("click", faceboxClickHandler);

    jQuery("a[rel*=facebox]").facebox();
    jQuery.catface.settings['time'] = 10;
    jQuery("a[rel*=catface]").catface();
});

if (! jQuery.zx) {  jQuery.zx = {};  }

/*
 * This plugin depends on the google-analytics plugin for jQuery
 */

(function($) {
  /**
   * Adds click tracking to elements. Usage:
   *
   *  $('a').zxLiveTrack('click')
   *
   * Uses Google Analytics event tracking.  Will by default just report a click on the
   * element's href, id, or class attribute value.  Can be overridden with an options
   * hash and various data-* settings on the element itself.
   *
   *
   *
   *
   */
  $.fn.zxLiveTrack = function(event, options) {
    // Add live event handler to a jQuery selector
    var selector = this.selector;

    if (!event) event = 'click';

    if (typeof event == 'object') {
      options = event;
      event = options['event'] || 'click';
    }

    // Use default options, if necessary
    var settings = $.extend({}, $.fn.zxLiveTrack.defaults, options);

    trackFunc = function(jsevent) {
      var element = $(this);

      // Merge custom options with defaults.
      var category = evaluate(jsevent, 'category');
      var action   = evaluate(jsevent, 'action');
      var label    = evaluate(jsevent, 'label');
      var value    = evaluate(jsevent, 'value');
      var event_name = evaluate(jsevent, 'event-name');

      var message = "category:'" + category + "' action:'" + action + "' label:'" + label + "' value:'" + value + "'";

      debug('LiveTracking ' + event_name + ' ' + message);

      // Should we skip internal links? REFACTOR
      var skip = settings.skip_internal && (element[0].hostname == location.hostname);

      if(!skip) {
        $.trackEvent(category, action, label, value);
        debug('Tracked ' + message);
        // alert("Tracked " + message);
      } else {
        debug('Skipped ' + message);
      }

      return true;
    };

    $(selector).live(event + '.livetrack', trackFunc);

    /**
     * Evaluates a setting for a live tracked element.  Returns a value from one
     * of the following sources:
     *
     * Category:
     *   - data-track-category
     *   - a value supplied in the binding options hash
     *   - the global default category setting /function result (usually "internal" or "external")
     *
     * Action:
     *  - data-track-action
     *  - a value supplied in the binding options hash
     *  - the Javascript event action
     *  - global default action setting
     *  - 'click'
     *
     * Label:
     *  - data-track-label
     *  - a value supplied in the binding options hash
     *  - te

     * - data-track-value
     *
     * If third parameter is a string: returns the value of the second parameter.
     * If the third parameter is a function: passes the element to the function and returns function's return value.
     */
    function evaluate(jsevent, name, text_or_function) {
      var res;
      var element = jsevent.target;
      var jqelt = $(element);

      debug("element is " + element + ", name=" + element.nodeName);

      if (name && (res=jqelt.attr('data-track-' + name))) {
        debug("returning HTML5 data for " + name);
        return res;
      }

      // grab named default
      if (name && ! text_or_function) {
        debug("pulled from settings " + name);

        text_or_function = settings[name];
      }

      if(typeof text_or_function == 'function') {
        text_or_function = text_or_function(element, jsevent);
      }
      return text_or_function;
    };
  };

  /**
   * Prints to Firebug console, if available. To enable:
   *   $.fn.track.defaults.debug = true;
   */
  function debug(message) {
    if (typeof console != 'undefined' && typeof console.debug != 'undefined' && $.fn.zxLiveTrack.defaults.debug) {
      console.debug(message);
    }
  };

  /**
   * Default (overridable) settings.
   */
  $.fn.zxLiveTrack.defaults = {
    'category'      :
        function(element,jsevent) {
          if (element.nodeName != "A") return "internal";
          return ($(element)[0].hostname == location.hostname) ? 'internal':'external';
        },
    'action'        : function(element, jsevent) { return (jsevent && jsevent.type) || 'click' },
    'label'         : function(element, jsevent) { return $(element).attr('href') || $(element).attr('id') || $(element).attr('class') || (jsevent && jsevent.currentTarget.nodeName); },
    'value'         : null,
    'skip-internal' : false,
    'event-name'    : 'click',
    debug           : true
  };
})(jQuery);
