﻿/* Attach Google Analytics */
if ( typeof $ == "function" ) {
    var _gaq = _gaq || [];
    $(document).ready(function () {
        // Make sure there is a global hhStatistics object and that we run only once
        if ( typeof hhStatistics != "object" || hhStatistics == null )
            hhStatistics = new Object();
        if ( hhStatistics.runOnce )
            return;
        hhStatistics.runOnce = true;
        
        // Basic settings
        hhStatistics.gaAccount = "UA-7610078-1";
        hhStatistics.siteDomains = "www.ifl.se,ifl.se";
        hhStatistics.downloadExtensions = "avi,bmp,bz2,doc,docx,dwg,dxf,emf,eps,exe,gz,jpg,mov,mp3,mpg,msi,odg,odp,ods,odt,pdf,png,pps,ppsx,ppt,pptx,rar,rss,rtf,svg,tar,tif,txt,wav,wmf,wmv,xls,xlsx,xml,zip";
        
        // Specification of areas for click-area tracking
        hhStatistics.areaSpecification = [
            { a : "TopLogo", i : "header" },
            { a : "MainMenu", i : "nav" },
            { a : "DropdownMenu", i : "menu-dropdown" },
            { a : "LeftMenu", i : "menu" },
            { a : "Breadcrumbs", i : "breadcrumbs" },
            { a : "MainContent", i : "content" },
            { a : "RightColumn", i : "side" },
            { a : "TopTools", i : "tools" },
            { a : "TopSearch", i : "ifl-search" },
            { a : "Testimonials", i : "testimonials" },
            { a : "ProgramBox", i : "programlist" },
            { a : "FeedBox", i : "feeds" },
            { a : "Footer", i : "footer" }
        ];
        
        // Verify the URL type
        if ( ! document.URL.match(/^https?:\/\/[^\/?#]+\/+/i) )
            return;
        
        // Determine language and normalize URL
        var a, e, i, u, v;
        u = null;
        if ( (e = $("#aspnetForm[action]")[0]) && (v = e.action.replace(/(^https?:\/\/[^\/]*|[?#].*$)/gi, "")).match(/^\/[a-z][a-z][\/]/i) ) {
            u = v;
            hhStatistics.languageCode = v.substring(1, 3);
        }
        if ( u == null ) {
            if ( ! hhStatistics.languageCode )
                hhStatistics.languageCode = ((e = $("meta[http-equiv=content-language]")[0]) ? e.content : "").toLowerCase();
            u = document.URL.replace(/(^https?:\/\/[^\/?#]+\/+|[?#].*$)/gi, "");
            a = u.split(/\/+/);
            for (i=0; i<a.length; i++)
                a[i] = a[i].toLowerCase();
            if ( a[0] != hhStatistics.languageCode )
                a.splice(0, 0, hhStatistics.languageCode);
            u = "/" + a.join("/");
        }
        hhStatistics.path = u;
        
        // Add 404 info
        if ( hhStatistics.path.match(/^\/(sv|en)\/404\/$/i) && (a = /^https?:\/\/[^\/?#]+\/+([^?#]+).*$/i.exec(document.URL)) != null && a.length == 2 ) {
            hhStatistics.path += a[1];
        }
        
        // Add search term
        if ( (e = $("div.searchform input[type=text]")[0]) && (v = e.value) && (v = v.replace(/  +/, " ").replace(/(^[ \t\r\n]*|[ \t\r\n]*$)/g, "")) != "" )
            hhStatistics.path += "?search=" + encodeURIComponent(v.toLowerCase());
        
        // Main click handler
        hhStatistics.clickHandler = function(e) {
            // Normalize event info and target
            if ( ! e )
                return;
            var t = (e.target ? e.target : (e.srcElement ? e.srcElement : null));
            while ( t && t.nodeName != "A" && t.nodeName != "INPUT" )
                t = t.parentNode;
            
            // Set up lists for link-type matching
            var b, c, d, e, f, h, i, j, r;
            var domainList = this.siteDomains;
            if ( (b = /^https?:\/\/([^:\/]*)/i.exec(document.URL)) != null && b.length == 2 )
                domainList = "," + b[1].toLowerCase() + "," + (domainList != null ? domainList + "," : "");
            var extensionList = ("," + this.downloadExtensions + ",").toLowerCase();
            
            // Examine link type
            d = e = r = null;
            if ( t.href != null ) {
                if ( domainList != null && (b = /^(https?:\/\/)([^:\/]*)([^\/]*)[\/]*(|[\/?#].*)$/i.exec(t.href)) != null && b.length == 5 && b[2].length > 0 && domainList.indexOf("," + b[2].toLowerCase() + ",") < 0 ) {
                    d = "Exit: " + b[2].toLowerCase() + b[4];
                    e = b[1].toLowerCase() + b[2].toLowerCase() + b[3] + b[4];
                } else if ( (b = /^mailto:([^?#]*).*$/i.exec(t.href)) != null && b.length == 2 && b[1].length > 0 ) {
                    d = "Mail: " + (b[1] == "" ? "Undefined recipient" : b[1].toLowerCase());
                    e = b[0].toLowerCase();
                } else if ( (b = /^https?:\/\/[^\/]+[\/]*(\/[^?#]*)(\.|\?output=)([^.?&#]*)([?&#].*|)$/i.exec(t.href)) != null && b.length == 5 && b[3].length > 0 && extensionList.indexOf("," + b[3].toLowerCase() + ",") >= 0 ) {
                    d = "File: " + b[1] + b[2] + b[3];
                    e = b[1] + b[2] + b[3];
                } else if ( t.href.match(/^ *javascript:.*print.*\( *\) *;? *$/i) && this.path != null && this.path != "" ) {
                    d = "Print: " + document.title;
                    e = this.path + "?print";
                }
            } else if ( t.className.match(/(^| )button( |$)/i) ) {
                if ( (b = $("#content")).has(t).length > 0 ) {
                    if ( b.has("#folder-order").length > 0 ) {
                        r = "Folder";
                        c = b.find("#chosen-folders li");
                        f = "";
                        for (j=0; j<c.length; j++)
                            f += (j ? ", " : "") + c.eq(j).text().replace(/[\u00A0, \t\r\n]+/g, " ").replace(/(^[ \t\r\n]*|[ \t\r\n]*$)/g, "");
                        d = "Form submission: Folder (" + f + ")";
                        c = b.find("#folder-list li input:checked");
                        f = ",";
                        for (j=0; j<c.length; j++)
                            if ( (h = c.eq(j).siblings("input[type=hidden]")).length > 0 )
                                f += h[0].value + ",";
                        e = "/" + this.languageCode + "/[forms]/Folder/" + f;
                    } else if ( b.has("#standard").length > 0 || b.has("#contact-form").length > 0 ) {
                        f = ((c = b.find("span.tel")).length > 0 ? c.text().replace(/[\u00A0\/ \t\r\n]+/g, " ").replace(/^([^a-z]*[0-9][^a-z]*)[a-z].*$/, "$1").replace(/(^[ \t\r\n]*|[ \t\r\n]*$)/g, "") : "general");
                        r = "Contact";
                        d = "Form submission: Contact (" + f + ")";
                        e = "/" + this.languageCode + "/[forms]/Contact/" + f;
                    } else if ( b.has("#coach-order").length > 0 ) {
                        if ( b.has("a[name=SeminarForm]").length > 0 ) {
                            f = ((c = b.find("h1")).length > 0 ? c.text().replace(/[\u00A0\/ \t\r\n]+/g, " ").replace(/(^[ \t\r\n]*|[ \t\r\n]*$)/g, "") : "unspecified");
                            r = "Seminar";
                            d = "Form submission: Seminar (" + f + ")";
                            e = "/" + this.languageCode + "/[forms]/Seminar/" + f;
                        } else {
                            r = "Coaching";
                            d = "Form submission: Coaching";
                            e = "/" + this.languageCode + "/[forms]/Coaching";
                        }
                    } else if ( b.has("#event").length > 0 ) {
                        f = ((c = b.find("h1")).length > 0 ? c.text().replace(/[\u00A0\/ \t\r\n]+/g, " ").replace(/(^[ \t\r\n]*|[ \t\r\n]*$)/g, "") : "unspecified");
                        r = "Meeting";
                        d = "Form submission: Meeting (" + f + ")";
                        e = "/" + this.languageCode + "/[forms]/Meeting/" + f;
                    } else if ( $("div.searchform").has(t).length == 0 ) {
                        r = "Other";
                        d = "Form submission: Other (" + document.title + ")";
                        e = "/" + this.languageCode + "/[forms]/" + ((c = b.find("h1")).length > 0 ? c.text().replace(/[\u00A0\/ \t\r\n]+/g, " ").replace(/(^[ \t\r\n]*|[ \t\r\n]*$)/g, "") : "[other]");
                    }
                } else if ( $("#newsletter").has(t).length > 0 || t.className.match(/(^| )newsletter( |$)/i) ) {
                    r = "Newsletter";
                    d = "Form submission: Newsletter";
                    e = "/" + this.languageCode + "/[forms]/Newsletter";
                }
            }
            if ( d != null ) {
                if ( r == null ) {
                    h = document.title;
                    document.title = d;
                    this.trackPage(e);
                    document.title = h;
                } else {
                    f = (this.lastFormClick ? this.lastFormClick : null);
                    r = hhGaCut(r, 64 - 4);
                    this.lastFormClick = "_gaq.push(['_setCustomVar', 3, 'Form', '" + r.replace(/([\'\\])/g, "\\$1") + "', 3]); var h = document.title; document.title = '" + d.replace(/([\'\\])/g, "\\$1") + "'; hhStatistics.trackPage('" + e.replace(/([\'\\])/g, "\\$1") + "'); document.title = h;";
                    hhReplaceEvent($("form")[0], this.lastFormClick, f, "submit");
                }
            }
        };
        
        // Click-event attacher
        hhStatistics.setupClickEvents = function() {
            var a, p;
            if ( ! (a = this.areaSpecification) || typeof a != "object" )
                return;
            if ( this.path )
                this.createCookie("hhclickref", this.path, null);
            var t = "hhStatistics.createCookie('hhclickarea', '###', 10); if (e && typeof hhStatistics.clickHandler == 'function') hhStatistics.clickHandler(e);";
            var d = t.replace(/###/g, "UntrackedLinkArea");
            var f = $("a,input[type=submit]");
            var e, g, h, i, j, k, o;
            for (i=0; i<f.length; i++)
                hhReplaceExistingOnClick(f[i], d);
            for (j=0; j<a.length; j++) {
                if ( typeof(a[j]) == "object" && a[j].i != null && (e = document.getElementById(a[j].i)) != null ) {
                    o = t.replace(/###/g, a[j].a.replace(/\\/g, "\\\\").replace(/\'/g, "\\'"));
                    if ( a[j].c == null && (f = $("#" + a[j].i + " a,#" + a[j].i + " input[type=submit]")).length > 0 ) {
                        for (i=0; i<f.length; i++)
                            hhReplaceExistingOnClick(f[i], o, d);
                    } else if ( a[j].c == null && e.nodeName == "A" ) {
                        hhReplaceExistingOnClick(e, o, d);
                    } else if ( a[j].c == null && e.parentNode.nodeName == "A" ) {
                        hhReplaceExistingOnClick(e.parentNode, o, d);
                    } else if ( a[j].c != null && (g = e.getElementsByTagName("DIV")) != null && g.length > 0 ) {
                        for (k=0; k<g.length; k++) {
                            if ( g[k].className == a[j].c && (f = g[k].getElementsByTagName("A")) != null && f.length > 0 ) {
                                for (i=0; i<f.length; i++)
                                    hhReplaceExistingOnClick(f[i], o, d);
                            }
                        }
                    }
                }
            }
        };
        
        // Statistics-cookie helpers
        hhStatistics.createCookie = function(name, value, seconds) {
            if ( seconds ) {
                var date = new Date();
                date.setTime(date.getTime() + (seconds*1000));
                var expires = "; expires=" + date.toGMTString();
            } else {
                var expires = "";
            }
            document.cookie = name + "=" + value + expires + "; path=/";
        }
        hhStatistics.readCookie = function(name) {
            var a = (new RegExp("(^|;) *" + name + "=([^;]*)", "")).exec(document.cookie);
            if ( a != null && a.length > 2 )
                return a[2];
            return null;
        }
        
        // Standard page tracker
        hhStatistics.trackPage = function(p) {
            // Click-areas measurement (slots 1 and 2)
            var clickArea = this.readCookie("hhclickarea");
            var refPath = this.readCookie("hhclickref");
            var i, n;
            if ( clickArea ) {
                i = 64 - (2 + clickArea.length);
                shortenPath = function(q) {
                    var a = q.split(/\/+/);
                    var j = 0;
                    var k = a.length-1;
                    var h = "";
                    var t = "";
                    while ( a[j] == "" && j < k )
                        j++;
                    while ( a[k] == "" && j < k )
                        k--;
                    while ( j <= k ) {
                        h += (h == "" ? "" : "_") + a[j++].replace(/[ \[\]()*?-]+/g, "");
                        if ( h.length + t.length + 1 >= i || j > k )
                            break;
                        t = a[k--].replace(/[ \[\]()*?-]+/g, "") + (t == "" ? "" : "_" + t);
                        if ( h.length + t.length + 1 >= i )
                            break;
                    }
                    return h + (t == "" ? "" : "_" + t);
                };
                if ( p.match(/^[^\/]*:/) )
                    n = p.replace(/^[^\/]*:([\/]*)/, "");
                else
                    n = shortenPath(p);
                _gaq.push(['_setCustomVar', 1, "R", clickArea + "-" + hhGaCut(n, i), 3]);
                if ( refPath )
                    _gaq.push(['_setCustomVar', 2, "F", hhGaCut(shortenPath(refPath), i) + "-" + clickArea, 3]);
            }
            // Page view
            _gaq.push(['_trackPageview', p]);
        }
        
        // Set up and perform page tracking
        _gaq.push(['_setAccount', hhStatistics.gaAccount]);
        hhStatistics.trackPage(hhStatistics.path);
        (function() {
            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';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
        })();
        
        // Attach click events (should also be run each time the DOM tree has changed)
        hhStatistics.setupClickEvents();
    });
}

/*
 * Add or replace an onclick event to an element
 * Version 2.1
 */
var hhExistingClickHandlers = new Object();
function hhOnClick(e, g) {
    hhReplaceOnClick(e, g, null);
}
function hhReplaceExistingOnClick(e, g) {
    var f, i, r;
    var a = ["id", "href", "className", "type", "tagName", "name", "value"];
    var k = "";
    for (f=e; f && f.tagName!="BODY"; f=f.parentNode) {
        for (i=0; i<a.length; i++)
            if ( f[a[i]] )
                k += f[a[i]];
        if ( f.id )
            break;
    }
    if ( ! (r = hhExistingClickHandlers[k]) )
        r = null;
    hhExistingClickHandlers[k] = g;
    hhReplaceOnClick(e, g, r);
}
function hhReplaceOnClick(e, g, r) {
    hhReplaceEvent(e, g, r, "click");
}
function hhReplaceEvent(e, g, r, n) {
    if ( typeof(e) != "object" || e == null || n == null )
        return;
    var f = (typeof(g) == "function" ? g : hhGetEventHandler(g));
    var q = (typeof(r) == "function" ? r : hhGetEventHandler(r));
    if ( typeof(e.addEventListener) == "function" || typeof(e.addEventListener) == "object" ) {
        if ( q != null ) {
            e.removeEventListener(n, q, false);
        }
        if ( f != null ) {
            e.removeEventListener(n, f, false);
            e.addEventListener(n, f, false);
        }
    } else if ( typeof(e.attachEvent) == "function" || typeof(e.attachEvent) == "object" ) {
        if ( q != null ) {
            e.detachEvent('on' + n, q);
        }
        if ( f != null ) {
            e.detachEvent('on' + n, f);
            e.attachEvent('on' + n, f);
        }
    }
}

/*
 * Create or get an event handler from a code string
 * Version 1.2
 */
var hhLiteralEventHandlers = new Object();
function hhGetEventHandler(t) {
    var f;
    if ( t == null )
        return null;
    if ( (f = hhLiteralEventHandlers[t]) != null )
        return f;
    f = hhLiteralEventHandlers[t] = new Function("e", t);
    return f;
}

/*
 * Plugin: hhByteCut
 * Version 1.0
 */
function hhByteCut(s, n) {
    if ( s == null )
        s = "";
    var t = s;
    var o = n;
    while ( hhLength(t) > n && o >= 0 ) {
        t = hhCut(s, o--);
    }
    return t;
}

function hhGaCut(s, n) {
    if ( s == null )
        s = "";
    var t = s;
    var o = n;
    while ( hhGaLength(t) > n && o >= 0 ) {
        t = hhCut(s, o--);
    }
    return t;
}

/*
 * Plugin: hhCut
 * Version 1.0
 */
function hhCut(s, n) {
    if ( s == null )
        s = "";
    if ( n < 1 )
        return "";
    if ( n < 4 )
        return "~";
    if ( s.length > n ) {
        var i = Math.ceil((n-3)*0.6);
        if ( s[i-1] == " " )
            i--;
        s = s.substring(0, i) + "..." + s.substring(s.length-(n-(i+3)));
    }
    return s;
}

/*
 * Plugin: hhLength
 * Version 1.0
 */
function hhLength(s) {
    if ( s == null )
        return 0;
    if ( typeof(encodeURI) == "function" ) {
        return encodeURI(s).replace(/%../g, "%").length;
    }
    return s.length + s.replace(/[ -~\t\r\n]/g, "").length;
}

function hhGaLength(s) {
    if ( s == null )
        return 0;
    if ( typeof(encodeURIComponent) == "function" ) {
        return encodeURIComponent(s).length;
    }
    return s.length + s.replace(/[ -~\t\r\n]/g, "").length;
}

