queue = new Array();
totalall = 0;
page = 14;
function inc_all() {
    totalall = totalall + 1;
    $('.totalall').html(totalall);

}


message_ids = new Array();
$(document).ready(function () {



    go_now()
    $("body").everyTime(7000, function () {
        go_now()
    });



    $("body").everyTime(2500, function () {

        if (queue.length > 0) {

            entry2 = queue.shift();
 


   $.getJSON('http://api.jo.je/justgiving/jsonp.php?d=' + entry2[0] + '&callback=?', {},  function (data) {   	
	   
	if (data.error == undefined ) {
	   
	   donations =  "";
	   target = "";
	   if (data.donations[0] != undefined && data.percentage_complete != 0 && data.donations[0] .message != "")
	   {
		donations = "<p class='donate_info'><span>"  + data.donations[0].person + "</span> donated "  + data.donations[0].amount + " on <span>"  + data.donations[0].date + "</span></p><p class='donate_message'>"  + data.donations[0].message + "</p>";
	   }
	   
	   if (data.has_target && data.percentage_complete != 0)
	   {
		target = "<div class='target_info'>" + data.percentage_complete + "%</div>";
	   }
	   
	   $('.debug').prepend("<div style='display: none' class='reshow'><div class='left_box'><img src='" + entry2[1].profile_image_url + "' width='48px'><div class='twitter_info'><a href='http://twitter.com/" + entry2[1].from_user + "'>@" + entry2[1].from_user + "</a><p>" + entry2[1].text + "</p></div><img src='" + data.charity_logo + "' width='100px'><div class='charity_info'>" + data.charity_details + "</div></div><div class='right_box target'><h3>" + data.title + "</h3><h4>Raising for " + data.charity + "</h4><h4 class='person'>" + data.page_creator + " raised <span>" + data.donations_total + "</span></h4><h4 class='link'><a href='http://www.justgiving.com/" + entry2[0] + "'>www.justgiving.com/" + entry2[0] + "</a></h4>" + donations + target + "</div></div>");
	   
	   	   
            $('.reshow').slideDown('slow');
            if ($(".reshow").size() > 4) {

                $(".reshow:last").slideUp("slow", function () {
                    $(this).remove();
                });

            }
	    
	    }
	    
	     })  
	    
            
        }

    });

})




function go_now() {


    url = 'http://search.twitter.com/search.json?&page=' + page + '&rpp=100&q="%2Fjustgiving.com%2F"&callback=?'
   
    $.getJSON(url, function (data) {
        $.each(data.results, function (i, item) {
	
            get_urls(item);
          });
    });
    
    page = page - 1
    if (page == 0) page = 1;
}





function get_urls(item) {
    var reMatch = /((((ht|f)tp(s?))\:\/\/)|(www.))([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?/g;
	//alert(item.text);
    var strMatches = item.text.match(reMatch);
    if (strMatches == null) return [];
    // now output matches into report document
    for (j = 0; j < strMatches.length; j++) {
        strMatches[j] = strMatches[j].replace("http://", "");
        if (strMatches[j].replace("justgiving.com/", "") != strMatches[j]) {
            strMatches[j] = strMatches[j].replace("justgiving.com/", "");
            strMatches[j] = strMatches[j].replace("www.", "");
            strMatches[j] = strMatches[j].replace(".", "");
            strMatches[j] = strMatches[j].replace(")", "");
            strMatches[j] = strMatches[j].replace("?", "");
            strMatches[j] = strMatches[j].replace("/", "");
            if (!in_array(strMatches[j], message_ids)) {
                queue.push([strMatches[j], item]);
		//alert(strMatches[j]);
		message_ids.push(strMatches[j]);
                
            }
        }
    }



}










function in_array(needle, haystack, argStrict) {

    var key = '',
        strict = !! argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    return false;
}
