function tweetBuildQueue(type, queryid) {
    var tweetUrl;

    if (type == "moderator") {
        tweetUrl = "/site/services/twitter/twit.asmx/Search?queryID="+queryid+"&sinceID=1&all=false&approved=false&hide=false&asked=false&limit=25";
    }
    if (type == "approved") {
        tweetUrl = "/site/services/twitter/twit.asmx/Search?queryID=" + queryid + "&sinceID=1&all=false&approved=true&hide=false&asked=false&limit=25";
    }


    $.ajax({
        type: "GET",
        url: tweetUrl,
        cache: false,
        dataType: "xml",
        success: function(xml) {
            $(xml).find('Table1').each(function() {

                var t_id = $(this).find('StatusUpdateID').text();

                var t_pic = $(this).find('TwitterImage').text();
                var t_user = $(this).find('TwitterUserName').text();
                var t_date = $(this).find('TwitterDate').text();

                var t_StatusUpdate = $(this).find('StatusUpdate').text();

                t_StatusUpdate = t_StatusUpdate.replace('Q @PDNWebcast', '<span class="tweetQ">Q @PDNWebcast</span>');

                var t_html = '<div class="clear tweet" id="tweet_' + t_id + '">';
                t_html = t_html + '	<div class="column avatar big">';
                t_html = t_html + '		<a href="#"><img src="' + t_pic + '" alt="avatar" /></a>';
                t_html = t_html + '	</div>';

                t_html = t_html + '	<div class="column text">';
                t_html = t_html + '		<p>' + t_StatusUpdate + '</p>';

                t_html = t_html + '		<p class="info">' + t_date + ' | <a href="http://twitter.com/' + t_user + '" target="_new">' + t_user + '</a></p>';
                t_html = t_html + '	</div>';

                t_html = t_html + '	<div class="column approvalbuttons">';

                if (type == "moderator") {
                    t_html = t_html + '		<a href="#"><img onclick="tweetApprove(' + t_id + ')"	id="tApprove_' + t_id + '"	src="http://saddleback.com/site/provider/siteprovider/saddleback.com/templates/Saddleback%20Home%20Default/twitteradmin/btn_approve.gif" alt="Approve"></a><br>';
                }
                if (type == "approved") {
                    t_html = t_html + '		<a href="#"><img onclick="tweetAsk(' + t_id + ')"	id="tAsk_' + t_id + '"	src="http://saddleback.com/site/provider/siteprovider/saddleback.com/templates/Saddleback%20Home%20Default/twitteradmin/btn_asked.gif" alt="Asked"></a><br>';
                }

                t_html = t_html + '		<a href="#"><img onclick="tweetHide(' + t_id + ')"	id="tHide_' + t_id + '"		src="http://saddleback.com/site/provider/siteprovider/saddleback.com/templates/Saddleback%20Home%20Default/twitteradmin/btn_hide.gif" alt="Hide"></a>';
                t_html = t_html + '	</div>';
                t_html = t_html + '</div>';

                $('<div class="items" id="link_' + t_id + '"></div>').html(t_html).appendTo('#pdn_tweets');


            });
        }
    });

}


function tweetBuildList(target, type, queryid, limit) {
    if (limit == undefined) { limit = 25; }
    
    var tweetUrl;

    if (type == "show") {
        tweetUrl = "/site/services/twitter/twit.asmx/Search?queryID=" + queryid + "&sinceID=1&all=false&approved=true&hide=false&asked=false&limit=" + limit;
    }
    if (type == "feature") {
        tweetUrl = "/site/services/twitter/twit.asmx/Search?queryID=" + queryid + "&sinceID=1&all=false&approved=true&hide=false&asked=true&limit=" + limit;
    }
    if (type == "allunapproved") {
        tweetUrl = "/site/services/twitter/twit.asmx/Search?queryID=" + queryid + "&sinceID=1&all=false&approved=false&hide=false&asked=false&limit=" + limit;
    }

    $.ajax({
        type: "GET",
        url: tweetUrl,
        cache: false,
        dataType: "xml",
        success: function(xml) {
            $(xml).find('Table1').each(function() {

                var t_id = $(this).find('StatusUpdateID').text();

                var t_pic = $(this).find('TwitterImage').text();
                var t_user = $(this).find('TwitterUserName').text();
                var t_date = $(this).find('TwitterDate').text();

                var t_StatusUpdate = $(this).find('StatusUpdate').text();

                t_StatusUpdate = t_StatusUpdate.replace('Q @PDNWebcast', '<span class="tweetQ">Q @PDNWebcast</span>');

                var t_html = '<div class="clear tweet" id="tweet_' + t_id + '">';
                t_html = t_html + '	<div class="column avatar big">';
                t_html = t_html + '		<a href="#"><img src="' + t_pic + '" alt="avatar" /></a>';
                t_html = t_html + '	</div>';

                t_html = t_html + '	<div class="column text">';
                t_html = t_html + '		<p>' + t_StatusUpdate + '</p>';

                t_html = t_html + '		<p class="info">' + t_date + ' | <a href="http://twitter.com/' + t_user + '" target="_new">' + t_user + '</a></p>';
                t_html = t_html + '	</div>';

                t_html = t_html + '</div>';

                $('<div class="items" id="link_' + t_id + '"></div>').html(t_html).appendTo('#' + target);


            });
        }
    });

}

/* Moderating */
function tweetApprove(t_id) {

    $.ajax({
        url: "/site/services/twitter/twit.asmx/Approve?statusUpdateID=" + t_id,
        cache: false,
        success: function(html) {
            $("#tweet_" + t_id).slideUp("fast");
        }
    });
}

function tweetHide(t_id) {

    $.ajax({
        url: "/site/services/twitter/twit.asmx/Hide?statusUpdateID=" + t_id,
        cache: false,
        success: function(html) {
            $("#tweet_" + t_id).slideUp("fast");
        }
    });
}

function tweetAsk(t_id) {

    $.ajax({
        url: "/site/services/twitter/twit.asmx/Ask?statusUpdateID=" + t_id,
        cache: false,
        success: function(html) {
            $("#tweet_" + t_id).slideUp("fast");
        }
    });
}




/* Posting */
function checkoAuth() {
    var isoAuthed; 
    $.ajax({
        async: false,
        type: "GET",
        url: "/Site/Services/Twitter/twit.asmx/OAuthed?",
        cache: false,
        dataType: "xml",
        success: function(xml) {
            isoAuthed = $(xml).find('string').text();
        }
    });
    return isoAuthed === "True";
}

function tweetProcess(isoAuthed, defaulText) {

    var tweetFail = false;

    if (isoAuthed == false) {
        window.open("/site/services/twitter/oauth.aspx?", "Window1", "menubar=no,width=525,height=400,toolbar=no");
        tweetFail = true;
    }

    /*
    var tweetUserName = $("#tweet_username").val();
    if (tweetUserName == "" || tweetUserName == "Username") { alert("No Username Set"); tweetFail = true; } 
    */

    var tweetText = $("#tweet_text").val().replace('#','%23');
    if (tweetText == "") { alert("No Message Set"); tweetFail = true; }

    if (tweetFail != true) {
        $("#tweet_submit").attr("src", "/site/provider/siteprovider/saddleback.com/templates/saddleback home default/webcast/radicalis/btn_tweet-notactv.png");
        tweetSubmitOAuth(tweetText,defaulText,"",'#tweet_submit');
    }
}


function tweetResetInputs(defaulText) {
    $("#tweet_text").attr("value", defaulText);
}


function tweetSubmitOAuth(tweetText, defaulText, requiredText, submitButtonName) {

    $("#tweet_inputs").slideUp("fast");
    $("#tweet_sending").slideDown("fast");

    $.ajax({
        url: "/site/services/twitter/twit.asmx/PostOAuth?message=" + requiredText + tweetText,
        cache: false,
        success: function(html) {
            tweetResetInputs(defaulText)
            $("#tweet_sending").slideUp("fast");
            $("#tweet_sent").slideDown("fast");

            setTimeout(function() {
                $("#tweet_sent").fadeOut("fast");
                $("#tweet_inputs").slideDown("fast");

                $(submitButtonName).attr("src", "/site/provider/siteprovider/saddleback.com/templates/saddleback home default/webcast/radicalis/btn_tweet-actv.png");

            }, 2500);

            //$(submitButtonName).removeAttr("disabled"); 
        },
        error: function(xhr, desc, exceptionobj) {
            alert(xhr.responseText);
        }

    });

}

/*
function tweetSubmit(tweetUserName,tweetPassword,tweetText,submitButtonName) {

$("#tweet_inputs").slideUp("fast");	
$("#tweet_sending").slideDown("fast");

$.ajax({
url: "/site/services/twitter/twit.asmx/Post?username=" + tweetUserName +"&password=" + tweetPassword + "&message=Q @PDNWebcast " + tweetText,
cache: false,
success: function(html){
tweetResetInputs ()
$("#tweet_sending").slideUp("fast");
$("#tweet_sent").slideDown("fast");
	  	
setTimeout(function(){
$("#tweet_sent").fadeOut("fast");
$("#tweet_inputs").slideDown("fast");
			
$(submitButtonName).attr("src", "http://saddleback.com/site/provider/siteprovider/saddleback.com/templates/saddleback%20webcast%20default/webcast/pdn/btn_tweet.jpg");
				
},2500);

//$(submitButtonName).removeAttr("disabled"); 
},
error: function (xhr, desc, exceptionobj) {
alert(xhr.responseText);
}
	
});

}
*/

function limitChars(textid, limit, infodiv) {
    var text = $('#' + textid).val();
    var textlength = text.length;
    if (textlength > limit) {
        $('#' + infodiv).html('You cannot write more then ' + limit + ' characters!');
        $('#' + textid).val(text.substr(0, limit));
        return false;
    }
    else {
        $('#' + infodiv).html('You have ' + (limit - textlength) + ' characters left.');
        return true;
    }
}


