$(function() {
    /* redefine getScript to allow caching */    
    $.getScript = function(url, callback, cache){
        $.ajax({
            type: "GET",
            url: url,
            success: callback,
            dataType: "script",
            cache: cache
        });
    };

    // set up hover color transition for main nav
    $("#nav li a").hover(function() {
        $(this).animate({
            borderTopColor:"#000",
            borderRightColor:"#000",
            borderBottomColor:"#000",
            borderLeftColor:"#000",
            color:"#000"
        }, 500)}, function() {
        var bgImage = $(this).css("background-image");
        if (bgImage.indexOf("url") == -1) {
            $(this).animate({
                borderTopColor:"#bbb",
                borderRightColor:"#bbb",
                borderBottomColor:"#bbb",
                borderLeftColor:"#bbb",
                color:"#bbb"
            }, 250);
        }
    });

    // rotator.js - http://www.josephfinsterwald.com
    (function($) {
        $.rotate = function(s) {
            return $.rotate13($.rotate5(s));
        }

        $.rotate5 = function(s) {
            var b = [],c,i = s.length,a = '0'.charCodeAt(),z = a + 10;
            while (i--) {
                c = s.charCodeAt(i);
                if (c >= a && c < z) { b[i] = String.fromCharCode(((c - a + 5) % (10)) + a); }
                else { b[i] = s.charAt(i); }
            }
            return b.join('');
        };

        $.rotate13 = function(s) {
            var b = [],c,i = s.length,a = 'a'.charCodeAt(),z = a + 26,A = 'A'.charCodeAt(),Z = A + 26;
            while (i--) {
                c = s.charCodeAt(i);
                if (c >= a && c < z) { b[i] = String.fromCharCode(((c - a + 13) % (26)) + a); }
                else if (c >= A && c < Z) { b[i] = String.fromCharCode(((c - A + 13) % (26)) + A); }
                else { b[i] = s.charAt(i); }
            }
            return b.join('');
        };
    })(jQuery)

    // for schedule page
    $(function(){
        $("#schedule h5 + div").hide().toggleClass("open");
        $("#schedule h5").click(function(){
            $(this).toggleClass("open");
            $("#" + $(this).attr("rel")).slideToggle("slow");
        });
    });

    // for scrolling anchor links
    $("a.anchorLink").anchorAnimate();

    // add icon to "Return To Top" links
    $("a[href='#top']").addClass('backToTop');
    
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function(){
        try {
            var pageTracker = _gat._getTracker("UA-11921532-2");
            pageTracker._trackPageview();
        } catch(err) {}

        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;

        $('a[href]').each(function(){
            var href = $(this).attr('href');
            if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
                $(this).click(function() {
                    var extLink = href.replace(/^https?\:\/\//i, '');
                    pageTracker._trackEvent('External', 'Click', extLink);
                    window.open(this.href);
                    return false;
                });
            }
            else if (href.match(/^mailto\:/i)){
                $(this).click(function() {
                    var mailLink = href.replace(/^mailto\:/i, '');
                    pageTracker._trackEvent('Email', 'Click', mailLink);
                });
            }
            else if (href.match(filetypes)){
                $(this).click(function() {
                    var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                    var filePath = href.replace(/^https?\:\/\/(www.)metroeastdarts\.com\//i, '');
                    pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
                    window.open(this.href);
                    return false;
                });
            }
        });
    });    
});


