(function($) {
    $(function() {
        function initTopNav() {
            if ($("div#topNav").length > 0) {
                var navItems = $("div#topNav li");
                var timerID;
                navItems.each(function() {
                    var child = $(this).children("ul");
                    var li = $(this);
                    if (child.length > 0) {
                        $(this).addClass("hasChild");
                        if ($(this).width() < 100) {
                            child.width($(this).width() + 85);
                        } else {

                            child.width($(this).width() + 5);
                        }
                        $(this).bind("mouseenter",
                        function() {
                            timerID = setTimeout(function() {
                                child.slideDown();
                                li.addClass("on");
                            },
                            200);
                        });
                        $(this).bind("mouseleave",
                        function() {
                            clearTimeout(timerID);
                            child.slideUp();
                            li.removeClass("on");
                        });
                    }
                });
            }
        }
        function dropList() {
            if ($("div#searchCate").length > 0) {
                var selected = $("div#selectedCate");
                var list = $("ul#cateList");
                var listItem = list.find("a");
                selected.click(function() {
                    list.toggle()
                });
                listItem.each(function() {
                    $(this).click(function() {
                        selected.text($(this).text());
                    });
                });
                $("body").click(function(event) {
                    if (event.target.id != "selectedCate") {
                        list.hide()
                    }
                });
            }
        }
        function sideNav() {
            var li = $("div.sideNav>ul>li");
            li.each(function() {
                if ($(this).attr("class") != "on") {
                    var ul = $(this).find("ul");
                    $(this).bind("mouseenter",
                    function() {
                        if (ul.length > 0) {
                            $(this).css({
                                "position": "relative"
                            });
                            ul.css({
                                "position": "absolute",
                                "left": "210px",
                                "top": "0px",
                                "z-index": "5"
                            });
                            ul.show();
                        }
                    });
                    $(this).bind("mouseleave",
                    function() {
                        if (ul.length > 0) {
                            $(this).css({
                                "position": "static"
                            });
                            ul.css({
                                "position": "static",
                                "left": "210px",
                                "top": "0px"
                            });
                            ul.hide();
                        }
                    });
                }
            });
        }
        function indTab() {
            var tabs = $("ul#indGoodsTab>li");
            var tabCons = $("div#indGoods ul.ulSty1");
            var timerID;
            tabs.mouseover(function() {
                var li = $(this);
                var i = (tabs.index(li));
                //	timerID = setTimeout(function(){
                //		var old=	$(".on",tabs) ;
                //	console.log("df")
                tabs.removeClass("on");
                li.addClass("on");
                tabCons.not(this).hide();
                tabCons.eq(i).fadeIn("fast");

            });
        /*var li = $(this);
			li.bind("mouseenter",function(){
			console.log(tabs.index(li))
					timerID = setTimeout(function(){
						tabs.removeClass("on");
						li.addClass("on");
						
						tabCons.eq(i).fadeIn("normal");
					},200);	
			});
			li.bind("mouseleave",function(){
				clearTimeout(timerID);
			}); */
            //});
        }
        initTopNav();
        dropList();
        sideNav();
        indTab();
    });
})(jQuery);

