	
		var tweetQueue = new Array();
		var zw='';
		var pushIntervalID = 0, popIntervalID = 0;
		var tweetsHovering = false;
		var URLRegex = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
		var MentionRegex = /(\W)(@)(\w+)(\W)/g;
		var searchTerm = []; // should be string, but initially array for first use
		var latestTweets = new Object;
		var searchScroll=null;
		var checkBoxesCount=0;
		var isPlaying=true;
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/twitterpause_off.png';
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/twitterplay_off.png';
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/fancy_closebox.png';
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/scroll_up_arrow_hover.png';
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/scroll_down_arrow_hover.png';
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/prevbutton_on.jpg';
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/nextbutton_on.jpg';
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/save_selection_on.png';
		new Image().src='http://thehamptonspy.com/design/hamptonsspy_des/images/blbtn_on.png';
		
		    function popTweet () {
			if (!tweetsHovering||true) {
			    var t = $('#tweets');
			    var tli = $('#tweets li:first');
			    var iHTML = tweetQueue.shift();
			    var lid = '';


			    if (iHTML != undefined) {
				lid = $(iHTML).attr('id');
				if (parseInt(t.css('margin-top')) <= 0) {
				    t.append(iHTML);
				    tli.animate({marginTop: "-90px"},1000,'swing',function() {
					$(this).remove();
				    });
				} else {
				    t.append(iHTML).animate({marginTop: "-=90px"},1000,'swing');
				}
			    }
			    if ((searchTerm.indexOf&&searchTerm.indexOf(' OR ')) != -1 && lid != '') {
				latestTweets[searchTerm] = lid;
				for (var s in latestTweets) {
				    latestTweets[s] = lid;
				}
			    } else if (lid != '') {
				latestTweets[searchTerm] = lid;
			    }
			}
		    }

		$(function(){
		    $('#CustomButton a').fancybox({
			hideOnContentClick:false,
			callbackOnShow:function(){
				$('#search_terms a[rel=keyword]').each(function(i,obj){
				   $('#fancy_div #KeywordPane input[type=checkbox]').each(function(i2,obj2){
					if($(obj2).attr('value')==$(obj).attr('title')){
					    $(obj2).attr('checked','true');
					    checkBoxesCount++;
					}
				    });	
				});
			    $('#fancy_content #KeywordPane input[type=checkbox]').click(function(){
				if($(this).attr('checked')){
				    if(checkBoxesCount<8)
					checkBoxesCount++;
				    else{
					alert('You can only choose up to 8 words/phrases');
					return false;
				    }
				}   
				else
				    checkBoxesCount--;	
			    });
			},
			frameWidth:644,
			frameHeight:301
		    });

		    $('#KeywordSave a').live('click',function(){
			var checkboxes=$('#fancy_div #KeywordPane input[type=checkbox]');
			var cookieArr=new Array();
			for(x in checkboxes){
			    if(checkboxes[x].checked)
				cookieArr.push(checkboxes[x].value);
			}
			if(cookieArr.length>0){
			    jQuery.cookie('my_keywords',cookieArr.join(','));
			    location.reload();
			}
			return false;
		    });

		    $('#KeywordClear a').live('click',function(){
			$('#fancy_div #KeywordPane input[type=checkbox]').removeAttr('checked');

			checkBoxesCount=0;

			return false;
		     });

		    $('#KeywordDefault a').live('click',function(){
			 var checkboxes=$('#fancy_div #KeywordPane input[type=checkbox]');
			 checkboxes.attr('checked',function(arr){
				if(jQuery.inArray(checkboxes[arr].value,['the hamptons','shelter island','montauk','east hampton','jitney','axe lounge','the candy kitchen'])>=0)
				return 'checked';

				return false;
			 });

			 checkBoxesCount=$('#fancy_div #KeywordPane input[type=checkbox][checked]').length;

			 return false;
		    });

		    $('#tweets').hover(function() {
			tweetsHovering = true;
			$('#nest').css({'border-color':'#EDFF25'});
		    }, function() {
			tweetsHovering = false;
			$('#nest').css({'border-color':''});
		    });

		    // atq => addToQuery
		    function atq (q,s) {
			s = s+'';
			if (s.length!=0 && s!='undefined' && q.length!=0) { return '&'+q+'='+encodeURIComponent(s); }
			else { return ''; }
		    }

		    function makeTime (d1, d2) {
			var t1 = d1.getTime()/1000, t2 = d2.getTime()/1000, tdiff = t2 - t1;
			var mins = Math.floor(((t2-t1) % 3600)/60), hrs = Math.floor(((t2-t1) % 86400)/3600), days = Math.floor((t2-t1) /86400);
			if (tdiff<120)
				return '&nbsp;&nbsp;-&nbsp;&nbsp;just now';

			if(tdiff<3600)
				return '&nbsp;&nbsp;-&nbsp;&nbsp;'+mins+' minutes ago';

			if (tdiff<86400)
			    	return '&nbsp;&nbsp;-&nbsp;&nbsp;'+hrs+' hour'+(hrs!=1?'s':'')+', '+mins+' minute'+(mins!=1?'s':'')+' ago';
			
			return '&nbsp;&nbsp;-&nbsp;&nbsp;'+days+' day'+(days!=1?'s':'')+' ago';
		    }

		    function pushTweets (url) {
			// only fill up the queue if we're in need of some more tweets, otherwise
			// just wait till the next time
			if (tweetQueue.length < 200) {
			    $('#QueryStatus').html('<img src="http://thehamptonspy.com/design/hamptonsspy_des/images/tweetloading.gif" />');
			    var grabURL=url||jsonURL();
			    $.getJSON(grabURL, function(data){

				$.each(data.results||data,function(key,item) {                    
				    if (item.to_user_id == null) {
					var patts = searchTerm.replace(/"/gi,'').split(' OR '), rex = new RegExp('','');
					var d8 = new Date(item.created_at), txt = item.text, profImage=item.profile_image_url||item.user.profile_image_url, username=item.from_user||item.user.screen_name;
					(new Image()).src=profImage;
					for (var m in patts) {
					    rex = new RegExp(patts[m],'gi');
					    txt = txt.replace(rex,'<em class="'+activeClass+'">$&</em>');
					    if (rex.test(txt)) { continue; }
					}
					txt = txt.replace(URLRegex,'<a href="$&" target="_blank">$&</a>'); // make links work
					txt = txt.replace(MentionRegex, '$1<a href="http://twitter.com/$3" target="_blank">$2$3</a>$4'); // link mentions to Twitter profiles
					if (username.toLowerCase()==highlightedUser.toLowerCase()) {
					    tweetQueue.push('<li id="'+item.id+'" rel="'+username+'" class="spy_tweet"><table cellspacing="0" cellborder="0" cellpadding="0"><tr><td class="textCell"><a href="http://twitter.com/'+username+'" target="_blank"><b>'+username+':</b></a>'+txt+'<span class="when">'+makeTime(d8,today)+'</span></td><td class="imgCell"><a href="http://twitter.com/'+username+'" target="_blank"><img src="'+profImage+'" width="48" height="48" /></a></td></tr></table></li>');
					} else {
					    tweetQueue.push('<li id="'+item.id+'" rel="'+username+'"><table cellspacing="0" cellborder="0" cellpadding="0"><tr><td class="textCell"><a href="http://twitter.com/'+username+'" target="_blank"><b>'+username+':</b></a>'+txt+'<span class="when">'+makeTime(d8,today)+'</span></td><td class="imgCell"><a href="http://twitter.com/'+username+'" target="_blank"><img src="'+profImage+'" width="48" height="48" /></a></td></tr></table></li>');
					}
				    }
				});
				if (data.max_id=="-1") {
				    // leave the latestTweet what it is
				} else {
				    // if all the search terms are being used, then set the global latestTweet
				    // if not, then only set the latestTweet for the search term, so that if you
				    //  click on another one, you won't miss older tweets
				    // (same code used below)
				    if (searchTerm.indexOf(' OR ') != -1) {
					latestTweets[searchTerm] = data.max_id;
					for (var s in latestTweets) {
					    latestTweets[s] = data.max_id;
					}
				    } else {
					latestTweets[searchTerm] = data.max_id;
				    }
				}
			    	
				setTimeout(function(){$('#QueryStatus').html('')},4300);

				//var loadedTweets=$('#tweets li');
				//if(loadedTweets.length==4)
				//    $(loadedTweets[3]).remove();

				if(popIntervalID==0)
					$('#PlayButton').click();
			
			    });
			    // EXPERIMENTAL: change speed of scrolling based on how many tweets are left, to keep
			    // continuous tweet flow
			    /*if (tweetQueue.length < 29) {
				clearInterval(popIntervalID);
				popIntervalID = window.setInterval(popTweet,60000/tweetQueue.length);
			    }*/
			}
		    }

		    // To see all the various ways to construct a twitter query, go to 
		    // http://search.twitter.com/advanced and play with all the combinations
		    var lang = 'en';
		    var numResults = '100'; // note: this is the MAX number of tweets, any @ replies will be removed, so may be less
		    var highlightedUser = 'garyvee';
		    var today = new Date();
		    var activeClass = '';
		    var jsonBaseURL = 'http://search.twitter.com/search.json';
		    //var jsonURL = function() { return jsonBaseURL + '?callback=?' + atq('lang',lang) + atq('rpp',numResults) + atq('q',searchTerm) + atq('since_id',latestTweets[searchTerm]); };
		    //Removed since_id so clicks always return latest *rpp* tweets
		    var jsonURL = function() { return jsonBaseURL + '?callback=?' + atq('lang',lang) + atq('rpp',numResults) + atq('q','hamptons '+searchTerm) ; };

		    $('#search_terms a[rel=keyword]').click(function() {
			if (searchTerm != '"'+$(this).attr('title')+'"'&&(!searchTerm.indexOf || searchTerm.indexOf(' OR ') == -1 || $(this).attr('title')!='all')) { 
			    var cl_old = $(this).attr('class'), cl_new = cl_old+'_on';
			    activeClass = cl_old;
			    tweetQueue = [];

			    if($(this).attr('title')=='all'){
			    	searchTerm=[];
		    		$('#search_terms a[title!=all][rel=keyword]').each(function() {
				    searchTerm.push( '"'+$(this).attr('title')+'"' );
		    		});

		    		searchTerm = searchTerm.join(' OR ');
			    }
			    else
			    	searchTerm = '"'+$(this).attr('title')+'"';

			    $('#CurrentStream').attr('className','twitter_color').addClass($(this).attr('className'));

			    pushTweets();

			    $('#search_terms a[rel=keyword]').each(function(){
				var cl = $(this).attr('class');
				if (cl.indexOf('_on') != -1) {
				    $(this).attr('class',cl.split('_on').join(''));
				}
			    });
			    $(this).attr('class',cl_new);
			    $('#tweets').attr('class',cl_new);

			    $('#CurrentStream').text($(this).text());
			}

			return false;
		    });

		    $('#search_terms a.username').click(function() {
			if ($(this).attr('title')) { 
			    var cl_old = $(this).attr('class'), cl_new = cl_old+'_on';
			    activeClass = cl_old;
			    tweetQueue = [];

			    searchTerm = '"'+$(this).attr('title')+'"';

			    var userURL='http://twitter.com/statuses/user_timeline/'+$(this).attr('title')+'.json?count=200&callback=?';

			    $('#CurrentStream').attr('className','twitter_color').addClass($(this).attr('className'));

			    pushTweets(userURL);

			    $('#search_terms a[rel=keyword]').each(function(){
				var cl = $(this).attr('class');
				if (cl.indexOf('_on') != -1) {
				    $(this).attr('class',cl.split('_on').join(''));
				}
			    });
			    
			    $(this).attr('class',cl_new);
			    $('#tweets').attr('class',cl_new);

			    $('#CurrentStream').text($(this).text());
			}

			return false;
		    });

		    $(document).ready(function(){
		    	$('#search_terms a:first[title!=all][rel=keyword]').click();


		    $('#PlayButton').click(function(){
			if(isPlaying)
				return $('#PauseButton').click();
			if(popIntervalID==0){
			    popTweet();
		   	    popIntervalID = window.setInterval(popTweet,2400);
			    $(this).attr('src','http://thehamptonspy.com/design/hamptonsspy_des/images/twitterplay_on.png');
			    $('#PauseButton').attr('src','http://thehamptonspy.com/design/hamptonsspy_des/images/twitterpause_off.png');
			}
			if(pushIntervalID==0){
		   	    pushIntervalID = window.setInterval(pushTweets,60000);
			}
			isPlaying=true;
		    });
		    $('#ScrollControl').click(function(){
			    $('#PlayButton').click();
		    });
		    $('#PauseButton').click(function(){
			if(!isPlaying)
				return $('#PlayButton').click();
			if(popIntervalID!=0){
			    clearInterval(popIntervalID);			    
			    $(this).attr('src','http://thehamptonspy.com/design/hamptonsspy_des/images/twitterpause_on.png');
			    $('#PlayButton').attr('src','http://thehamptonspy.com/design/hamptonsspy_des/images/twitterplay_off.png');
			    popIntervalID=0;
			}
			if(pushIntervalID!=0){
			    clearInterval(pushIntervalID);			    
			    pushIntervalID=0;
			}
			isPlaying=false;
		    });

		    });

		    popIntervalID = window.setInterval(popTweet,2400);
		    pushIntervalID = window.setInterval(pushTweets,60000);

		    $('#UpArrow').click(function(){
			$('#ScrollBarDiv').slider('value',$('#ScrollBarDiv').slider('value')+10);
		    });

		    $('#DownArrow').click(function(){
			$('#ScrollBarDiv').slider('value',$('#ScrollBarDiv').slider('value')-10);
		    });

		    $('#ScrollBarDiv').slider({
			orientation: 'vertical',
			value: 100,
			min: 0,
			max: 100,
			slide:function(event,ui){
			    var searchTermsDiv=$('#search_terms')[0];
			    var scrollValue=(100-ui.value)/100*(searchTermsDiv.scrollHeight-$(searchTermsDiv).height());
			    $(searchTermsDiv).scrollTop(scrollValue);
			},
			change:function(event,ui){
			    var searchTermsDiv=$('#search_terms')[0];
			    var scrollValue=(100-ui.value)/100*(searchTermsDiv.scrollHeight-$(searchTermsDiv).height());
			    $(searchTermsDiv).scrollTop(scrollValue);
			}			
		    });

		});
