/*
 *     Click Truth Helper Library
 *     version 1.0     10/23/2010
 *
 *  This file contains a number of wrapper functions to call the Click Truth
 *  API functions from within the onclick handler of a link tag.  This file
 *  should be placed on your servers locally, instead of loading it from the
 *  Click Truth servers.  This will ensure that you don't need to add extra
 *  error handling to your links in the event that the user's browser is unable
 *  to reach the Click Truth servers.
 *
 *  Usage:
 *  Each function operates identically to its standard API version, except when
 *  added to the onclick handler of a link, the link object must be passed as a
 *  the first argument, followed by the standard arguments for that function.
 *
 *  Example:
 *  To call the 'ssig_tag_selection' function from a link, you will use the
 *  helper function 'ssig_link_tag_selection'.
 *  Suppose the function previously exists in an on-page block of script such as
 *    ssig_tag_selection('tag1','tag2');
 *  And you want to move this to this link:
 *    <a href="/link/target.html">Link Text</a>
 *  The link tag should be modified to become:
 *    <a href="/link/target.html" onclick="ssig_link_tag_selection(this,'tag1',
 *    'tag2')">Link Text</a>
 *  Note the first argument is now 'this' which passes the link object, which is
 *  required for all these link functions.
 *
 *  For support, contact ryan@clicktruth.com
 */


function ssig_generic_link(ssig_fn, args) {
  try {
    var link = args[0];
    eval(ssig_fn+"(ssig_arg_slice(args,1))");
    if (link.target == '_blank') {
        setTimeout('window.open("' + link.href + '")', 100);
    } else {
        setTimeout('document.location = "' + link.href + '"', 100);
    }
    return false;
  } catch(err){
    return true;
  }
}

function ssig_link_tag_selection() {
    return ssig_generic_link('ssig_tag_selection',ssig_link_tag_selection.arguments);
}

function ssig_link_tagged_content() {
    return ssig_generic_link('ssig_tagged_content',ssig_link_tagged_content.arguments);
}

function ssig_link_site_event() {
    return ssig_generic_link('ssig_site_event',ssig_link_site_event.arguments);
}

function ssig_link_countable_event() {
    return ssig_generic_link('ssig_countable_event',ssig_link_countable_event.arguments);
}

function ssig_link_offsite_event() {
    return ssig_generic_link('ssig_offsite_event',ssig_link_offsite_event.arguments);
}

function ssig_link_visitor_property() {
    return ssig_generic_link('ssig_visitor_property',ssig_link_visitor_property.arguments);
}

function ssig_link_decision_selection() {
    return ssig_generic_link('ssig_decision_selection',ssig_link_decision_selection.arguments);
}

/* perform a slice on an args object, which is 'array-like' but has no slice fn*/
function ssig_arg_slice(args, start_at) {
    var result = [];
    for (var i=start_at; i<args.length; i++) {
        result.push(args[i])
    }
    return result;
}
