
$(document).ready(initPage);

// setup the page
function initPage() {
    intTopNav();
    initSlides();
    initSearchBox();
    initHeaderImage();
    stabliseColumnHeight();
}

function initHeaderImage() {
    $("#headerImageWrap").append($("#pageHeaderImage"));

    if ($("#pageHeaderImage").children().length > 0) {
        $("#headerImageWrap").show();
        $("#pageHeaderImage").show();
    }
}

// initilises the site search box
function initSearchBox() {

    $("#siteSearch").val($("label[for='" + $("#siteSearch").attr("id") + "']").html());

    $("#siteSearch").focus(function() {
        if ($(this).val() == $("label[for='" + $(this).attr("id") + "']").html())
            $(this).val("");
    });
    $("#siteSearch").blur(function() {
        if ($(this).val() == "") {
            $(this).val($("label[for='" + $(this).attr("id") + "']").html());
        }
    });
}

// setup up any interfade slides
function initSlides() {
    $("div.slidesContainer").each(function() {
        var cont = $(this);
        cont.height(cont.find("img").height());
        cont.find("ul.slides").each(function() {
            var ul = $(this);
            ul.find("li:not(:has(a img))").remove();
            unwrap(ul.find("li a span img"));
            unwrap(ul.find("li a[@href=''] img"));
        }).innerfade({ speed: 1000, timeout: 5000, type: 'sequence' });
    });
}

function unwrap(jqobj) {
    if (jqobj.length == 1) {
        var tbr = jqobj.parent();
        tbr.parent().append(jqobj);
        tbr.remove();
    }
}

function intTopNav() {
    $("#topNav").children("ul").children("li").hoverIntent(function() {
        $(this).children("a").addClass("hover");
        $(this).children("ul").slideDown("fast");
    },
    function() {
        $(this).children("ul").slideUp("fast");
        $(this).children("a").removeClass("hover");
    });
}

/**
* Used to ensure the nav column and the the content column are the same
* height
**/
function stabliseColumnHeight() {

    var heightCols = $(".setHeight");
    var maxHeight = 0;

    for (i = 0; i < heightCols.length; i++) {
        if ($(heightCols[i]).height() > maxHeight)
            maxHeight = $(heightCols[i]).height();
    }
    $(".setHeight").each(function() { $(this).height(maxHeight) });
    
}



