﻿$(function () {

    $(window).hashchange(function () {
        loadPortfolio(location.hash);
    });

    if ($("#folio").length > 0) {
        if (window.location.hash) {
            $(window).hashchange();
        } 
//        else {
//            loadPortfolio("latest");
//        }
    }    

    $("#nav-folio-list a[href='" + location.hash + "']").addClass("on");

    $("#nav-folio-list li a").click(function () {

        if ($(this).hasClass("arrow")) {
            return false;
        }

    });

    $("#nav-view .btn-list").click(function () {

        if (!$(this).hasClass("on")) {

            $(this).addClass("on");
            $("#nav-view .btn-grid").removeClass("on");
            $("#folio-list").show();
            $("#grid").hide();

        }

        return false;

    });

    $("#nav-view .btn-grid").click(function () {

        if (!$(this).hasClass("on")) {

            $(this).addClass("on");
            $("#nav-view .btn-list").removeClass("on");
            switchToGrid();

        }

        return false;

    });

});

function loadPortfolio(strType) {

    $("#nav-folio-list li a").removeClass("on");
    $("#nav-folio-list li a[href='" + location.hash + "']").toggleClass("on");

    if (location.hash.indexOf("client/") >= 0) {
        $("#nav-folio-list li a[href='/clients']").addClass("on").addClass("open");
    }

//    if (location.hash) {
//        $("#nav-folio-list li a[href='" + location.hash + "']").toggleClass("on");
//    } else {
//        $("#nav-folio-list li a[href='#!" + strType + "']").toggleClass("on");
//    }    

    $("#folio-content").fadeTo(0, 0.5);

    strType = strType.replace('#!', '');

    if ($(window).scrollTop() < 200) {
        $.scrollTo($("#portfolio"), 500);
    }

    $.cookie("LEWISNav", strType);

    $.ajax({
        type: "POST",
        url: "/ajax.asmx/GetPortfolio",
        data: "{'strType':'" + strType + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            document.getElementById("folio-content").innerHTML = msg.d; // IE needs non-jquery to play nice

            if ($("#nav-view .btn-grid").hasClass("on")) {
                switchToGrid();
            }

            $("#folio-content").fadeTo(1000, 1.0, function () {
                $(this).css("filter", "none");
            });

            $(".solid, .feature-image a").hover(function () {
                $(this).children(".image-hover").not(":animated").fadeIn(250);
            }, function () {
                $(this).children(".image-hover").fadeOut(250);
            });

            Cufon.refresh();
            
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("There was a problem supplying the content - Please try again. " + errorThrown);
        }
    });

}

function switchToGrid() {

    // Copy the html from the list view into the grid section
    $("#grid").html($("#folio-list").html()).show();

    $("#grid .list-feature").each(function () {

        $(this).removeClass("list-feature").addClass("grid-item");

        var thumb = $(this).find(".feature-image").find("img").attr("rel");
        $(this).find(".feature-image").find("img").attr("src", thumb);
        var $img = $(this).find(".feature-image").removeClass("feature-image").addClass("grid-image").remove();
        var $cat = $(this).find(".box-info").children("p:first");

        $(this).find(".box-info").remove();
        $(this).find(".box").removeClass("box-switch").removeClass("solid").addClass("grid-box").before($img).append($cat);

    });

    $("#grid article:nth-child(3n)").addClass("mr0");

    Cufon.replace(['.grid-box h2'], {
        hover: true, fontFamily: 'Clarendon BT Bold'
    });

    // Work out in each row which box is the tallest and set each box in that row to that height
    var gridBoxTallest = 0;
    var gridBoxes = $("#grid .grid-item .box").length;
    var gridBoxesPerRow = 3;
    var gridRows = Math.ceil(gridBoxes / gridBoxesPerRow);

    for (i = 1; i < gridRows + 1; i++) {

        for (b = 1; b < gridBoxesPerRow + 1; b++) {

            var box = (((i - 1) * gridBoxesPerRow) + b) - 1;

            var gridHeight = $("#grid .grid-item .box:eq(" + box + ")").height();
            if (gridHeight > gridBoxTallest) {
                gridBoxTallest = gridHeight;
            }

        }

        b = 1;

        for (b = 1; b < gridBoxesPerRow + 1; b++) {
            var box = (((i - 1) * gridBoxesPerRow) + b) - 1;
            $("#grid .grid-item .box:eq(" + box + ")").height(gridBoxTallest);
        }

        gridBoxTallest = 0;

    }

    $("#folio-list").hide();

}
