/**
 * 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')
    tom = {post:{}};


var XHConnDig = function() {
    this.xmlhttp = false;
    this.isComplete = false;
    try { this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) { try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch (e) { try { this.xmlhttp = new XMLHttpRequest(); }
    catch (e) { this.xmlhttp = false; }}}
}
XHConnDig.prototype = {
    connectAsync: function(url, method, vars, callback) {
        if (!this.xmlhttp) return false;
        this.isComplete = false;
        method = method.toUpperCase();
        try {
            if (method == "GET") {
                this.xmlhttp.open(method, url + "?" + vars, true);
                vars = "";
            } else {
                this.xmlhttp.open(method, url, true);
                this.xmlhttp.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
                this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            }
            var _this = this;
            this.xmlhttp.onreadystatechange = function() {
                if (_this.xmlhttp.readyState == 4 && !_this.isComplete) {
                    _this.isComplete = true;
                    callback(_this.xmlhttp);
                }
            };
            this.xmlhttp.send(vars);
        } catch(z) {
            return false;
        }
        return true;
    },
    connectSync: function(url, method, vars) {
        if (!this.xmlhttp) return false;
        this.isComplete = false;
        method = method.toUpperCase();
        try {
            if (method == "GET") {
                this.xmlhttp.open(method, url + "?" + vars, false);
                vars = "";
            } else {
                this.xmlhttp.open(method, url, true);
                this.xmlhttp.setRequestHeader("Method", "POST " + url + " HTTP/1.1");
                this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            }
            this.xmlhttp.send(vars);
        } catch(e) {
            alert(e);
            return false;
        }
        return {
            text: this.xmlhttp.responseText,
            xml: this.xmlhttp.responseXML
        };
    }
}


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_digg_num = 'show_digg_num';
	this.show_trample_num = 'show_trample_num';

    
    this.show_suffix    = '';
    
    
    this.aid = aid;

    if(this.aid == '') return false;
    if(this.aid.length<9) this.aid = '12345678'+this.aid  ;

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

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

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

    /*得到顶踩数*/
tom.post.digg.prototype.getDiggNumAndWrite=function(c){
        this.content = c;
        var _self = this;
        
        new XHConnDig().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.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);
        //ee[0].innerHTML = parseInt(this.digInfo.digg);
		if(document.getElementById(this.show_digg_num+this.show_suffix) != null) document.getElementById(this.show_digg_num+this.show_suffix).innerHTML    = parseInt(this.digInfo.digg);
        //if(document.getElementById(this.show_trample_area+this.show_suffix).childNodes[0] != null) document.getElementById(this.show_trample_area+this.show_suffix).childNodes[0].innerHTML = parseInt(this.digInfo.trample);
		if(document.getElementById(this.show_trample_num+this.show_suffix) != null) document.getElementById(this.show_trample_num+this.show_suffix).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 XHConnDig().connectAsync(this.requestURL.diggURL+'&content='+this.content+'&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 XHConnDig().connectAsync(this.requestURL.trampleURL+'&content='+this.content+'&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[0] != null) document.getElementById(this.show_digg_area+this.show_suffix).childNodes[0].innerHTML = parseInt(document.getElementById(this.show_digg_area+this.show_suffix).childNodes[0].innerHTML)+1;
		if(document.getElementById(this.show_digg_num+this.show_suffix) != null) document.getElementById(this.show_digg_num+this.show_suffix).innerHTML = parseInt(document.getElementById(this.show_digg_num+this.show_suffix).innerHTML)+1;
    };

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


