﻿$(document).ready(function() {
    // add shadows for IE7+

    if ($.browser.msie && $.browser.version > 6) {
        if ($(".edit-mode").length == 0 && $('object[data*=player-viral.swf]').length == 0) {
            $("#body-container").addClass("shadows");
            var innermostwrapper = $("<div id=\"wrapper-inner2\"></div>").append($("#body-container").children());
            $("<div id=\"wrapper-inner\"></div>").prependTo("#body-container").append(innermostwrapper);
        }
    }


    //Search box focus/blur events
    $("#search-nav input").focus(function() {
        var $this = $(this);
        if ($this.val() === "Search the site") {
            $this.val("");
        }
    }).blur(function() {
        var $this = $(this);
        if ($this.val() === "") {
            $this.val("Search the site");
        }
    });

    //footer dropdown
    $("#info-topic").change(function() {
        window.location = $(this).attr("value");
    });

    $("#primary-navigation ul li:has(ul)").hover(function() {
        $("ul", this).addClass("hover"); //.css("top", this.offsetHeight + "px");
    }, function() {
        $("ul", this).removeClass("hover");
    });

    $("#primary-navigation li:has(li)").hover(function() {
        $("ul", this).addClass("over"); //.css("top", this.offsetHeight + "px");
    }, function() {
        $("ul", this).removeClass("over");
    });
    $("#primary-navigation li").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    // move menus for IE7 
    if ($.browser.msie && $.browser.version == 7) {
        $('#primary-navigation ul li:has(ul)').mouseenter(function() {
            var left = $(this).offset().left;
            var ulleft = $('ul', this).offset().left;

            if (ulleft > left) {
                $('ul', this).css('marginLeft', (ulleft - left + 24) * -1);
            }
        });
    }

    $("#primary-navigation ul ul li").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    $("#primary-navigation ul ul").find("li:last").css("border-bottom", "none");

    $(".parent-open").parent().addClass("parent-open-li");
    $(".subnavigation ul li:has(ul) a:first").addClass("toUpper");

    //related links subhead
    $(".relatedlinks ul li:has(h3)").addClass("rl-subhead");
    $(".relatedlinks ul").find("li:first:has(h3)").css("margin-top", "0px");

    //Google event to track individual download links
    var link = {
        href: "",
        _url: function() {
            var key = "/download.axd/";
            return this.href.substr(key.length);
        },
        extension: function() {
            return this._url().substr(33, 3);
        },
        filename: function() {
            return this._url().substr(this._url().search("/?d=") + 3);
        },
        path: function() {
            return this.filename() + "." + this.extension();
        }
    };

    $("a[href*=/download.axd/]").bind("click", function(e) {
        link.href = $(this).attr("href");
        _gaq.push(['_trackPageview', '/downloads/' + link.path()]);
    });
    $("a[href*=http:/]:not(a[href*=www.msmc.edu]), [href*=https:/]:not(a[href*=www.msmc.edu])")
    .each(function() {
        $(this).click(function() {
            recordOutboundLink($(this).attr("href"), "Outbound Links", $(this).attr("href"));
            return false;
        });
    });

    // Equal Height should be last to run
    $(".content #content, .content #components, .content #side-navigation").equalHeight();
});

(function($) {
    $.fn.equalHeight = function() {
        var max_bottom = 0;

        this.each(function() {
            var bottom = $(this).offset().top + $(this).outerHeight();
            if (bottom > max_bottom) max_bottom = bottom;
        });

        return this.each(function() {
            var offtop = $(this).offset().top,
				curheight = $(this).outerHeight(),
				diff = curheight - $(this).height(),
				bottom = offtop + curheight;
				
            if (max_bottom > bottom) {
				$(this).height(max_bottom - offtop - diff);
			
			}
        });
    };

    $.fn.makeAbsIfNotBottom = function($compareDiv) {
        return this.each(function() {
            var $this = $(this);
            if ($this.length) {
                var contentbottom = $compareDiv.offset().top + $compareDiv.outerHeight(),
			        supplementbottom = $this.offset().top + $this.outerHeight();
                if (contentbottom > supplementbottom)
                    $this.css('position', 'absolute').addClass('toBottom');
            }
        });
    };
})(jQuery);

function recordOutboundLink(link, category, action) {
    try {
        var myTracker = _gat._getTrackerByName();
        _gaq.push(['_trackEvent', category, action]);
        setTimeout('document.location = "' + link + '"', 100);
    } catch (err) { }
}
