$(function () {

    // iPad detaction
    function isiPad() {
        return (navigator.platform.indexOf("iPad") != -1);
    }

    // add the hash to the 'static' URL
    $("a[href^='/']:not('a.teammember')").live('click', function (e) {

        var href = $(this).attr("href");
        if (href.indexOf("career/") >= 0) {
            var index = href.lastIndexOf("career/");
            var url = href.substr(index).replace('/', '!');
        } else if (href.indexOf("article/") >= 0) {
            var index = href.lastIndexOf("article/");
            var url = href.substr(index).replace('/', '!');
        } else if (href.indexOf("case-study/") >= 0) {
            var index = href.lastIndexOf("case-study/");
            var url = href.substr(index).replace('/', '!');
        } else {
            var index = href.lastIndexOf("/");
            var url = href.substr(index).replace('/', '');
        }

        window.location.hash = url;
        e.preventDefault();
    });

    // Bind an event to window.onhashchange that, when the history state changes,
    // gets the url from the hash and displays either our cached content or fetches
    // new content to be displayed.
    $(window).bind('hashchange', function (e) {

        $('#top').remove();
	
	// Get the hash (fragment) as a string, with any leading # removed. Note that
        // in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
        var url = $.param.fragment();
        var p = "";
        var id2 = 0;

        if (url == "home" || url == "") {
            // If url is '' (no fragment), display the homepage content.
            showHomepage();
        } else {
            if (url.indexOf("!") >= 0) {
                p = url.substring(url.indexOf("#") + 1, url.indexOf("!"));
                id2 = url.substring(url.indexOf("!") + 1, url.length);
            } else {
                p = url.substring(url.indexOf("#") + 1, url.length);
            }

            // Show "loading" content while AJAX content loads - except on initial slide-up.
            if ($('#wrapper').css('background-position') != "0px 570px") {
                $('#loading').show();
            }

            // Remove .current class from any previously "current" link(s).
            $('a.current').removeClass('current');

            // Hide any visible ajax content.
            $('.bbq-content').children(':visible').hide();

            // Add .bbq-current class to "current" nav link(s), only if url isn't empty.
            url && $('#navigation a[href="/' + url + '"]').addClass('current');
            url && $('#navigation li.' + url + ' a').addClass('current');

            // Load external content via AJAX. Note that in order to keep this
            // example streamlined, only the content in .infobox is shown. You'll
            // want to change this based on your needs.
            $.get("/home/GetAjaxPage?" + Math.floor(Math.random() * 999999999),
                { key: p, ident: id2 },
                    function (data) {
                        var $d = $(data);

                        // hide the homepage box background
                        $("#homepage-box").fadeOut(300);

                        $('#wrapper').animate({ backgroundPosition: ("0 140px") }, 300, function () {
                            $('#page').show().empty();
                            $(".bbq-item").append($d);
                            // Update the document title.
                            document.title = $('#pagetitle').text();
                        });
                    },
                "html").complete(function () { $('#loading').hide(); });
        }

        // add floating back to top link (not for iPad)
        if (!isiPad()) {
            if (url == "about" || url == "capabilities") {
                $('#top').remove();
                $('#main-wrapper').append('<a id="top" href="#top" style="display:none;">Back to top</a>');

                var menuYloc = null;

                menuYloc = parseInt($("#top").css("top").substring(0, $("#top").css("top").indexOf("px")))
                $(window).scroll(function () {
                    var offset = menuYloc + $(document).scrollTop() + "px";
                    var maxHeight = $('#main-wrapper').height() - 157;
                    if (menuYloc + $(document).scrollTop() > maxHeight) {
                        offset = maxHeight + "px";
                    }
                    // fade link in/out
                    if (menuYloc + $(document).scrollTop() == 232) {
                        $('#top').fadeOut('slow', function () { $(this).hide(); });
                    } else {
                        $('#top').fadeIn('slow', function () { $(this).show(); });
                    }
                    // animate link
                    $("#top").animate({ top: offset }, { duration: 500, queue: false });
                });

                $("#top").live('click', function (e) {
                    $('html, body').animate({ scrollTop: 0 }, 'fast');
                    e.preventDefault();
                });

            } else {
                $('#top').remove();
            }
        }

    });

    // Since the event is only triggered when the hash changes, we need to trigger
    // the event now, to handle the hash the page may have loaded with.
    $(window).trigger('hashchange');

    // show homepage content
    function showHomepage() {
        // remove current class from subnavs
        $('#navigation li a').removeClass('current');
        // hide page content
        $('#page').hide().empty();
        // slide the wrapper
        $('#wrapper').animate({ backgroundPosition: ("0 560px") }, 600, function () {
            $('.bbq-default').fadeIn(600);
            // Update the homepage title.
            document.title = $('#homepagetitle').text();
            // show the homepage box
            $("#homepage-box").fadeIn(300);
        });
    };

});
