    var map, geocoder, searchControl, lastMarker; 

    var showAuthors = false;

    var showTexts = true;

    var scale=2; 

    var currentRadius = 10;

    function fAuthors(t)
    {
      showAuthors = !showAuthors;

      if (t=="top")
      {
        document.forms['mf'].c1.checked = document.forms['mf0'].c10.checked;
        document.forms['mf'].c2.checked = document.forms['mf0'].c20.checked;
      }
      else
      {
        document.forms['mf0'].c10.checked = document.forms['mf'].c1.checked;
        document.forms['mf0'].c20.checked = document.forms['mf'].c2.checked;
      }

      init();
    }

    function fTexts(t)
    {
      showTexts = !showTexts;

      if (t=="top")
      {
        document.forms['mf'].c1.checked = document.forms['mf0'].c10.checked;
        document.forms['mf'].c2.checked = document.forms['mf0'].c20.checked;
      }
      else
      {
        document.forms['mf0'].c10.checked = document.forms['mf'].c1.checked;
        document.forms['mf0'].c20.checked = document.forms['mf'].c2.checked;
      }

      init();
    }


    function init()
    {
         var count = 20;
         if (!showTexts) count+=8;

         var s ="http://search.twitter.com/search.json?lang=all&q=twitpic+OR+yfrog&rpp="+count;
         if (lat!='' && lng!='')
           s+="&geocode="+lat+"%2C"+lng+"%2C"+currentRadius+"mi";

         getTwitter(s+"&callback=results");
    }
      
    function load() 
    {
      if (GBrowserIsCompatible()) 
      {

        var opts = {
           googleBarOptions : {
           style : 'new',
           adsOptions : {
            client : 'partner-pub-9490590989861158'
           }
         }
        } 

        map = new google.maps.Map2(document.getElementById("map"),opts);
        geocoder = new google.maps.ClientGeocoder();

        map.addControl(new google.maps.LargeMapControl());
        map.addControl(new google.maps.MapTypeControl());
        map.enableGoogleBar();

        initMap(lat,lng,zml);

        map.setCenter(new google.maps.LatLng(eval(lat),  eval(lng)), eval(zml), google.maps.G_NORMAL_MAP);

        google.maps.Event.addListener(map,"moveend",function()
         {setCookie('gllat',map.getCenter().lat(),365);
          setCookie('glltd',map.getCenter().lng(),365);
          setCookie('glzml',map.getZoom(),365);});     

        google.maps.Event.addListener(map, "click", clickFunction);  

        var publisher_id = "pub-9490590989861158"; 
                       
        adsManagerOptions = {
        maxAdsOnMap : 1,
        style: 'adunit',
        channel: '' // This field is optional - replace with a channel number that you created for GooYAMLgle AdSense tracking
       };

       adsManager = new GAdsManager(map, publisher_id, adsManagerOptions);
       adsManager.enable(); 
      }

      // Create a search control
      //searchControl = new google.search.SearchControl();

      // Add in a full set of searchers
      //var localSearch = new google.search.LocalSearch();
      //searchControl.addSearcher(localSearch);
      //searchControl.addSearcher(new google.search.WebSearch());
      //searchControl.addSearcher(new google.search.NewsSearch());
      //searchControl.addSearcher(new google.search.ImageSearch());
      //searchControl.addSearcher(new google.search.VideoSearch());
      //searchControl.addSearcher(new google.search.BlogSearch());

      // Set the Local Search center point
      //localSearch.setCenterPoint(google.loader.ClientLocation.address.city+","+google.loader.ClientLocation.address.country);

     
      // Tell the searcher to draw itself and tell it where to attach
      //searchControl.draw(document.getElementById("searchcontrol"));
    }

     function getExpirydate( nodays)
      {
        var UTCstring;
        Today = new Date();
        nomilli=Date.parse(Today);
        Today.setTime(nomilli+nodays*24*60*60*1000);
        UTCstring = Today.toUTCString();
        return UTCstring;
      }

      function getCookie(cookiename) 
      {
      var cookiestring=""+document.cookie;
       var index1=cookiestring.indexOf(cookiename);
       if (index1==-1 || cookiename=="") return ""; 
       var index2=cookiestring.indexOf(';',index1);
       if (index2==-1) index2=cookiestring.length; 
       return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
      }

     function setCookie(name,value,duration)
     {
      cookiestring=name+"="+escape(value)+";EXPIRES="+getExpirydate(duration);
      document.cookie=cookiestring;
     }


     function initMap(_lat,_lng,_zml)
     {      
      if (_lat) lat = _lat;
      else 
       lat = getCookie('gllat');

      if (google.loader.ClientLocation)
	      if (lat.length==0) lat = google.loader.ClientLocation.latitude; 

      if (!lat) lat = 55.68455275165637;

      if (_lng) lng = _lng;
      else 
       lng = getCookie('glltd');

      if (google.loader.ClientLocation)
	      if (lng.length==0) lng = google.loader.ClientLocation.longitude;

      if (!lng) lng = 37.733917236328125;

      if (_zml) zml = _zml;
      else 
       zml = getCookie('glzml');

      if (zml.length==0) zml=8;
     }


    function clickFunction(overlay, latlng) 
    {
      if (latlng) 
      {
         var lat = latlng.lat();
         var lng = latlng.lng();

         var s ="http://search.twitter.com/search.json?lang=all&geocode="+lat+"%2C"+lng+"%2C"+"15mi"+"&rpp=20";
         document.getElementById("bottom").innerHTML="<div style='padding-top:30px;text-align:center' id='indicator'><img border='0' alt='in process ...' src='ajax_progress2.gif'></div>";
         getTwitter(s+"&callback=results");

         if (lastMarker) map.removeOverlay(lastMarker);
         var point = new GLatLng(lat, lng);
         lastMarker = new GMarker(point,{title:'See local faces'});
         map.addOverlay(lastMarker);         
      }

    }

    function results(t)
    {
       var v = t.results;

       if (!v)
       {
         document.getElementById("twitpic").innerHTML="<div class='error'>nobody writes here</div>";   
       }

       else
        if (v.length==0)
        {
          document.getElementById("twitpic").innerHTML="<div class='error'>nobody writes here</div>";   
        }
        else
        {
         var sText = "<table border='0'><tr>";  
         var max_id=-1;
         var count = 0;
         var columns = 2;

         if (!showTexts) columns = 4;

         for (var i=0; i<v.length; i++)
         { 
          var entry = v[i];
          var txt = entry.text;
          if (txt.indexOf("http://twitpic.com")>=0 || txt.indexOf("http://yfrog.com")>=0)
          {  
           if (count%columns == 0 && count!=0) sText+="</tr><tr>";

           if (entry.max_id>max_id) max_id=entry.max_id;
           sText +="<td valign='top'>"+getEntry(entry)+"</td>";
           count++;
          }

         }

         while (count%columns!=0)
         {
           sText+="<td>&nbsp;</td>"; 
           count++;
         } 

         sText+="</tr></table>"; 
         document.getElementById('twitpic').innerHTML=sText;

         if (document.getElementById('controls').style.display=='none') 
           document.getElementById('controls').style.display='block';

         if (document.getElementById('controls0').style.display=='none') 
           document.getElementById('controls0').style.display='block';
       } 
    }

    function getEntry(entry)
    {
       var sText = '';

           var sPic = proceedPics(entry.text);

           if (sPic!=entry.text) 
           {
             sText+=sPic; 
             sText += "<br/>";
           }

          if (showAuthors)
          {
           sText +="<a style='text-decoration:none' href='http://tinfo.linkstore.ru?u="+entry.from_user+"' target='_blank'><img src='"+entry.profile_image_url+"' style='float:left;padding-right:7px' border='0' width='48' height='48'/></a>"; 
          }
  
          if (showTexts)
          { 
           sText +=proceedUrl(entry.text) + "<br/>";
          
           sText+="<span style='font-size:80%;color:#555555'>"+dateDiff(new Date(entry.created_at), new Date());
 
          if (entry.from_user) 
             sText+=" by <a href='http://twitter.com/"+entry.from_user+"' target='_blank'>"+entry.from_user+"</a>";

           sText+="&nbsp;";
           sText+=" <a href='http://twitter.com/home?status=@"+entry.from_user+"&in_reply_to_status_id="+entry.id+"&in_reply_to="+entry.from_user
            +"' target='_blank' title='Reply to '"+entry.from_user+"'>Reply</a> "; 
           sText+="&nbsp;";
           sText+=" <a href='http://twitter.com/"+entry.from_user+"/statuses/"+entry.id
                 +"' target='_blank' title='Show tweet'>View tweet</a>";

           sText+="</span>";

          }

          sText+="<br/><br/><br/>";

           return sText;
    }

    function proceedUrl(text)
    {
        var tweet = text;
        var re = new RegExp("(([a-zA-Z]+:\/\/)([a-zA-Z][a-zA-Z0-9_\.-]*[a-zA-Z]{2,6})([a-zA-Z0-9~\#\/\._\?\&%-=]*[a-zA-Z0-9~\#\/_\?\&%-=]))", "g");
        tweet = tweet.replace(re, '<a style="' + 'text-decoration:none;" target="_new" href=$1 >$1</a>');
        re = new RegExp("@([a-zA-Z0-9_]+)", "g");
	tweet = tweet.replace(re, '@<a style="' + 'text-decoration:none;" target="_new" href=http://twitter.com/$1>$1</a>');
        tweet = tweet.replace(/&/g, '&');

        return tweet; 
    }

    function proceedPics(text)
    {
        var tweet = text;
     
        re = new RegExp("(http://twitpic.com/)([0-9a-zA-Z]*)");
	var m = re.exec(tweet);
	if  (!m || m.length == 0 || m[2] == "photos" || m[2] == "photo" || m[2] == "tag") {} 
	else {tweet = '<center><a href="http://twitpic.com/' + m[2] + '" target="_blank"><img src="http://twitpic.com/show/thumb/' + m[2] + '" border=0 height=150 width=150></a></center>';}	

        re = new RegExp("(http://yfrog.com/)([0-9a-zA-Z]*)");
	var m = re.exec(tweet);
	if  (!m || m.length == 0) {} 
	else {tweet = '<center><a href="http://yfrog.com/' + m[2] + '" target="_blank"><img src="http://yfrog.com/' + m[2] + '.th.jpg" border=0 height=150 width=150></a></center>';}	

      return tweet;
    }

    function processText(_s)
    {
      var s = _s; 

      var re = new RegExp("(([a-zA-Z]+:\/\/)([a-zA-Z][a-zA-Z0-9_\.-]*[a-zA-Z]{2,6})([a-zA-Z0-9~\#\/\._\?\&%-=]*[a-zA-Z0-9~\#\/_\?\&%-=]))", "g");
      s =  s.replace(re, ' ');
       
      re = new RegExp("[0-9]","g");
      s =  s.replace(re, ' ');

      re = new RegExp('["«»]','g');
      s =  s.replace(re, ' ');

      re = new RegExp('&lt;','g');
      s =  s.replace(re, ' ');

      re = new RegExp('&gt;','g');
      s =  s.replace(re, ' ');


      re = new RegExp('&quot;','g');
      s =  s.replace(re, ' ');

      re = new RegExp("[-+>=~<():,@.\t'\"{}%!?\/\\\\]","g");
      s =  s.replace(re, ' ');

      var elems = s.split(" ");
      var cloud=[];
      var tag;
      var max = 0; 
      var res;
              
      for (var i=0; i<elems.length; i++)
      {
       tag = elems[i].toLowerCase();
       if (tag.length>3)
       {
        res = -1;

        for (var j=0; j<cloud.length; j++)
          if (cloud[j].el==tag)
           {
               res = j;
               j = cloud.length+1;               
           }


           if (res<0)
           {
             cloud[cloud.length] = { count: 1, el: tag };
             max = Math.max(1, max);
           }
           else 
           {
             cloud[res].count += 1;
             max = Math.max(cloud[res].count, max);
           }

       }
          
    }
      
    s='';    
    for (var i = 0; i < cloud.length; i++) {
      if (cloud[i].count>=2)
       s+='<a class="link1" style="font-size:' + Math.ceil((cloud[i].count / max) * scale) + 'em" href="http://search.twitter.com/search?q='+cloud[i].el+'" target="_blank">' + cloud[i].el + '</a> &nbsp; ';
      else
       s+='<span style="color:blue;font-size:' + Math.ceil((cloud[i].count / max) * scale) + 'em">' + cloud[i].el + '</span> &nbsp; ';
    }

    return s;  

    }

    function getTwitter(query)
    {
      var cbscript = document.createElement("script");
      cbscript.src = query;
      cbscript.type = 'text/javascript';
      document.body.appendChild(cbscript);
    }

   function dateDiff(d1, d2)
   {
     var diff  = new Date();

     diff.setTime(Math.abs(d1.getTime() - d2.getTime()));

     var timediff = diff.getTime();

     var weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
     timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

     var days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
     timediff -= days * (1000 * 60 * 60 * 24);

     var hours = Math.floor(timediff / (1000 * 60 * 60)); 
     timediff -= hours * (1000 * 60 * 60);

     var mins = Math.floor(timediff / (1000 * 60)); 
     timediff -= mins * (1000 * 60);

     var secs = Math.floor(timediff / 1000); 
     timediff -= secs * 1000;

     var ans ="";
     if (weeks!=0) ans+=weeks+" weeks ";

     if (days!=0)  ans+=days+" days ";

     if (hours!=0) ans+=hours+" hours ";
     else
     {
       if (mins!=0)  ans+=mins+" mins ";   
       else
          ans += secs +" secs "    
     }

     return ans+"ago";
   }

  