var timer;

$(document).ready(function(){
    
    $('#updates').html('<div class="hide"></div>');
    $('#content').append('<span id="loading"><img src="images/misc/loading.gif" alt="Loading" /></span>');
    $('#loading').hide();
    
    // Open all links in a new window
    $('a[href^="http://"]').attr({
	target: "_blank", 
	title: "Opens in a new window"
    });
    
    // Hmmm, I wonder what this is...
    $(document).cheatCode({
	activated : function(){
	    $('<div class="large"><object width="618" height="371"><param name="movie" value="http://www.youtube-nocookie.com/v/0JyhSDbf7eQ&hl=en&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/0JyhSDbf7eQ&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="618" height="371"></embed></object></div>').insertBefore('#updates div:first').hide().slideDown('slow');
	}
    });
    
    getSearch();
    
});

function getSearch() {
    
    clearTimeout(timer);
    
    // Show loading image
    $('#loading').show();
    
    var query = urlencode('#wossybookclub'); // Twitter search term
    var rpp = 10; // Number of tweets to retrieve from json
    
    
    // Yes I know its messy I will sort out later I just wanted to get the site live
    $.getJSON('http://search.twitter.com/search.json?callback=?&q='+query+'&rpp='+rpp, function(data){
	
	$.each(data.results, function(i, item){
	    
	    // Create Links
	    var link_regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
	    var twt_text = item.text.replace(link_regexp,"<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$1</a>");
	    
	    // Create User Links
	    var user_regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
	    var twt_text = twt_text.replace(user_regexp,"<a href=\"http://twitter.com/$1\" target=\"_blank\" rel=\"nofollow\">@$1</a>");
	    
	    // Create Hashtag Links
	    var hash_regexp = /[\#]+([A-Za-z0-9-_]+)/gi;
	    var twt_text = twt_text.replace(hash_regexp,"<a href=\"http://search.twitter.com/search?q=&tag=$1\" target=\"_blank\" rel=\"nofollow\">#$1</a>");
	    
	    // If Wossy tweet add class to div
	    var wossyCode = "";
	    if (item.from_user == "Wossy" || item.from_user == "atwossybookclub") {
			wossyCode = 'class="wossy"';
	    }
	    
	    // Tweet Output
	    var newDiv = '<div id="'+item.id+'" '+wossyCode+'><a href="http://www.twitter.com/'+item.from_user+'" target="_blank" rel="nofollow"><img src="'+item.profile_image_url+'" /></a><p><a href="http://www.twitter.com/'+item.from_user+'" target="_blank" rel="nofollow" class="twitter_user">'+item.from_user+'</a> - '+twt_text+'</p></div>';
	    
	    // Check to see if first run
	    if ( $('.hide').length > 0 ) {
		
		$(newDiv).insertBefore('#updates div:last').hide().fadeIn(1500);
		
	    } else {
		
		// Check to see if tweet is already on page
		var woo = '#'+item.id;
		if ( $(woo).length == 0 ) {
		    //$('#updates').prepend(newDiv);
		    $(newDiv).insertBefore('#updates div:first').hide().animate( { opacity:0 }, 1).slideDown('slow').animate( { opacity:1 }, 1500);
		}
		
	    }
	    
	    // Remove tweets when number over 11
	    //if ( $("#updates").children().length > 50 ) {
	    //	$('#updates div:last-child').fadeOut().remove();
	    //}
	    
	    // Hide Loading image
	    $('#loading').hide();
	    
	});
	
	if ( $('.hide').length > 0 ) {
	    $('.hide').remove();
	}
	
    });
    

    
    timer = setTimeout('getSearch()', 10000);
    
}

function urlencode(str) {
    return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}
