// julib v3.0 我爱七月 174581928 JULCMS.COM 2010-7-30
(function(window,undefined){
  var JUL = window.JUL = {
    find : function(par1,par2){
      /*语法：
       *基本：#id | .class | * | tag | *tag
       *属性：基本[attr] | 基本[attr=val] | 基本[attr!=val]
       *层级：基本(属性)>基本(属性)
       *例：JUL.find(#id>.class>tag[attr=val]);
       *par1,par2：(obj) (string) ([],string)
       **/
      var regId = /#((?:[\w\u00c0-\uFFFF\-]|\\.)+)/;
      var regCss = /\.((?:[\w\u00c0-\uFFFF\-]|\\.)+)/;
      var regTag = /\*((?:[\w\u00c0-\uFFFF\-]|\\.)+)/;
      var regTAG = /((?:[\w\u00c0-\uFFFF\-]|\\.)+)/;
      var regAttr = /\[\s*((?:[\w\u00c0-\uFFFF\-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/;
      if(typeof(par1) === "string"){
        return JUL.find([document], par1);
      }else if(par1.nodeType === 1){
        return [par1];
      }else if(JUL.isArray(par1)){
        var items,selector=par1,contexts=[],doms,regs;
        var i,j,k;
        for(i in items = par2.split(">")){
          switch(items[i].charAt(0)){
            case "#":
              contexts.push(document.getElementById(regId.exec(items[i])[1]));
              break;
            case ".":
              for(j in selector){
                for(k in doms = selector[j].getElementsByTagName("*")){
                  if(doms[k].className && doms[k].nodeType === 1){
                    if(doms[k].className.match(regCss.exec(items[i])[1])){
                      regs = regAttr.exec(items[i]);
                      if(regs){
                        switch(regs[2]){
                          case "=":
                            if(doms[k].getAttribute(regs[1]).match(regs[4])) contexts.push(doms[k]);
                            break;
                          case "!=":
                            if(!doms[k].getAttribute(regs[1]).match(regs[4])) contexts.push(doms[k]);
                            break;
                          default:
                            if(doms[k].getAttribute(regs[1])) contexts.push(doms[k]);
                        }
                      }else{
                        contexts.push(doms[k]);
                      }
                    }
                  }
                }
              }
              break;
            case "*":
              regs = regTag.exec(items[i]);
              if(regs){
                doms = document.createElement(regs[1]);
                if(selector[0].nodeType !== 1){
                  document.body.appendChild(doms);
                }else{
                  selector[0].appendChild(doms);
                }
                contexts.push(doms);
              }else{
                for(j in selector){
                  for(k in doms = selector[j].getElementsByTagName("*")){
                    if(doms[k].nodeType === 1){
                      regs  = regAttr.exec(items[i]);
                      if(regs){
                        switch(regs[2]){
                          case "=":
                            if(!doms[k].getAttribute(regs[1])) break;
                            if(doms[k].getAttribute(regs[1]).match(regs[4])) contexts.push(doms[k]);
                            break;
                          case "!=":
                            if(!doms[k].getAttribute(regs[1])) break;
                            if(!doms[k].getAttribute(regs[1]).match(regs[4])) contexts.push(doms[k]);
                            break;
                          default:
                            if(doms[k].getAttribute(regs[1])) contexts.push(doms[k]);
                        }
                      }else{
                        contexts.push(doms[k]);
                      }
                    }
                  }
                }
              }
              break;
            default :
              for(j in selector){
                for(k in doms = selector[j].getElementsByTagName(regTAG.exec(items[i])[0])){
                  if(doms[k].nodeType === 1){
                    regs  = regAttr.exec(items[i]);
                    if(regs){
                      switch(regs[2]){
                        case "=":
                          if(!doms[k].getAttribute(regs[1])) break;
                          if(doms[k].getAttribute(regs[1]).match(regs[4])) contexts.push(doms[k]);
                          break;
                        case "!=":
                          if(!doms[k].getAttribute(regs[1])) break;
                          if(!doms[k].getAttribute(regs[1]).match(regs[4])) contexts.push(doms[k]);
                          break;
                        default:
                          if(doms[k].getAttribute(regs[1])) contexts.push(doms[k]);
                      }
                    }else{
                      contexts.push(doms[k]);
                    }
                  }
                }
              }
          }
          selector = contexts;
          contexts = [];
        }
        return selector;
      }else{
        return [];
      }
    },
    isArray : function(obj){return Object.prototype.toString.call(obj) === "[object Array]";},
    isCss : function(el,classname){return el.className.match(new RegExp("(^|\\s)"+classname+"(\\s|$)","g")) == null ? false : true;},
    ajax : function(options){
      var that = this;
      that.options = options || {};
      that.type      = that.options.type      || "post";
      that.url       = that.options.url       || false;
      that.async     = that.options.async     || true;
      that.sendData  = that.options.sendData  || "";
      that.dataType  = that.options.dataType  || "txt"; //txt | xml | js
      that.onWait    = that.options.onWait    || function(){};
      that.onSuccess = that.options.onSuccess || function(){};
      that.onError   = that.options.onError   || function(){};
      that.xmlhttp = null;
      that.createXmlhttp = function(){
        if(window.XMLHttpRequest){
          return new XMLHttpRequest();
        }else if(window.ActiveXObject){
          return new ActiveXObject("Microsoft.XMLHTTP");
        }else{
          return new Msxml2.XMLHTTP();
        }
      };
      that.stateChange = function(){
        that.xmlhttp.onreadystatechange = function(){
          if(that.xmlhttp.readyState == 4){
            if(that.xmlhttp.status == 200){
              if(that.dataType == "txt"){
                that.onSuccess(that.xmlhttp.responseText);
              }else if(that.dataType == "xml"){
                that.onSuccess(that.xmlhttp.responseXML);
              }else if(that.dataType == "js"){
                eval.call(window,that.xmlhttp.responseText);
              }else{
                that.onError();
              }
            }else{
              that.onWait();
            }
          }else{
            that.onWait();
          }
        }
      };
      that.send = function(){
        if(!(that.xmlhttp = that.createXmlhttp())) return;
        that.stateChange();
        that.xmlhttp.open(that.type,that.url,that.async);
        that.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        that.xmlhttp.setRequestHeader('If-Modified-Since','0');
        that.xmlhttp.send(that.sendData);
      };
      if(options) that.send();
    },
    bgPng : function(el,path){
      if(/msie 6.0/.test(navigator.userAgent.toLowerCase())){
        el.style.filter ='progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src="'+path+'")';
      }else{
        el.style.background = "url("+path+")";
      }
    },
    each : function(Arrs,callback){
      for(var i=0;i<Arrs.length;i++){callback.call(this,Arrs[i]);}
    },
    ROOT : function(){
      var elements = JUL.find("script");
      for(var i = 0,len = elements.length; i < len; i++){
        if (elements[i].src && elements[i].src.match(/julib[\w\-\.]*\.js/)){
          return elements[i].src.substring(0, elements[i].src.lastIndexOf('/') + 1);
        }
      }
      return "/";
    }
  };
  JUL.dom = {
    Attr : function(el,name,val){
      if(el == undefined || el.nodeType != 1 || name == undefined || val == undefined) return;
      if(document.all && name == "class") name ="className";
      el.setAttribute(name,val);
    },
    Attrs : function(el,pros){
      for(var i in pros){el.setAttribute(i,pros[i]);}
    },
    removeAttr : function(el,name){
      if(typeof(name) == "string"){
        if(document.all && name == "class") name = "className";
        el.removeAttribute(name);
      }else if(JUL.isArray(name)){
        for(var i=0; i<name.length; i++){
          if(document.all && name[i] == "class") name[i] = "className";
          el.removeAttribute(name[i]);
        }
      }
    },
    addCss : function(el,classname){el.className += el.className.length > 0 ? " " + classname : classname;},
    delCss : function(el,classname){el.className = el.className.replace(new RegExp("( ?|^)"+classname+"\\b","g"),"");},
    Style : function(el,pro,val){
      if(pro == "Float"){
        if(document.all){
          el.style.styleFloat = val;
        }else{
          el.style.cssFloat = val;
        }
      }else if(pro == "opacity"){
        if(el.filters){
          el.style.filter = "alpha(opacity=" + val + ")";
          el.style.zoom = 1;
        }else{
          el.style.opacity = val/100;
        }
      }else{
        el.style[pro] = val;
      }
    },
    Styles : function(el,pros){
      for(var i in pros){
        JUL.dom.Style(el,i,pros[i]);
      }
    },
    getAttr : function(el,attr){
      if(el == undefined || el.nodeType != 1 || attr == undefined){
        return false;
      }else{
        return el.getAttribute(attr);
      }
    },
    getCss: function(el,pro){
      if(el.style[pro]){
        return el.style[pro];
      }else if(el.currentStyle){
        return el.currentStyle[pro];
      }else if(document.defaultView && document.defaultView.getComputedStyle){
        pro = pro.replace(/([A-Z])/g,'-$1').toLowerCase();
        var s = document.defaultView.getComputedStyle(el,'');
        return s.getPropertyValue(pro);
      }else{
        return null;
      }
    },
    getLeft : function(el){
      if(el.offsetParent){
        return el.offsetLeft + JUL.dom.getLeft(el.offsetParent);
      }else{
        return el.offsetLeft;
      }
    },
    getTop : function(el){
      if(el.offsetParent){
        return el.offsetTop + JUL.dom.getTop(el.offsetParent);
      }else{
        return el.offsetTop;
      }
    },
    getWidth : function(el){
      if(JUL.dom.getCss(el,"display") == "none"){
        JUL.dom.Style(el,"display","block");
        var w = el.clientWidth || parseInt(JUL.dom.getCss(el,"width"));
        JUL.dom.Style(el,"display","none");
        return w;
      }else{
        return el.offsetWidth || parseInt(JUL.dom.getCss(el,"width"));
      }
    },
    getHeight: function(el){
      if(JUL.dom.getCss(el,"display") == "none"){
        JUL.dom.Style(el,"display","block");
        var h = el.clientHeight || parseInt(JUL.dom.getCss(el,"height"));
        JUL.dom.Style(el,"display","none");
        return h;
      }else{
        return el.offsetHeight || parseInt(JUL.dom.getCss(el,"height"));
      }
    },
    Parent: function(el,index){
      index = index || 1;
      var elements = el;
      for(var i=0; i<index; i++){
        if(elements!=null){
          elements = elements.parentNode;
        }
      }
      return elements;
    },
    Prev: function(el,index){
      index = index || 1;
      var elements = el;
      var tempelem = elements;
      for(var i=0; i<index; i++){
        if(tempelem == null) return elements;
        tempelem = tempelem.previousSibling;
        while(tempelem && tempelem.nodeType != 1){tempelem = tempelem.previousSibling;}
        if(tempelem && tempelem.nodeType == 1) elements = tempelem;
      }
      return elements;
    },
    Next: function(el,index){
      index = index || 1;
      var elements = el;
      var tempelem = elements;
      for(var i=0; i<index; i++){
        if(tempelem == null) return elements;
        tempelem = tempelem.nextSibling;
        while(tempelem && tempelem.nodeType != 1){tempelem = tempelem.nextSibling;}
        if(tempelem && tempelem.nodeType == 1) elements = tempelem;
      }
      return elements;
    },
    Child : function(el,index){
      if(!el) return false;
      var num = index || 1;
      var childs = el.children;
      var elements = el;
      for(var i=0; i<childs.length && num > 0; i++){
        if(childs[i].nodeType == 1){
          elements = childs[i];
          num--;
        }
      }
      return elements;
    },
    FirstChild : function(el){
      var elem = el.firstChild;
      while(elem&&elem.nodeType!=1){elem = elem.nextSibling;}
      return elem;
    },
    LastChild : function(el){
      var elem = el.lastChild;
      while(elem&&elem.nodeType!=1){elem = elem.previousSibling;}
      return elem;
    },
    remove : function(el){
      el.parentNode.removeChild(el);
    },
    empty : function(el){
      el.innerHTML = "";
    },
    clone : function(el){
      return el.cloneNode(true);
    },
    append : function(el,val){
      if(typeof(val) == "string"){
        el.innerHTML = val;
      }else if(val.nodeType == 1){
        el.appendChild(val.cloneNode(true));
      }
    },
    appendTo: function(el,val){
      if(typeof(val) == "object" && val.nodeType == 1){
        val.appendChild(el);
      }
    },
    slide : function(el,mode,speed,callback){
      if(el == undefined || el.nodeType != 1) return;
      mode = mode || "down";
      callback = callback || function(){};
      speed = speed || 3;
      var width = JUL.dom.getWidth(el);
      var height = JUL.dom.getHeight(el);
      var nowW = 0;
      var nowH = 0;
      var speeds = 0;
      var timer;
      var fns;
      JUL.dom.Styles(el,{"overflow":"hidden","width":"0px","height":"0px","display":"block"});
      if(mode == "down" || mode == "up"){
        speeds = height*speed/100;
        JUL.dom.Style(el,"width",width+"px");
        if(mode == "down"){nowH = 0;}else{nowH = height;}
        fns = function(){
          if(mode == "down"){nowH += speeds;}else{nowH -= speeds;}
          if(nowH <=0 ) nowH=0;
          JUL.dom.Style(el,"height",nowH+"px");
          if(nowH >= height || nowH <= 0){
            if(mode == "down"){JUL.dom.Style(el,"height",height+"px");}else{JUL.dom.Style(el,"height","0px");}
            clearInterval(timer);
            callback();
          }
        };
      }else{
        speeds = width*speed/100;
        JUL.dom.Style(el,"height",height+"px");
        if(mode == "right"){nowW = 0;}else{nowW = width;}
        fns = function(){
          if(mode == "right"){nowW += speeds;}else{nowW -= speeds;}
          if(nowW <=0 ) nowW=0;
          JUL.dom.Style(el,"width",nowW+"px");
          if(nowW >= width || nowW <= 0){
            if(mode == "right"){JUL.dom.Style(el,"width",width+"px");}else{JUL.dom.Style(el,"width","0px");}
            clearInterval(timer);
            callback();
          }
        };
      }
      timer = setInterval(fns,10);
    },
    fade : function(el,speed,callback){
      if(el == undefined || el.nodeType != 1) return;
      callback = callback || function(){};
      speed = speed || 3;
      var d = JUL.dom.getCss(el,"display");
      var v = JUL.dom.getCss(el,"visibility");
      var o = (d == "none" || v == "hidden") ? 0 : 100;
      JUL.dom.Style(el,"opacity",o);
      JUL.dom.Styles(el,{"display":"block","visibility":"visible"});
      var timer = setInterval(function(){
        if(d == "none" || v == "hidden"){o += speed;}else{o -= speed;}
        JUL.dom.Style(el,"opacity",o);
        if(o >= 100 || o <= 0){
          JUL.dom.Style(el,"opacity",((d == "none" || v == "hidden") ? 100 : 0));
          clearInterval(timer);
          callback();
        }
      },10);
    }
  };
  JUL.event = {
    add : function(el,event,listener){
      if(el.addEventListener){
        el.addEventListener(event,listener,false);
      }else if(el.attachEvent){
        el.attachEvent("on"+event,listener);
      }
    },
    remove : function(el,event,listener){
      if (el.removeEventListener){
        el.removeEventListener(event,listener,false);
      }else if(el.detachEvent){
        el.detachEvent("on"+event,listener);
      }
    },
    stop : function(e) {
      if (e.preventDefault) e.preventDefault();
      if (e.stopPropagation) e.stopPropagation();
      if (e.cancelBubble !== undefined) e.cancelBubble = true;
      if (e.returnValue !== undefined) e.returnValue = false;
    },
    bind : function(el, event, listener,that) {
      this.add(el, event, function(e) {
        listener(el,that);
        JUL.event.stop(e);
        return false;
      });
    },
    getKey : function(e){
      e = e || window.event;
      return e.keyCode||e.which;
    }
  };
  JUL.mouse = {
    getLeft : function(e){
      e = e || window.event;
      return e.pageX?e.pageX:(e.clientX + document.documentElement.scrollLeft);
    },
    getTop : function(e){
      e = e || window.event;
      return e.pageY?e.pageY:(e.clientY + document.documentElement.scrollTop);
    },
    eLeft : function(e){
      e = e||window.event;
      return JUL.mouse.getLeft(e) - JUL.dom.getLeft(JUL.mouse.target(e));
    },
    eTop : function(e){
      e = e||window.event;
      return JUL.mouse.getTop(e) - JUL.dom.getTop(JUL.mouse.target(e));
    },
    target : function(e){
      e = e || window.event;
      return e.target||e.srcElement;
    }
  };
  JUL.page = {
    getWidth : function(){return document.body.offsetWidth;},
    getHeight : function(){return document.body.offsetHeight;},
    scrollLeft : function(){return self.pageXOffset || document.documentElement.scrollLeft/*ie,ff,opera*/||document.body.scrollLeft;/*chrome,safari*/},
    scrollTop : function(){return self.pageYOffset || document.documentElement.scrollTop/*ie,ff,opera*/||document.body.scrollTop;/*chrome,safari*/},
    workWidth : function(){return document.documentElement.clientWidth;},
    workHeight : function(){return document.documentElement.clientHeight;}
  };
  JUL.files = {
    loadJs : function(url,onsuccess,waiting,onfailure){
      onsuccess = onsuccess || function(){};
      waiting   = waiting || function(){};
      onfailure = onfailure || function(){};
      var js = JUL.find("script");
      var i = 0;
      for(i=0; i<js.length; i++){
        if(js[i].src && js[i].src.indexOf(url) != -1){
          onsuccess();
          return;
        }
      }
      var head = JUL.find("HEAD")[0];
      js = JUL.find("head>*script")[0];
      JUL.dom.Attrs(js, {"type":"text/javascript","language":"javascript","src":url});
      js.onload = js.onreadystatechange = function(){
        if(this.readyState && this.readyState=="loading") waiting();
        onsuccess();
      };
      js.onerror = function(){
        head.removeChild(js);
        onfailure();
      };
    },
    loadCss : function(url){
      JUL.dom.Attrs(JUL.find("head>*link")[0],{"type":"text/css","rel":"stylesheet","href":url});
    },
    removeCss : function(url){
      var Css = JUL.find("link");
      for(var i=0; i<Css.length; i++){
        if(window.location.href.substring(0,window.location.href.lastIndexOf("/")+1)+url==linkCss.getDom(i).href||linkCss.getDom(i).href==url) jul(Css[i]).remove();
      }
    }
  };
  var jul = window.jul =function(par1,par2){return new jul.fn.init(par1,par2);};
  jul.fn = jul.prototype = {
    elements : [],
    init : function(par1,par2){
      /*par1 : [] | element | string
       *par2 : element | string
       **/
      if(typeof(par1) === "string"){
        this.elements = JUL.find(par1);
      }else{
        var selector = [];
        if(par1.nodeType === 1){
          selector.push(par1);
        }else if(JUL.isArray(par1)){
          selector = par1;
        }else{
          selector = [document]
        }
        if(par2 == undefined){
          this.elements = selector;
        }else if(typeof(par2) === "string"){
          this.elements = JUL.find(selector,par2);
        }else if(par2.nodeType === 1){
          par1.appendChild(par2);
          this.elements.push(par2);
        }else{
          this.elements = selector;
        }
      }
    },
    each : function(callback){
      var arr = new Array(),i;
      for(i=0;i<this.elements.length;i++){arr.push(this.elements[i]);}
      for(i=0;i<arr.length;i++){callback.call(this,arr[i],i)}
      return this;
    },
    get : function(index){return jul(this.elements[index])},
    getDom: function(index){
      if(index == undefined){
        return this.elements;
      }else{
        return this.elements[index];
      }
    },
    size: function(){return this.elements.length;},
    html: function(val){
      if(!val) return this.elements[0].nodeName === "INPUT" || this.elements[0].nodeName === "TEXT" ? this.elements[0].value : this.elements[0].innerHTML;
      return this.each(function(el){if(el.nodeName === "INPUT" || el.nodeName === "TEXT"){el.value = val;}else{el.innerHTML = val;}});
    },
    Attr : function(name,val){
      return this.each(function(el){JUL.dom.Attr(el,name,val);});
    },
    Attrs : function(pros){
      return this.each(function(el){JUL.dom.Attrs(el,pros);});
    },
    removeAttr : function(name){
      return this.each(function(el){JUL.dom.removeAttr(el,name);});
    },
    addCss : function(classname){
      return this.each(function(el){JUL.dom.addCss(el,classname);});
    },
    delCss : function(classname){
      return this.each(function(el){JUL.dom.delCss(el,classname);});
    },
    Style : function(pro,val){
      return this.each(function(el){JUL.dom.Style(el,pro,val);});
    },
    Styles : function(pros){
      return this.each(function(el){JUL.dom.Styles(el,pros);});
    },
    getAttr : function(attr){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.getAttr(this.elements[0],attr);
      }
    },
    getCss: function(pro){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.getCss(this.elements[0],pro);
      }
    },
    getLeft : function(){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.getLeft(this.elements[0]);
      }
    },
    getTop : function(){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.getTop(this.elements[0]);
      }
    },
    getWidth : function(){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.getWidth(this.elements[0]);
      }
    },
    getHeight: function(){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.getHeight(this.elements[0]);
      }
    },
    Parent: function(index){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.Parent(this.elements[0],index);
      }
    },
    Prev: function(index){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.Prev(this.elements[0],index);
      }
    },
    Next: function(index){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.Next(this.elements[0],index);
      }
    },
    Child : function(index){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.Child(this.elements[0],index);
      }
    },
    FirstChild : function(){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.FirstChild(this.elements[0]);
      }
    },
    LastChild : function(){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.LastChild(this.elements[0]);
      }
    },
    remove : function(){
      return this.each(function(el){JUL.dom.remove(el);});
    },
    empty : function(){
      this.html(" ");
    },
    clone : function(){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return null;
      }else{
        return JUL.dom.clone(this.elements[0]);
      }
    },
    append : function(val){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return this;
      }else{
        JUL.dom.append(this.elements[0],val);
        return this;
      }
    },
    appendTo: function(val){
      if(!this.elements[0] || this.elements[0].nodeType != 1){
        return this;
      }else{
        JUL.dom.appendTo(this.elements[0],val);
        return this;
      }
    },
    slide : function(mode,speed,callback){
      return this.each(function(el){JUL.dom.slide(el,mode,speed,callback);});
    },
    fade : function(speed,callback){
      return this.each(function(el){JUL.dom.fade(el,speed,callback);});
    }
  };
  jul.fn.init.prototype = jul.fn;
  JUL.each(("click,blur,focus,load,resize,scroll,unload,dblclick,mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave,change,select,submit,keydown,keypress,keyup,error").split(","),function(item){jul.fn[item] = function(fn){var that = this;return this.each(function(el){JUL.event.bind(el,item,fn,that);});};});
})(window);
