// JavaScript Document
cat1 = { num:0,catNum:"cat1",color: "", cat: "", searchText:"", twitCount: 0, twitterResults: [] }
cat2 = { num:1,catNum:"cat2",color: "", cat: "", searchText:"", twitCount: 0, twitterResults: [] }
cat3 = { num:2,catNum:"cat3",color: "", cat: "", searchText:"", twitCount: 0, twitterResults: [] }
cat4 = { num:3,catNum:"cat4",color: "", cat: "",searchText:"", twitCount: 0, twitterResults: [] }
cat5 = { num:4,catNum:"cat5",color: "", cat: "",searchText:"", twitCount: 0, twitterResults: [] }
cat6 = { num:5,catNum:"cat6",color: "", cat: "",searchText:"", twitCount: 0, twitterResults: [] }
var mainArray = [cat1, cat2, cat3, cat4,cat5,cat6]; 
var category = 0;
var twitCat = 0;
var newsCat = 0;
var badWords=[];
var totalCount=0;

function getValues(){
	for (var i=0;i<mainArray.length;i++){
	mainArray[i].color=$("#"+mainArray[i].catNum).attr("color");
	mainArray[i].searchText=escape($("#"+mainArray[i].catNum).attr("search"));
	mainArray[i].cat=$("#"+mainArray[i].catNum).text();
	
	}
}

$(document).ready(function() {

    getValues();
    getTwitFeeds();
	getBadWords();
	getAndDisplayNews();
});
function getAndDisplayNews(){
	
$("#tweetDisplay a").click(function(){pageTracker._trackPageview("/twiiterTweet/")});

	//get and display news
	$.ajax({
		   type:"GET",
		   url:"http://tools.10thousanddoors.org/data/pull.aspx?zone=19&max=20&type=0&cb=?",
		   dataType:"jsonp",
		   success: function(data, textStatus){
			  $("#newsDisplay div.loader").hide();
			  $.each(data.value.items, function(i,item){
					var text = item.description;
					var title = item.title;
					var byline="";
					if(item.byline){byline="<br /><span class='byline'>-"+item.byline+"</span>"}
					$('#newsDisplay').append('<div id="newsPiece'+i+'" class="nowStory"><div class="block'+i+'"><span class="storyTitle"><a target="_blank" href="'+item.link+'" >'+title+'</a></span></div><div class="block'+i+'">'+text.substring(0,120)+'...'+byline+'<br /></div></div>');
				 if (i==19) return false;
				});			  
 		    $(".nowStory:nth-child(odd)").addClass("nowStoryZebra");
		   },
		   error: function(XMLHttpRequest, textStatus, errorThrown){
		   $("#newsDisplay div.loader").hide();
		   $('#newsDisplay').append('<div id="newsPiece" class="nowStory"><div><span class="storyTitle">Our aplogy.</span></div><div>We are aware of the error you\'ve just encountered and should have it resolved shortly. In the meantime, try refreshing your browser. <br />-The 10thousanddoors.org Team</div>');
		   }
		});	
}


function getTwitFeeds() {
	var text;
    var cat = mainArray[twitCat].searchText;
    $.ajax({
        type: "GET",
        url: "http://search.twitter.com/search.json?callback=?&lang=en&rpp=50&q=" + cat,
        dataType: "jsonp",
        success: function(data, textStatus) {

            $.each(data.results, function(i, item) {
				text=makeAnchors(item.text);
                result = {title: text}
                mainArray[twitCat].twitterResults[i] = result;
            });
			
			$("#"+mainArray[twitCat].catNum).animate({color:mainArray[twitCat].color});
			var position=twitCat;
			$("#"+mainArray[position].catNum).click(function() { category = mainArray[position].num; $(".selected").animate({ fontSize: "20px",marginTop:"0px" }).removeClass("selected"); $(this).animate({ fontSize: "30px", marginTop: "-7px" }, 800).addClass("selected"); });
			if(twitCat==0){displayFeed();$("#feed .loader").remove();}

				
		
			if (twitCat < mainArray.length - 1) { twitCat++; getTwitFeeds(); }
        }
    });
}

function displayFeed() {

    	var title = mainArray[category].twitterResults[mainArray[category].twitCount].title;
        mainArray[category].twitCount++;
    
	if(mainArray[category].twitterResults.length-mainArray[category].twitCount==10){grabMore(mainArray[category].num);}
	if(mainArray[category].twitterResults.length-mainArray[category].twitCount==5){resetCat(mainArray[category].num);}
	
    var regExFirst = new RegExp(mainArray[category].cat, "ig");
	var regEx = new RegExp("<span style", "i");
    title = title.replace(regExFirst, "<span style='color:" + mainArray[category].color + "'>" + mainArray[category].cat + "</span>");
	title = title.replace(regEx, "<br /><span style");
	title=filterBadWords(title);
	
    $("#feed").prepend('<div class="post">' + title + '</div>');
	if(isEven(totalCount)){$("#feed div:first").addClass("zebra");}
	totalCount++;
    $("#feed div:first").animate({ marginTop: "0px", marginBottom: "20px",marginTop:"10px" }, 750);
	$("#feed div:eq(10)").remove();
    setTimeout(function() { displayFeed(); }, 4500);
}
function resetCat(localCategory){
mainArray[localCategory].twitterResults=[];
mainArray[localCategory].twitCount=0;
grabMore(localCategory);
}

function grabMore(localCategory){
	var text;
	var cat = mainArray[localCategory].searchText;
	var page=Math.round((mainArray[localCategory].twitterResults.length/50)+1);
    
    $.ajax({
        type: "GET",
        url: "http://search.twitter.com/search.json?callback=?&lang=en&rpp=50&q="+cat+"&page="+page,
        dataType: "jsonp",
        success: function(data, textStatus) {

            $.each(data.results, function(i, item) {
				text=makeAnchors(item.text);
                result = {
                    title: text
                }
                mainArray[localCategory].twitterResults.push(result);

            });
        }
		
    });	
}

function filterBadWords(text){
	for(var i=0;i<badWords.length;i++){	
		text = text.replace(new RegExp(badWords[i], 'gi' ),"****");
	}
	return text;
}
function makeAnchors(text){
	text = text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, "<a target='_blank' href='$1'>$1</a>");
	return text;
}

function getBadWords(){
    $.ajax({
        type: "GET",
        url: "http://www.10thousanddoors.org/atf/cf/%7B84649513-0f80-42be-ab73-e2a9135e2bfa%7D/WORDS.JS",
        dataType: "json",
        success: function(data, textStatus) {
            $.each(data.words, function(i, item) {
			badWords[i]=item;
            });

        }
    });
}

function isEven(num){
return !(num % 2);	
}