﻿///<reference path="jquery-1.2.3-intellisense.js" />
var QUERY;
var ReturnQurey;
var twitter={
    setup:function(){
        
		$.ajaxSetup({cache:false});
		this.conceptLoading=$("#conceptLoading");
		this.digulistLoading=$("#digulistLoading");
		
		this.timelineBox=$("#timeline");
		this.conceptBox=$("#conceptList");
		this.topicBox=$("#topicList");
		this.queryName=$("#queryName");
		
		this.noResultDiv=$("#noResultDiv");
		this.noConceptDiv=$("#noConceptDiv");
		

		},
   
    searchResult:function(q){
        var a=this;
        
        $("#noResultDiv").hide();
        $("#toTop").hide();
        $("#digulistLoading").show();
        $("#timeline").html("");
        
        var searchUrl="Search.aspx?t="+new Date();
 
		$.getJSON(searchUrl,{query:q,type:"status"},function(b){
		    //if(b.query!=QUERY)return;
			if(b.results.length==0)
			{
				$("#noResultDiv").show();
				$("#digulistLoading").hide();
				return;
			}
		     
			a.process(b);
			
			if(b.results.length>10){
					$("#toTop").show();
		    }
		});
		
	},
	 searchResultForConcept:function(q){
	    QUERY=q;
        var a=this;
        $("#ConceptContentDiv").show();
        $("#noConceptDiv").hide();
        $("#conceptLoading").show();
        $("#conceptList").html("");
        var searchUrl="Search.aspx?t="+new Date();
       
		$.getJSON(searchUrl,{query:q,type:"concept"},function(b){
		    
		    if(b.query!=QUERY)return;
			if(b.results.length==0){
				$("#noConceptDiv").show();
				$("#conceptLoading").hide();
				return;
			}
		    a.processConcept(b);
			
		});
		
	},
	process:function(e){
	  			
	  			var b=e.results;
	  			var a=b.length-0;
	  			
	  		     var nextpage=e.next_page;
	  		     var page=e.page;
	  		     var q=e.query;
	  		     var since_id=e.since_id;
	  		     var max_id=e.max_id;
	  		     $("#timeline").html("");
	  			if(a>0){
	  				var c=[];
	  				var f="";
	  				for(var d=a-1;d>=0;d--){
	  					var temp=this.createItem(b[d],e.query);
	  				    c.unshift(temp);
	  					
	  				}
	  				f=c.join("");
	  				$("#timeline").prepend(f);
	  				
	  			}
	  			
	  			if(typeof(nextpage)!="undefined")
	  		    {
	  		      //  nextpage="?page="+temp+"&q="+q;
	  		        $("#moreDiv a").attr("href","javascript:searchMore(\""+nextpage+"\")");	
	  		        $("#moreDiv2 a").attr("href","javascript:searchMore(\""+nextpage+"\")"); 		        
	  				$("#moreDiv").show();
	  				$("#moreDiv2").show();
	  		    }else{
	  		        $("#moreDiv").hide();
	  		        $("#moreDiv2").hide();
	  		    }
	  		    
	  		    if(page!=1)
	  		    {
	  		        var temp=page-1;
	  		        var prevpage="?page="+temp+"&max_id="+max_id+"&q="+q;
	  		        $("#prevDiv a").attr("href","javascript:searchMore(\""+prevpage+"\")");	  
	  		        $("#prevDiv2 a").attr("href","javascript:searchMore(\""+prevpage+"\")");	  			        
	  				$("#prevDiv").show();
	  				$("#prevDiv2").show();
	  		    }else{
	  		        $("#prevDiv").hide();
	  		        $("#prevDiv2").hide();
	  		    }
	  			$("#digulistLoading").hide();
	  			
	},
	processConcept:function(e){
	  		
	  			var b=e.results;
	  			var a=b.length-0;
	  			
	  		    $("#conceptList").html("");
	  			if(a>0){
	  				var c=[];
	  				var f="";
	  				for(var d=a-1;d>=0;d--){
	  					var temp=this.createItemForConcept(b[d]);
	  				    c.unshift(temp);
	  					
	  				}
	  				f=c.join("");
	  				$("#conceptList").prepend(f);
	  				
	  			}
	  			$("#conceptLoading").hide();
	},
	createItem:function(f,query){
	    query=query.replace(/[\+]+/g," ");
	    ReturnQurey=query;
	    var source=f.source;
	    source=source.replace(/\&lt;/g, "<").replace(/\&gt;/g, ">").replace(/\&quot;/g, "\"");
	
	    var showtime="";
	    var time=f.created_at;
	    var t=Date.parse(time);   
	    var now =new Date();
	    var cha=(now-t)/1000;
	    var minute=parseInt(cha/60);
	    
	    if(minute==0){
	        showtime="less than a minute ago ";
	    }else if(minute<60){
	        showtime=minute+" minute ago ";
	    }else {
	        var hour=parseInt(minute/60);
	        if(hour<24){
	            showtime="about "+hour+" hours ago ";
	        }
	        else{
	            var day=parseInt(hour/24);
	            showtime="about "+day+" days ago ";
	        }
	        
	    }
	    var text=" "+f.text;
	    text=text.replaceAll(query);
	    text=text.replaceAllLink();
	    text=text.replaceUserLink();
	    //alert(text);
	 
	var c="";
	c="<li class=\"hentry u-christt status\" id=\"status_"+f.id+"\"><div class=\"listable\"><span class=\"thumb vcard author\">";
    c+="<a href=\"http://twitter.com/"+f.from_user+"\" class=\"url\"><img alt=\""+f.from_user+"\" class=\"photo fn\" height=\"48\" src=\""+f.profile_image_url+"\" width=\"48\" /></a></span></div>";     
    c+="<span class=\"status-body\"><strong><a href=\"http://twitter.com/"+f.from_user+"\" class=\"screen-name\" title=\""+f.from_user+"\">"+f.from_user+":</a></strong>"; 
    c+="<span class=\"entry-content\">"+text+"</span><span class=\"meta entry-meta\">";
    c+="<a href=\"http://twitter.com/"+f.from_user+"/status/"+f.id+"\" class=\"entry-date\" rel=\"bookmark\">";
    c+="<span class=\"published\">"+showtime+"</span></a> <span>from "+source+"</span>"; 
               
    c+="</span></span></span></li>";
            return c;
        
	},
	createItemForConcept:function(f){
	    var c="<li ><img src=\"images/rank"+f.rank+".gif\"><a  href=\"#\" onclick=\"javascript:mySearch(&quot;"+f.name+"&quot;);\">"+f.name+"</a></li>";
	  //var c="<li ><a  href=\"#\" onclick=\"javascript:mySearch(&quot;"+f.name+"&quot;);\">"+f.name+"</a></li>";
        return c;
        
	}
};





String.prototype.replaceAll=function(AFindText){
   
    var arrTmp=AFindText.split(" ");　
    var temp=this;
    for(var i=0;i<arrTmp.length;i++)　
    {　
       var raRegExp = new RegExp("\\b("+arrTmp[i]+")\\b","ig");
       temp=temp.replace(raRegExp,test);
       
    }
    return temp;
}
function test($1){
       return "<b>"+$1+"</b>";
}


String.prototype.replaceAllLink=function(){
    var temp=this;
    //(http:\/\/[a-zA-z0-9.\/?&-]+)
    //(http\:\/\/)?([\w.]+)(\/[\w- \.\/\?%&=]*)?
    var raRegExp = new RegExp("(http:\/\/[\\w.\/?%&-=<>]*)","g");
    temp=this.replace(raRegExp,testLink);
    //raRegExp = new RegExp("@<b>[a-zA-Z0-9]+<\/b>","g");
   
    return temp;
}	
String.prototype.replaceUserLink=function(){
  
    var temp=this;
    var raRegExp = new RegExp("@[<>\/a-zA-Z0-9_]+","g");
    return temp.replace(raRegExp,userlink);
}	
function testLink($1)
{
    var arrTmp=ReturnQurey.split("+");　
    var temp=$1;
    for(var i=0;i<arrTmp.length;i++)　
    {　
       var raRegExp = new RegExp("<b>"+arrTmp[i]+"<\/b>","ig");
       temp=temp.replace(raRegExp,arrTmp[i]);  
    }
    return "<a href=\""+temp+"\" target=\"_blank\"><font color=\"#0099cc\">"+temp+"</font></a>";
}

function userlink($1)
{
    var temp=$1;
    //return "";
    var word=temp.replace("<b>","").replace("</b>","").substring(1); 
    return "<a href=\"http:\/\/twitter.com\/"+word+"\" target=\"_blank\"><font color=\"#0099cc\">"+temp+"</font></a>";
}