/*! Adapted by Adam Powell via http://www.adaminfinitum.com from the source commented out below, the HTML5 Bolierplate docs, and the blogs od Juatin Cutroni and Avinash Kaushik,  Modified to include video searches,  h5bp.com docs https://github.com/h5bp/html5-boilerplate/blob/master/doc/extend.md */ 
 
/*! From cutroni.com */

// Google Analytics Rank Tracking Script
if (document.referrer.match(/google.([a-zA-Z]{2,5})/gi) && document.referrer.match(/cd/gi)) {
  var myString = document.referrer;
  var r        = myString.match(/cd=(.*?)&/);
  var rank     = parseInt(r[1]);
  var kw       = myString.match(/q=(.*?)&/);
  if (kw[1].length > 0) {
    var keyWord  = decodeURI(kw[1]);
  } else {
    keyWord = "(not provided)";
  }
  var p        = document.location.pathname;
  _gaq.push(['_trackEvent', 'RankTracker', keyWord, p, rank, true]);
};


/*! From HTML5 Bolierplate docs */

//Track JS Errors in Google Analytics
(function(window){
    var undefined,
        link = function (href) {
            var a = window.document.createElement('a');
            a.href = href;
            return a;
        };
    window.onerror = function (message, file, line, column) {
        var host = link(file).hostname;
        _gaq.push([
            '_trackEvent',
            (host == window.location.hostname || host == undefined || host == '' ? '' : 'external ') + 'error',
            message, file + ' LINE: ' + line + (column ? ' COLUMN: ' + column : ''), undefined, undefined, true
        ]);
    };
}(window)); 

/*! This JavaScript was developed by GA-Experts (http://www.ga-experts.co.uk) for Google Analytics Please use this code to overwrite the default set of Search Engines in the GA tracking code Use this code at your own risk.  GA Experts takes no responsibility for errors, loss of data or any other complications arising from the use of this code Version 2.0 (Mar 2007) - many thanks to Brian Clifton and Tomas Remotigue for help with this For enquiries please contact mail@ga-experts.co.uk Dutch Search Engines added by Joost de Valk (http://www.joostdevalk.nl) For enquiries specifically about the Dutch SE's please contact joost@joostdevalk.nl */
_gaq.push(
	['_addOrganic','images.google.co.uk','q'],
	['_addOrganic','images.google.es','q'],
	['_addOrganic','images.google.pt','q'],
	['_addOrganic','images.google.it','q'],
	['_addOrganic','images.google.fr','q'],
	['_addOrganic','images.google.nl','q'],
	['_addOrganic','images.google.be','q'],
	['_addOrganic','images.google.de','q'],
	['_addOrganic','images.google.no','q'],
	['_addOrganic','images.google.se','q'],
	['_addOrganic','images.google.dk','q'],
	['_addOrganic','images.google.fi','q'],
	['_addOrganic','images.google.ch','q'],
	['_addOrganic','images.google.at','q'],
	['_addOrganic','images.google.ie','q'],
	['_addOrganic','images.google.ru','q'],
	['_addOrganic','images.google.pl','q']
);
// Other Google Image search
_gaq.push(
	['_addOrganic','images.google.com','q'],
	['_addOrganic','images.google.ca','q'],
	['_addOrganic','images.google.com.au','q'],
	['_addOrganic','images.google','q']
);
// Blogsearch
_gaq.push(
	['_addOrganic','blogsearch.google','q']
);
// Google EMEA Domains
_gaq.push(
	['_addOrganic','google.co.uk','q'],
	['_addOrganic','google.es','q'],
	['_addOrganic','google.pt','q'],
	['_addOrganic','google.it','q'],
	['_addOrganic','google.fr','q'],
	['_addOrganic','google.nl','q'],
	['_addOrganic','google.be','q'],
	['_addOrganic','google.de','q'],
	['_addOrganic','google.no','q'],
	['_addOrganic','google.se','q'],
	['_addOrganic','google.dk','q'],
	['_addOrganic','google.fi','q'],
	['_addOrganic','google.ch','q'],
	['_addOrganic','google.at','q'],
	['_addOrganic','google.ie','q'],
	['_addOrganic','google.ru','q'],
	['_addOrganic','google.pl','q']
);
// Yahoo EMEA Domains
_gaq.push(
	['_addOrganic','uk.yahoo.com','p'],
	['_addOrganic','es.yahoo.com','p'],
	['_addOrganic','pt.yahoo.com','p'],
	['_addOrganic','it.yahoo.com','p'],
	['_addOrganic','fr.yahoo.com','p'],
	['_addOrganic','nl.yahoo.com','p'],
	['_addOrganic','be.yahoo.com','p'],
	['_addOrganic','de.yahoo.com','p'],
	['_addOrganic','no.yahoo.com','p'],
	['_addOrganic','se.yahoo.com','p'],
	['_addOrganic','dk.yahoo.com','p'],
	['_addOrganic','fi.yahoo.com','p'],
	['_addOrganic','ch.yahoo.com','p'],
	['_addOrganic','at.yahoo.com','p'],
	['_addOrganic','ie.yahoo.com','p'],
	['_addOrganic','ru.yahoo.com','p'],
	['_addOrganic','pl.yahoo.com','p']
);
// UK specific
_gaq.push(
	['_addOrganic','hotbot.co.uk','query'],
	['_addOrganic','excite.co.uk','q'],
	['_addOrganic','bbc','q'],
	['_addOrganic','tiscali','query'],
	['_addOrganic','uk.ask.com','q'],
	['_addOrganic','blueyonder','q'],
	['_addOrganic','search.aol.co.uk','query'],
	['_addOrganic','ntlworld','q'],
	['_addOrganic','tesco.net','q'],
	['_addOrganic','orange.co.uk','q'],
	['_addOrganic','mywebsearch.com','searchfor'],
	['_addOrganic','uk.myway.com','searchfor'],
	['_addOrganic','searchy.co.uk','search_term'],
	['_addOrganic','msn.co.uk','q'],
	['_addOrganic','uk.altavista.com','q'],
	['_addOrganic','lycos.co.uk','query']
);
// NL specific
_gaq.push(
	['_addOrganic','chello.nl','q1'],
	['_addOrganic','home.nl','q'],
	['_addOrganic','planet.nl','googleq=q'],
	['_addOrganic','search.ilse.nl','search_for'],
	['_addOrganic','search-dyn.tiscali.nl','key'],
	['_addOrganic','startgoogle.startpagina.nl','q'],
	['_addOrganic','vinden.nl','q'],
	['_addOrganic','vindex.nl','search_for'],
	['_addOrganic','zoeken.nl','query'],
	['_addOrganic','zoeken.track.nl','qr'],
	['_addOrganic','zoeknu.nl','Keywords']
);
// Extras
_gaq.push(
	['_addOrganic','alltheweb','q'],
	['_addOrganic','ananzi','qt'],
	['_addOrganic','anzwers','search'],
	['_addOrganic','araby.com','q'],
	['_addOrganic','dogpile','q'],
	['_addOrganic','elmundo.es','q'],
	['_addOrganic','ezilon.com','q'],
	['_addOrganic','hotbot','query'],
	['_addOrganic','indiatimes.com','query'],
	['_addOrganic','iafrica.funnel.co.za','q'],
	['_addOrganic','mywebsearch.com','searchfor'],
	['_addOrganic','rambler.ru','words'],
	['_addOrganic','search.aol.com','encquery'],
	['_addOrganic','search.indiatimes.com','query'],
	['_addOrganic','searcheurope.com','query'],
	['_addOrganic','suche.web.de','su'],
	['_addOrganic','terra.es','query'],
	['_addOrganic','voila.fr','kw']
);
// Testing tracking Google Video Search
_gaq.push(
	['_addOrganic','video.google.co.uk','q'],
	['_addOrganic','video.google.es','q'],
	['_addOrganic','video.google.pt','q'],
	['_addOrganic','video.google.it','q'],
	['_addOrganic','video.google.fr','q'],
	['_addOrganic','video.google.nl','q'],
	['_addOrganic','video.google.be','q'],
	['_addOrganic','video.google.de','q'],
	['_addOrganic','video.google.no','q'],
	['_addOrganic','video.google.se','q'],
	['_addOrganic','video.google.dk','q'],
	['_addOrganic','video.google.fi','q'],
	['_addOrganic','video.google.ch','q'],
	['_addOrganic','video.google.at','q'],
	['_addOrganic','video.google.ie','q'],
	['_addOrganic','video.google.ru','q'],
	['_addOrganic','video.google.pl','q']
);
// Other Google Image search
_gaq.push(
	['_addOrganic','video.google.com','q'],
	['_addOrganic','video.google.ca','q'],
	['_addOrganic','video.google.com.au','q'],
	['_addOrganic','video.google','q']
);

// Google Analytics Scroll Tracking Script
jQuery(function($) {
    // Debug flag
    var debugMode = false;
    // Default time delay before checking location
    var callBackTime = 100;
    // # px before tracking a reader
    var readerLocation = 150;
    // Set some flags for tracking & execution
    var timer = 0;
    var scroller = false;
    var endContent = false;
    var didComplete = false;
    // Set some time variables to calculate reading time
    var startTime = new Date();
    var beginning = startTime.getTime();
    var totalTime = 0;
    // Track the aticle load
    if (!debugMode) {
        _gaq.push(['_trackEvent', 'Reading', 'ArticleLoaded', '', , true]);
    }
    // Check the location and track user
    function trackLocation() {
        bottom = $(window).height() + $(window).scrollTop();
        height = $(document).height();
        // If user starts to scroll send an event
        if (bottom > readerLocation && !scroller) {
            currentTime = new Date();
            scrollStart = currentTime.getTime();
            timeToScroll = Math.round((scrollStart - beginning) / 1000);
            if (!debugMode) {
                _gaq.push(['_trackEvent', 'Reading', 'StartReading', '', timeToScroll]);
            } else {
                alert('started reading ' + timeToScroll);
            }
            scroller = true;
        }
        // If user has hit the bottom of the content send an event and if they
        // did it in less than 60 sec then set a custom var
        if (bottom >= $('.entry-content').scrollTop() + $('.entry-content').innerHeight() && !endContent) {
            currentTime = new Date();
            contentScrollEnd = currentTime.getTime();
            timeToContentEnd = Math.round((contentScrollEnd - scrollStart) / 1000);
            if (!debugMode) {
                if (timeToContentEnd < 60) {
                    _gaq.push(['_setCustomVar', 5, 'ReaderType', 'Scanner', 3]);
                } else {
                    _gaq.push(['_setCustomVar', 5, 'ReaderType', 'Reader', 3]);
                }
                _gaq.push(['_trackEvent', 'Reading', 'ContentBottom', '', timeToContentEnd]);
            } else {
                if (timeToContentEnd < 60) {
                    alert('SCANNER: end content section ' + timeToContentEnd);
                } else {
                    alert('READER: end content section ' + timeToContentEnd);
                }
            }
            endContent = true;
        }
        // If user has hit the bottom of page send an event
        if (bottom >= height && !didComplete) {
            currentTime = new Date();
            end = currentTime.getTime();
            totalTime = Math.round((end - scrollStart) / 1000);
            if (!debugMode) {
                _gaq.push(['_trackEvent', 'Reading', 'PageBottom', '', totalTime]);
            } else {
                alert('bottom of page ' + totalTime);
            }
            didComplete = true;
        }
    }
    // Track the scrolling and track location
    $(window).scroll(function() {
        if (timer) {
            clearTimeout(timer);
        }
        // Use a buffer so we don't call trackLocation too often.
        timer = setTimeout(trackLocation, callBackTime);
    });
});