//Design By Eric Xu 2008-5-28
jQuery.fn.extend({
//start
  ejqaccordion: function(options) {
  var firstOpen=-1;
  if(options)
  firstOpen=(options.firstOpen!=null&&options.firstOpen>=0)?options.firstOpen:-1;
  $(this).find("dd:visible").css("display","none");
  if(firstOpen>=0)
  $($(this).find("dd").get(firstOpen)).css("display","block");
  $(this).find("dt").click(function(){
      var vdd=$(this).parent().find("dd:visible");
      var ndd=$(this).next();
      vdd.slideUp('fast');
      if(ndd.css("display")=="none")
      ndd.slideDown('fast');
  });
  }
,
ejqscroll:function (options){
var ts=$(this);
setInterval(function(){esad(ts);},options.speed);
function esad(ms){
var va=$(ms.find("a:visible"));
var ha;
if(va.next().is("a")){
ha=va.next();
}else{
ha=$(ms.children().get(0));
}
va.hide();
ha.animate({opacity: 'toggle'}, "slow");
}
}
,

ejqtab:function (options){
//currentClassName:选中tab样式
//useCookie:使用cookie保存回显状态
var main=$(this);
if(options.useCookie)
var ckv=getCookie("ejqtab"+main.attr("name")+main.attr("class")+main.attr("id"));
if(ckv&&ckv!=null&&ckv!=''){
showtab(ckv)
}
else{
$(main.children("div").get(0)).css("display","block");
}
$(main.children("ul").get(0)).children("li").each(function(i,n){
    $(n).click(function(){
    
    if(options.useCookie)
    setCookie("ejqtab"+main.attr("name")+main.attr("class")+main.attr("id"),i)
    showtab(i);
             
     });
});



function showtab(i){
        main.children("div").each(function(j,m){
            if(j==i){
                $(m).css("display","block");
            }else{
                $(m).css("display","none");
            }
         });
                 $(main.children("ul").get(0)).children("li").each(function(j,m){
            if(j==i){
                $(m).attr("className",options.currentClassName);
            }else{
                $(m).attr("className","");
            }
         });
}
}
,


ejqzoom:function (options){
var top=options.top!=null?options.top:0;
var left=options.left!=null?options.left:300;
var main=$(this);
var zoomdiv=main.find("div");
var zoomimg=zoomdiv.find("img");

zoomimg.css("position","relative");
zoomimg.css("top","0px");
zoomimg.css("left","0px");
main.css("position","relative");
main.find("img").css("margin","0px");
main.find("img").css("padding","0px");
zoomdiv.css("position","absolute");
zoomdiv.css("top",top);
zoomdiv.css("left",left);
zoomdiv.css("display","none");
zoomdiv.css("overflow","hidden");


var mdivw=getNum(zoomdiv.css("width"));
var mdivh=getNum(zoomdiv.css("height"));
var zhongxinL=mdivw/2;
var zhongxinT=mdivh/2;

main.children("img").hover(
function(){
$(document.body).mousemove(function(e){
zoomdiv.animate({opacity: 'show'}, "slow");
var proportion=(zoomimg.get(0).width)/(main.children("img").get(0).width);
var mouse=new MouseEvent(e);
var mouseL=mouse.x-(main.get(0).offsetLeft)-(main.children("img").get(0).offsetLeft);
var mouseT=mouse.y-(main.get(0).offsetTop)-(main.children("img").get(0).offsetTop);
var mouseLB=mouseL*proportion;
var mouseTB=mouseT*proportion;
zoomimg.get(0).width;
zoomimg.get(0).height;
var weiyil=(mouseLB-zhongxinL);
var weiyit=(mouseTB-zhongxinT);

if(mouseLB<=zhongxinL)mouseLB=zhongxinL;
if(mouseTB<=zhongxinT)mouseTB=zhongxinT;
if(((mdivw+weiyil)>=zoomimg.get(0).width))
mouseLB=zoomimg.get(0).width-mdivw+zhongxinL;
if(((mdivh+weiyit)>=zoomimg.get(0).height))
mouseTB=zoomimg.get(0).height-mdivh+zhongxinT;

zoomimg.css("left",-mouseLB+zhongxinL+"px");
zoomimg.css("top",-mouseTB+zhongxinT+"px");

});


},
function(){
zoomdiv.animate({opacity: 'hide'}, "fast");
$(document.body).unbind("mousemove");
}
);

}
,
ejqmenu1:function (options){
var main=$(this);
$(this).children("dl").each(function(i,n){
$(n).hover(function(){
$(this).children("dd").show();
},function(){
$(this).children("dd").hide();
});
});
}

//end
});


function MouseEvent(e) {
this.x = e.pageX
this.y = e.pageY
}

function getNum(n){
return Number(n.substring(0,n.length-2));
}




function getCookieVal(offset)
{
var iEndStr=document.cookie.indexOf(";",offset);
if(iEndStr==-1)
iEndStr=document.cookie.length;
return unescape(document.cookie.substring(offset,iEndStr));
}

function getCookie(name)
{
var strArg=name+"=";
var iArgLength=strArg.length;
var iCookieLength=document.cookie.length;
var iIndex=0;
while(iIndex<iCookieLength)
{
var kIndex=iIndex+iArgLength;
if(document.cookie.substring(iIndex,kIndex)==strArg)
return getCookieVal(kIndex);
iIndex=document.cookie.indexOf(" ",iIndex)+1;
if(iIndex==0)
break;
}
return null;
}

function setCookie(name,value)
{
var strArgValue=setCookie.arguments;
var iArgLength=setCookie.arguments.length;
var expires=(2<iArgLength)?strArgValue[2]:null;
//var path=(3<iArgLength)?strArgValue[3]:null;
var path="/";
var domain=(4<iArgLength)?strArgValue[4]:null;
var secure=(5<iArgLength)?strArgValue[5]:false;
document.cookie=name+"="+escape(value)+((expires==null)?"":(";expires="+expires.toGMTString()))+
((path==null)?"":(";path="+path))+((domain==null)?"":(";domain="+domain))+
((secure==true)?";secure":"");
}
		