if(typeof(tom.space) == 'undefined')
    tom.space = tom.Class.create();


if(typeof(tom.space.digg) == 'undefined')
    tom.space.digg = tom.Class.create();

tom.extend(tom.space.digg.prototype , {
    //文章号
    aid:'',

    //是否可以digg
    canDigg:'',

    //顶踩数
    diggInfo:{},

    //请求的 URL
    requestURL:{'diggTrampleNum':'',
                'diggURL':'',
                'trampleURL':'',
                'canDigg':'',
                'baseURL':''
                },


    //元素 ID 绑定
    show_digg_area:'show_digg_area',
    show_trample_area:'show_trample_area',

    
    /*初始化*/
    initialize:function(aid){
        aid = aid.trim();
        if((aid == '') || (aid.length<9))   return false;

         //请求的 URL
        this.requestURL.diggTrampleNum = this.spellURL('/proxy/digg_trample.php?aid='+aid);
        this.requestURL.diggURL        = this.spellURL('/proxy/digg_trample.php?action=digg_article&aid='+aid);
        this.requestURL.trampleURL     = this.spellURL('/proxy/digg_trample.php?action=trample_article&aid='+aid);
        this.requestURL.canDigg        = this.spellURL('/proxy/digg_trample.php?action=can_digg&aid='+aid);
        this.requestURL.baseURL        = this.spellURL('/proxy/digg_trample.php');

        this.aid = aid;
    },


    /*是否可以顶踩*/
    canDigg:function(){
        
    },

    /*得到顶踩数*/
    getDiggNumAndWrite:function(){
        new tom.XHConn().connectAsync(this.requestURL.diggTrampleNum+'&timestamp='+new Date().getTime(), 'GET', '', function(xhr){
               
               var retData;
               try{
                  retData = eval('('+xhr.responseText+')');
               }catch(e){
                  window.status = e;
                  return false;
               }

               var dig = new tom.space.digg(retData.aid);
               dig.digInfo = tom.Class.create();
               tom.extend(dig.digInfo , retData);
               dig.parseHTML();
               
        });
    },

    /*处理getDiggNumAndWrite:function()执行 AJAX 后的调用*/
    afterGDNW:function(xhr){
          var retData;
          try{
              retData = eval('('+xhr.responseText+')');
          }catch(e){
              window.status = e;
              return false;
          }

          var dig = new tom.space.digg(retData.aid);
          tom.extend(dig.digInfo , retData);
          dig.parseHTML();
    },

    /*得到 host*/
    spellURL:function(aid){
        return 'http://'+window.location.host+aid;
    },
    
    /*绑定元素*/
    parseHTML:function(){
        //{"code":0,"data":"操作成功","desc":"","aid":"E10003BE143","trample":19,"digg":35}
        $(this.show_digg_area).childNodes[0].innerHTML    = parseInt(this.digInfo.digg);
        //ee[0].innerHTML = parseInt(this.digInfo.digg);

        $(this.show_trample_area).childNodes[0].innerHTML = parseInt(this.digInfo.trample);
        
        var _self = this;

        $(this.show_digg_area).onclick = function(){
            var diggg = new tom.space.digg(_self.digInfo.aid);
            diggg.diggArticle();
            return false;
            
        }
        $(this.show_trample_area).onclick = function(){
             var trample = new tom.space.digg(_self.digInfo.aid);
             trample.trampleArticle();
             return false;
             
        }
    },

    /*顶文章*/
    diggArticle:function(){
        //{"code":0,"data":"操作成功","desc":"200","aid":"E10003BE143","trample":0,"digg":0}
        _self = this;
        new tom.XHConn().connectAsync(this.requestURL.diggURL+'&timestamp='+new Date().getTime(), 'GET', '', function(xhr){
            var retData;
    
            try{
              retData = eval('('+xhr.responseText+')');
            }catch(e){
              window.status = e;
              return false;
            };


            //200 already trample
            //400 already digg 
            if(retData.desc == 200){
                tom.information("您已经踩过了，不能再顶" , 3000);
                _self.cancelDiggTrample(); 
            }else if(retData.desc == 400){
                tom.information("您已经顶过了，不能一顶再顶" , 3000);
                _self.cancelDiggTrample();
            }else if(retData.desc == 1){
                _self.plusDiggArticle();
                _self.cancelDiggTrample();
            }

        }); 
    },

    
    /*踩文章*/
    trampleArticle:function(){
         //{"code":0,"data":"操作成功","desc":"200","aid":"E10003BE143","trample":0,"digg":0}
        _self = this;
        new tom.XHConn().connectAsync(this.requestURL.trampleURL+'&timestamp='+new Date().getTime(), 'GET', '', function(xhr){
            var retData;
    
            try{
              retData = eval('('+xhr.responseText+')');
            }catch(e){
              window.status = e;
              return false;
            };


            //200 already trample
            //400 already digg 
            if(retData.desc == 200){
                tom.information("您已经踩过了，不能一踩再踩" , 3000);
                _self.cancelDiggTrample(); 
            }else if(retData.desc == 400){
                tom.information("您已经顶过了，不能顶完再踩" , 3000);
                _self.cancelDiggTrample();
            }else if(retData.desc == 1){
                _self.plusTrampleArticle();
                _self.cancelDiggTrample();
            }

        }); 
    
    },


    /*取消顶踩区域*/
    cancelDiggTrample : function(){
        /*
          <div class="push number">
            <strong>80</strong>
            <span>我顶</span>
          </div>
          <div class="tread number">
            <strong>96</strong>
            <span>我踩</span>
          </div>
 
    //元素 ID 绑定
    show_digg_area:'show_digg_area',
    show_trample_area:'show_trample_area',
       */
        
       $(this.show_digg_area).style.cursor = 'default';
       $(this.show_trample_area).style.cursor = 'default';
       
       $(this.show_digg_area).onclick = function(){return false;}
       $(this.show_trample_area).onclick = function(){return false;}
        
    },

    /*增加文章顶数*/
    plusDiggArticle: function(){
        $(this.show_digg_area).childNodes[0].innerHTML = parseInt($(this.show_digg_area).childNodes[0].innerHTML)+1;
    },

    /*踩文章顶数*/
    plusTrampleArticle: function(){
        $(this.show_trample_area).childNodes[0].innerHTML = parseInt($(this.show_trample_area).childNodes[0].innerHTML)+1;
    }
});

function iframeDingTrample(articleId, width, height, cssUrl, digg, trample) {
  cssUrl = encodeURIComponent(cssUrl);
  digg = encodeURIComponent(digg || "顶");
  trample = encodeURIComponent(trample || "踩");
  var src = 'http://club.tom.com/testdigg1.html?'+articleId+'&digg='+digg+'&trample='+trample+'&ca='+cssUrl;
  document.write('<iframe frameborder="0" scrolling="no" marginheight="0" width="'+width+'" height="'+height+'" marginwidth="0" src="'+src+'"><\/iframe>');
}
