function pageCollect(obj, id, type, opetate, url) {
  if(url==undefined || url=="") {
    url = document.location.href;
  }
  if(opetate==undefined || opetate==""){
    opetate="collect";
  }
  // 替换 / 为 |
  url=url.replace(/[/]/g,"|");
  // 处理没有 commDomain 的情况
  if(commId==0 || commDomain=="" || commId==0 ) {
    commDomain = userDomainId;
  }
  if(commDomain!=undefined && commDomain!=""){
    var toUrl="/"+commDomain+"/collect/gather/cid/"+id+"/type/"+type+"/operate/"+opetate+"/url/"+url+"/?TB_iframe=true&height=120&width=320";
    obj.href=toUrl;
  }
}

/** 购买 或是其它的处理 */
function pageJoin(obj, id, type, opetate, url, opt2) {
  if(url==undefined || url=="") {
    url = document.location.href;
  }
  if(opetate==undefined || opetate==""){
    opetate="collect";
  }
  if(opt2!=undefined || opt2!="") {
    opt2="/opt2/"+opt2;
  } else {
    opt2="";
  }
  
  // 替换 / 为 | 
  url=url.replace(/[/]/g,"|");
  if(commDomain!=undefined){
    obj.href="/"+commDomain+"/collect/join/cid/"+id+"/type/"+type+"/operate/"+opetate+opt2+"/url/"+url+"/?TB_iframe=true&height=160&width=320";
  }
}

/** 不买了 或是其它的处理 */
function pageUnjoin(obj, id, type, opetate, url) {
  if(url==undefined || url=="") {
    url = document.location.href;
  }
  if(opetate==undefined || opetate==""){
    opetate="collect";
  }
  // 替换 / 为 | 
  url=url.replace(/[/]/g,"|");
  if(commDomain!=undefined){
    obj.href="/"+commDomain+"/collect/unjoin/cid/"+id+"/type/"+type+"/operate/"+opetate+"/url/"+url+"/?TB_iframe=true&height=160&width=320";
  }
}

function getCollectAndOther(cid, type) {
  // 处理没有 commDomain 的情况
  if(commDomain==undefined || commDomain=="" || commId==0 ) {
    commDomain = userDomainId;
  }
  var cUrl = "/"+commDomain+"/collect/listPart";
  param ="ajax=1&cid="+cid+"&type="+type;
  // ajax 调用
  $.ajax({type:"POST",url:cUrl, data:param, success:callBack, dataType:"json"});
}

/** 查看全部收藏的和感兴趣的 */
function setAllListLink(cid, type) {
  // 处理没有 commDomain 的情况
  if(commDomain==undefined || commDomain=="" || commId==0 ) {
    commDomain = userDomainId;
  }
  var listUrl = "/"+commDomain+"/collect/listAll/type/"+type+"/cid/"+cid;
  if($("#collectAllList")) {
    $("#collectAllList").attr("href", listUrl);
    $("#collectAllList").attr("target", "_blank");
  }
}

function callBack(data) {
  if(data.status=='1') {
    var list = data.data;
    if(list.html!=undefined){
      $("#interestDiv").html(list.html);
      $("#allNumberSpan").html(list.allNumber);
      $("#othNumberSpan").html(list.othNumber);
      // 隐藏查看全部
      if(list.allNumber==undefined || list.allNumber<=0) {
        if($("#collectAllList")) {
          $("#collectAllList").hide();
        }
      } else {
        if($("#collectAllList")) {
          $("#collectAllList").show();
        }
      }
      // 使用样式表控制
      if($(".allNumberSpan")){
        $.each($(".allNumberSpan"), function(i, n){ $(n).text(list.allNumber); }); 
      }
      if($(".othNumberSpan")){
        $.each($(".othNumberSpan"), function(i, n){ $(n).text(list.othNumber); }); 
      }
      // 控制按钮显示
      if(list.can=="N"){
        $("#userCannotOperate").hide();
        $("#userNotCollect").show();
        $("#userCanOperate").show();
      } else if(list.can=="C"){
        $("#userNotCollect").hide();
        $("#userCannotOperate").hide();
        $("#userCanOperate").show();
      } else if(list.can=="Y") {
        $("#userNotCollect").hide();
        $("#userCanOperate").hide();
        $("#userCannotOperate").show();
      }
    }else {
      $("#interestDiv").html("没有用户");
    }
  }
}

// 页面ID列表
var collectEventIds = "";
function getCollectAndOperate(type) {

  var ids = "";
   $(".gxq").each(function(i){ 
    colId=$(this).attr('id');
    ids +=colId.substr(13)+",";
   });
   if(ids!="" ) {
      var cUrl = "/"+commDomain+"/collect/getCollectNumber";
      var param ="ajax=1&ids="+ids+"&type="+type;
      $.ajax({type:"POST",url:cUrl, data:param, success:collectCallback, dataType:"json"});
      collectEventIds=ids;
   }
}

/** 感兴趣与收藏ajax回调函数 */
function collectCallback(collectData) {
  if(collectData.status==1) {
    data = collectData.data;
    if(data.result=="Y") {
      if(collectEventIds!="") {
        idsArr = collectEventIds.split(",");
        for(i=0; i<idsArr.length; i++) {
          collectSpanId = "span_collect_"+idsArr[i];
          operateSpanId = "span_operate_"+idsArr[i];
          $("#"+collectSpanId).attr("innerHTML",data.collect[idsArr[i]] );
          $("#"+operateSpanId).attr("innerHTML",data.operate[idsArr[i]] );
        }
      }
    }
  }
}


// 页面 onload 时读取收藏和其它操作
$(document).ready(function(){
  // 如果未定义 collectId 则退出
    if (typeof(collectId)=="undefined") {
      return;
    }
    getCollectAndOther(collectId, collectType);
    // 设置查看全部的URL
    setAllListLink(collectId, collectType);
});

