﻿
$(document).ready(function(){
    twitter.setup();
	pickATip();
	$("#queryText").bind("keydown",keydown);
    $("#queryText").select();
	$("#queryText").focus(function(){
		$(this).bind("keydown",keydown);
    });
    
    $('.sidePullArr').each(function(){
					var _this = $(this); 
					var id = _this.attr('id');
					var temp=id.substring(0,id.length-9);
					$(this).click(function(){
						var header = $(this);
						$('#'+temp+'ContentDiv').slideToggle('fast',function(){
							if($(this).css('display')!='none'){
								
								header.removeClass('sidePullArrDown');
							}else{
								
								header.addClass('sidePullArrDown');
							}
						});
					});
				});
  
            var b=topics.trends;
	  			var a=b.length-0;
	  			if(a>0){
	  				var c=[];
	  				var f="";
	  				for(var d=a-1;d>=0;d--){
	  				  var temp="<li ><a  href=\"#\" onclick=\"javascript:mySearchForTopic(&quot;"+b[d].name+"&quot;);\">"+b[d].name+"</a></li>";
	  				    c.unshift(temp);
	  					
	  				}
	  				f=c.join("");
	  				$("#topicList").prepend(f);
	  				var random=Math.random();
                    random=Math.round(random*a);
	  		        mySearchForTopic(b[random-1].name);	
	  			}else{
	  			     $("#noTopicDiv").show();
	  			     mySearchForTopic("twitter");
	  			}
	  			
	  			
			

    
});



function onPost(){

   // $("#mainPageDiv").hide();
    //$("#conSubHeader").show();

	var q=$("#queryText").val();
	if($("#updateSubmit").attr("disabled")==true){return}
	var l=/^s*$/;
	if(l.test(q)){
		//ye_msg.open("please enter something!",2,2);
		alert("please enter something!");
		return;
	}
	$("#queryName").html(q);
	var realQuery=q.replaceJinHao();
	realQuery=realQuery.replace(/(^\s*)|(\s*$)/g, ""); 
	twitter.searchResult("?q="+realQuery);
	twitter.searchResultForConcept(realQuery);
}

function mySearch(text){
   // $("#mainPageDiv").hide();
   // $("#conSubHeader").show();
  var q=$("#queryText").val();
  
  $("#queryText").attr("value",q+" "+text);
  $("#queryName").html(q+" "+text);
  
  var realQuery=(q+" "+text).replaceJinHao();
    realQuery=realQuery.replace(/(^\s*)|(\s*$)/g, ""); 
    twitter.searchResult("?q="+realQuery);
	twitter.searchResultForConcept(realQuery);
}
function mySearchForTopic(text){
    //$("#mainPageDiv").hide();
   // $("#conSubHeader").show();
  
  $("#queryText").attr("value",text);
  $("#queryName").html(text);
  
  var realQuery=text.replaceJinHao();
   realQuery=realQuery.replace(/(^\s*)|(\s*$)/g, ""); 
    twitter.searchResult("?q="+realQuery);
	twitter.searchResultForConcept(realQuery);
}
function keydown(a){
 if(a.keyCode==13){
   onPost();
 }
}	

function searchMore(url){
    twitter.searchResult(url);
}

function testReply($1)
{
    return $1.substring(1); 
}
String.prototype.replaceJinHao=function(){
    var temp=this;
    raRegExp = new RegExp("[#]+([a-zA-Z0-9]+)","g");
    return temp.replace(raRegExp,testReply);
}	

function info(title, content, type, x, y) {

	$('#infobox_title').addClass('infotop');
	$('#infobox_content').addClass('infomiddle');
	$('#infobox_bottom').addClass('infobottom');
	$('#infobox_title').html("<DIV style='float:left;padding-top:3px;'>"+title+"</DIV>");
	$('#infobox_content').html(content);
	
	if (x!=null && y!=null)
	{
		$("#infobox").css({ top:y, left:x});
	} else {
		$("#infobox").css({ top: mouseY-10, left:mouseX+30});
	}

	if (type==1)
	{
		$("#infobox_title").append("<DIV style='float:right;width:12;padding-top:6px;padding-right:7px;' id='infoclose'><a href='#'><img src='images/infoclose.gif' onMouseOver=\"$(this).attr('src','images/infoclose_mo.gif')\" onMouseOut=\"$(this).attr('src','images/infoclose.gif')\"></a></DIV>");
		$("#infoclose").mousedown( function() {$("#infobox").hide();} );
	} else {
		$("#infobox_title").mouseover( function() {$("#infobox").hide();} );
		$("#infobox_content").mouseover( function() {$("#infobox").hide();} );
	}

	$("#infobox").show();
}
function pickATip() {
			// DID YOU KNOW SECTION 开始
			/////////////////////////////////////////
			
			var pick = Math.floor(Math.random() * 10);
			var picktitle = "";			
			var pickcontent = "";
			if (pick<3)
			{
				picktitle = "Get the real-time scoop";			
				pickcontent = "Enter a keyword to search, TopicScoop will find the tweets and extract the topics talked about in the tweets. Very cool!";
			} else if (pick<5)
			{
				picktitle = "Get the scoop, fast!";			
				pickcontent = "Search for anything you want, TopicCcoop finds tweets and summarizes topics covered by the tweets in real-time. How cool is that?";
			} else
			{
				picktitle = "Get the scoop in real-time";
				pickcontent = "<a onClick=\"$('#queryText').val('twitter');\">Twitter</a>, <a onClick=\"$('#queryText').val('trending topics');\">Trending topic</a>, or any other keyword(s) ... Enter into the search box and check out what TopicScoop can do for you!<br><img src='images/peek.gif' style='border:0px;'>";
			}
			
			info(picktitle,pickcontent,1,calculateOffsetLeft(document.getElementById('queryText'))+80+130,calculateOffsetTop(document.getElementById('queryText'))+31);
			/////////////////////////////////////////
	}
function calculateOffsetLeft(field) {
	return calculateOffset(field, "offsetLeft");
}

function calculateOffsetTop(field) {
	return calculateOffset(field, "offsetTop");
}
function calculateOffset(field, attr) {
	var offset = 0;
	while(field) {
		offset += field[attr]; 
		field = field.offsetParent;
	}
	return offset;
}
