$(document).ready(function () {
    // Google Analytics mailto, tel, external & files
    // http://permanentinkdesign.com/articles/google-analytics-jquery-and-file-downloads/
    // 04.11.2009 Modified to use event tracking for not to mess up the actual data.
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function () {

        try {
            var pageTracker = _gat._getTracker("UA-6571240-1"),
            addthis_config = {
                data_ga_tracker: pageTracker,
                data_track_clickback: true
            };
            pageTracker._trackPageview();
        } catch (err) {}

        var filetypes = /\.doc$|\.odt$|\.rtf$|\.xls$|\.ppt$|\.zip$|\.pdf$/i;

        $("a").live("click", function () {
            var url = '';
            if ($(this).attr("href").match(/^mailto\:/i)) {
                url = $(this).attr("href").replace(/^mailto\:/i, "");
                pageTracker._trackEvent('Links', 'Mailto', '/mailto/' + url);
            }
            else if ($(this).attr("href").match(/^tel\:/i)) {
                url = $(this).attr("href").replace(/^tel\:/i, "");
                pageTracker._trackEvent('Links', 'Tel', '/tel/' + url);
            }
            else if (location.host != this.host.replace(/\:80$/i, "")) {
                url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "");
                pageTracker._trackEvent('Links', 'Outgoing', '/outgoing/' + url);
            }
            else if ($(this).attr("href").match(/.+(\/rss\/).+/)) {
                url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "");
                pageTracker._trackEvent('Links', 'RSS', '/rss/' + url);
            }
            else if ($(this).attr("href").match(filetypes)) {
                var host = location.host.replace(/\./, "\\."),
                pattern = new RegExp('^(http\:\/\/)*(www\.)*(' + tkrekry.fi + ')*\/', 'i');
                url = $(this).attr("href").replace(pattern, "");
                pageTracker._trackEvent('Links', 'Downloads', '/downloads/' + url);
            }
        });
    });

    // Megamenu http://www.sitepoint.com/blogs/2009/03/31/make-a-mega-drop-down-menu-with-jquery/
    // Added an iframe to block select boxes in IE6.

    function addMega() {
        $(this).addClass("hovering");
        
        var ewidth = $('.hovering div').width(),
        eheight = $('.hovering div').height(),
        iwidth = 22 + ewidth,
        iheight = 21 + eheight,
        position = $('.hovering div').position();
        
        $('.hovering').prepend('<iframe style="border: 0; position: absolute;"></iframe>');
        $('iframe').width(iwidth).height(iheight).css({
            'left': position.left,
            'top': position.top
        });
    }

    function removeMega() {
        $(this).removeClass("hovering");
        $('iframe').remove();
    }

    var megaConfig = {
        interval: 400,
        sensitivity: 4,
        over: addMega,
        timeout: 400,
        out: removeMega
    };

    $(".erva").hoverIntent(megaConfig);

    $("#tays-erva a").focus(function () {
        $("#tays-erva").addClass("hovering");
    });
    $("#tays-erva a").blur(function () {
        $("#tays-erva").removeClass("hovering");
    });
    $("#oys-erva a").focus(function () {
        $("#oys-erva").addClass("hovering");
    });
    $("#oys-erva a").blur(function () {
        $("#oys-erva").removeClass("hovering");
    });
    $("#hus-erva a").focus(function () {
        $("#hus-erva").addClass("hovering");
    });
    $("#hus-erva a").blur(function () {
        $("#hus-erva").removeClass("hovering");
    });
    $("#tyks-erva a").focus(function () {
        $("#tyks-erva").addClass("hovering");
    });
    $("#tyks-erva a").blur(function () {
        $("#tyks-erva").removeClass("hovering");
    });
    $("#kys-erva a").focus(function () {
        $("#kys-erva").addClass("hovering");
    });
    $("#kys-erva a").blur(function () {
        $("#kys-erva").removeClass("hovering");
    });

    // Style fixes
    $('.paginator li:first-child').addClass('first');

});

// Selectboxes

function change_option_values() {
    $('#sairaanhoitopiiri').removeOption(/./);
    $('#sairaanhoitopiiri').addOption("Kaikki", "Kaikki");
    if ($('#alue').val().match('koko_suomi')) {
        $.each(domains, function (i, domain) {
            $.each(domain.active_districts, function (i, district) {
                if (district.slug) {
                    $('#sairaanhoitopiiri').addOption(district.slug, district.name);
                }
            });
        });
    } else {
        $.each(domains, function (i, domain) {
            if (domain.slug.match($('#alue').val())) {
                $.each(domain.active_districts, function (i, district) {
                    if (district.slug) {
                        $('#sairaanhoitopiiri').addOption(district.slug, district.name);
                    }
                });
            }
        });
    }
    $('#sairaanhoitopiiri').selectOptions("Kaikki");
}
