//globals
var easing = "easeOutCubic";

function toggleNav(f) {

    toggleArrow();

    $("#nav ul").stop(true,true).toggle("slide",{ easing: easing },2000,function() {

        if (typeof f == "function") f();

        if (!$("#nav ul:visible").length) startAnimation();

    });

}

function toggleContent(location) {

    stopAnimation();

    var contentVisible = $("#content:visible").length,
        lastLocation = readCookie("location"),
        content,
        matches,
        toggleWrapper;

    createCookie("location",location);

    toggleWrapper = location == lastLocation || !contentVisible;

    getContent(location,toggleWrapper);

}

function getContent(location,toggleWrapper) {

    $("#content").removeClass("project").fadeOut(500,function() {

        $("#content").hide();

        $.ajax({
            url : location,
            cache : false,
            success: function(data) {

                var content = $(data).find("#content").html(),
                    matches = data.match(/<title>(.*?)<\/title>/);

                document.title = matches[1];

                $("#content").html(content);

                $("#project a[rel]").colorbox();

                if (toggleWrapper) {

                    $("#wrapper").slideToggle(1500,easing,function() {

                        $("#content").fadeIn(500);

                    });

                } else {

                    $("#content").fadeIn(500);

                }

            }

        });

    });

    if (location.indexOf('wellbeck') != -1 ||
        location.indexOf('derby') != -1 ||
        location.indexOf('pattonair') != -1 ||
		location.indexOf('louis_group') != -1 ||
		location.indexOf('midland_fixings_group') != -1 ||
		location.indexOf('the_showroom') != -1 ||
        location.indexOf('youth_hub') != -1) {

        $("#content").addClass("project");

    }

}

function toggleArrow() {

    if ($("#nav ul:visible").length) {

        $("#menu").html("Menu <span>&gt;</span>");

    } else {

        $("#menu").html("<span>&lt;</span> Menu");

    }

}

var request_hash;

$(document).ready(function() {
						   
						   

    $("#nav ul,#wrapper").hide();

    $("#menu").click(function() {

        stopAnimation();

        if ($("#wrapper:visible").length) {

            $("#wrapper").slideUp(1000,function() { toggleNav(); });

        } else {

            toggleNav();

        }
		
		

    });

    $("#nav a,#projects a,a.internal").live("click",function() {

        var location = this.pathname,
            parts = location.split('/'),
            file = parts[parts.length - 1];

        //toggleContent(file); //not needed now as it is captured by window.change!

        window.location.hash = "!"+file;

        window.history.current = window.location;

        return false;

    });

    $(window.location).bind("change hashchange",function(obj) {

        request_hash = window.location.hash.replace('#!','');

        toggleContent(request_hash);

    });

    if (request_hash = window.location.hash) {

        request_hash = request_hash.replace('#!','');

        toggleNav(function() {

            toggleContent(request_hash);

        });

    }

});
