
//login

function checkTwitUserAuth(frm){
    if (frm.username.value ==""){
	       alert("insert username");
		    frm.username.focus();		
	       return false;
	    }else
	    if (frm.password.value ==""){
	       alert("insert password");
		   frm.password.focus();	
	       return false;
	    }
		$.post("ajax/login.jsp", {username: frm.username.value, password :frm.password.value},
		       function(data){
		           //alert(data);
		          if (data.errorCode==0){
		             location.href = data.location;
		          }else{
		             alert(data.errorMsg);
		          }
		          return false;
		       }, 
		       "json" 
		       ); 
			return false
}
//end of login

function animateMsg(msg){
  	var obj = jQuery(document.createElement('div')).addClass('animateMsg').html(msg).css('position','absolute');
  	obj.css('background-color', '#ffffff');
  	jQuery('body').prepend(obj);
  	obj.animate({ 
          width: "70%",
          opacity: 0.4,
          marginLeft: "0.6in",
          fontSize: "3em", 
          borderWidth: "10px"
        }, 1500 );

		var d = function() { closeMsg() };
		var timeoutId = setTimeout(d,3000);
  }
  function closeMsg(){
	  jQuery('.animateMsg').remove();
  }
  
//search
function tweet_search(query, filter, sdate, edate, page){  
	jQuery.post("/ajax_tweet_search.jsp", {q: query, filter:filter, sdate:sdate, edate:edate, page:page},
			function(data){
				//jQuery("#searched_"+idno).html(data).show();
				jQuery(".search_result").append(data);
				jQuery(".more").show();
				//quering = false;
				//if (data.indexOf("searcus_query_last")>-1){
				//	ended = true;
				//}
			}, 
   "html" 
   ); 
}  
function tweet_search_photo(query, filter, sdate, edate, page){  
	jQuery.post("/ajax_tweet_search_photo.jsp", {q: query, filter:filter, sdate:sdate, edate:edate, page:page},
			function(data){
				//jQuery("#searched_"+idno).html(data).show();
				jQuery(".search_result").append(data);
				jQuery(".more").show();
			}, 
   "html" 
   ); 
}  

function tweet_wherebeen(userid, lastno){  
	jQuery.post("/ajax_tweet_wherebeen.jsp", {userid: userid, lastid:lastno},
			function(data){
				jQuery(".search_result").append(data);
				jQuery(".more").show();
			}, 
   "html" 
   ); 
}  
function tweet_hot(){
    var gid = (new Date().getTime());
	jQuery.post("/ajax_cluster_top.jsp", {groupid:gid},
			function(data){
				jQuery(".hot_link").html(data);
			}, 
   "html" 
   ); 
}
function tweet_hot_image(){
    var gid = (new Date().getTime());
	jQuery.post("/ajax_cluster_image.jsp", {groupid:gid},
			function(data){
				jQuery(".hot_image").html(data);
			}, 
   "html" 
   ); 
}
function tweet_hot_video(){
    var gid = (new Date().getTime());
	jQuery.post("/ajax_cluster_video.jsp", {groupid:gid},
			function(data){
				jQuery(".hot_video").html(data);
			}, 
   "html" 
   ); 
}
//end of search

//twitter api
function twitter_home(action, page){  
	jQuery.post("/ajax_twitter_home.jsp", {action:action, page:page},
			function(data){
				//jQuery("#searched_"+idno).html(data).show();
				jQuery(".search_result").append(data);
				jQuery(".more").show();
				//quering = false;
				//if (data.indexOf("searcus_query_last")>-1){
				//	ended = true;
				//}
			}, 
   "html" 
   ); 
} 
function twitter_dm(action, page){  
	jQuery.post("/ajax_twitter_dm.jsp", {action:action, page:page},
			function(data){
				//jQuery("#searched_"+idno).html(data).show();
				jQuery(".search_result").append(data);
				jQuery(".more").show();
				//quering = false;
				//if (data.indexOf("searcus_query_last")>-1){
				//	ended = true;
				//}
			}, 
   "html" 
   ); 
} 
function twitter_action(action, status_id){  
	jQuery.post("/ajax_twitter_action.jsp", {action:action, status_id:status_id},
			function(data){
				animateMsg('처리되었습니다.');
			}, 
   "html" 
   ); 
} 

function updateDM(msg, to){
	jQuery.post("/ajax_twitter_action.jsp", {user_name: to, 
			    msg:msg,
				action:"dmwrite"},
			   function(data){
		          //alert(data);
				  if(data.indexOf("true")>-1){
			    	  animateMsg("작성되었습니다.");
			    	  Tips.hideAll();
			      }else{
			    	  alert("something wrong to write direct message");
			      }
			   }, 
			   "html" 
			   ); 
}
function twitter_extmenu(){  
	jQuery.post("/ajax_twitter_menu.jsp", {},
			function(data){
		      jQuery("#api_ext_menu").html(data).show();
			}, 
   "html" 
   ); 
} 
//end of twitter api

//chart
function drawChart(q){
	if (q==""){
		return false;
	}
	jQuery.post("/ajax_chart.jsp", {query: q},
		  function(data){
		           //alert(data);
		          if (data.errorCode==0){
		        	  var data = new google.visualization.DataTable();
		              data.addColumn('string', 'Date');
		              data.addColumn('number', q);
		              data.addRows(data.count);
		        	  for (i=0;i<data.count;i++){   
		        	  }
		          }else{
		             alert(data.errorMsg);
		          }
		          return false;
		  }, 
		  "json" 
    ); 
	return false
}
//end of chart


//short urls
function bitlyinfo(json){  
    if (json.errorCode==0){                  
    	jQuery(".status_shorturl").each(function(){
		   if (typeof json.results[jQuery(this).attr("srturl_name")] === 'object'){ 
			   jQuery(this).html(json.results[jQuery(this).attr("srturl_name")].longUrl).show(); 
              if (json.results[jQuery(this).attr("srturl_name")].htmlTitle){
            	  jQuery(this).html(jQuery(this).text() + " [" + json.results[jQuery(this).attr("srturl_name")].htmlTitle + "]");
   			  }
              return false;
		   }
		}); 
      }
}  
function shortUrlConvert(ids){ 
	  var srts = ids.split(",");
	  for (i=0;i<srts.length;i++){
		  jQuery('body').append('<script src=\"http://api.bit.ly/info?version=2.0.1&hash=' + srts[i] + '&callback=bitlyinfo&login=typos&apiKey=R_ecc0cddb19eaa8e959165945ca0344bf\"><\/script>');
	  }
	  return false;
  }
//end of short urls

//status replies
function replyCount(uId, sId, seqtime){  
	jQuery.post("/ajax_tweet_reply_cnt.jsp", {userId: uId, statusId: sId, type: "count"},
   function(data){
      if (data.errorCode==0){
         //alert(data.count);
          if (data.count!=0){
        	  //alert(data.count);
        	  jQuery("#status_conv_box_"+data.statusId).html(" has some replies <img id=\"thread_" + seqtime + "\" class=chat_ico style=\"cursor:pointer;\" src=\"/img/thread_conver.png\" height=13 border=0>"
              		+ "</span>").show();
        	  showReplies(seqtime ,'via_'+seqtime, uId ,sId );
              
          }
      }
   }, 
   "json" 
   ); 
}  
function checkReplycounts(ids){
  var srts = ids.split(",");
  var user_status ;
  for (i=0;i<srts.length;i++){
      user_status = srts[i].split(":");
      replyCount(user_status[0], user_status[1], user_status[2]);
  }
  return false;
}
//end of status replies


///tooltips
function showUserProfile2(tipid, tiptitle, tiptarget, userid){
	jQuery('#'+tipid).tipsy({html:true, opacity: 1.0, title: function() { return jQuery.ajax({
		  url: '/ajax_user_tooltip.jsp?userid=' + userid,
		  async: false
		}).responseText; } });
}
function showUserProfile(tipid, tiptitle, tiptarget, userid){
			new Tip(tipid, {
			    title : tiptitle,
			    delay: 0.3,
				target: tiptarget,
				ajax: {
					url: '/ajax_user_tooltip.jsp?userid=' + userid,
					//url: 'ajax_tweet_geo.jsp?userid=' + userid,
					options: {
						onComplete: function(transport) {
							// you could do something here after the ajax call is finished
						}
					}
				},
				hideOn: false,// { element: 'closeButton', event: 'click' },
				hideAfter: 1,
				width: 300, // We don't want the default 250px.
				               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
				hook: { target: 'bottomMiddle', tip: 'topLeft' },
				stem: 'topLeft',
				offset: { x: 4, y: 0 }
			});
		}
function showTweetPhoto(tipid, tiptitle, userid, statusid){
  new Tip('img_' + tipid, {
		    title : tiptitle,
		    //delay: 0.5,
		    showOn: 'click',
			target: 'img_' + tipid,
			ajax: {
				url: '/ajax_tweet_image.jsp?userid='+userid+'&statusid='+statusid,
				options: {
					onComplete: function(transport) {
						// you could do something here after the ajax call is finished
					}
				}
			},
			hideOn: { element: 'closeButton', event: 'click' },
			//hideAfter: 1,
			width: 450, // We don't want the default 250px.
			               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
			hook: { target: 'bottomMiddle', tip: 'topRight' },
		stem: 'topRight',
		offset: { x: 6, y: -30 }
		});
}
function showReplies(tipid, tiptarget, userid, statusid){
  new Tip('thread_'+tipid, {
	    title : 'Conversations',
	    //delay: 0.3,
	    showOn: 'click',
		target: tiptarget,
		ajax: {
			url: '/ajax_tweet_thread.jsp?userid='+userid+'&statusid='+statusid,
			options: {
				onComplete: function(transport) {
					// you could do something here after the ajax call is finished
				}
			}
		},
		hideOn: { element: 'closeButton', event: 'click' },
		//hideAfter: 1,
		width: 550, // We don't want the default 250px.
		               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
		hook: { target: 'bottomMiddle', tip: 'topLeft' },
		stem: 'topLeft',
		offset: { x: 4, y: -20 }
	});
}
function showLinkCluster(tipid, tiptitle, link){
	  new Tip('link_' + tipid, {
		    title : tiptitle,
		    //delay: 0.5,
		    showOn: 'click',
			//target: 'link_' + tipid,
			ajax: {
				url: '/ajax_tweet_link.jsp?link='+link,
				options: {
					onComplete: function(transport) {
						// you could do something here after the ajax call is finished
					}
				}
			},
			hideOn: { element: 'closeButton', event: 'click' },
			//hideAfter: 1,
			width: 550, // We don't want the default 250px.
			               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
			hook: { target: 'bottomMiddle', tip: 'topLeft' },
			stem: 'topLeft',
		offset: { x: 6, y: -30 }
		});
}

//tweet update
function updateTweet(msg, inreplyto){
	jQuery.post("/ajax_tweet_update.jsp", {status: msg, in_reply_to_status_id: inreplyto},
			   function(data){
		          //alert(data);
			      if (data.errorCode==0){
			    	  animateMsg("작성되었습니다.");
			    	  Tips.hideAll();
			      }else{
			    	  alert("something wrong to write tweet");
			      }
			   }, 
			   "json" 
			   ); 
}
function checkStatusInputBox(tipid){
	try{
		var currlength = jQuery('#status_text'+tipid).val().length ;
			jQuery('#status-field-char-counter'+tipid).html(140 - currlength);
			if((currlength > 0)&&(currlength <= 140)){
				//$('input',obj).removeAttr('disabled').removeClass('disabled').addClass('enabled');
				jQuery('#update-submit'+tipid).removeAttr('disabled').removeClass('disabled').addClass('enabled');
			}
			else{
				//$('input',obj).attr('disabled','true').removeClass('enabled').addClass('disabled');
				jQuery('#update-submit'+tipid).attr('disabled','true').removeClass('enabled').addClass('disabled');
			}
			jQuery('#status_text'+tipid).one('keydown',function(){
				var timeout = setTimeout("checkStatusInputBox('" +tipid + "')",1);
		});
	}catch(err){
	}
}
function showTweetWrite(tipid){
	  new Tip(tipid, {
		    title : 'new tweet writing',
		    //delay: 0.5,
		    showOn: 'click',
			//target: 'link_' + tipid,
			ajax: {
				url: '/tweetwrite.jsp?action=new&tipid=' + tipid ,
				options: {
					onComplete: function(transport) {
						// you could do something here after the ajax call is finished
		  checkStatusInputBox('_new'+tipid);
					}
				}
			},
			hideOn: { element: 'closeButton', event: 'click' },
			//hideAfter: 1,
			width: 450, // We don't want the default 250px.
			               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
			hook: { target: 'bottomMiddle', tip: 'topRight' },
			stem: 'topRight',
		offset: { x: 6, y: -30 }
		});
}
function showTweetReply(tipid, userscreenname, in_reply_to_id){
	
	  new Tip('reply_' + tipid, {
		    title : 'reply to',
		    //delay: 0.5,
		    showOn: 'click',
			//target: 'link_' + tipid,
			ajax: {
				url: '/tweetwrite.jsp?action=reply&tipid=' + tipid + '&in_reply_to_id='+in_reply_to_id+'&in_reply_to_userscreenname='+userscreenname,
				options: {
					onComplete: function(transport) {
						// you could do something here after the ajax call is finished
		  checkStatusInputBox('_reply'+tipid);
					}
				}
			},
			hideOn: { element: 'closeButton', event: 'click' },
			//hideAfter: 1,
			width: 550, // We don't want the default 250px.
			               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
			hook: { target: 'bottomMiddle', tip: 'topRight' },
			stem: 'topRight',
		offset: { x: 6, y: -30 }
		});
}
function showTweetReTweet(tipid, userscreenname, statusMsg){
	  new Tip('rt_' + tipid, {
		    title : 'ReTweet this',
		    //delay: 0.5,
		    showOn: 'click',
		    hideOthers: true,
			//target: 'link_' + tipid,
			ajax: {
				url: '/tweetwrite.jsp?action=rt&tipid=' + tipid + '&in_reply_to_userscreenname=' + userscreenname + '&status='+statusMsg,
				options: {
					onComplete: function(transport) {
		  //$("#status_text").removeClass("status_text");
		  //$("#status-field-char-counter").removeClass("char-counter");
		  				checkStatusInputBox('_rt'+tipid);
					}
				}
			},
			hideOn: { element: 'closeButton', event: 'click' },
			//hideAfter: 1,
			width: 550, // We don't want the default 250px.
			               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
			hook: { target: 'bottomMiddle', tip: 'topRight' },
			stem: 'topRight',
		offset: { x: 6, y: -30 }
		});
}

function showDMBox(tipid, tousername){
	  new Tip('dm_' + tipid, {
		    title : 'reply to',
		    //delay: 0.5,
		    showOn: 'click',
			//target: 'link_' + tipid,
			ajax: {
				url: '/dmwrite.jsp?tipid=' + tipid + '&touser='+tousername,
				options: {
					onComplete: function(transport) {
						// you could do something here after the ajax call is finished
		  checkStatusInputBox('_reply'+tipid);
					}
				}
			},
			hideOn: { element: 'closeButton', event: 'click' },
			//hideAfter: 1,
			width: 550, // We don't want the default 250px.
			               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
			hook: { target: 'bottomMiddle', tip: 'topRight' },
			stem: 'topRight',
		offset: { x: 6, y: -30 }
		});
}
function tweetGeo(tipid, lat, lng){
	  new Tip('geo_'+tipid, {
		    title : 'Geo Location : ' + lat + "," + lng,
		    delay: 0.3,
		    //showOn: 'click',
			target: 'geo_'+tipid,
			ajax: {
				url: '/ajax_tweet_geo.jsp?lat='+lat+'&lng='+lng,
				options: {
					onComplete: function(transport) {
						// you could do something here after the ajax call is finished
		               //alert(document.getElementById("testid"));
		               //setTimeout("testEvent()",1000) 
		               var geo_canvas = "geo_"+lat+"_"+lng;
		               setTimeout("showMap('"+geo_canvas+"',"+lat+","+ lng+")",100) 
		               //setTimeout("showMap('geo_"+lat+"_"+lng+"',"+lat+","+ lng+")",100) 
		               //showMap("geo_"+lat+"_"+lng,lat, lng);
					}
				}
			},
			hideOn: { element: 'closeButton', event: 'click' },
			//hideAfter: 1,
			width: 300, // We don't want the default 250px.
			               // Images inside the tooltip will need to have dimensions set since Prototip needs to fixate width for proper rendering.
			hook: { target: 'bottomMiddle', tip: 'topLeft' },
			stem: 'topLeft'
			//offset: { x: 4, y: 0 }
		});
	}
//end of tooltips
function testEvent(lat, lng){
	//alert(document.getElementById("testid"));
	//showMap("geo_"+lat+"_"+lng,lat, lng);
	//showMap("testid", 37.5665350, 126.9779692);
}

///map

var tid;
function initTimer(lat, lng) {
    //tid=setInterval("newMapView("+lat+","+ lng+")",100);
	setTimeout("newMapView("+lat+","+ lng+")",100) 
}
function newMapView(lat, lng) {
	//clearInterval(tid);
	showMap("map_canvas", lat, lng);
	//markStatus(35.155227 + 0.1, 129.0586, "<img src='http://a3.twimg.com/profile_images/415969031/thumbnail_normal.jpg'> hello");
    
}

function showMap(eleid, lat, lng){
    var latlng = new google.maps.LatLng(lat, lng);
    var myOptions = {
  	      zoom: 16,
	      center: latlng,
	      mapTypeId:google.maps.MapTypeId.ROADMAP,
	      disableDefaultUI:true,
	      scrollwheel:false,
	      navigationControl:true
    };
    var map = new google.maps.Map(document.getElementById(eleid), myOptions);
    var marker = new google.maps.Marker({
        position: latlng, 
        map: map, 
        title:"Hello Searcus!"
    }); 
}
//end of map