var minHeight = 800;
var resizeHeightOffset = 25;

function resizeMenu() {
    if ($("#menu").height() < ($("#result").height() + resizeHeightOffset)) {
        $("#menu").height($("#result").height() + resizeHeightOffset);
    }		
}

function resizeList(items) {
    //var $items = $("#list .list-item");
    var itemCount = $(items).size();
    var itemRowValues = new Array();
    
    $.each(
        $(items),
        function(index) {
            var val = parseInt($(this).height());
            var headerVal;
            var priceDetailVal;
            var detailVal;
            
            if (val <= 0) {
                headerVal = parseInt($(this).find("h4").height());
                priceDetailVal = parseInt($(this).find(".price-detail").height());
                detailVal = parseInt($(this).find(".detail").height());
                val += headerVal + Math.max(priceDetailVal, detailVal);
                $(this).height(val);
            }
            
            var itemRowIndex;
            if ($(this).attr("class").indexOf("two-column-list-item") >= 0) {
                itemRowIndex = Math.floor(index / 2);
            }
            else if ($(this).attr("class").indexOf("three-column-list-item") >= 0) {
                itemRowIndex = Math.floor(index / 3);
            }
            if ((itemRowValues[itemRowIndex] === undefined) || (itemRowValues[itemRowIndex] < val)) {
                itemRowValues[itemRowIndex] = val;
            }
        }
    );
    $.each(
        $(items),
        function(index) {
            var itemRowIndex;
            if ($(this).attr("class").indexOf("two-column-list-item") >= 0) {
                itemRowIndex = Math.floor(index / 2);
            }
            else {
                itemRowIndex = Math.floor(index / 3);
            }			
            $(this).css("height", itemRowValues[itemRowIndex]);
        }
    );
}

function fadeOutCallback(url) {
    $("#menu").height(minHeight);
    $("#result").load(url, function() { loadCallback(); });
}

function loadCallback() {
    $("#result").toggle("blind", "", "slow", function() { fadeInCallback(); });
}

function fadeInCallback() {
    $.each(
        $(".list"),
        function(index, value) {
            var $items = $(this).find(".list-item");
            if ($items.size() > 0) {
                resizeList($items);
            }
        }
    )
    /*
    var $items = $("#list .list-item");
    if ($items.size() > 0) {
        resizeList($items);
    }
    */
    resizeMenu();
}

function clickMenu(menuId) {
    $(menuId).parent().trigger("mouseenter").trigger("click");
    $(menuId).click();
    return false;
}

$.ajaxSetup({
   cache: false 
});

$(function() {
    $("#content-tabs").tabs({
        fx: {
            opacity: "toggle",
            duration: "slow"
        }
    });
});

$(function() {
    $("ul#menu-lavalamps").lavaLamp({fx: "swing", speed: "slow"});
});
            
$(document).ready(function() {
    resizeMenu();
    
    //$("a[href=#]").click(function(){return false;});

    $(function() {
        $("#result").load("includes/home.html");
    });
    
    var links = ["home",
                 "service",
                 "service-acupuncture",
                 "service-massage",
                 "service-body-treatment",
                 "service-waxing",
                 "service-medical-aesthetics",
                 "service-nail",
                 "service-out-call",
                 //"service-other",
                 "massage",
                 "bridal-party",
                 "princess-party",
                 "cupping-massage",
                 "shellac",
                 "price-list",
                 "ladies-leisure-talk",
                 "monthly-specials",
                 "daily-specials",
                 "photo-gallery",
                 "promotions",
                 "on-site-group-party",
                 "gift-certificates",
                 "specials",
                 "events",
                 "contact-us"];
    
    $.each(
        links,
        function(index, value) {
            $("#" + value + "-link").click(
                function() {
                    $("#result").toggle("blind", "", "slow", function() { fadeOutCallback("includes/" + value + ".html"); });
                }
            );
        }
    );
});

