/**
 * Create by hanshu for digg from anywhere and anyway.Just comfortabled the DOM struct;
 *
 * 2008/4/14
**/

//defining the namespace if not
if(typeof(tom) == 'undefined')
    var tom = {post:{digg:{}}};

if(typeof(tom.post) == 'undefined')
    tom.post = {digg:{}};

//if(typeof(tom.post.digg) == 'undefined')
    //tom.post.digg = {};

tom.post.digg = function(aid){
    //文章号
    this.aid = '';

    //是否可以digg
    this.canDigg = '';

    //顶踩数
    this.diggInfo = {};

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

    //元素 ID 绑定
    this.show_digg_area = 'show_digg_area';
    this.show_trample_area = 'show_trample_area';
    
    this.show_suffix    = '';


    //请求的 URL
    this.requestURL.diggTrampleNum = '/proxy/digg_trample.php?aid=';
    this.requestURL.diggURL        = '/proxy/digg_trample.php?action=digg_article&aid=';
    this.requestURL.trampleURL     = '/proxy/digg_trample.php?action=trample_article&aid=';
    this.requestURL.canDigg        = '/proxy/digg_trample.php?action=can_digg&aid=';
    this.requestURL.baseURL        = '/proxy/digg_trample.php';
    
    
    
    //如果有赋值，那么就处理
    this.aid = 0;
    if(typeof(aid) != 'undefined'){
        this.aid = aid;
        if(this.aid.length<9) this.aid = '12345678'+this.aid  ;


        //请求的 URL
        this.requestURL.diggTrampleNum = this.spellURL(this.requestURL.diggTrampleNum+this.aid);
        this.requestURL.diggURL        = this.spellURL(this.requestURL.diggURL+this.aid);
        this.requestURL.trampleURL     = this.spellURL(this.requestURL.trampleURL+this.aid);
        this.requestURL.canDigg        = this.spellURL(this.requestURL.canDigg+this.aid);
        this.requestURL.baseURL        = this.spellURL(this.requestURL.baseURL);
    }


    //写进去的内容
    //this.content = '';
}

/*是否可以顶踩*/
tom.post.digg.prototype.canDigg=function(){
        //tom.post.digg.prototype.
};

    /*得到顶踩数*/
tom.post.digg.prototype.getDiggNumAndWrite=function(aid){
 
        //如果从这里面赋值
        if(typeof(aid) != 'undefined'){
            this.aid = aid;
            if(this.aid.length<9) this.aid = '12345678'+this.aid  ;

            //请求的 URL
            this.requestURL.diggTrampleNum = this.spellURL(this.requestURL.diggTrampleNum+this.aid);
            this.requestURL.diggURL        = this.spellURL(this.requestURL.diggURL+this.aid);
            this.requestURL.trampleURL     = this.spellURL(this.requestURL.trampleURL+this.aid);
            this.requestURL.canDigg        = this.spellURL(this.requestURL.canDigg+this.aid);
            this.requestURL.baseURL        = this.spellURL(this.requestURL.baseURL);
        }
        

        if(typeof(aid) == 'undefined'){
            //this.content = '';
            this.show_suffix = '';
        }else{
            //this.content = '_'+aid;
            this.show_suffix = '_'+aid;
        }


        var _self = this;
        
        new 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.post.digg(retData.aid);
               dig.digInfo = {};//tom.Class.create();
               dig.digInfo = retData;
               //dig.content = _self.content;
               //dig.aid     = _self.aid
               dig.show_suffix = _self.show_suffix;
               dig.parseHTML();
               
        });
    };

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

          var dig = new tom.post.digg(retData.aid);
          dig.digInfo = retData;
          dig.parseHTML();
    };

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

        if(document.getElementById(this.show_trample_area+this.show_suffix).childNodes && document.getElementById(this.show_digg_area+this.show_suffix).childNodes[1]) document.getElementById(this.show_trample_area+this.show_suffix).childNodes[1].innerHTML = parseInt(this.digInfo.trample);
        
        var _self = this;
        
        document.getElementById(this.show_digg_area+this.show_suffix).onclick = function(){
            var diggg = new tom.post.digg(_self.digInfo.aid);
            diggg.show_suffix = _self.show_suffix;
            //diggg.content = _self.content;
            diggg.diggArticle();

            return false;
            
        }


        document.getElementById(this.show_trample_area+this.show_suffix).onclick = function(){
             var trample = new tom.post.digg(_self.digInfo.aid);
             trample.show_suffix = _self.show_suffix;
             //trample.content = _self.content;
             trample.trampleArticle();
             return false;
             
        }
    };

    /*顶文章*/
tom.post.digg.prototype.diggArticle=function(){
        //{"code":0,"data":"操作成功","desc":"200","aid":"E10003BE143","trample":0,"digg":0}
        _self = this;
        new 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){
                alert("您已经踩过了，不能再顶");
                _self.cancelDiggTrample(); 
            }else if(retData.desc == 400){
                alert("您已经顶过了，不能一顶再顶");
                _self.cancelDiggTrample();
            }else if(retData.desc == 1){
                _self.plusDiggArticle();
                _self.cancelDiggTrample();
            }

        }); 
    };

    
    /*踩文章*/
tom.post.digg.prototype.trampleArticle=function(){
         //{"code":0,"data":"操作成功","desc":"200","aid":"E10003BE143","trample":0,"digg":0}
        _self = this;
        new 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){
                alert("您已经踩过了，不能一踩再踩");
                _self.cancelDiggTrample(); 
            }else if(retData.desc == 400){
                alert("您已经顶过了，不能顶完再踩");
                _self.cancelDiggTrample();
            }else if(retData.desc == 1){
                _self.plusTrampleArticle();
                _self.cancelDiggTrample();
            }

        }); 
    
    };


    /*取消顶踩区域*/
tom.post.digg.prototype.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',
       */
       document.getElementById(this.show_digg_area+this.show_suffix).style.cursor = 'default';
       document.getElementById(this.show_trample_area+this.show_suffix).style.cursor = 'default';
       
       document.getElementById(this.show_digg_area+this.show_suffix).onclick = function(){return false;}
       document.getElementById(this.show_trample_area+this.show_suffix).onclick = function(){return false;}
        
    };

    /*增加文章顶数*/
tom.post.digg.prototype.plusDiggArticle= function(){
        if(document.getElementById(this.show_digg_area+this.show_suffix).childNodes[1] != null) document.getElementById(this.show_digg_area+this.show_suffix).childNodes[1].innerHTML = parseInt(document.getElementById(this.show_digg_area+this.show_suffix).childNodes[1].innerHTML)+1;
    };

    /*踩文章顶数*/
tom.post.digg.prototype.plusTrampleArticle= function(){
        if(document.getElementById(this.show_trample_area+this.show_suffix).childNodes[1] != null) document.getElementById(this.show_trample_area+this.show_suffix).childNodes[1].innerHTML = parseInt(document.getElementById(this.show_trample_area+this.show_suffix).childNodes[1].innerHTML)+1;
    };


