/**
 * Vstrack is a simple jQuery plugin that provides an interface to sending
 * data to the Visual Science database. It works by requesting a file on the server
 * (usually a image) with tracking data attached in the query string.
 *
 * The goal of Vstrack is to provide developers with a standard method of sending
 * data to the Visual Science database.
 *
 * Usage:
 * It is easy to use Vstrack. The first parameter is a object literal containing
 * the data to be passed. The second parameter is for optional options.
 *
 * @example $.vstrack({cg1: 'WWW', cg2: 'Credit Cards'}, {options});
 * @example $.vstrack({linkid: 'WWW_0608_CARD_TGUNS04_CLINTRO_Z_01_G_CLSCL'}, {callback: myFunc});
 * @example $.vstrack({any: data});
 *
 * @name Vstrack
 * @type jQuery
 * @requires jQuery v1.2.2
 * @author Roderick R. Randolph
 * @version 1.0
 */
(function ($) {
    /**
     * Stand-alone function to create a modal dialog.
     *
     * @param {object} data object literal containing data to track
     * @param {object} [options] An optional object containing options overrides
     */
    $.vstrack = function(data, options) {
        // merge defaults and user options
	this.opts = $.extend({}, $.vstrack.defaults, options);

        if (data.length <= 0) return;

        var _data = {Log: 1}; // All request to VS must have Log=1 in the request

        // This loop is performed so that Log=1 is always at the beginning of the query string
        // when making the server request
        for (var i in data) {
            _data[i] = data[i];
        }

        // Add a random number to prevent caching of the tracker
        _data['num'] = Math.floor(Math.random()*100000000);

        // Make the request to the server using the GET request method
        $.get(this.opts.tracker, _data, this.opts.callback, this.opts.type);
    };

    /**
     * Vstrack default options
     *
     * tracker (String:'/images/https-common/tracker.gif') The file to use for pinging tracking requests
     * type: (String:'image') The file type of the tracker
     * callback: (Function:null) The callback function to call once the track request is complete
     */
    $.vstrack.defaults = {
        tracker: '/images/https-common/tracker.gif',
        type: 'image',
        callback: null
    };

})(jQuery);
