$(document).ready(function(){
    $("#IsContent img").each(function (){
        changeImgSize(this);
    });
    SetMenu();
    SetQQ();
    $("#MoudleVal").bind('click',function(){ShowSelect();});
});
//-----------------------------------------------------------------------
//前台缩略图按比例缩放函数
var flag=false;
function setPicRange(ImgD,iwidth,iheight)
{
    //参数(图片,允许的宽度,允许的高度)
    var image=new Image();
    image.src=ImgD.src;
    if(image.width>0 && image.height>0)
    {
        flag=true;
        if(image.width/image.height>= iwidth/iheight)
        {
            if(image.width>iwidth)
            {  
                ImgD.width=iwidth;
                ImgD.height=(image.height*iwidth)/image.width;
            }
            else
            {
                ImgD.width=image.width;  
                ImgD.height=image.height;
            }
        }
        else
        {
            if(image.height>iheight){  
                ImgD.height=iheight;
                ImgD.width=(image.width*iheight)/image.height;        
            }
            else
            {
                ImgD.width=image.width;  
                ImgD.height=image.height;
            }
        }
    }
}
//正文图片载入大小限制
function changeImgSize(obj)
{
	$(obj).css("cursor","pointer");

	//setPicRange(obj,500,500);
	
	if (!$(obj).parent().is("a"))
	{
	    $(obj).wrap("<a href='"+obj.src+"' target='_blank' title='按此在新窗口浏览图片'></a>"); 
	    $(obj).attr("border","0");
	}
}
//-----------------------------------------------------------------------
//免激活方式显示FLASH
function insertFlash(url, w, h) { 
var str = ''; 
str += '<object width="'+ w +'" height="'+ h +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0">'; 
str += '<param name="movie" value="'+ url +'">'; 
str += '<param name="wmode" value="transparent">'; 
str += '<param name="quality" value="autohigh">'; 
str += '<embed width="'+ w +'" height="'+ h +'" src="'+ url +'" quality="autohigh" wmode="transparent" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>'; 
str += '</object>'; 
document.write(str); 
}
//-----------------------------------------------------------------------
function CheckInput(ID,Str,DefauleValue)
{
    if ($.trim($('#'+ID).attr("value"))=="")
    {
        alert(Str);
        $('#'+ID).focus();
        return false;
    }
    if (DefauleValue)
    {
        if ($.trim($('#'+ID).attr("value"))==DefauleValue)
        {
            alert(Str);
            $('#'+ID).focus();
            return false;
        }
    }
    return true;
}
//-----------------------------------------------------------------------
//导航条的相关JS
_defaultMenu=0;
_menuCount=0;
_IsHide=false;
function IsShowMenu(i)
{
    HideAllMenu();
    ShowMenu(i);
}

function IsShowDefaultMenu()
{
    HideAllMenu();
    ShowMenu(_defaultMenu);
}

function ShowMenu(i)
{
    $("#Link_"+i).attr("class","MenuSelected");
    if ($("#Child_"+i).html()!=undefined)
    {
       $("#Child_"+i).show();
       $("#Child_"+i).css("left",SetChildX(i).x);
       $("#Child_"+i).css("top",SetChildX(i).y);
    }
    else
    {
        $("#Child_None").show();
        $("#Child_None").css("left",SetChildX(i).x);
        $("#Child_None").css("top",SetChildX(i).y);
    }
}

function HideAllMenu()
{
    $("#Child_None").hide();
    for (var i=0;i<_menuCount;i++)
    {
       HideMenu(i);
    }
}

function HideMenu(i)
{
    $("#Link_"+i).attr("class","");
    if ($("#Child_"+i).html()!=undefined)
    {
       $("#Child_"+i).hide();
    }
    else
    {
        $("#Child_None").hide();
    }
}

function SetChildBoxWidth(i)
{
    if ($("#Child_"+i).html()!=undefined)
    {
        $("#Child_"+i).css("display","block");
        var Width=0,Num=0;
        $("#Child_"+i).children().each(function(i){
            Width+=$(this).width();
            Num++;
        });

        if (Width>0)
            $("#Child_"+i).width(Width+(Num*10));
            
        $("#Child_"+i).css("display","none");
    }
}

function SetChildX(i)
{
    var Num=0;
    var ChildWidth=0;
    if ($("#Child_"+i).html()!=undefined)
    {
        ChildWidth=$("#Child_"+i).width();
    }
    else
    {
        ChildWidth=$("#Child_None").width();
    }
    Num=ChildWidth/2;
    Num=Num-40;
    if (getElementPos($("#Link_"+i)[0]).x-Num<getElementPos($("#Link_0")[0]).x-80) Num=0;
    return {x:getElementPos($("#Link_"+i)[0]).x-Num,y:getElementPos($("#Link_"+i)[0]).y+35};
}

function SetMenu()
{
    for (var i=0;i<_menuCount;i++)
    {
       $("#Link_"+i).mouseover(function(){IsShowMenu($(this).attr("id").replace("Link_",""))});
       SetChildBoxWidth(i);
    }
    
    $("#Child_None").css("display","block");
    $("#Child_None").width($("#Child_None").width());
    $("#Child_None").css("display","none");
    
    //鼠标移上为：mouseenter
    $("#Link_Pannel").bind("mouseleave",function(){IsShowDefaultMenu();})
    IsShowDefaultMenu();
}
//---------------------------------------------------
//切换模块的相关JS
function ShowPannel(PannelName,CurrentNum,Count,TitleName,TitleInClass,TitleLeaveClass)
{
    for (var i=0;i<Count;i++)
    {
        $("#"+PannelName+"_"+i).hide();
    }
    $("#"+PannelName+"_"+CurrentNum).show();
    
    if (TitleName!=undefined)
    {
        for (var i=0;i<Count;i++)
        {
            if (TitleLeaveClass!=undefined)
            {
                $("#"+TitleName+"_"+i).attr("class",TitleLeaveClass)
            }
            else
            {
                $("#"+TitleName+"_"+i).attr("class","")
            }
        }
        if (TitleInClass!=undefined)
        {
            $("#"+TitleName+"_"+CurrentNum).attr("class",TitleInClass);
        }
    }
}
//---------------------------------------------------
//QQ下拉列表JS
function SetQQ()
{
    $("#QQList").bind("mouseenter",function(){ShowQQ();});
    $("#QQList").bind("mouseleave",function(){HideQQ();});
}

function ShowQQ()
{
    $("#QQList dd").each(function(){
        $(this).show();
    });
}

function HideQQ()
{
    $("#QQList dd").each(function(){
        $(this).hide();
    });
}
//---------------------------------------------------
//帮助搜索部分JS
function ShowHelpSearch()
{
    if (CheckInput("Key","请输入搜索关键字！"))
    {
        return true;
    }
    return false;
}
//---------------------------------------------------
//搜索下拉列表JS
function ShowSearch()
{
    if (CheckInput("Key","请输入搜索关键字！"))
    {
        var Url=$("#Moudle").val();
        if (Url.indexOf('?')!=-1)
            Url+="&";
        else
            Url+="?";
        this.location=Url+"key="+$("#Key").val();
    }
}

$(document).click(function(){HideSelect();});

SelectMode=0;

function HideSelect()
{
    if (SelectMode==0)
    {
        $("#MoudleSelect dd").each(function(){
            $(this).hide();
        });
    }
    else
        SelectMode=0;
}

function ShowSelect()
{
    SelectMode=1;
    $("#MoudleSelect dd").each(function(){
        $(this).show();
    });
}

function IsSelect(Text,Value)
{
    $("#MoudleVal").text(Text);
    $("#Moudle").val(Value);
}
//---------------------------------------------------
function getElementPos(el) {
 var ua = navigator.userAgent.toLowerCase();
 var isOpera = (ua.indexOf('opera') != -1);
 var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof

 if(el.parentNode === null || el.style.display == 'none') {
  return false;
 }      
 var parent = null;
 var pos = [];     
 var box;     
 if(el.getBoundingClientRect)    //IE
 {         
  box = el.getBoundingClientRect();
  var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
  return {x:box.left + scrollLeft, y:box.top + scrollTop};
 }else if(document.getBoxObjectFor)    // gecko    
 {
  box = document.getBoxObjectFor(el); 
  var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0; 
  var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0; 
  pos = [box.x - borderLeft, box.y - borderTop];
 } else    // safari & opera    
 {
  pos = [el.offsetLeft, el.offsetTop];  
  parent = el.offsetParent;     
  if (parent != el) { 
   while (parent) {  
    pos[0] += parent.offsetLeft; 
    pos[1] += parent.offsetTop; 
    parent = parent.offsetParent;
   }  
  }   
  if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) { 
   pos[0] -= document.body.offsetLeft;
   pos[1] -= document.body.offsetTop;         
  }    
 }              
 if (el.parentNode) { 
    parent = el.parentNode;
   } else {
    parent = null;
   }
 while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled ancestors
  pos[0] -= parent.scrollLeft;
  pos[1] -= parent.scrollTop;
  if (parent.parentNode) {
   parent = parent.parentNode;
  } else {
   parent = null;
  }
 }
 return {x:pos[0], y:pos[1]};
}