(function($){
$.fn.extend({
        Scroll:function(opt,callback){

                if(!opt) var opt={};
                var _this=this.eq(0).find("ul:first");
                var        lineH=_this.find("li:first").height(),
                        line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10),
                        speed=opt.speed?parseInt(opt.speed,10):500,
                        timer=opt.timer?parseInt(opt.timer,10):3000;
                if(line==0) line=1;
                var upHeight=0-line*lineH;				
				
                //��������
                scrollUp=function(){
                        _this.animate({
                                marginTop:upHeight
                        },speed,function(){
                                for(i=1;i<=line;i++){
                                        _this.find("li:first").appendTo(_this);
                                }
                                _this.css({marginTop:0});
                        });
                }
				

				
                _this.hover(function(){
                        clearInterval(timerID);
                },function(){						
                        timerID=setInterval("scrollUp()",timer);
                }).mouseout();
        }        
})
})(jQuery);

$(function(){
	var relid = $("a.nav_current").attr("rel");
	if (relid)
	{
			$("#"+relid).show(); //Show the subnav
	}

	
	$("div.nav_main li").hover(function() { //Hover over event on list item
		$("a.nav_current").removeClass("nav_current");
		$(this).find("a").addClass("nav_current");
		$(".nav_son").hide();
		var relid = $(this).find("a").attr("rel");
		if (relid)
		{			
			$("#"+relid).show(); //Show the subnav
		}
	} , function() { //on hover out...	
	});

	//��ҳtab
	$(".indexTab li a").hover(function(){
		var tabTitCont = $(this).parent().parent("ul");
		var tabCont = tabTitCont.attr("rel");
		$("a",tabTitCont).removeAttr("class");
		$(this).attr("class","now");
		$("."+tabCont).hide();
		$("."+tabCont).eq(parseInt($(this).attr("id"))-1).show();
		return false;
	})

//	$("#scrollInfo").Scroll({line:3,speed:500,timer:3000});

   $("#searchtext").focus(function(){
       if ($("#searchtext").val() == "标准号或名称")
       {
           $("#searchtext").val("");
           $("#searchtext").css("color","#000");
       }
   });

    $('#category li').hover(function(){
        $(".subCategory").hide();
        $(this).addClass("current_li");
        var i = $("#category li").index(this);
        var s = $(".subCategory").eq(i);

        var e = $(this);
        var z = 20,
            u = 0;
        var C = e.offset(), p=C.top,q=$(window).scrollTop(),w = $(window).height(),y=s.height();
//            y = e.height(),
//            v = $(".subCategory").offset(),
//            t = $(".subCategory").height(),
//            w = $(window).height(),
//            q = $(window).scrollTop(),
//            r = w - t - (C.top - q),
//            x = Math.abs(C.top - q - v.top),
//            B = w - (C.top - q),
//            p = C.top - v.top;

//        if (r <= 0) {
//            r = Math.abs(r);
//            if (B > y) {
//                u = B - y;
//                if (u > z) {
//                    p = p - r - z + 7;
//                } else {
//                    p = p - r;
//                }
//            } else {
//                p = p - r + z + B + 20;
//            }
//        }


//        p = p-2;
//        alert(p+","+$(window).scrollTop()+","+$(window).height());
        var z =  w-(p-q) -e.height();

        if (z <  y)
        {
            p = p-(y-z);
            if ((p+y) < (C.top+e.height()))
//                p = C.top - y;
                p = C.top+e.height()-y-20;

        }
        if (p < 0) {
            p = 0;
        }

        ww = ($(window).width()-988)/2+197;

        s.css("top", p + "px");
        s.css("left",ww+"px");

        $("#category li").eq(i).css("z-index",20);

        s.show();

    },function(){
        $(this).removeClass("current_li");
        $(".subCategory").hide();
        $(this).css("z-index",0);
    });

    $('.subCategory').mouseover(function(){
        var i = $(".subCategory").index(this);
        $('#category li').eq(i).addClass("current_li");
        $('#category li').eq(i).css("z-index",20);

        $(this).show();
    }).mouseout(function(){
    var i = $(".subCategory").index(this);

        var obj = $(this);
        obj.hide();
        $('#category li').eq(i).removeClass("current_li");
        $('#category li').eq(i).css("z-index",0);

    });


});

function login()
{
    if ($("#j_username").val() == "")
    {
        alert("请输入用户名");
        $("#j_username").focus();
        return;
    }
    if ($("#j_password").val() == "")
    {
        alert("请输入密码");
        $("#j_password").focus();
        return;
    }

    $("#loginForm").submit();
}

function loginajax()
{
    if ($("#j_username").val() == "")
    {
        alert("请输入用户名");
        $("#j_username").focus();
        return;
    }
    if ($("#j_password").val() == "")
    {
        alert("请输入密码");
        $("#j_password").focus();
        return;
    }
    var j_username = $("#j_username").val();
    var j_password = $("#j_password").val();
    var redirectURL = location.href;
    $.ajax({
             type : "POST",
             url : "/j_spring_security_check",
             data : "j_username="
                     + j_username
                     + "&j_password="
                     + j_password
                     + "&ajax=ajax"+ "&redirectURL="+redirectURL,
             success : function(msg) {
                 eval("var jsonObj="+ msg);
                 if (jsonObj.success == "true") {
//                     window.location.reload(true);
                     $("#login").html("");
                     $("#login").html("<div id='reg'>欢迎:&nbsp;"+jsonObj.username+"&nbsp;<a title='用户中心' href='user.action'>【用户中心】</a>&nbsp;<a title='退出' href='j_spring_security_logout'>【安全退出】</a></div>");
                 } else if (jsonObj.success == "false") {
                     // 写入登录失败信息
                     var errors = "对不起，用户名或密码不正确！";
                    alert(errors);
                 }
             }
         });

}

function loginstatus()
{
    $.ajax({
             type : "POST",
             url : "/login!islogin.action",
             success : function(msg) {
                 eval("var jsonObj="+ msg);
                 if (jsonObj.success == "true") {
                     $("#login").html("");
                     $("#login").html("<div id='reg'>欢迎:&nbsp;"+jsonObj.username+"&nbsp;<a title='用户中心' href='user.action'>【用户中心】</a>&nbsp;<a title='退出' href='j_spring_security_logout'>【安全退出】</a></div>");
                 } else if (jsonObj.success == "false") {
                     $("#login").html("");
                     $("#login").html('<form id="loginForm" action="/j_spring_security_check" method="post">' +
                             '<input type="hidden" value="ajax" name="ajax"/>' +
                             '用户名&nbsp;<input type="text" class="required input_w" name="j_username" id="j_username"/>' +
                             '&nbsp;&nbsp;密码&nbsp;<input type="password" class="input_w" name="j_password" id="j_password"/>' +
                             '</form><div class="clearfloat"></div>' +
                            '<div id="reg"><a href="/login!reg.action">注册</a>&nbsp;&nbsp;<a href="javascript:loginajax();">登陆</a></div>');
                 }
             }
         });
}
function search(){
    if ($("#searchtext").val() == "标准号或名称" || $("#searchtext").val() == "")
    {
        alert("请输入查询条件");
        $("#searchtext").val("");
        $("#searchtext").focus();
        return false;
    }else
        $("#searchform").submit();
}
