﻿$().ready(function() {

    $.fn.vscroll = function() {

        var moveStep = 0;
        //滚动初始值
        var step = 0;
        //是否需要滚动
        var canScroll = true;
        var interval;
        var $scroll;
        var containerHeight;
        var actualHeight;
        var diff;

        //  return this.each(function() {


        //不需要滚动
        //if (diff < 1) {
        //   canScroll = false;
        // }
        // else {
        //       $scroll.before('<div class="imgup" style="width: 190px;"><img src="images/nav_up.png" alt="" /></div>');
        //       $scroll.after('<div class="imgdown" style="width: 190px;"><img src="images/nav_dn.png" alt="" /></div>');
        //}


        //    $scroll.hover(function() {
        //        interval = setInterval(scroll, 50);
        //    }, function() {
        //        clearInterval(interval);
        //    });
        /******
        *  include cookie.js
        
        myscript = document.createElement("script");
        myscript.setAttribute("type", "text/javascript");
        myscript.setAttribute("src", "jquery.cookie.js"); // 在这里引入了a.js
        */


        $(this).prev(".imgup").hover(function() {
            moveStep = -2;

            /* if ($scroll == null) {
            $scroll = $(this).next(".scroll");

                //滚动框高度
            containerHeight = $scroll.height();

                var $li = $scroll.children("ul").children("li");
            var totalItems = $li.length;
            var itemHeight = $li.eq(0).height() == 0 ? $li.eq(0).find("a").height() : $li.eq(0).height();

                //滚动内容总高
            actualHeight = totalItems * itemHeight;
            //内容框高度和滚动框高度的差值，用于重定位滚动内容
            diff = actualHeight - containerHeight;
            }*/
            interval = setInterval(scroll, 50);
        }).mouseout(function() {
            moveStep = 0;
            clearInterval(interval);
        });

        $(this).next(".imgdown").hover(function() {

            moveStep = 2;

            //   if ($scroll == null || $scroll == undefined)
            $scroll = $(this).prev(".scroll");

            //用step控制位移量
            //    if (step == 0 && $.cookie('step') == null) {   //滚动框高度

            containerHeight = $scroll.height();
            var $li = $scroll.children("ul").children("li"); //$(".scroll >ul >li");
            var totalItems = $li.length;
            var itemHeight = $li.eq(0).height() == 0 ? $li.eq(0).find("a").height() * 2 : $li.eq(0).height() * 2;

            //滚动内容总高
            actualHeight = totalItems * itemHeight;
            //  }

            // alert($.cookie('step'));

            //内容框高度和滚动框高度的差值，用于重定位滚动内容
            diff = actualHeight - containerHeight; // -$.cookie('step');

            /*  if ($.cookie('step')) {
            step = $.cookie('step');
            }
            else {
            $.cookie('step', step);
            }*/

            interval = setInterval(scroll, 50);

        }).mouseout(function() {

            //   $.cookie('step', step); //, { expires: 7, path: '/', domain: 'jquery.com', secure: true }

            moveStep = 0;
            clearInterval(interval); //移动速度清零
        });


        /*  if (canScroll) {
        $(".scroll >ul:first").clone().appendTo(".scroll");
        interval = setInterval(scroll, 50);
        }*/




        function scroll() {

            if (step > actualHeight) {
                step = 1;
            }

            if (step <= 0) {
                step = 1; //actualHeight;
            }
            step += moveStep;

            if ($scroll != null) {
                $scroll.scrollTop(step);
            }
        }


        //  });

    }

    //*********  var currentnewstitle   //define in some page for use for crossing js 
    var clickblockmenu;
    var clicksubmenu;
    var currentmenu; //current submenu
    var hovernewstitle; // include hover newstitle > a or popmenu > li

    var pagename;
    var lineheight = 22; //define line height

    // search： ?id=mwg  hash： #1
    var id;
    if (window.location.search.length > 0) {
        id = window.location.search.split("=")[1];
    }

    pagename = location.toString().substring(location.toString().lastIndexOf("/") + 1, location.toString().lastIndexOf("."));
 
    if (pagename == "career") {
        currentmenu = $(".menutitle:eq(5)").find(".submenu:first");
        $(".menutitle:eq(5)").children("a").css("color", "#00ccff");
    }
    else if (pagename != "home" && pagename != ".cn/") {

        $("#leftmenu .submenu").each(function() {

            if ($(this).find("a:first").attr("href").indexOf(pagename) >= 0) {
                currentmenu = $(this);

                //show 2th menu
                currentmenu.parents(".leftslide").addClass("selected").show();

                var subnum = currentmenu.parent(".sub").children().length;
                if (subnum > 0) {
                    var subheight = lineheight * subnum;
                    currentmenu.parents(".leftslide").children(".subhead").css({ height: subheight + "px" });
                }

                // if no popmenu or midmenu ,just no the third level menu
                if (currentmenu.children("div").length < 1) {
                    if (id != null && id != "") {
                        currentmenu.parent(".sub").children(".submenu").each(function() {
                            var temphref = $(this).find("a:first").attr("href");
                            var curmidmenuname = temphref.split("=")[1];
                            if (curmidmenuname == id) {
                                currentmenu = $(this);
                                return false;
                            }
                        });
                    }
                }
                else if (currentmenu.children("div").length > 0)//have the 3rd memu then show 3rd menu
                {
                    // if have  popmenu or midmenu ,just the third level menu
                    if (currentmenu.children("div").not(".expanded").length > 0) {

                        if ($(this).children("div").is(".midmenu")) {
                            //set default one of the slide list on news page or truethought page 
                            //if location.search > 0 // search： ?id=mwg  hash： #1
                            if (id != null && id != "") {
                                //  set default submenu title's backgroundcolor
                                currentmenu.parents(".sub").find(".midmenu").each(function() {
                                    var temphref = $(this).find(".newstitle a:first").attr("href");
                                    var curmidmenuname = temphref.split("=")[1].substring(0, temphref.split("=")[1].indexOf("#"));

                                    if (curmidmenuname == id) {
                                        currentmenu = $(this).parents(".submenu");

                                        return false;
                                    }
                                });
                            }

                            //set midmenu's offsettop
                            var curmenutitle = $.trim(currentmenu.parents(".menublock").find(".menutitle").text());

                            var depth = (curmenutitle == "NEWS" || curmenutitle == "近日动态") ? 0 : 1;
                            var thisdiv = currentmenu;
                            while (thisdiv.prev(".submenu").text() != "") {
                                thisdiv = thisdiv.prev(".submenu");
                                depth += 1;
                            }
                            var offsettop = lineheight * depth;
                            currentmenu.children("div.midmenu").css("top", "-" + offsettop + "px");

                        }
                    }

                    currentmenu.children("div").not(".expanded").addClass("expanded").show(500);

                    currentmenu.find('.scroll').vscroll();
                    // $("#scriptcontainer").html("<script type='text/javascript'>$().ready(function(){currentmenu.find('.scroll').vscroll();});</script>");
                    //  $("#scriptcontainer").html("<script type='text/javascript'>$().ready(function() { $('.listscroll').jScrollPane({scrollbarWidth: 0 });});</script>");

                    //set default one of the slide list on news page or truethought page 
                    if (location.hash.length > 0) {
                        $(".expanded").find(".newstitle a").each(function() {

                            if ($(this).attr("href").substring($(this).attr("href").indexOf("#")) == location.hash) {
                                currentnewstitle = $(this); //.parent("li").siblings("li").children("a").css({ color: "#ffffff" }).css({ fontSize: "12px" });

                                setNewsTitle(currentnewstitle);

                                return false;
                            }
                        });
                    }
                    if (id != null && id != "") { //popmenu

                        $(".expanded").find("a").each(function() {

                            if ($(this).attr("href").substring($(this).attr("href").lastIndexOf("?")) == location.search) {
                                currentnewstitle = $(this).parent("li");
                                $(this).parent("li").css({ backgroundColor: "#00ccff" });
                                return false;
                            }
                        });
                    }

                }

                currentmenu.css({ backgroundColor: "#ffffff" }).parents(".menublock").css({ color: "#00ccff" });

                // loop each circle
                return false;
            }
        });

    }

    // set sytle for newstitleobj including currentnewstitle or hovernewstitle
    function setNewsTitle(newstitleobj) {

        var dheight = newstitleobj.css({ color: "#00ccff" }).css({ fontSize: "14px" }).height();
        newstitleobj.next("div").remove();
        newstitleobj.prev("div:first").html("<div style='height:" + dheight + "px;' class='subhead'>&nbsp;</div>");
        newstitleobj.after("<div class='subhead'style='height:" + dheight + "px;float:right;'>&nbsp;</div>");

    };

    //example :
    //   1. $(function(){
    //jQuery.fn.extend({
    //    check: function() {
    //        alert("44");
    //    }
    //   6.         uncheck: function() {
    //   7.             return   this.each(function() { this.checked = false; });
    //   8.         };
    //});
    //  10.     $("#check").bind("click", function(){ $("input[@type=checkbox]","#prod_list").check(); }),
    //  11.     $("#uncheck").bind("click", function(){ $("input[@type=checkbox]","#prod_list").uncheck(); }),
    //  12. );



    $("#leftmenu .menutitle").click(function() {
        $(".menutitle:eq(5)").children("a").css("color", "#ffffff");

        clickblockmenu = $(this).parent(".menublock");
        //close others sub menu
        $("#leftmenu .selected").removeClass("selected");
        $(this).parent().css({ color: "#00ccff" }).siblings(".menublock").css({ color: "#ffffff" }).children("div.leftslide").hide();
        //show sub menu
        if ($(this).next("div").children("div.sub").children(".submenu").html().length > 0) {
            var subnum = $(this).next("div").addClass("selected").children("div.sub").children().length;

            if (subnum > 0) {
                var subheight = lineheight * subnum;
                $(this).siblings(".leftslide").show(600).children("div.subhead").css({ height: subheight + "px" });
            }
        }

    });

    $("div.submenu").hover(function() {

        $(this).css({ backgroundColor: "#ffffff" }).siblings("div.submenu").css({ backgroundColor: "Transparent" });
    });

    $("div.submenu").click(function() {

        clicksubmenu = $(this);

        $("#leftmenu .expanded").not($(this)).removeClass("expanded");
        $(this).siblings("div.submenu").children("div").hide();

        // if has sub menu then set and show it
        if ($(this).children("div").length > 0) {
            if ($(this).children("div").is(".midmenu")) {

                //count midmenu's offsettop
                var curmenutitle = $.trim($(this).parents(".menublock").find(".menutitle").text());
                var depth = (curmenutitle == "NEWS" || curmenutitle == "近日动态") ? 0 : 1;
                var thisdiv = $(this);
                while (thisdiv.prev(".submenu").text() != "") {
                    thisdiv = thisdiv.prev(".submenu");
                    depth += 1;
                }
                var offsettop = lineheight * depth;
                $(this).children("div.midmenu").css("top", "-" + offsettop + "px");
            }

            $(this).children("div").not(".expanded").addClass(".expanded").show(500);
            //  $("#scriptcontainer").html("<script type='text/javascript'>$().ready(function() { $('.listscroll').jScrollPane({scrollbarWidth: 0 });});</script>");
            // $("#scriptcontainer").html("<script type='text/javascript'>$().ready(function(){$(this).find('.scroll').vscroll();})</script>");
            $(this).find('.scroll').vscroll();
        }

    });

    $("div.popmenu ul li").hover(function() {

        $(this).siblings("li").css({ backgroundColor: "Transparent" });
        //*****
        hovernewstitle = $(this).css({ backgroundColor: "#00ccff" });
    });


    $("div.newstitle ul li a").hover(function() {

        var temp = $(this).parent("li").siblings("li").children("a");

        temp.css({ color: "#ffffff" }).css({ fontSize: "12px" });
        temp.prev("div").html("&nbsp;");
        temp.next(".subhead").remove();



        hovernewstitle = $(this);
        setNewsTitle(hovernewstitle);

        /*  var dheight = hovernewstitle.height();
        hovernewstitle.next("div").remove();
        hovernewstitle.prev("div:first").html("<div style='height:" + dheight + "px;' class='subhead'>&nbsp;</div>");
        hovernewstitle.after("<div class='subhead'style='height:" + dheight + "px;float:right;'>&nbsp;</div>");
        */

    });

    var recovermenu = function() {
        if (pagename == "career") {
            $(".menutitle:eq(5)").children("a").css("color", "#00ccff");
        }
        if (clickblockmenu != null) {
            if (clickblockmenu.children(".menutitle").text() != currentmenu.parents(".blockmenu").children(".menutitle").text()) {
                clickblockmenu.css({ color: "#ffffff" }).children(".leftslide").hide();
                currentmenu.parents(".leftslide").show().parents(".blockmenu").css({ color: "#00ccff" });
            }
        }
        if (clicksubmenu != null) {

            if (clicksubmenu.children("div").length > 0) {
                clicksubmenu.css({ backgroundColor: "Transparent" }).children("div").hide();
            }
        }
        if (currentmenu != null) {
            currentmenu.children("div").show();
            currentmenu.css({ backgroundColor: "#ffffff" }).siblings("div.submenu").css({ backgroundColor: "Transparent" }).parents(".menublock").css({ color: "#00ccff" });
        }
    };


    $(".bodyright").hover(function() {

        setTimeout(recovermenu, 3000);

    });


    if (currentmenu != null && currentmenu.children("div").length > 0) {

        currentmenu.children("div").mouseout(function() {

            if ($(this).attr("class").indexOf("midmenu") > -1) {
                // recover current news title
                if (hovernewstitle != null) {
                    hovernewstitle.css({ color: "#ffffff" }).css({ fontSize: "12px" }).next(".subhead").remove();
                    hovernewstitle.prev("div:first").html("&nbsp;");
                }
                if (currentnewstitle != null) {
                    /*  currentnewstitle.next("div").remove();
                    currentnewstitle.css({ color: "#00ccff" }).css({ fontSize: "14px" });
                    var dheight = currentnewstitle.height();
                    currentnewstitle.prev("div:first").html("<div style='height:" + dheight + "px;' class='subhead'>&nbsp;</div>");
                    currentnewstitle.after("<div class='subhead'style='height:" + dheight + "px;float:right;'>&nbsp;</div>");
                    */
                    setNewsTitle(currentnewstitle);
                }
            }
            else // if ($(this).attr("class") == "popmenu")
            {
                currentnewstitle.css({ backgroundColor: "#00ccff" });
                hovernewstitle.css({ backgroundColor: "Transparent" });
            }
        });
    }

})
