﻿function setupLibMenu() {
    jQuery("#LfMenu li").bind('mouseover', function() {
        var child = jQuery(this).find("ul");
        if (child.length <= 0 && jQuery(this).parent().parent().parent().attr("id") != "LfMenu") return; //leaf item
        else if (child.length <= 0 && jQuery(this).parent().parent().parent().attr("id") == "LfMenu")// non-leaf, no child
            jQuery("#LfMenu li").find("ul").hide("clip");
        else if (child.css("display") == "none") {
            jQuery("#LfMenu li").find("ul").hide("clip");
            child.show("clip");
        }
    });
}

function LibHome() {
    var page = 0;
    var clicked = "";
    var $pager = jQuery("#cPaging");
    var styles = jQuery("#LibCar").find("li");
    $pager.html("<div class='np' id=\"prev\"><< previous</div>");
    for (i = 0; i < styles.length; i++) {
        var html = $pager.html() + "<div id=\"" + (i + 1) + "\">" + (i + 1) + "</div>";
        $pager.html(html);
    }
    var html = $pager.html() + "<div class='np' id=\"next\">next >></div>";
    $pager.html(html);

    function mycarousel_initCallback(carousel) {
        jQuery("#cPaging div").bind('click', function() {
            carousel.options.auto = 0;
            carousel.stopAuto();
            clicked = jQuery(this).attr("id");
            if (jQuery(this).attr("class") != "np") {
                carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr("id")));
                page = clicked;
                //   jQuery("#cPaging div").css({ "background-color": "#fff", "color": "#5071AE" });
                // jQuery(this).css({ "background-color": "#5071AE", "color": "#fff" });
            }
            return true;
        });

        jQuery("#cPaging #prev").bind('click', function() {
            carousel.options.auto = 0;
            carousel.stopAuto();
            carousel.prev();
            clicked = "prev";
            return true;
        });


        jQuery("#cPaging #next").bind('click', function() {
            clicked = "next";
            carousel.options.auto = 0;
            carousel.stopAuto();
            carousel.next();
            return true;
        });
    }

    function LibCar_AfterAnim(carousel) {
        var current = jQuery("#cPaging div").eq(page)
        current.css({ "background-color": "#5071AE", "color": "#fff" });
        jQuery("#LibCar li").css("display", ""); //fadeIn("fast");

    }
    function LibCar_BeforeAnim(carousel) {
        jQuery("#LibCar li").css("display", "none"); //fadeOut("fast");
        var current = jQuery("#cPaging div").eq(page)
        current.css({ "background-color": "#fff", "color": "#5071AE" });
        if (clicked != "prev" && page > jQuery("#cPaging div").length - 3) {
            //end of loop
            page = 1;
        }
        else if (clicked == "next") page++;
        else if (clicked == "prev" && page == 1) {
            page = jQuery("#cPaging div").length - 2;
        }
        else if (clicked == "prev") page--;
        else if (clicked != "prev" && clicked != "next") {
            page++;
        }
    }
    jQuery('#LibCar').jcarousel({
        buttonPrevHTML: '',
        buttonNextHTML: '',
        scroll: 1,
        start: 0,
        auto: 5,
        visible: 1,
        wrap: "both",
        initCallback: mycarousel_initCallback,
        itemLoadCallback: { onBeforeAnimation: LibCar_BeforeAnim, onAfterAnimation: LibCar_AfterAnim }
    });
}




//set up carousel page indicator. returns # of pages
function setUpPaging(control, itemsPerPage, itemWidth) {
    var children = control.find("li");
    page = control.parent().find("span.paging");
    if (page != null) {
        //	alert(control.attr("id") + ", " + itemWidth);
        page.css("width", itemWidth * Math.ceil(children.length / itemsPerPage) + "px");
        return Math.ceil(children.length / itemsPerPage);
    }
    return -1;
}


function paging(control, next, currentP, maxP, itemWidth, circular) {
    //alert( currentP + ", " + maxP + ", " + itemWidth);

    //stop if reach the end
    if (!circular && next && currentP == maxP)
        return maxP;
    if (!circular && !next && currentP <= 1) return 1;
    //find paging position	
    var param = "background-position-x";
    if (control == null || control.html() == null) return -1;
    var length = control.css("width");
    var bgX = control.css(param);

    if (bgX == 'undefined' || bgX == null || bgX == "") {
        param = "background-position";
        bgX = control.css(param).split(" ")[0];
    }
    var position = Number(bgX.replace("px", ""));

    var init = (0 - itemWidth * Math.max(maxP - 1, 4));

    //if control circulates, and it reaches the end, circulate!
    if (circular && next && currentP == maxP) {
        control.css(param, init + "px");
        return 1;
    }
    //if control circulates, and it reaches the beginning, circulate!
    if (circular && !next && currentP == 1) {
        control.css(param, (init + (itemWidth * (maxP - 1))) + "px");
        return maxP;
    }
    //regular case paging
    if (next) {
        control.css(param, position + itemWidth + "px");
        return (currentP + 1) % (maxP + 1);
    }
    else {
        control.css(param, position - itemWidth + "px");
        return (currentP - 1) % (maxP + 1);
    }
}



/*******************************************************
PopUp Textbox OnMouseover on Font Control Icon
*******************************************************/
function kidsRelated() {
    var kids = jQuery("img.kr");
    jQuery("body").append("<div id='kidTool'>Kids Related Content</div>");
    jQuery("img.kr").hover(function() {
        var pos = jQuery(this).position();
        jQuery("#kidTool").css({ "display": "block", "left": pos.left + 10, "top": pos.top - 20 });
    });
    jQuery("img.kr").bind('mouseleave', function() {

        jQuery("#kidTool").css({ "display": "none" });
    });
}

