// gatools.js (call before closing <head>)
var _gaq;
if(!_gaq) {
    _gaq = [];
}

(function($) {
    
    var G = this, W = G.window, D = W.document;
    
    if(!G.LOP) {
        return;
    }
    
    if(!W.console) {
        W.console = {
            log: function(msg) {
                // W.alert(msg);
            }
        };
    }
    
    var gaAccount = 'UA-24206624-1', pageTracker;
    
    W.console.log('gaAccount = ' + gaAccount);
    
    var sanitizeUrl = function(url) {
        if(url.indexOf('#') > -1) {
            url = W.location.pathname + '/' + url.replace(/^.*#(.+)$/, '$1');
        }
        url = url.replace(/^http:\/\/[^\/]+/, '');
        return url;
    };
    
    LOP.execStatGA = function(datas) {
        var url;
        if(!datas) {
            url = W.location.href;
        } else {
            if(datas.url) {
                url = datas.url;
            } else if(datas.hash) {
                url =  W.location.pathname + datas.hash.replace('#', '/');
            }
        }
        _gaq.push(['_setSiteSpeedSampleRate', 10]);
        W.console.log('_gaq.push(["_trackPageview", "' + sanitizeUrl(url) + '"])');
        _gaq.push(['_trackPageview', sanitizeUrl(url)]);
    };
    
    $(D).ready(function() {
        if(LOP.popins) {
            LOP.popins.addListener('open', function(e, id) {
                var url = e.owner.url[id];
                if(url) {
                    LOP.execStatGA({
                        url: sanitizeUrl(url)
                    });
                }
            });
        }
    });
    
    _gaq.push(['_setAccount', gaAccount]);
    LOP.execStatGA();
    
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    
}) (jQuery);
