if(typeof $j == "undefined") {
  var $j = jQuery.noConflict();
}

var siteNav = new Object();

siteNav = {
  ajaxUrl : null,
  rootUrl : "http://cunwu.cuncun8.com.cn",
  urlTail : ".cuncun8.com.cn/cunwu/",
  header : {
    objId : "#sites_nav_html",
    doingObjId : "#doing_obj_id"
  },
  village : {
    objId : "#quyudaohang-city ul",
    urHereObj : "#current_site_here"
  },
  
  geoDatas : new Array(),
  lastDomain : "www",

  headerInit : function() {
    if(typeof siteNavArgs != "undefined") {
      this.urlTail+=siteNavArgs.channelUrl;
    }
    
    if($j(this.header.objId+" iframe").html() == null) {
      $j(this.header.objId).append("<iframe scrolling='no' frameborder='0' vspace='0' hspace='0' marginheight='0' marginwidth='0'></iframe>");
    }
    $j(this.header.objId).css({ width:"400px",position:"absolute" });
    $j(this.header.objId +" iframe").css({
      zIndex : 99,position:"absolute", width: $j(this.header.objId+" ul").width()-2+"px",
      height: $j(this.header.objId+" ul").height()-2+"px", top: "0px",left: "0px"
    });
    if($j.browser.msie && $j.browser.version == "6.0") {
      $j(this.header.objId +" iframe").css({ width: $j(this.header.objId+" ul").width(),  height: $j(this.header.objId+" ul").height() });
    }
    $j(this.header.objId +" ul").css({ zIndex : 100,position:"absolute",top: "0px",left: "0px" });
    $j(this.header.objId).hide();

    $j(this.header.doingObjId).mouseover(function() {
      siteNav.showLayer($j(this));
    });
  },

  showLayer : function(obj) {
    var p = obj.position();
    $j(this.header.objId).show();
    $j(this.header.objId).css({ left:p.left+"px", top:p.top+obj.height()+"px" });
    obj.mouseout(function () { 
      $j(siteNav.header.objId).hide();
    });
    $j(this.header.objId).mouseover(function () { 
      $j(this).show();
    });
    $j(this.header.objId).mouseout(function (){
      $j(this).hide();
    });
  },

  headerDo : function(geoCode) {
    this.buildNewList(geoCode, "header");
  },

  villageDo : function(geoCode) {
    this.buildNewList(geoCode, "village");
  },

  buildNewList : function(geoCode, place) {
    var data = this.getDataByGeo(geoCode);

    var parentGeo = data.parent;
    var currentGeo = data.current;
    var childGeo = data.child;
    var parentLevel = parentGeo != null ? parentGeo.level : 0;
    var currentLevel = currentGeo != null ? currentGeo.level : 0;

    var htmlStr = "";
    for(var i = 0; i < childGeo.length; i++) {
      htmlStr+="<li>";
      if(place == "header") {
        if(parentLevel < 2 && childGeo[i].token == 2) {
          htmlStr+= "<a href='Javascript:siteNav.headerDo("+childGeo[i].geo_code+");'>"+childGeo[i].display_name+"</a>";
        } else if (parentLevel < 2 && childGeo[i].token == 3 && childGeo[i].domain != "") {
          htmlStr+= "<a href='http://"+childGeo[i].domain+this.urlTail+"'>"+childGeo[i].display_name+"</a>";
        } else {
          htmlStr+= childGeo[i].display_name;
        }
      } else if(place == "village") {
        if(this.lastDomain == "") this.lastDomain = "www";
        if(currentGeo.domain != null && currentGeo.token == 3) {
          this.lastDomain = currentGeo.domain;
        }
        
        if(childGeo[i].token == 2 || currentLevel == 3) {
          htmlStr+= "<a href='Javascript:siteNav.villageDo("+childGeo[i].geo_code+");'>"+childGeo[i].display_name+"</a>";
        } else if(currentLevel == 4 && childGeo[i].token == 3) {
          if(window.location.href.indexOf(".com.cn") === -1) {
            var cunwuTail = ".com";
          } else {
            var cunwuTail = ".com.cn";
          }
          htmlStr+= "<a href='http://"+this.lastDomain+".cuncun8"+cunwuTail+"/cunwu/villageHomePage.php?geoCode="+childGeo[i].geo_code+"'>"+childGeo[i].display_name+"</a>";;
        } else if(currentLevel < 4 && childGeo[i].token == 3) {
          htmlStr+= "<a href='http://"+childGeo[i].domain+this.urlTail+"'>"+childGeo[i].display_name+"</a>";
        } else {
          htmlStr+= childGeo[i].display_name;
        }

        try { $j(this.village.urHereObj).html(currentGeo.full_name);} catch(e) {}
      } else {
        htmlStr+="算你狠，你从什么地方进来的啊？";
      }

      htmlStr+="</li>";
    }

    if(parentGeo != null) {
      htmlStr+="<li style='clear:both;'>";
      if(parentGeo.token == 3 && currentGeo.token != 2 && currentLevel != 4) {
        htmlStr+= "<a href='http://"+parentGeo.domain+this.urlTail+"'>返回上一级</a>";
      } else if(currentGeo.token == 2 || currentLevel == 4) {
        if(place == "header") {
          htmlStr+= "<a href='Javascript:siteNav.headerDo("+parentGeo.geo_code+");'>返回上一级</a>";
        } else if (place == "village") {
          htmlStr+= "<a href='Javascript:siteNav.villageDo("+parentGeo.geo_code+");'>返回上一级</a>";
        } else {
          htmlStr+="算你狠，你从什么地方进来的啊？";
        }
      } else {
        htmlStr+= parentGeo.display_name;
      }
      htmlStr+=" 　 <a href='"+this.rootUrl+"'>返回总站</a>";
      htmlStr+="</li>";
    } else if(currentGeo != null) {
      htmlStr+="<li style='clear:both;'><a href='"+this.rootUrl+"'>返回总站</a></li>";
    }

    if(place == "header") {
      $j(this.header.objId+" ul").empty();
      $j(this.header.objId+" ul").append(htmlStr);
      this.headerInit();
      siteNav.showLayer($j(this.header.doingObjId));
    } else if(place == "village") {
      $j(this.village.objId).empty();
      $j(this.village.objId).append(htmlStr);
    } else {
      alert("unknow place");
    }
  },

  getDataByGeo : function ( geoCode ) {
    //缓存数据
    if(typeof this.geoDatas[geoCode] != "undefined") {
      return this.geoDatas[geoCode];
    } else {
      if(this.ajaxUrl == null) return null;
      var data = $j.getYoyoJSON(this.ajaxUrl+"&geo_code="+geoCode);
      this.geoDatas[geoCode] = data;
      return data;
    }
  }
}
