/*
 * Trackiffer v0.3.3
 * Easy GA event tracking and debugging
 * https://github.com/averyvery/trackiffer
 *
 * Copyright 2011, Doug Avery
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://jquery.org/license
 *
 * Should be compressed with http://jscompress.com/
 */

;          
(function(a,b){"use strict";var c={version:"0.3.3",is_oldbrowser:navigator.userAgent.indexOf("MSIE 6")!=-1||navigator.userAgent.indexOf("MSIE 7")!=-1,jquery:{min_version:"1.5.0",loaded:false},tracked_elems:{},delegated_elems:{},event_types:{form:"submit",select:"change"},defineGa:function(){b._gaq=b._gaq||[]},init:function(){c.defineGa();c.checkjQuery();setTimeout(c.checkHash,500)},rules:{},loadScript:function(b){c.log("|    loading "+b);var d=a.createElement("script"),e=a.getElementsByTagName("script"),f=e[e.length-1];d.type="text/javascript";d.src=b;f.parentNode.insertBefore(d,f.nextSibling)},checkjQuery:function(){c.log("");c.log("+ checking for jQuery");if(c.isjQueryVersionHighEnough()===false){c.log("|    jQuery version too low");c.loadScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js");c.polljQueryVersion()}else{c.log("|    jQuery present");c.jquery.loaded=true;c.bindRules()}},normalizeVersion:function(a){var b=a+"",c=b.replace(/\./g,"");while(c.length<3){c=c+"0"}return parseInt(c,10)},isjQueryVersionHighEnough:function(){var a=typeof b.jQuery==="function"&&jQuery.fn.jquery||"0",d=c.normalizeVersion(a),e=c.normalizeVersion(c.jquery.min_version),f=d>e;f||c.log("|    wrong version: "+a+" (needs to be > "+c.jquery.min_version+")");return f},polljQueryVersion:function(){c.log("|    waiting 100ms");c.isjQueryVersionHighEnough()?c.jQueryHasLoaded():setTimeout(c.polljQueryVersion,100)},jQueryHasLoaded:function(){c.log("|    loaded jQuery");c.jquery.loaded=true;jQuery.noConflict();c.bindRules()},formatData:function(a,b){var d=a.shift(),e=c.parseTokens(a,b);e.unshift(d);return e},getReplacement:function(a,b){var c;if(a){switch(a){case"{{value}}":c=b.val();break;case"{{text}}":c=b.text();break;default:var d=a.replace(/\{\{/g,"").replace(/\}\}/g,"");c=b.attr(d)}return c}},parseTokens:function(a,b){for(var d=0,e=a.length;d<e;d++){var f=a[d],g,h=typeof f==="string";if(typeof f==="function"){f=f(b)}else if(h){var i=/\{\{.+?\}\}/g,j=f.match(i),k,l;if(j){for(var m=0,n=j.length;m<n;m++){k=c.getReplacement(j[m],b);if(k){l=jQuery.trim(k);f=f.replace(i,l)}}}}g=c.replaceBadCharacters(f);a[d]=g}return a},replaceBadCharacters:function(a){if(a.replace){a=a.replace(/,/g,"\\,");a=a.replace(/"/g,'\\"');a=a.replace(/'/g,"\\'")}return a},executeDelayedAction:function(a,d){var e=d.get(0);if(c.debugging!==true){e.submit&&e.submit();if(a==="click"){b.location=d[0].href}}},delayAction:function(a,b,d){a.preventDefault();var e=function(){c.log("|    outbound event - firing after 100ms");c.executeDelayedAction(b,d)};c.debugging||d.unbind(b+".trackiffer");setTimeout(e,100)},trackRules:function(a){c.rules=a;c.jquery.loaded?c.bindRules():c.log("|    delaying init until jQuery loads")},bindRules:function(){c.log("");c.log("+  binding rules");for(var a in c.rules){if(c.rules.hasOwnProperty(a)){c.bindRulesToSelector(a)}}c.debugging&&c.highlightAllElements();c.rules={}},bindRulesToSelector:function(a){var b=jQuery(a),d=c.rules[a],e=function(e){e===0&&c.log("|    "+b.length+"x "+a);c.bindEvent(d,jQuery(this),a)};b.each(e);d&&d.delegate&&e(0)},getEventType:function(a){var b="click";for(var d in c.event_types){if(c.event_types.hasOwnProperty(d)){if(a.is(d)){b=c.event_types[d]}}}return b},isArray:function(a){return a.constructor.toString().indexOf("Array")!==-1},elemHasUrl:function(a){var b=a.attr("action")||a.attr("href"),c=b&&b.slice(0)!=="#";return b&&c},bindEvent:function(a,d,e){var f={delay:true,delegate:false,rule:a,type:c.getEventType(d)},g=function(a){c.log("");if(f.delegate){c.log("+  "+f.type+" on "+f.delegate+" delegated from "+e)}else{c.log("+  "+f.type+" on "+e)}var h=c.formatData(f.rule.slice(0),d);c.log("|    parsing ",f);c.log("v    ");b._gaq.push(h);c.log("^    ");if(f.delay&&c.elemHasUrl(d)){c.log("|    outbound event - delaying");c.delayAction(a,f.type,d,g)}else if(c.debugging){return false}};if(c.isArray(a)===false){f=$.extend(f,a)}if(f.delegate){d.delegate(f.delegate,f.type+".trackiffer",g)}else{d.bind(f.type+".trackiffer",g)}c.bindDebugHover(d,e,f.delegate)},debugging:false,debug_wait_count:0,debug_outlines:{highlight:"rgb(0,200,200) 3px solid",hover:"rgb(250,0,0) 3px solid",highlight_delegated:"rgba(0,200,200, 0.3) 6px solid",hover_delegated:"rgba(250,0,0, 0.3) 6px solid"},checkHash:function(){c.debugging=b.location.hash==="#trackiffer_debug";if(c.debugging){c.log("+  debug hash detected");setTimeout(c.debugAfterGALoads,1e3)}},debugAfterGALoads:function(){c.log("|    waiting for GA to load before debugging...");var a=_gaq!=="undefined"&&c.isArray(_gaq)===false;if(a){c.log("|    GA has loaded");c.log("");c.debug()}else if(c.debug_wait_count<19){setTimeout(c.debugAfterGALoads,500);c.debug_wait_count++}else{c.log("|    looks like GA isn't going to load");c.log("|    shut it down")}},log:function(){if(c.debugging&&typeof console==="object"&&console.log){var a=Array.prototype.slice.call(arguments);a.unshift("");console.log.apply(console,a)}},undefineGa:function(){c.log("|    unsetting existing GA");b._gat=undefined;b._gaq=[["_setAccount","UA-00000000-1"]]},debug:function(){c.debugging=true;c.log("+  debug mode");c.undefineGa();c.loadScript("http://www.google-analytics.com/u/ga_debug.js");c.debug_outlines.property=c.is_oldbrowser?"border":"outline";c.highlightAllElements();jQuery(b).keydown(c.leaveDebugIfEsc)},undebug:function(){c.log("+  leaving debug mode");c.debugging=false;c.unHighlightAllElements()},highlightAllElements:function(){c.actOnTrackedElements(c.highlightElement);c.actOnDelegatedElements(c.highlightDelegatedElement)},unHighlightAllElements:function(){c.actOnTrackedElements(c.unHighlightElement);c.actOnDelegatedElements(c.unHighlightElement)},actOnTrackedElements:function(a){for(var b in c.tracked_elems){if(c.tracked_elems.hasOwnProperty(b)){jQuery.each(c.tracked_elems[b],a)}}},actOnDelegatedElements:function(a){for(var b in c.delegated_elems){if(c.delegated_elems.hasOwnProperty(b)){jQuery.each(c.delegated_elems[b],a)}}},unHighlightElement:function(){jQuery(this).css(c.debug_outlines.property,"")},highlightElement:function(){jQuery(this).css(c.debug_outlines.property,c.debug_outlines.highlight)},hoverElement:function(){jQuery(this).css(c.debug_outlines.property,c.debug_outlines.hover)},highlightDelegatedElement:function(){jQuery(this).css(c.debug_outlines.property,c.debug_outlines.highlight_delegated)},hoverDelegatedElement:function(){jQuery(this).css(c.debug_outlines.property,c.debug_outlines.hover_delegated)},bindDebugHover:function(a,b,d){var e=d?c.delegated_elems:c.tracked_elems,f=function(){c.debugging&&e[b].each(d?c.hoverDelegatedElement:c.hoverElement)},g=function(){c.debugging&&e[b].each(d?c.highlightDelegatedElement:c.highlightElement)};if(typeof e[b]==="undefined"){e[b]=jQuery()}e[b]=e[b].add(a);a.hover(f,g)},leaveDebugIfEsc:function(a){a.keyCode===27&&c.undebug()}};b.trackiffer=function(a){var b=typeof a==="object",d=typeof c[a]==="function",e=c;if(b){c.trackRules(a)}else if(d){e=c[a]()}else if(a){e=c[a]}return e};c.init()})(document,window)
;


