﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

var maxHeight;

$(document).ready(function ()
{
    buildBreadCrumbs();

    $("#mainpage_highlight .more")
        .before('<img src="images/spacer.png" height="40" alt="Compound Profit"/>')
        .addClass("more_img")
        .find("a")
            .first()
            .append('<img src="images/more_large.png" alt="Compound Profit" />');

    $("#mainpage_copy .more")
        .before('<img src="images/spacer.png" height="20" alt="Compound Profit"/>')
        .addClass("more_img")
        .find("a")
            .first()
            .append('<img src="images/more_small.png" alt="Compound Profit" />');

    $("#mainpage_copy").each(set_HighlightHeight);

    $(".copy_2col").columnize({ columns: 2, doneFunc: set_2ColumnHeight, buildOnce: true });

    $(".copy_3col.notched").append('<img src="images/spacer.png" height="240" alt="Compound Profit"/>');
    $(".copy_3col").columnize({ columns: 3, doneFunc: set_3ColumnHeight, buildOnce: true });

});

function set_HighlightHeight()
{
    var utubeHeight = parseInt($("#mainpage_youtube").css("height"));
    var copyHeight = parseInt($("#mainpage_copy").css("height"));

    utubeHeight = isNaN(utubeHeight) ? 0 : utubeHeight;
    copyHeight = isNaN(copyHeight) ? 0 : copyHeight;

    if(utubeHeight >= 20)
    {
        $("#mainpage_highlight").css("min-height", copyHeight + utubeHeight - 20);
    }
    //$("#page_content").css("min-height", copyHeight + utubeHeight);
}

function set_2ColumnHeight()
{
    var target = $(this)[0];

    maxHeight = 0;

    $(".column", target).each(getHeight);

    $(".column", target).css("height", maxHeight + 20);
    $(".column_txt", target).css("height", maxHeight);

    set_HighlightHeight();
}

function set_3ColumnHeight()
{
    var target = $(this)[0];

    maxHeight = 0;

    $(".column", target).each(getHeight);

    $(".column", target).css("height", maxHeight);
    $(".last.column", target).css("height", maxHeight - 240);
    $(".last.column", target).find(".column_txt").css("height", maxHeight - 240);
    $("#page_content").css("min-height", "300px"); // for IE (bug?)
}

function getHeight()
{
    var target = $(this);

    if(target.hasClass("last")) return;

    var height = target[0].offsetHeight;

    if(height > maxHeight) maxHeight = height;
}


function buildBreadCrumbs()
{
    var target = $(".menu_current");

    if($(target).length)
    {
        var crumbLine = "";

        var currentCrumb = $("span", target).first().html();
        var currentLink = $("a", target).first();
        var currentLinkUrl = "";

        if(currentCrumb.length)
        {
            if(currentLink.length)
            {
                if($(currentLink).attr("href") !== undefined)
                {
                    currentLinkUrl = ' href="' + $(currentLink).attr("href") + '"';
                }
            }
            currentCrumb = '<a' + currentLinkUrl + '">' + currentCrumb + '</a><span class="crumb">»</span><span class="current">' + $("title").html() + '</span>';
        }

        var hasMoreCrumbs = true;
        while(hasMoreCrumbs)
        {
            var nextPop = getParentMenuItem(target);

            if(nextPop != null)
            {
                var nextItem = nextPop.closest("li");

                if($(nextItem).length === 0 || $(nextItem).hasClass("sub_menu")) { continue; }

                var thisCrumb = $("span", nextItem).first().html();
                var thisLink = $("a", nextItem).first();
                var thisLinkTarget = "";

                if(thisCrumb.length)
                {
                    if(thisLink.length)
                    {
                        if($(thisLink).attr("href") !== undefined)
                        {
                            thisLinkTarget = ' href="' + $(thisLink).attr("href") + '"';
                        }
                    }

                    crumbLine = '<a' + thisLinkTarget + '">' + thisCrumb + '</a><span class="crumb">»</span>' + crumbLine;
                }
                target = nextItem;
            }
            else
            {
                hasMoreCrumbs = false;
            }
        }

        $("#breadcrumbs").html('<a href="index.html">Home</a><span class="crumb">»</span>' + crumbLine + currentCrumb);
        $("#breadcrumbs").html();
    }
    else
    {
        // home
        //$("#breadcrumbs").html("");
    }
}

function getParentMenuItem(target)
{
    var popUL = $(target).closest("ul");

    if(!$(popUL).length) { return null; }

    if($(popUL).hasClass("menu")) { return null; }

    //var nextItem = $(popUL).closest("li");

    //return $(nextItem).length ? nextItem : null;
}
