var paintedEggshell = {
	requestlog : function(logmsg) {
		var nocache = Math.random();
    var sender = new Image();
    sender.src = "/requestlog?type=paintedeggshell&" + logmsg + "&_=" + nocache;
	},
	getCurrentStyle : function(obj) {
    if(obj.currentStyle) {
      return obj.currentStyle;
    }
    return document.defaultView.getComputedStyle(obj, null);
  },
  leftParser : /(-?\d*)px/,
  adjustLeft : function(element) {
    var mid = document.body.offsetWidth / 2;
    if(this.leftParser.test(element.oripos)) {
      var left = parseInt(RegExp.$1) + mid;
      element.style.left = left + "px";
    } else {
      element.style.left = mid + "px";
    }
  },
  adjustHoverDiv : function(hoverDiv, css, tip) {
    hoverDiv.title = tip;
    hoverDiv.style.cssText = css;
    hoverDiv.repeat = "true";
    hoverDiv.src = "http://shared.youdao.com/images/alphapic.gif"
    hoverDiv.style.position = "absolute";
    hoverDiv.style.display = "block";
    hoverDiv.oripos = hoverDiv.style.left;
    hoverDiv.oriborder = hoverDiv.style.border;
    hoverDiv.style.border = "none";
    this.adjustLeft(hoverDiv);
  },
  addEvent : function(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent) {
      var r = elm.attachEvent('on' + evType, fn);
      return r;
    } else {
      elm['on' + evType] = fn;
    }
  },
  getMousePos : function(e) {
    var pos = new Object();
    if(document.all) {
      pos.x = event.clientX + document.body.scrollLeft;
      pos.y = event.clientY + document.body.scrollTop;
    } else {
      pos.x = e.pageX;
      pos.y = e.pageY;
    }
    return pos;
  },
  fadeIn : {
    timeDelay : 10,
    start : function(element, step) {
      this.el = element;
      this.el.style.display = "block";
      if(step >= 100) {
        return;
      }
      if(step > 0) {
        this.alphaStep = document.all ? step : step / 100;
      } else {
        this.alphaStep = document.all ? 5 : 0.05;
      }
      if(document.all) {
        this.el.style.filter="alpha(opacity='0')";
        this.el.filters.alpha.opacity = 0;
      } else {
        this.el.style.opacity = 0;
      }
      this.doFadeIn();
    },
    doFadeIn : function() {
      if(document.all) {
        this.el.filters.alpha.opacity += this.alphaStep;
        if(this.el.filters.alpha.opacity < 100) {
          setTimeout("paintedEggshell.fadeIn.doFadeIn()", this.timeDelay);
        }
      } else {
        var opacity = parseFloat(this.el.style.opacity) + this.alphaStep;
        this.el.style.opacity = opacity;
        if(opacity < 1) {
          setTimeout("paintedEggshell.fadeIn.doFadeIn()", this.timeDelay);
        }
      }
    }
  }, 
  createPaintedEggshell : function(logmsg, id, clickDivCss, showDivCss, step) {
    var clickDiv = document.createElement("div");
    clickDiv.name = 'paintedeggshelltip';
    clickDiv.style.cssText = clickDivCss;
    clickDiv.oripos = clickDiv.style.left;
    this.adjustLeft(clickDiv);
    clickDiv.style.position = "absolute";
    clickDiv.style.display = "block";
    
    var showDiv = document.createElement("div");
    showDiv.name = 'paintedeggshelltip';
    showDiv.id = id;
    showDiv.style.cssText = showDivCss;
    showDiv.oripos = showDiv.style.left;
    this.adjustLeft(showDiv);
    showDiv.style.position = "absolute";
    showDiv.style.display = "none";
    
    document.body.appendChild(clickDiv);
    document.body.appendChild(showDiv);
    this.addEvent(document, "click", function(e){
      showDiv = document.getElementById(id);
      var mousePos = paintedEggshell.getMousePos(e);
      if(mousePos.x >= clickDiv.offsetLeft && mousePos.x <= clickDiv.offsetLeft + clickDiv.offsetWidth
        && mousePos.y >= clickDiv.offsetTop && mousePos.y <= clickDiv.offsetTop + clickDiv.offsetHeight) {
      	if(logmsg) paintedEggshell.requestlog('click=' + encodeURIComponent(logmsg));
        if(showDiv.style.display == "none") {
          paintedEggshell.fadeIn.start(showDiv, step);
        }
      } else {
        showDiv.style.display = "none";
      }
    });
    this.addEvent(window, "resize", function() {
      showDiv = document.getElementById(id);
      paintedEggshell.adjustLeft(clickDiv);
      paintedEggshell.adjustLeft(showDiv)
    });
  },
  createBigTip : function(logmsg, hoverDivCss, href, tip) {
    var hoverDiv = document.createElement("img");
    this.adjustHoverDiv(hoverDiv, hoverDivCss, tip);
    
    var a = document.createElement("a");
    a.name = 'paintedeggshelltip';
    a.href = href;
    a.target = "_blank";
    a.appendChild(hoverDiv);
    document.body.appendChild(a);
    
    this.addEvent(hoverDiv, "mouseover", function() {
      hoverDiv.style.border = hoverDiv.oriborder;
    });
    this.addEvent(hoverDiv, "mouseout", function() {
      hoverDiv.style.border = "none";
    });
    this.addEvent(a, "click", function() {
      if(logmsg) paintedEggshell.requestlog('click=' + encodeURIComponent(logmsg));
    });
    
    this.addEvent(window, "resize", function() {
      paintedEggshell.adjustLeft(hoverDiv);
    });
  },
  changeSkin: function(hoverDiv, switcher) {
    hoverDiv.switcher = switcher;
    var bgContainer = document.getElementById("nhdrwrap");
    var skinContainer = document.getElementById("nhdrwrapinner");
    var logoContainer = document.getElementById("skin_logo");
    bgContainer.style.backgroundImage = switcher == 0 ? hoverDiv.bg0 : hoverDiv.bg1;
    skinContainer.style.backgroundImage = switcher == 0 ? hoverDiv.skin0 : hoverDiv.skin1;
    logoContainer.style.cssText = switcher == 0 ? hoverDiv.logocss0 : hoverDiv.logocss1;
    
    if(hoverDiv.gadget0) {
      var gadget = switcher == 0 ? hoverDiv.gadget0 : hoverDiv.gadget1;
      jQuery("#skin .modbar td").css("background-image", gadget);
      jQuery("#skin .modbar .leftpic").css("background-image", gadget);
      jQuery("#skin .modbar .rightpic").css("background-image", gadget);
      jQuery("#skin .modbar .arrowdown").css("background-image", gadget);
    }
    if(hoverDiv.maincolor0) {
      var maincolor = switcher == 0 ? hoverDiv.maincolor0 : hoverDiv.maincolor1;
      jQuery(".maincolor").css("background-color", "#" + maincolor);
    }
    if(hoverDiv.boxbordercolor0) {
      var boxbordercolor = switcher == 0 ? hoverDiv.boxbordercolor0 : hoverDiv.boxbordercolor1;
      jQuery("#skin .box").css("border-right", "1px solid #" + boxbordercolor);
      jQuery("#skin .box").css("border-left", "1px solid #" + boxbordercolor);
      jQuery("#skin .box").css("border-bottom", "1px solid #" + boxbordercolor);
    }
    if(hoverDiv.textcolor0) {
      var textcolor = switcher == 0 ? hoverDiv.textcolor0 : hoverDiv.textcolor1;
      jQuery("#skin .text16").css("color","#"+textcolor);
    }
    if(hoverDiv.linkcolor0) {
      var linkcolor = switcher == 0 ? hoverDiv.linkcolor0 : hoverDiv.linkcolor1;
      jQuery(".linkColor").css("color", "#" + linkcolor);
    }
    if(hoverDiv.bigtextcolor0) {
      var bigtextcolor = switcher == 0 ? hoverDiv.bigtextcolor0 : hoverDiv.bigtextcolor1;
      jQuery(".bigtext").css("color", "#" + bigtextcolor);   
    }
    
    var hoverDivCss = switcher == 0 ? hoverDiv.css0 : hoverDiv.css1;
    var tip = switcher == 0 ? hoverDiv.tip0 : hoverDiv.tip1;
    this.adjustHoverDiv(hoverDiv, hoverDivCss, tip);
  },
  createChangeSkinTip: function(logmsg, hoverDivCss0, hoverDivCss1, tip0, tip1, bgpic0, bgpic1, skinpic0, skinpic1, logo0, logo1, gadgetpic0, gadgetpic1, maincolor0, maincolor1, 
    boxbordercolor0, boxbordercolor1, linkcolor0, linkcolor1, bigtextcolor0, bigtextcolor1, textcolor0, textcolor1) {
    var hoverDiv = document.createElement("img");
    hoverDiv.name = 'paintedeggshelltip';
    this.adjustHoverDiv(hoverDiv, hoverDivCss0, tip0);
    document.body.appendChild(hoverDiv);
    
    hoverDiv.tip0 = tip0;
    hoverDiv.tip1 = tip1;
    hoverDiv.css0 = hoverDivCss0;
    if(!hoverDivCss1) 
      hoverDiv.css1 = hoverDivCss0;
    else
      hoverDiv.css1 = hoverDivCss1;
    
    var bg0 = this.getCurrentStyle(document.getElementById("nhdrwrap")).backgroundImage;
    var skin0 = this.getCurrentStyle(document.getElementById("nhdrwrapinner")).backgroundImage;

    hoverDiv.switcher = 0;
    hoverDiv.bg0 = bg0;
    hoverDiv.skin0 = skin0;
    
    var logocontainer = document.getElementById("skin_logo");
    var widthheightcss = "width:" + logocontainer.offsetWidth + ";height:" + logocontainer.offsetHeight;
    hoverDiv.logocss0 = widthheightcss + ";background-image:url("+logo0+"); background-repeat:no-repeat; background-position:left;_background:none;_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+logo0+"', sizingMethod='scale')";
    hoverDiv.logocss1 = widthheightcss + ";background-image:url("+logo1+"); background-repeat:no-repeat; background-position:left;_background:none;_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+logo1+"', sizingMethod='scale')";
    
    // 替换的图片使用文件名作为参数
    hoverDiv.bg1 = bg0.replace(bgpic0, bgpic1);
    hoverDiv.skin1 = skin0.replace(skinpic0, skinpic1);
      
    // 替换个性首页的gadget图片
    if(gadgetpic0) {
      hoverDiv.gadget0 = bg0.replace(bgpic0, gadgetpic0);
      hoverDiv.gadget1 = bg0.replace(bgpic0, gadgetpic1);
    }
    if(maincolor0) {
      hoverDiv.maincolor0 = maincolor0;
      hoverDiv.maincolor1 = maincolor1;
    }
    if(boxbordercolor0) {
      hoverDiv.boxbordercolor0 = boxbordercolor0;
      hoverDiv.boxbordercolor1 = boxbordercolor1;
    }
    if(textcolor0) {
      hoverDiv.textcolor0 = textcolor0;
      hoverDiv.textcolor1 = textcolor1;
    }
    if(linkcolor0) {
      hoverDiv.linkcolor0 = linkcolor0;
      hoverDiv.linkcolor1 = linkcolor1;
    }
    if(bigtextcolor0) {
      hoverDiv.bigtextcolor0 = bigtextcolor0;
      hoverDiv.bigtextcolor1 = bigtextcolor1;
    }
        
    this.addEvent(hoverDiv, "mouseover", function() {
      hoverDiv.style.border = hoverDiv.oriborder;
    });
    this.addEvent(hoverDiv, "mouseout", function() {
      hoverDiv.style.border = "none";
    });
    this.addEvent(hoverDiv, "click", function() {
      if(logmsg) paintedEggshell.requestlog('click=' + encodeURIComponent(logmsg));
      if(hoverDiv.switcher == 0) {
        paintedEggshell.changeSkin(hoverDiv, 1);
      } else {
        paintedEggshell.changeSkin(hoverDiv, 0);
      }
    });
    this.addEvent(window, "resize", function() {
      paintedEggshell.adjustLeft(hoverDiv);
    });
    
    return hoverDiv;
  },
  isInTimeArea : function(beginMonth, beginDate, beginHour, beginMinute, endMonth, endDate, endHour, endMinute) {
    var now = new Date();
    var nowtime = (now.getMonth()+1) * 30 * 24 * 60 + now.getDate() * 24 * 60 + now.getHours() * 60 + now.getMinutes();
    var begintime = beginMonth * 30 * 24 * 60 + beginDate * 24 * 60 + beginHour * 60 + beginMinute;
    var endtime = endMonth * 30 * 24 * 60 + endDate * 24 * 60 + endHour * 60 + endMinute;
    return nowtime >= begintime && nowtime <= endtime;
  }
}