
$(document).ready(function() {
	$("#twubSearchBar_SearchBar").focus(function() {
	    if ($(this).val() == "Enter a Hashtag") {
		     $(this).val("").removeClass("twubSearchBar_searchTextBoxLight");
	    }
	    else {
		    $(this).select();
	    }
    });
	
	$("#twubSearchBar_SearchBar").blur(function() {
			if ($.trim($(this).val()) == "") {
			    $(this).val("Enter a Hashtag").addClass("twubSearchBar_searchTextBoxLight");
			}
		}
	);
	
    $("#twubSearchBar_SearchBar").keypress(function(e) {
    	// enter
    	if (e.which == 13)
        	twubsSearch();
    });
    
    $("#twubSearchBar_searchGoButton").click(function(e) {
        twubsSearch();
    });
    
    $("#twubSearchBar_searchArea").change(function() { $("#twubSearchBar_SearchBar").focus(); } );
    
        $("#twubSearchBar_SearchBar").autocomplete("http://twubs.com/ajax360/5-720896/searchTwubs", {
        delay:10,
		minChars:2,
		autoFill:false,
		maxItemsToShow:10,
		matchContains:1,
		matchSubset:10,
		onItemSelect:function() {
		    twubsSearch();
		}
	});
    });

function twubsSearch()
{
    	    var twubName = $("#twubSearchBar_SearchBar").val();
    	
	    $.ajax({
		    type: 'POST', 
		    url: 'http://twubs.com/ajax360/5-720896/',
		    data: 'searchArea=&searchText='+twubName,
            dataType: 'text',
            success: function (data) {
                window.location = data;
                // $("#").trigger('Update',[data]);
		    }
	    });
    }
$(document).ready(function() {
	$('#login_username').focus(function() {
		if ($('#login_username').val() == 'Username' || $('#login_username').val() == 'Email')
			$('#login_username').val('');
	});
    
    $('#login_password').focus(function() {
        $('#login_password').select();
    });
	
    $("#login_loginSubmit").click(function() {
        login();
    });
    
    $("#login_username").keypress(function(e) {
        if (e.which == 13) login();
    });
    
    $("#login_password").keypress(function(e) {
        if (e.which == 13) login();
    });
    
    $("#login_signinButtonClosed").click(function() {
        $(".signinOverlay").show();
        $("#login_signinButtonOpen").fadeIn("medium");
        $("#login_loginArea").fadeIn("medium");
    });
    
    $("#login_close").click(function() {
        $(".signinOverlay").hide();
        $("#login_signinButtonOpen").fadeOut("medium");
        $("#login_loginArea").fadeOut("medium");
    });

    $(".signinOverlay").click(function() {
        $(".signinOverlay").hide();
        $("#login_signinButtonOpen").fadeOut("medium");
        $("#login_loginArea").fadeOut("medium");
    });

});

// var i = 1;
function login()
{
    var rememberMe = 0;
    if($('#login_rememberMe').attr('checked')) {
        rememberMe = 1;
    }

	$.ajax({
		type: 'POST', 
		url: 'http://twubs.com/ajax360/255873-6750208/',
		data: 'username='+$('#login_username').val()+'&password='+$('#login_password').val()+'&rememberMe='+rememberMe,
		complete: function (data) {
			// $('#login_results').html(data.responseText +' - '+ i);
			
			// authentication successful
			if (data.responseText.match("Success")) {
				window.location = 'http://twubs.com/pk2upv';
			}
            else {
                $('#login_results').html(data.responseText);
                setTimeout(function() {
                        $('#login_results').html($('#login_connectLinks').html());
                    },
                    3000);
            }
		}
	});
}

$(document).ready(function() {
	$("#8_917504").unbind('Update').bind('Update', function(e, data) {
	    var parts = data.split("<<<embedpageseperator>>>");
	    $('#' + $(this).attr("id") + '_head').empty().html(parts[0]);
	    $('#' + $(this).attr("id") + '_body').empty().html(parts[1]);
	});
});
$(function() {
    $("#ajaxSpinner").
    ajaxStart(function() {
        $(this).show();
    }).
    ajaxSuccess(function() {
        $(this).hide();
    }).
    ajaxStop(function() {
        $(this).hide();
    }).
    ajaxError(function() {
        $(this).hide();
    });
});

$(document).ready(function() {
	$("#21_917504").unbind('Update').bind('Update', function(e, data) {
	    var parts = data.split("<<<embedpageseperator>>>");
	    $('#' + $(this).attr("id") + '_head').empty().html(parts[0]);
	    $('#' + $(this).attr("id") + '_body').empty().html(parts[1]);
	});
});
$(document).ready(function(){
	$('#adsenseFeed_edit').click(function(){
		$.post('http://twubs.com/ajax360/212118-6684672/',{action:'edit',prefix:$('#adsenseFeed_prefix').val()},function(data){
			$('#adsenseFeed_container').html(data);
		});
	});
});

// document.domain = 'twubs.com';

/**
 * Class for managing the RichText
 */
function RichText(setResourceId, setAjaxUrl)
{
    var _this = this;
    
    // Set initial vars
    _this.resourceId = setResourceId;
    _this.ajaxUrl = setAjaxUrl;
    
    // Grab the DOM nodes
    _this.$container = $('.RichText_container_' + _this.resourceId);
    _this.$innerContainer = $('.RichText_innerContainer', _this.$container);
}

// *** DEFINE ANY METHODS FOR THIS CLASS *** */
RichText.prototype = {
    
  view_init: function()
  {
    var _this = this;
    
    // Grab local DOM nodes
    _this.$view_editIcon = $('.RichText_View_editIcon', _this.$container);
    _this.$view_spinner = $('.RichText_View_spinner', _this.$container);
    
    // Bind event listeners
    _this.$view_editIcon.bind('click', {_this:_this}, _this.view_editIconClick);
  },
    
    /**
     * Triggered after user clicks on the edit icon
     */
    view_editIconClick: function(e)
    {
        var _this = e.data._this;
        
        // Hide the edit icon and set the spinner a'spinnin'
        _this.$view_editIcon.hide();
        _this.$view_spinner.show();
        
        // Configure the ajax call to load the edit pane
        var ajaxOptions = {
            url: _this.ajaxUrl + 'getEditPane',
            type: 'get',
            dataType: 'html',
            success: function(data)
            {
                // Load the returned html into the container
                _this.$innerContainer.html(data);
            }
        };
        
        // Fire the ajax
        $.ajax(ajaxOptions);
    },
    
    /**
     * Initialize the loaded edit pane
     */
    edit_init: function()
    {
        var _this = this;
        
        // Grab DOM nodes
        _this.$edit_editor = $('.RichText_Edit_editor', _this.$container);
        
        _this.$edit_imageForm = $('.RichText_Edit_imageForm', _this.$container);
        
        _this.$edit_spinner = $('.RichText_Edit_spinner', _this.$container);
        _this.$edit_save = $('.RichText_Edit_save', _this.$container);
        _this.$edit_saveAndExit = $('.RichText_Edit_saveAndExit', _this.$container);
        
        // Initialize the ajax form for uploading images
        _this.$edit_imageForm.ajaxForm({
            success:function(data)
            {
                // Take what was returned and add it into the editor
                _this.$edit_editor.tinymce().execCommand('mceInsertContent', false, data);
            }
        });
        
        // Initialize tinymce on the editor
        _this.$edit_editor.tinymce({
            // Set the script location
            script_url: '/static/js/tiny_mce/tiny_mce.js',
            // Theme and plugins
            theme: "advanced",
            plugins: "safari,pagebreak,advlink,advimage,style,inlinepopups,paste,fullscreen,visualchars,nonbreaking,table",
            theme_advanced_buttons1: "undo,redo,|,formatselect,|,bold,italic,underline,strikethrough,forecolor,backcolor,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink,|,image,|,code,fullscreen",
            theme_advanced_buttons2: "tablecontrols,|,bullist,numlist",
            theme_advanced_buttons3: "",
            theme_advanced_toolbar_location : "external",
            theme_advanced_toolbar_align: "left",
            theme_advanced_statusbar_location: "bottom",
            theme_advanced_resizing: true,
            // Configuration
            relative_urls: false,
            remove_script_host: false,
            auto_cleanup_word: true,
            extended_valid_elements: "iframe[src|width|height|name|align|frameborder|scrolling|allowtransparency]"
        });
        
        // Bind event listeners
        _this.$edit_save.bind('click', {_this:_this}, _this.edit_saveClick);
        _this.$edit_saveAndExit.bind('click', {_this:_this}, _this.edit_saveAndExitClick);
    },
    
    /**
     * User clicked on the save button while editing
     */
    edit_saveClick: function(e)
    {
        var _this = e.data._this;
        
        // Trigger TinyMCE to save
        tinyMCE.triggerSave();
        
        // Set the spinner a-spinnin'
        _this.$edit_spinner.show();
        
        // Configure the ajax request to save the content
        var ajaxOptions = {
            url: _this.ajaxUrl + 'save',
            type: 'post',
            async: false,
            data: {
                content: _this.$edit_editor.val()
            },
            success: function()
            {
                // We completed, so hide the spinner
                _this.$edit_spinner.hide();
            }
        };
        
        // Fire the AJAX request to save
        $.ajax(ajaxOptions);
    },
    
    /**
     * User clicked on the save and exit button
     */
    edit_saveAndExitClick: function(e)
    {
        var _this = e.data._this;
        
        // Simulate someone clicking on the save button
        _this.$edit_save.click();
        
        // Set the spinner a-spinnin'
        _this.$edit_spinner.show();
        
        // Configure an ajax request to load the view pane
        var ajaxOptions = {
            url: _this.ajaxUrl + 'getViewPane',
            type: 'get',
            dataType: 'html',
            success: function(data) {
                // Load the returned html into the container
                _this.$innerContainer.html(data);
            }
        };
        
        // Trigger the ajax request
        $.ajax(ajaxOptions);
    }
};
$(document).ready(function()
{
	RichText_277691_7536640.view_init();
});

function TweetThisTwub(setPrefix) {
    // Set our module prefix
    this.prefix = setPrefix;
    
    // For uploading images -- if the user asked for a submit, we must wait for the image to finish, and then we need to submit the comment
    this.submit = false;
    this.sending = false; // Are we in the middle of sending already
    
    // Grab our DOM elements
    this.$form = $("#" + this.prefix + "tweetThisTwub");
    this.$charsLeft = $("#" + this.prefix + "charsLeft", this.$form);
    this.$contain = $("#" + this.prefix + "attachItemsContainer");
    
    this.$twubField = $("#" + this.prefix + "twub", this.$form);
    this.$replyStatusID = $("#" + this.prefix + "replyStatusID", this.$form);
    this.$textArea = $("#" + this.prefix + "textArea", this.$form);
    this.$sendTweet = $("#" + this.prefix + "sendTweet", this.$container);
    this.$sendTweetSpinner = $("#" + this.prefix + "sendTweetSpinner", this.$container);
    
    this.$previewContainer = $("#" + this.prefix + "previewContainer", this.$form);
    this.$previewText = $("#" + this.prefix + "previewText", this.$form);
    this.$previewItems = $("#" + this.prefix + "previewItems", this.$form);
    
    this.$attachPhoto = $("#" + this.prefix + "attachPhoto", this.$contain);
    this.$attachVideo = $("#" + this.prefix + "attachVideo", this.$contain);
    this.$attachLink = $("#" + this.prefix + "attachLink", this.$contain);
    
    this.$attachPhotoContainer = $("#" + this.prefix + "attachPhotoContainer", this.$contain);
    this.$attachVideoContainer = $("#" + this.prefix + "attachVideoContainer", this.$contain);
    this.$attachLinkContainer = $("#" + this.prefix + "attachLinkContainer", this.$contain);
    
    this.$attachmentInfo = $("#" + this.prefix + "attachmentInfo", this.$contain);
    
    // Grab any initial fields from the DOM elements
    this.twub = this.$twubField.val();
    
    // Bind our events
            this.$textArea.unbind('keyup').bind('keyup', {'ttt':this}, this.disable);
        this.$textArea.unbind('focus').bind('focus', {'ttt':this}, this.disable);
        
        this.$attachPhoto.unbind('click').bind('click', {'ttt':this}, this.disable);
        this.$attachVideo.unbind('click').bind('click', {'ttt':this}, this.disable);
        this.$attachLink.unbind('click').bind('click', {'ttt':this}, this.disable);
    
    // Initialize our items to nothing
    this.items = [];
    this.defaultItems = [];
}

TweetThisTwub.prototype = {
    update:function(e) {
        if(e != null && e.data != null && e.data.ttt != null) {
            var ttt = e.data.ttt;
        } else {
            var ttt = this;
        }
                
        ttt.openPreview();
        
        // Grab the tweet from the text area
        var tweet = ttt.$textArea.val();
        
        // Update the preview text
        ttt.$previewText.text(tweet);
        
        // Update the character count
        var charsLeft = 140 - tweet.length;
        
                // Loop through the items and subtract their text length + 1 for space
        $.each(ttt.items, function() {
            charsLeft -= this.text.length + 1;
        });
                
        // Update the characters left
        ttt.$charsLeft.text(charsLeft);
        
        // Change the color if we get close to the end
        ttt.$charsLeft.removeClass('charLimitNotice').removeClass('charLimitWarning');
        ttt.$sendTweet.removeClass('grayButtonDisabled');
        
        if(charsLeft < 0) {
            ttt.$sendTweet.addClass('grayButtonDisabled');
        }
        

        if(charsLeft < 10) { 
    	    ttt.$charsLeft.addClass('charLimitWarning'); 
    	} else if(charsLeft < 20) {
    	    ttt.$charsLeft.addClass('charLimitNotice');
    	}
    	
    	if (ttt.submit)
            ttt.$sendTweet.click();
        
    },
    disable:function(e) {
        if(e != null) {
            var ttt = e.data.ttt;
        } else {
            var ttt = this;
        }
        
        ttt.$textArea.attr("disabled", true).val('Please sign in...');
    	ttt.$sendTweet.addClass('grayButtonDisabled').find('.grayButtonContent').text('Please sign in');
    	
    	ttt.$attachPhoto.find("a").css("color", "#A0A0A0");
    	ttt.$attachVideo.find("a").css("color", "#A0A0A0");
    	ttt.$attachLink.find("a").css("color", "#A0A0A0");
    },
    sendTweet:function(e) {
        var ttt = e.data.ttt;
        
        // Already trying to send
        if(ttt.sending) { return; }
        
                    $("#ajaxSpinner").trigger("ajaxStart");
            
                ttt.disable();
        return;
                
        if (ttt.$attachLinkContainer.css("display") == "block") {
            ttt.submit = true;
            ttt.tryAttachLink(e);
            return false;
        }
        if (ttt.$attachVideoContainer.css("display") == "block") {
            ttt.submit = true;
            ttt.tryAttachVideo(e);
            return false;
        }
        //need to wait for upload to finish before continuing...this call is async
        if (ttt.$attachPhotoContainer.css("display") == "block") {
            ttt.submit = true;
            ttt.$attachPhotoContainer.find("form").submit();
            return false;
        }
        
        // Grab the status
    	var status = ttt.$textArea.val();
    	
    	// Check for an empty
    	if(status.length == 0 && !confirm("Are you sure you want to send a Tweet with an empty message?")) {
    	    return false;
    	}
    	
    	// Loop through the items and add their text
    	    	$.each(ttt.items, function() {
    	    status += ' ' + this.text;
    	});
    	    	
    	if(status.length > 140) {
    	    alert("Your update is over 140 characters and was not submitted.");
    	    return false;
    	}
    	
    	ttt.sending = true;
        ttt.$sendTweet.addClass("grayButtonDisabled");
        ttt.$sendTweetSpinner.show();
    	
    	var replyStatusID = ttt.$replyStatusID.val();
    	
    	var ajaxOptions = {
    	    'url': 'http://twubs.com/ajax360/31-1966080/sendTweet',
    	    'type': 'post',
    	    'dataType':'json',
    	    'data': {
        	    'func': 'sendTweet',
        	    'replyStatusID': replyStatusID,
        	    'status': status
    	    },
    	    'complete':function() {
    	        ttt.sending = false;
                ttt.$sendTweet.removeClass("grayButtonDisabled");
                ttt.$sendTweetSpinner.hide();
    	    },
    	    'success':function(data) {
    	        switch(data.results) {
    	            case 'success':
    	                ttt.resetForm();  	                   
    	                ttt.setAttachmentInfo('<div class="successBox" style="margin:5px 0;">Your Tweet was posted successfully and will show up in the feed shortly.</div>');
    	                
    	                    	                
    	                break;
    	            case 'message':
    	                ttt.setAttachmentInfo('<div class="errorBox" style="margin:5px 0;">' + data.message + ' Perhaps Twitter is not responding :(</div>');
    	                break;
    	        }
    	    }
    	};
    	
    	ttt.submit = false;
    	
        $.ajax(ajaxOptions);
        return false;
    },
    reply:function(username, statusID, mode) {
        window.location.hash = this.prefix;

        var prefixChar = '@';
        switch (mode) {
    	    case 'RT':
    	    case 'DM':
    	    	prefixChar = '';
    	    	break;
    	    default:
    	    	prefixChar = '@';
        		break;
        }

        this.$textArea.val(prefixChar + username).focus();
        this.$replyStatusID.val(statusID);
        
        this.items = this.defaultItems.slice();
        this.redrawItems();
        
        this.update();
    },
    preload:function(text) {
        window.location.hash = '#';
        
        this.$textArea.val(text).focus();
        
        this.flash();
        this.update();
    },
    flash:function() {
        var ttt = this.$textArea;
        
        ttt.animate({ backgroundColor: "#ffebc6" }, 1200, 'linear', function() {
        	ttt.animate({ backgroundColor: "#ffffff" }, 1200)
        });
    },
    resetForm:function() {
        this.$textArea.val('');
        this.$replyStatusID.val('');
        
        this.items = this.defaultItems.slice();
        this.redrawItems();
        
                this.hideAttachments();
                
        this.update();
    },
    clearDefaultItems:function() {
        this.defaultItems = [];
    },
    attachPhoto:function(e) {
        var ttt = e.data.ttt;
        
        ttt.openPreview();
                
        // Hide all the attachment containers and show this one
        ttt.hideAttachments();
        ttt.$attachPhotoContainer.show();
        
    },
    attachVideo:function(e) {
        var ttt = e.data.ttt;
        
        ttt.openPreview();
        
        // Hide all the attachment containers and show this one
        ttt.hideAttachments();
        ttt.$attachVideoContainer.show();
    },
    tryAttachVideo:function(e) {
        var ttt = e.data.ttt;
        
        var ajaxOptions = {
            'url':'http://twubs.com/ajax360/31-1966080/attachVideo',
            'type':'post',
            'dataType':'json',
            'data':{
                'videoURL':$('#' + ttt.prefix + 'videoURL').val()
            },
            'success':function(data) {
                
                ttt.setAttachmentInfo(data.message);
                
                if(parseInt(data.success) > 0) {
                    ttt.prependItem('video', data.shortURL);
                    ttt.hideAttachments();
                    ttt.update();
                }
            }
        }
        
        $.ajax(ajaxOptions);
    },
    attachLink:function(e) {
        var ttt = e.data.ttt;
        
        ttt.openPreview();
        
        // Hide all the attachment containers and show this one
        ttt.hideAttachments();
        ttt.$attachLinkContainer.show();
    },
    tryAttachLink:function(e) {
        var ttt = e.data.ttt;
        
        var ajaxOptions = {
            'url':'http://twubs.com/ajax360/31-1966080/attachLink',
            'type':'post',
            'dataType':'json',
            'data':{
                'linkURL':$('#' + ttt.prefix + 'linkURL').val()
            },
            'success':function(data) {
                
                ttt.setAttachmentInfo(data.message);
                
                if(parseInt(data.success) > 0) {
                    ttt.prependItem('video', data.shortURL);
                    ttt.hideAttachments();
                    ttt.update();
                }
            }
        }
        
        $.ajax(ajaxOptions);
    },
    hideAttachments:function(e) {
        if(e != null) {
            var ttt = e.data.ttt;
        } else {
            var ttt = this;
        }
        
        // Hide the photo container
        ttt.$attachPhotoContainer.hide();
        // Reset the photo frame source if we need to
        $("#tweetTwubReplyBox_attachPhotoContainerForm").html($.ajax({
                url: "http://twubs.com/ajax360/31-1966080/attachPhoto?prefix=tweetTwubReplyBox_",
                async: false,
                success: function() {
                    $(document).trigger("ttt.photoFormDone");
                }
            }).responseText
        );
        
        // Hide the video container and reset the text field
        ttt.$attachVideoContainer.hide();
        $('#' + ttt.prefix + 'videoURL').val('');
        
        // Hide the link container and reset the text field
        ttt.$attachLinkContainer.hide();
        $('#' + ttt.prefix + 'linkURL').val('');
    }, 
    setAttachmentInfo:function(info) {
        this.$attachmentInfo.html(info);
        setTimeout(this.prefix + "tweetThisTwub.resetAttachmentInfo();", 5000);
    },
    resetAttachmentInfo:function() {
        var ttt = this;
    
        ttt.$attachmentInfo.children().slideUp('normal', function() {
            ttt.$attachmentInfo.children().remove();
        });
    },
    openPreview:function() {
    
        $("#tweetTwubReplyBox_showTweetThisTwub").hide();
        $("#tweetTwubReplyBox_tweetThisTwub").show();
        $("#tweetTwubReplyBox_attachItemsContainer").show();
        
        // If the preview container is hidden, slide it down
        if(this.$previewContainer.css('display') == 'none') {
            this.$previewContainer.slideDown();
        }
    },
    appendItem:function(type, text, data) {
        if(data == null) {
            data = {};
        }
        
        // Create the new item
        var newItem = new TweetThisTwub.Item(this, type, text, data);
        
        
        // Find where we should insert the item.
        if(this.items.length > 0 && this.items[this.items.length - 1].type == 'twubLink') {
            // If the last item is a twub link, add the item before it
            var spliceIndex = this.items.length - 1;
        } else {
            // else add it to the end
            var spliceIndex = this.items.length;
        }
        
        // Add the new item at the correct location
        this.items.splice(spliceIndex, 0, newItem);
        
        // Redraw the items
                this.redrawItems();
            },
    prependItem:function(type, text, data) {
        if(data == null) {
            data = {};
        }
    
        // Create the new item
        var newItem = new TweetThisTwub.Item(this, type, text, data);
        
        // Add the new item at the beginning
        this.items.splice(0, 0, newItem);
        
        // Redraw the items
        this.redrawItems();
    },
    redrawItems:function() {
        // Clear out any existing items
        this.$previewItems.html("");
        
        // Redraw each item in sequence
        $.each(this.items, function() {
            this.draw();
        });
    },
    saveDefaultItems:function() {
        this.defaultItems = this.items.slice();
    }
};

TweetThisTwub.Item = function(setTTT, setType, setText, setData) {
    // Set the basic information
    this.ttt = setTTT;
    this.type = setType;
    this.text = setText;
    this.data = setData;
}

TweetThisTwub.Item.prototype = {
    draw:function() {
                // Create the DOM element
        var $node = $('<span class="item" />');
        $node.text(this.text);
        
        var $delete = $('<img src="http://static.360hubs.com/images/icons/cancel.gif" />');
        $node.append($delete);
        
        var data = {'item':this};
        $delete.unbind('click').bind('click', data , this.deleteItem);

        this.ttt.$previewItems.append($node);
            },
    deleteItem:function(e) {
    
        var item = e.data.item;
        
        // Find the reference to this node in the parent items array
        var itemIndex = -1;
        
        $.each(item.ttt.items, function(i) {
            if(this == item) {
                itemIndex = i;
                return false;
            }
        });
                
        // If we found it remove it from the array and redraw
        if(itemIndex > -1) {
            item.ttt.items.splice(itemIndex, 1);
            item.ttt.redrawItems();
            item.ttt.update();
        }
    }
};


var tweetTwubReplyBox_tweetThisTwub;
$(document).ready(function() {

    $("#tweetTwubReplyBox_showTweetThisTwub").click(function() {
        tweetTwubReplyBox_tweetThisTwub.openPreview();
    });

    tweetTwubReplyBox_tweetThisTwub = new TweetThisTwub("tweetTwubReplyBox_");

    tweetTwubReplyBox_tweetThisTwub.$textArea.val("");

        tweetTwubReplyBox_tweetThisTwub.appendItem("hashtag", "#pk2upv", null);
        tweetTwubReplyBox_tweetThisTwub.saveDefaultItems();
    tweetTwubReplyBox_tweetThisTwub.redrawItems();
    });





function checkThumbs() {
    if (typeof rebindThumbs == 'function') {
        rebindThumbs();
    } 
}

// ** TwitterFeedWidget Constructor ** //
function TwitterFeedWidget(setPrefix, setTwub, setFeedID, setFilter, setIsTwubAdmin, setIsLiveEvent, setFeeds) {
    var _this = this;
    
    // will be set to false on first request for updates
    this.isStartup = true;
    
    // Grab our basic information conditions
    this.prefix = setPrefix;
    
    this.twub = setTwub;
    this.feedID = setFeedID;
    this.filter = setFilter;
    this.isTwubAdmin = setIsTwubAdmin;
    this.isLiveEvent = setIsLiveEvent;
        
    this.feeds = setFeeds;
    this.jsonChunks = {};
    this.isSearching = 0;
    
    
    // Grab some defaults from our controller
    this.maxViewableTweets = 10;
    this.updateIntervalSecs = 10;
    
    // Initialize some feed states
    this.viewableTweets = [];
    this.realTimeBufferTweets = [];
    this.newestTweets = [];
    this.approvedTweets = [];
    
    this.broadcastInterval = 30;
    this.broadcastTimeout = null;
    this.broadcastAutoQueue = false;
    this.broadcastAutoCycle = false;
        
    this.hasOlderTweets = false;
    this.numNewerTweets = 0;
    
    this.realTimeMode = true;
    
    this.latestUpdateTweetTime = 0;
    this.updateTimeout = null;
    
    //to be set to true if we just switched from one filter to another
    this.switchFilters = false;
    
    this.initialTime = null;    
    
    // Grab our DOM references for quick access
    this.$tweetsList = $("#" + this.prefix + "tweetsList");
    this.$olderButton = $("#" + this.prefix + "olderButton");
    this.$newerButton = $("#" + this.prefix + "newerButton");
    
    this.$realTime = $("#" + this.prefix + "realTime");
    this.$paused = $("#" + this.prefix + "paused");
    
    this.$pauseButton = $("#" + this.prefix + "pauseButton");
    this.$resumeButton = $("#" + this.prefix + "resumeButton"); 
    
    this.$allFilterLinks = $("#" + this.prefix + "toolbarFilter span");
    this.$filterAll = $("#" + this.prefix + "filterAll");
    this.$filterMedia = $("#" + this.prefix + "filterMedia");
    this.$filterMember = $("#" + this.prefix + "filterMember");
    
    this.$feedMessageSlider = $("#" + this.prefix + "feedMessageSlider");
    
    if (this.isLiveEvent && this.isTwubAdmin) {
        
        this.$broadcastControllerDown = $("#" + this.prefix + "broadcastControllerDown");
        this.$broadcastControllerUp = $("#" + this.prefix + "broadcastControllerUp");
        this.$broadcastController = $("#" + this.prefix + "broadcastController");
        
        this.$broadcastToggleSettings = $("#" + this.prefix + "broadcastToggleSettings");
        this.$broadcastSettings = $("#" + this.prefix + "broadcastSettings");
        this.$broadcastAutoQueue = $("#" + this.prefix + "broadcastAutoQueue");
        this.$broadcastAutoCycle = $("#" + this.prefix + "broadcastAutoCycle");
        
        this.$fullscreenTweets = $("#" + this.prefix + "fullscreenTweets");
        
        this.$fullscreenHeaderInput = $("#" + this.prefix + "fullscreenHeaderInput");
        this.$fullscreenFooterInput = $("#" + this.prefix + "fullscreenFooterInput");
        
        // Handle the color pickers
        _this.$broadcastConfiguration = $("#" + _this.prefix + "broadcastConfiguration");
        _this.$broadcastHeaderBgColor = $("#" + _this.prefix + "broadcastHeaderBgColor");
        _this.$broadcastHeaderFontColor = $("#" + _this.prefix + "broadcastHeaderFontColor");
        _this.$broadcastFooterBgColor = $("#" + _this.prefix + "broadcastFooterBgColor");
        _this.$broadcastFooterFontColor = $("#" + _this.prefix + "broadcastFooterFontColor");
        
        _this.$broadcastHeaderBgColor.ColorPicker({
    	    'color':'',
    	    'onSubmit': function(hsb, hex, rgb, el) {
                _this.$broadcastHeaderBgColor.val("#"+hex);
            },
            'onChange': function (hsb, hex, rgb) {
                _this.$broadcastHeaderBgColor.val("#"+hex);
            }
    	});
    	
    	_this.$broadcastHeaderFontColor.ColorPicker({
    	    'color':'',
    	    'onSubmit': function(hsb, hex, rgb, el) {
                _this.$broadcastHeaderFontColor.val("#"+hex);
            },
            'onChange': function (hsb, hex, rgb) {
                _this.$broadcastHeaderFontColor.val("#"+hex);
            }
    	});
    	
    	_this.$broadcastFooterBgColor.ColorPicker({
    	    'color':'',
    	    'onSubmit': function(hsb, hex, rgb, el) {
                _this.$broadcastFooterBgColor.val("#"+hex);
            },
            'onChange': function (hsb, hex, rgb) {
                _this.$broadcastFooterBgColor.val("#"+hex);
            }
    	});
    	
    	_this.$broadcastFooterFontColor.ColorPicker({
    	    'color':'',
    	    'onSubmit': function(hsb, hex, rgb, el) {
                _this.$broadcastFooterFontColor.val("#"+hex);
            },
            'onChange': function (hsb, hex, rgb) {
                _this.$broadcastFooterFontColor.val("#"+hex);
            }
    	});
        
        _this.$broadcastSaveConfiguration = $("#" + _this.prefix + "broadcastSaveConfiguration");
        _this.$broadcastSaveConfigurationSpinner = $("#" + _this.prefix + "broadcastSaveConfigurationSpinner");
        
        _this.$broadcastConfiguration.ajaxForm({
            url:'http://twubs.com/ajax360/33-2097152/',
            beforeSubmit:function() {
                _this.$broadcastSaveConfigurationSpinner.show();
            },
            success:function(data) {            
                _this.$broadcastSaveConfigurationSpinner.hide();
            }
        });
        
        _this.$broadcastSaveConfiguration.unbind().bind('click', function(e) 
        {
            _this.$broadcastConfiguration.submit();
        });
        
        
        this.$fullscreenOpen = $("#" + this.prefix + "fullscreenOpen");
        
        this.$fullscreenPlaySlider = $("#" + this.prefix + "fullscreenPlaySlider");
        this.$fullscreenPlayIntervalNum = $("#" + this.prefix + "fullscreenPlayIntervalNum");
        
        this.$fullscreenPlay = $("#" + this.prefix + "fullscreenPlay");
        this.$fullscreenPause = $("#" + this.prefix + "fullscreenPause");
        this.$fullscreenFastForward = $("#" + this.prefix + "fullscreenFastForward");
        
        // Bind broadcast events
        this.$broadcastControllerDown.unbind().bind('click', {'_this':this}, this.broadcastControllerDown);
        this.$broadcastControllerUp.unbind().bind('click', {'_this':this}, this.broadcastControllerUp);
        
        this.$broadcastToggleSettings.unbind().bind('click', {'_this':this}, this.broadcastToggleSettings);
        this.$broadcastAutoQueue.unbind().bind('click', {'_this':this}, this.broadcastToggleAutoQueue);
        this.$broadcastAutoCycle.unbind().bind('click', {'_this':this}, this.broadcastToggleAutoCycle);
        
        this.$fullscreenPlaySlider.slider({
            'value':_this.broadcastInterval,
            'min':2,
            'max':120,
            'step':2,
            'slide':function(e, ui) {
                _this.setBroadcastInterval(ui.value);
            }
        });
                
        this.$fullscreenOpen.unbind().bind('click', {'_this':this}, this.fullscreenOpen);
        
        this.$fullscreenPlay.unbind().bind('click', {'_this':this}, this.playBroadcast);
        this.$fullscreenPause.unbind().bind('click', {'_this':this}, this.pauseBroadcast);
        
        
        $(".mainFeed_fullscreenInputDefault").bind('focus', function() {
            $(this).removeClass("mainFeed_fullscreenInputDefault").val("");
        });
        
        this.$fullscreenFastForward.unbind().bind('click', {'_this': this}, this.progressBroadcast);
    }
    
    // Register event handlers
    this.$olderButton.unbind().bind('click', {'widget':this}, this.showOlder);
    this.$newerButton.unbind().bind('click', {'widget':this}, this.showNewer);
    
    this.$pauseButton.unbind().bind('click', {'widget':this}, this.pause);
    this.$resumeButton.unbind().bind('click', {'widget':this}, this.resume);
    
    this.$filterAll.unbind().bind('click', {'widget':this}, function(e) {
        e.data.widget.setFilter('');
    });
    
    this.$filterMedia.unbind().bind('click', {'widget':this}, function(e) {
        e.data.widget.setFilter('media');
    }); 
    
    this.$filterMember.unbind().bind('click', {'widget':this}, function(e) {
        e.data.widget.setFilter('member');
    });   
}

// ** TwitterFeedWidget class methods ** //
TwitterFeedWidget.prototype = {

    /**
     * initialize - initialize the feed with the block of latest tweets.
     * 
     * @param initialTweets - the tweet block we want to start with
     */
    initialize:function(initialTweets, initialRealTimeBufferTweets) {
        var _this = this;
        
        // Handle some stuff for the time updating
        _this.initialTime = _this.getLocalTime();
        
        $.each(initialTweets, function () {
            this.localTime = _this.initialTime;
        });
        
        $.each(initialRealTimeBufferTweets, function() {
            this.localTime = _this.initialTime;
        });
    
        // Take the newest tweet we have for the tweet to update from
        if(initialRealTimeBufferTweets.length > 0) {
            _this.latestUpdateTweetTime = initialRealTimeBufferTweets[initialRealTimeBufferTweets.length-1].arrivalTime;
        } else if (initialTweets.length > 0) {
            _this.latestUpdateTweetTime = initialTweets[initialTweets.length-1].arrivalTime;
        }
                
        // Add our tweets to the newest buffer (for switching from paused to realtime)
        this.addNewestTweets(initialTweets);
        if(initialRealTimeBufferTweets.length > 0) {            
            this.addNewestTweets(initialRealTimeBufferTweets);
        }
        
        // Function used for adding the initial tweets to the feed
        // Call scroll up which will add them quickly
        _this.scroll(initialTweets, 'up', null, function() {
            // Add the scrolling tweets to our real time buffer, animate based on the update interval
            _this.realTimeBufferTweets = initialRealTimeBufferTweets;
            
            // Check if we should display the more button, we might make this more intelligent later.
            if(_this.viewableTweets.length + _this.realTimeBufferTweets.length >= _this.maxViewableTweets) {
                _this.setHasOlderTweets(true);
            }
        });
        
        // perform search right away if given
        _this.performSearch();
        
        // set next update. this may be preempted if an initial search was requested
        _this.setNextUpdate(_this.getNextUpdateDelay());
    },
        
    tweetRemove:function(e)
    {
        var _this = e.data._this;
        
        var $tweet = $(this).parents(".twitterFeedWidgetTweet");
        
        var tweetId = $tweet.attr('rel');
        var tweetType = $tweet.attr('tweetType');
        var $busy = $(this).siblings(".tweetOptionBusy");
        var tweetText = $(this).parents(".tweetOption").siblings(".msgTweet").html();
        var $remove = $(this);
                
        $remove.hide();
        $busy.show();
        
        var ajaxOptions = {
            url:'http://twubs.com/ajax360/33-2097152/',
            type:'POST',
            data:{
                'twub':_this.twub,
                'func':'promptRemoveTweet',
                'feedid':'95339-2695168',
                'tweetId':tweetId,
                'tweetType':tweetType,
                'tweetText':tweetText
            },
            dataType:'text',
            success:function(data) {
                $.facebox(data);
                $busy.hide();
                $remove.show();
            }
        };
        
        $.ajax(ajaxOptions);
    },

    broadcastAdd:function(e)
    {
        var _this = e.data._this;
        
        var $tweet = $(this).parents(".twitterFeedWidgetTweet");
        
        var tweetId = $tweet.attr('rel');
        var tweetType = $tweet.attr('tweetType');
        var $busy = $(this).siblings(".tweetOptionBusy");
        
        var $add = $(this);
        
        $add.hide();
        $busy.show();
        
        var ajaxOptions = {
            url:'http://twubs.com/ajax360/33-2097152/',
            type:'POST',
            data:{
                'twub':_this.twub,
                'func':'approveTweet',
                'feedid':'95339-2695168',
                'tweetId':tweetId,
                'tweetType':tweetType
            },
            dataType:'json',
            success:function(data) {
                $add.show();
                $busy.hide();
                                    
                _this.updateBroadcastQueue(data);
            }
        };
        
        $.ajax(ajaxOptions);
    },
    
    broadcastToggleAutoQueue:function(e)
    {
        var _this = e.data._this;
        _this.broadcastAutoQueue = $(this).is(':checked');
    },
    
    broadcastToggleAutoCycle:function(e)
    {
        var _this = e.data._this;
        _this.broadcastAutoCycle = $(this).is(':checked');
    },
    
    broadcastToggleSettings:function(e)
    {
        var _this = e.data._this;
        _this.$broadcastSettings.slideToggle();
    },
    
    broadcastControllerUp:function(e)
    {
        var _this = e.data._this;
        
        _this.$broadcastControllerUp.hide();
        _this.$broadcastControllerDown.show();
        _this.$broadcastController.slideUp();
    },
    
    broadcastControllerDown:function(e)
    {
        var _this = e.data._this;
        
        _this.$broadcastControllerUp.show();
        _this.$broadcastControllerDown.hide();
        _this.$broadcastController.slideDown();
    },
    
    playBroadcast:function(e) {
        var _this = e.data._this;
        
        _this.$fullscreenPlay.hide();
        _this.$fullscreenPause.show();
        
        _this.playBroadcastUpdate();
    },
    
    playBroadcastUpdate:function() {
        var _this = this;
        
        _this.progressBroadcast();
        
        _this.broadcastIntervalTimeout = setTimeout(
            function() {
                _this.playBroadcastUpdate();
            }, (_this.broadcastInterval * 1000)
        );
    },
    
    pauseBroadcast:function(e) {
        var _this = e.data._this;
        
        _this.$fullscreenPlay.show();
        _this.$fullscreenPause.hide();
        
        clearTimeout(_this.broadcastIntervalTimeout);
    },
    
    setBroadcastInterval:function(setBroadcastInterval) {
        var _this = this;
        
        _this.broadcastInterval = setBroadcastInterval;
        _this.$fullscreenPlayIntervalNum.text(_this.broadcastInterval);
    },
    
    fullscreenOpen:function(e) {
        var _this = e.data._this;
                
        window.open("http://twubs.com/ajax360/33-2097152/?twub="+_this.twub+"&feedid="+_this.feedID+"&fullscreen", "_blank", "fullscreen,resizable");
    },
    
    progressBroadcast:function(e) {
        if(e != null) {
            var _this = e.data._this;
        } else {
            var _this = this;
        }
                
        var ajaxOptions = {
            'url':'http://twubs.com/ajax360/33-2097152/',
            'type':'POST',
            'data':{
                'func':'progressBroadcast',
                'twub':_this.twub,
                'feedid':_this.feedID,
                'autoCycle':_this.broadcastAutoCycle
            },
            'dataType':'json',
            'success':function(data) {
                _this.updateBroadcastQueue(data)
            }
        };
        
        $.ajax(ajaxOptions);
    },
    
    updateBroadcastQueue:function(tweets) {
        var _this = this;
        
        /* --- OLD SCROLLING CODE, KEEP AROUND FOR A BIT FOR REFERENCE
        // Scroll out any outdated tweets
        _this.$fullscreenTweets.children().each(function() {
            var $oldTweet = $(this);
            var oldTweetId = $oldTweet.attr("rel");
            
            var removeTweet = true;
            
            $.each(tweets, function(i) {                
                if(oldTweetId == this.id) {
                    tweets.splice(i, 1);
                    removeTweet = false;
                    updatedBroadcasting = this.broadcasting;
                    return false;
                }
            });
            
            if(removeTweet) {
                $oldTweet.slideUp('normal', function() {
                    $oldTweet.remove();
                });
                
                // Unhighlight the main one
                $(".twitterFeedWidgetTweet[rel="+oldTweetId+"]").removeClass("twitterFeedWidgetTweetApproved"); 
            } else {
                if (updatedBroadcasting) {
                    $oldTweet.removeClass('tweetPreviewQueued');
                } else {
                    $oldTweet.addClass('tweetPreviewQueued');
                }
            }
        });
        
        // Scroll in new tweets
        $.each(tweets, function() {
            var $newTweet = $(this.html);
            
            if (this.broadcasting) {
                $newTweet.removeClass('tweetPreviewQueued');
            } else {
                $newTweet.addClass('tweetPreviewQueued');
            }
            
            _this.$fullscreenTweets.prepend($newTweet);
            $newTweet.slideDown();
            
            // Highlight the main feed post
            $(".twitterFeedWidgetTweet[rel="+this.id+"]").addClass("twitterFeedWidgetTweetApproved"); 
            
            // Bind the remove button
            _this.bindRemoveBroadcastTweetButton($newTweet);
        });
        */
        
        // Scroll out any outdated tweets, and create old tweets stack
        var oldTweets = [];
        _this.$fullscreenTweets.children().each(function() {
            var $oldTweet = $(this);
            var oldTweetId = $oldTweet.attr("rel");
            
            var removeTweet = true;
            
            $.each(tweets, function(i) {
                if(oldTweetId == this.id) {
                    removeTweet = false;
                    return false;
                }
            });
            
            if(removeTweet) {
                $oldTweet.slideUp('normal', function() {
                    $oldTweet.remove();
                    $(".twitterFeedWidgetTweet[rel="+oldTweetId+"]").removeClass("twitterFeedWidgetTweetApproved"); 
                });
            } else {
                oldTweets.unshift($(this));
            }
        });
        
        var newTweets = tweets;
        
        // While we have tweets to process do it
        while (newTweets.length > 0 && oldTweets.length > 0)
        {            
            // Grab the oldest tweet id from both stacks
        	var lastOldTweet = oldTweets[0];
            var lastNewTweet = newTweets[0];
            
            var lastOldTweetId = lastOldTweet.attr('rel');
            var lastNewTweetId = lastNewTweet.id;
            
            var lastOldTweetPriority = lastOldTweet.attr('id');
            var lastNewTweetPriority = lastNewTweet.priority;
            
            
            if (lastOldTweetPriority == lastNewTweetPriority) {
                // Check if the new results have it queued
                if (lastNewTweet.broadcasting) {
                    // Make sure its not queued
                    lastOldTweet.removeClass('tweetPreviewQueued');
                } else {
                    // It should still be queued
                    lastOldTweet.addClass('tweetPreviewQueued');
                }
                
                newTweets.shift();
                oldTweets.shift();
            } else if (lastOldTweetPriority < lastNewTweetPriority) {
                // Remove this one, it will be added where its supposed to be later                    
                lastOldTweet.slideUp('normal', function() {
                    $(this).remove();
                });
                
                oldTweets.shift();
            } else {
                // Uh oh, they dont match up, add the new tweet after the old one
                                
                var $newTweet = $(lastNewTweet.html);

                if (lastNewTweet.broadcasting) {
                    $newTweet.removeClass('tweetPreviewQueued');
                } else {
                    $newTweet.addClass('tweetPreviewQueued');
                }

                $newTweet.insertAfter(lastOldTweet);
                $newTweet.slideDown();

                // Highlight the main feed post
                $(".twitterFeedWidgetTweet[rel="+lastNewTweet.id+"]").addClass("twitterFeedWidgetTweetApproved"); 

                // Bind the remove button
                _this.bindRemoveBroadcastTweetButton($newTweet);
                
                // Shift the new tweet off
                newTweets.shift();
            }
        }
        
                    
        // Scroll in any unmatched new tweets
        $.each(newTweets, function() {
            var $newTweet = $(this.html);
            
            if (this.broadcasting) {
                $newTweet.removeClass('tweetPreviewQueued');
            } else {
                $newTweet.addClass('tweetPreviewQueued');
            }
            
            _this.$fullscreenTweets.prepend($newTweet);
            $newTweet.slideDown();
            
            // Highlight the main feed post
            $(".twitterFeedWidgetTweet[rel="+this.id+"]").addClass("twitterFeedWidgetTweetApproved"); 
            
            // Bind the remove button
            _this.bindRemoveBroadcastTweetButton($newTweet);
        });
    },
    
    bindRemoveBroadcastTweetButton:function($tweetPreview) 
    {        
        var _this = this;
        
        $tweetPreview.find(".broadcastTweetRemove").unbind().bind('click', function(e) 
        {            
            $tweetPreview.find('.broadcastTweetRemoveButton').remove();
            $tweetPreview.find('.broadcastTweetRemoveBusy').show();
            
            var tweetId = $tweetPreview.attr("rel");
            var tweetType = $tweetPreview.attr("tweetType");
                                    
            var ajaxOptions = {
                'url':'http://twubs.com/ajax360/33-2097152/',
                'type':'POST',
                'data':{
                    'func':'removeBroadcastTweet',
                    'feedid':_this.feedID,
                    'twub':_this.twub,
                    'tweetId':tweetId,
                    'tweetType':tweetType
                },
                'dataType':'json',
                'success':function(data) {
                    _this.updateBroadcastQueue(data);
                }
            };
            
            $.ajax(ajaxOptions);
        });
    },
    
    
    setHasOlderTweets:function(setHasOlderTweets) {
        this.hasOlderTweets = setHasOlderTweets;
        
        if(this.hasOlderTweets) {
            this.$olderButton.show();
        } else {
            this.$olderButton.hide();
        }
    },
    
    setNumNewerTweets:function(setNumNewerTweets) {
        this.numNewerTweets = setNumNewerTweets;
        
        if(this.numNewerTweets > 0) {
            this.$newerButton.css('display', 'block');
            this.$newerButton.html("show newer ("+this.numNewerTweets+")");
        } else {
            this.$newerButton.css('display', 'none');
        }
    },
    
    /** 
     * setFilter - Function to change the feed filter
     *
     * @param setFilter - the filter we want to use ('' or 'media')
     */
    setFilter:function(setFilter) {
        // The filter is already set to this, return silly head
        if(this.filter == setFilter) { return; }
        
        this.filter = setFilter;
        
        // Change the view
        if(this.filter == '') {
            this.$allFilterLinks.removeClass(this.prefix + "filterSelected").addClass(this.prefix + "filter");
            this.$filterAll.removeClass(this.prefix + "filter").addClass(this.prefix + "filterSelected");
        } else if(this.filter == 'media') {
            this.$allFilterLinks.removeClass(this.prefix + "filterSelected").addClass(this.prefix + "filter");
            this.$filterMedia.removeClass(this.prefix + "filter").addClass(this.prefix + "filterSelected");
        } else if (this.filter == 'member') {
            this.$allFilterLinks.removeClass(this.prefix + "filterSelected").addClass(this.prefix + "filter");
            this.$filterMember.removeClass(this.prefix + "filter").addClass(this.prefix + "filterSelected");
        }
        
        // Switch to live feed mode
        this.realTimeMode = true;
        this.setHasOlderTweets(true);
        this.setNumNewerTweets(0);
        
        // Change the view to show the live mode
        this.$realTime.show();
        this.$paused.hide();
        
        // Clear out the feed
        this.resetFeed();
        
        this.switchFilters = true;
        
        // Update immediately
        this.update();
    },
    
    /**
     * addNewestTweets - function to add a set of tweets to the newest tweets array
     *  array will be used for switching from paused to realtime
     *
     * @param newTweets - The array of tweets we want to add
     */
    addNewestTweets:function(newTweets) {
        // Add the new tweets to the stack
        this.newestTweets = this.newestTweets.concat(newTweets);
        this.newestTweets = this.newestTweets.slice(Math.max(0, this.newestTweets.length - this.maxViewableTweets));
    },
    
    /**
     * displayFeedMessage - slide down a message below the feed control
     *
     * @param message - the message to display
     */
    displayFeedMessage:function(message) {
        var _this = this;
    
        _this.$feedMessageSlider.hide().html(message).slideDown('normal', function() {
            setTimeout(function() {
                _this.$feedMessageSlider.slideUp('normal', function() {
                    _this.$feedMessageSlider.html('');
                });
            }, 3000);
        });
    },
    
    /**
     * resetFeed - clear out and reset the feed
     */
    resetFeed:function() {
        // Initialize some feed states
        this.viewableTweets = [];
        this.realTimeBufferTweets = [];
        
        this.setHasOlderTweets(false);
        this.setNumNewerTweets(0);
        
        this.latestUpdateTweetTime = 0;
        this.$tweetsList.children().remove();
    },
    
    /**
     * pause - function to pause the live feed
     *
     * @param e - if fired by event, event object
     */
    pause:function(e) {
        if(e != null) {
            var _this = e.data.widget;
        } else {
            var _this = this;
        }
        
        // We are already paused
        if(!_this.realTimeMode) { return; }
        
        // Stop the current chain of updates
        clearTimeout(_this.updateTimeout);
        
        // Flag the object as paused
        _this.realTimeMode = false;
        
        // Change the view to show the paused button
        _this.$realTime.hide();
        _this.$paused.show();
        
        // Set the number new to the number left in real time buffer
        _this.setNumNewerTweets(_this.realTimeBufferTweets.length);
        
        // Start a new chain of updates in 5 seconds
        _this.setNextUpdate(5000);
    },
    
    /**
     * resume - function to resume the live feed
     *
     * @param e - if fired by event, event object
     */
    resume:function(e) {
        if(e != null) {
            var _this = e.data.widget;
        } else {
            var _this = this;
        }
        
        
        // We are already live
        if(_this.realTimeMode) { return; }
        
        // Stop the current chain of updates
        clearTimeout(_this.updateTimeout);
        
        // Flag the object as live
        _this.realTimeMode = true;
        
        // Clear the feed of everything
        _this.resetFeed();
        
        // If we have newest tweets, scroll them in then update
        if(_this.newestTweets.length > 0) {
            
            _this.latestUpdateTweetTime = _this.newestTweets[_this.newestTweets.length - 1].arrivalTime;
            
            _this.scroll(_this.newestTweets, 'up', null, function() {
                _this.update();
            });
            
        } else {
            // Otherwize just update
            _this.update();
        }
        
        _this.setHasOlderTweets(true);
        
        // Change the view to show the live mode
        _this.$realTime.show();
        _this.$paused.hide();
    },
    
    performSearch:function() {
        var _this = this;
        
        // A lame way to do a timeout, but w/e
        if (_this.isSearching > 4) {
           _this.isSearching = 0; 
        }
        
        _this.isSearching++;
        
        if (_this.isSearching > 1) {
            return;
        }
        
        var jsonChunks = {};
        var feeds = [];
        $.each(_this.feeds, function() {
           feeds.push(this); 
        });
        
        if (_this.feeds.length != 0) {        
            // Search all our hashtags
            _this.populateJsonChunks(feeds, jsonChunks);     
        }
        
//        setTimeout(function() {
//            _this.performSearch();
//        }, 10000);
    },
    
    populateJsonChunks:function(feeds, jsonChunks) {
        var _this = this;
        
        if(feeds.length == 0) { _this.reportSearch(jsonChunks); return; }                
        
        feed = feeds.pop();
                        
        $.getJSON("http://search.twitter.com/search.json?q="+escape(feed.hashTag)+"&rpp=100&since_id="+feed.sinceId+"&callback=?", function(data) {            
            jsonChunks[feed.feedId] = data;                  
            _this.populateJsonChunks(feeds, jsonChunks)
        });        
    },
    
    reportSearch:function(jsonChunks) {
        var _this = this;
        
        var jsonString = JSON.stringify(jsonChunks);
                
        var ajaxOptions = {
            'type':'post',
            'url': 'http://twubs.com/ajax360/33-2097152/',
            'data':{
                'func':'reportSearch',
                'twub':_this.twub,
                'search':jsonString
            }
        }
        
        // if we are reporting search results during the initial
        // startup we want to make sure we pick them up right away
        if (_this.isStartup) {
            ajaxOptions.success = function(data) {
                // schedule an update right away but with a little time 
                // for the server to process the new tweets
                _this.setNextUpdate(2000);
            }
        }
        
        $.ajax(ajaxOptions);
        
        _this.isSearching = 0;        
    },
    
    
    
    /**
     * update - updated the feed. either through an ajax call to get more tweets or by emptying the 
     *   real time buffer.
     *
     * @param nextUpdateDelay ms until you should fire off next update (overriden if new tweets from ajax)
     */
    update:function(nextUpdateDelay) {
        var _this = this;
        
        _this.isStartup = false;
        
        if(_this.viewableTweets.length > 0) {
            var lastViewableTweetTime = _this.viewableTweets[_this.viewableTweets.length-1].arrivalTime;
        } else {
            var lastViewableTweetTime = 0;
        }
        
        // Setup our ajax request, these options are common to realtime / paused
        var ajaxOptions = {
            'type':'post',
            'url':'http://twubs.com/ajax360/33-2097152/',
            'dataType':'json',
            'data':{
                'func':'fetchUpdate',
                'twub':_this.twub,
                'feedid':_this.feedID,
                'filter':_this.filter,
                'latestUpdateTweetTime':_this.latestUpdateTweetTime,
                'latestViewableTweetTime':lastViewableTweetTime
            }
        };
        
        if(_this.realTimeMode && _this.realTimeBufferTweets.length > 0) { 
            // ** Real time mode and have tweets in buffer ** //
            
            // Pop off the bottom tweets from the buffer stack
            var bottomTweet = _this.realTimeBufferTweets.shift();
            
            // Scroll the tweet into the viewport
            _this.scroll([bottomTweet], 'up', 1000, null, true);
            
            // Schedule the next update
            _this.setNextUpdate(nextUpdateDelay);
            
        } else if(_this.realTimeMode) { 
            // ** Real time mode and our buffer is empty, we need more tweets ** //
            
            // We are out of tweets in the buffer, we need new ones
            ajaxOptions.success = function(data) {
            
                // check for magic signal that user has multiple sessions when not allowed
                if (data.numNewerTweets == -99) {
                    window.location = '/';
                }
                
                // Set the local time for the tweets
                var localTime = _this.getLocalTime();
                $.each(data.tweets, function() {
                    this.localTime = localTime;
                });
                
                // Set the new latest update tweet id from the returned tweets
                if(data.tweets.length > 0) {
                    _this.latestUpdateTweetTime = data.tweets[data.tweets.length - 1].arrivalTime;
                    _this.setHasOlderTweets(true);
                    _this.addNewestTweets(data.tweets);
                }
                
                // Put the newest 10 tweets in the real time buffer
                while(data.tweets.length > 0 && _this.realTimeBufferTweets.length < 10) {
                    _this.realTimeBufferTweets.unshift(data.tweets.pop());
                }
                
                // If we have more than we can display by scrolling, say so
                if(data.numNewerTweets > _this.maxViewableTweets) {
                    _this.displayFeedMessage('<div class="successBox">' + (data.numNewerTweets - _this.maxViewableTweets) + ' more tweets came in than could be displayed at a reasonable rate. Pause the feed to view them.</div>');
                }
                
                if(data.tweets.length > 0) {
                    // ** If we have more left, take the newest of the viewport size and slide them down fast ** //
                    
                    if(data.tweets.length > _this.maxViewableTweets) {                                            
                        // Slice off the top viewport of these  tweets for fast sliding
                        data.tweets = data.tweets.slice(data.tweets.length - _this.maxViewableTweets);
                    }
                                            
                    _this.scroll(data.tweets, 'up', null, function() {
                        // Once we are done animating the new tweets in, set the next update delay
                        _this.setNextUpdate(_this.getNextUpdateDelay());
                    }, true);
                } else {
                    // ** No tweets to slide down fast, just schedule the next update now ** //
                    if (_this.realTimeBufferTweets.length > 0) {
                        // we've got new tweets to display. Shift in first tweet immediately
                        _this.update(_this.getNextUpdateDelay());
                    }
                    else {
                        _this.setNextUpdate(_this.getNextUpdateDelay());
                    }
                    
                }
                
                //update the hover handles
                hoverHeadshots();
                
                //update the video links and etc
                checkThumbs();
                
                // fire off a new search on whatever was requested
                _this.feeds = data.searchFeeds;
                _this.performSearch();
            };    
            
            $.ajax(ajaxOptions);
            
        } else { 
            // ** Not in real time mode ** //
            
            // Our ajax request should just grab the new number of tweets
            ajaxOptions.success = function(data) {
                // Set the local time for the tweets
                var localTime = _this.getLocalTime();
                $.each(data.tweets, function() {
                    this.localTime = localTime;
                });
            
                // Set the new latest update tweet id from the returned tweets
                if(data.tweets.length > 0) {
                    _this.latestUpdateTweetTime = data.tweets[data.tweets.length - 1].arrivalTime;
                    _this.setHasOlderTweets(true);
                    _this.addNewestTweets(data.tweets);
                }
                
                // Update the number of newe tweets
                _this.setNumNewerTweets(data.numNewerTweets);
                
                // Schedule the next update
                _this.setNextUpdate(_this.updateIntervalSecs * 1000);
                
                //update the hover handles
                hoverHeadshots();
                
                //update the video links and etc
                checkThumbs();
            };
            
            $.ajax(ajaxOptions);
        }
        
        // Update any existing viewable timestamps
        var localTime = _this.getLocalTime();
        
        $.each(_this.viewableTweets, function() {
            var newSecondsOld = this.secondsOld + localTime - this.localTime;
            _this.$tweetsList.find("[rel="+this.id+"] .timestamp").text(_this.prettyTimeSince(newSecondsOld));
        });
    },
    
    /**
     * getNextUpdateDelay - compute delay for the next update in ms
     */
    getNextUpdateDelay:function() { 
        return Math.round((this.updateIntervalSecs/(1 + this.realTimeBufferTweets.length)) * 1000);
    },
    
    /**
     * setNextUpdate - set the timeout for the next update to occur
     * 
     * @param updateTimeout - ms until the next update should be fired
     */
    setNextUpdate:function(updateTimeout) {
        var _this = this;
        clearTimeout(_this.updateTimeout); // Just in case
        _this.updateTimeout = setTimeout(function() { _this.update(updateTimeout); }, updateTimeout);
    },
    
    /**
     * showOlder - Paginate down to get older tweets
     *
     * @param e - the event object when clicked, null otherwise
     */
    showOlder:function(e) {
    
        // Grab the widget instance either from the event data or from this
        if(e != null && e.data != null && e.data.widget != null) {  
            var widget = e.data.widget;
        } else {
            var widget = this;
        }
        
        // Pause if we are in real time mode
        widget.pause();
        
        
        if(widget.viewableTweets.length > 0) {
            var oldestTweetTime = widget.viewableTweets[0].arrivalTime;
        } else if(widget.realTimeBufferTweets.length > 0) {
            var oldestTweetTime = widget.realTimeBufferTweets[0].arrivalTime;
        } else {
            widget.setHasOlderTweets(false);
            return;
        }
        
        
        // Set up our ajax request        
        var ajaxOptions = {
            'type':'post',
            'url':'http://twubs.com/ajax360/33-2097152/',
            'dataType':'json',
            'data':{
                'func':'fetchOlder',
                'twub':widget.twub,
                'feedid':widget.feedID,
                'filter':widget.filter,
                'oldestTweetTime':oldestTweetTime
            },
            'success':function(data) {
                // Set the local time for the tweets
                var localTime = widget.getLocalTime();
                $.each(data.tweets, function() {
                    this.localTime = localTime;
                });
                
                // If we get any less than the max we know there is no more older
                if(data.tweets.length == 0) {
                    widget.setHasOlderTweets(false);
                }
                                
                // Set how many newer tweets there are after we scrolled down
                widget.setNumNewerTweets((widget.numNewerTweets + data.tweets.length));
                                
                widget.scroll(data.tweets, 'down', null);
                
                //update the hover handles
                hoverHeadshots();
                
                //update the video links and etc
                checkThumbs();
            }
        }
        
        // Make our ajax request
        $.ajax(ajaxOptions);
    },
    
    /**
     * showNewer - Paginate up to get newer tweets
     *
     * @param e - the event object when clicked, null otherwise
     */
    showNewer:function(e) {
    
        // Grab the widget instance either from the event data or from this
        if(e != null && e.data != null && e.data.widget != null) {  
            var widget = e.data.widget;
        } else {
            var widget = this;
        }
        
        if(widget.viewableTweets.length > 0) {
            var newestTweetTime = widget.viewableTweets[widget.viewableTweets.length-1].arrivalTime;
        } else {
            var newestTweetTime = 0;
        }
        
        
        // Set up our ajax request        
        var ajaxOptions = {
            'type':'post',
            'url':'http://twubs.com/ajax360/33-2097152/',
            'dataType':'json',
            'data':{
                'func':'fetchNewer',
                'twub':widget.twub,
                'feedid':widget.feedID,
                'filter':widget.filter,
                'newestTweetTime':newestTweetTime
            },
            'success':function(data) {
                // Set the local time for the tweets
                var localTime = widget.getLocalTime();
                $.each(data.tweets, function() {
                    this.localTime = localTime;
                });
            
                // If we clicked this we should definitely have older tweets
                widget.setHasOlderTweets(true);
                
                // Set how many newer tweets there are after we scrolled down
                widget.setNumNewerTweets(widget.numNewerTweets - data.tweets.length);
                                
                widget.scroll(data.tweets, 'up', null);
                
                //update the hover handles
                hoverHeadshots();
                
                //update the video links and etc
                checkThumbs();
            }
        }
        
        // Make our ajax request
        $.ajax(ajaxOptions);
    },
    
    /**
     * scroll - Scroll in a block of new tweets
     * 
     * TODO ::: CLEAN ME UP! I have become VERY ugly
     *
     * @param tweets - a block of tweets with their id and HTML
     * @param direction - which way to scroll the feed ('up'(default) or 'down')
     * @param speed - speed in milliseconds to animate (default 1500)
     * @param callback - a function to be called when the animation completes
     */
    scroll:function(tweets, direction, speed, callback, newTweets) {
    
        // Keep of copy of this so we can use it later out of scope
        var widget = this;
        
        // Make sure we have tweets, otherwise exit
        if(tweets == null || tweets.length == 0) { return; }
        
        // Set the default direction to up
        if(direction == null || direction != 'down') { direction = 'up'; }
        
        // Set the default speed to 1500
        if(speed == null) { speed = 1500; }
        
        // Set the default that the tweets are not new
        if(newTweets == null) { newTweets = false; }
        
        // Scrolling up? -- The code diverges into up and down here ... maybe refactor later?
        if(direction == 'up') {
            // Make a jQuery DOM instance for all our tweets going in and out
            var $newTweetsContainer = $('<div class="tweetsContainer" style="position:relative; overflow:hidden;"></div>');
            var $newTweetsSlider = $('<div class="tweetsSlider" style="display:none; position:absolute; width:100%;"></div>');
            $newTweetsContainer.append($newTweetsSlider);

            var $oldTweetsContainer = $('<div class="tweetsContainer"></div>');
            
            // Prepend new tweets container (sliding in from top)
            widget.$tweetsList.prepend($newTweetsContainer);
            // Append old tweets container (sliding out from bottom)
            widget.$tweetsList.append($oldTweetsContainer);
            
            $('body').append('<div style="display:none;">SCROLL LENGTH: ' + tweets.length + '</div>');
             
            // Loop through our new tweets, adding them to the new container, any old ones to the dump container
            $.each(tweets, function() {
                // Push the bottom tweet onto the top of the tweets
                widget.viewableTweets.push(this);
                
                // TODO: remove this when uncommenting code below
                // $newTweetsSlider.prepend(this.html);
                
                var $newTweet;
                if (this.html != null)
                	$newTweet = $(this.html);
                else
                	$newTweet = null;
                
                if ($newTweet != null) {
	                // show translation option?
	                var msgContainer = $newTweet.find(".msgtxt");
	                var msgText = msgContainer.html();
	                google.language.detect(msgText, function(result) {
	                	var language = 'ENGLISH';
	                    if (!result.error) {
	                		for (l in google.language.Languages) {
	                			if (google.language.Languages[l] == result.language) {
	                				language = l;
	                				break;
	                			}
	                		}
	                	}
	                	
	                	var translateBlock = $newTweet.find(".msgTranslation");
	                	if (language != 'ENGLISH') {
	                		google.language.translate(msgText, "", "en", function(result) {
	                			if (!result.error)
	                				translateBlock.html(result.translation);
	                  		});
	                		
	                		if (msgText != result.translation)
	                			translateBlock.css('display','block');
	                	}
	                	else
	                		translateBlock.css('display','none');
	                });
	                
	                // add html of tweet to slider
	                $newTweetsSlider.prepend($newTweet);
                }
                else
                	$newTweetsSlider.prepend(this.html);
                
                // WARNING: HACK BELOW
                // If these are new tweets and we are auto queueing, start adding them to the queue
                // Using a spoofed event triggering. Why not just trigger the event? cause it hasnt been bound yet :)
                if (newTweets && widget.broadcastAutoQueue) {
                    widget.broadcastAdd.call($newTweet.find(".tweetBroadcastAdd").get(0), {'data':{'_this':widget}});
                }
                
                                if (!widget.switchFilters && this.media && this.media != undefined && this.media != "") {
                    jCarousel.addFirstAndScroll(this.media);
                    
                    //update the video links and etc
                    checkThumbs();
                }
                                
                $('body').append('<div style="display:none;">VTL: ' + widget.viewableTweets.length + '</div>');
                
                // Check if we have too many tweets
                if(widget.viewableTweets.length > widget.maxViewableTweets) {
                    // If we do, shift the bottom one off
                    widget.viewableTweets.shift();
                    
                    
                    // Add the last element to our container of things to be removed
                    $oldTweetsContainer.prepend(widget.$tweetsList.children(".twitterFeedWidgetTweet:last"));
                }
            });
            
            //we are done with changing filters, reset it
            this.switchFilters = false;
            
            var newTweetsHeight = $newTweetsSlider.height()+'px';
            
                        
            // Animate the new tweets container down        
            $newTweetsContainer.css('height', '0').animate({'height':newTweetsHeight}, speed, 'swing');

            $newTweetsSlider.css('display', 'block').css('top','-'+newTweetsHeight).animate({'top':'0'}, speed, 'swing', function() {
                $newTweetsContainer.after($newTweetsSlider.children());
                widget.rebindTweetControls();
                $newTweetsContainer.remove();
            });

            // Animate the old tweets up
            $oldTweetsContainer.slideUp(speed, function() {
                $oldTweetsContainer.remove();

                // Call the callback if there is any
                if(callback != null) { callback.call(widget); }
            });
            
        } else {
        
            // Make a jQuery DOM instance for all our tweets going in and out
            
            var $oldTweetsContainer = $('<div class="tweetsContainer" style="position:relative; overflow:hidden;"></div>');
            var $oldTweetsSlider = $('<div class="tweetsSlider" style="width:100%;"></div>');
            $oldTweetsContainer.append($oldTweetsSlider);

            var $newTweetsContainer = $('<div class="tweetsContainer" style="display:none;"></div>');
        
            // Append new tweets container (sliding in from bottom)
            widget.$tweetsList.append($newTweetsContainer);
            // Prepend old tweets container (sliding out from top)
            widget.$tweetsList.prepend($oldTweetsContainer);
            
            
            // We need to go through these tweets newest to oldest
            for(var i = tweets.length-1; i >= 0; i--) {
                var tweet = tweets[i];
                // Push the bottom tweet onto the top of the tweets
                widget.viewableTweets.unshift(tweet);
                $newTweetsContainer.append(tweet.html);
                
                // Check if we have too many tweets
                if(widget.viewableTweets.length > widget.maxViewableTweets) {
                    // If we do, pop one off the top
                    widget.viewableTweets.pop();
                
                    // Add the last element to our container of things to be removed
                    $oldTweetsSlider.append(widget.$tweetsList.children(".twitterFeedWidgetTweet:first"));
                }
            }
            
            
            var oldTweetsHeight = $oldTweetsSlider.height();
            
            // Animate the old tweets container out   
            $oldTweetsContainer.css('height', oldTweetsHeight+"px").animate({'height':0}, speed, 'swing');

            $oldTweetsSlider.css('position', 'absolute').css('top','0').animate({'top':'-'+oldTweetsHeight+"px"}, speed, 'swing', function() {
                $oldTweetsContainer.remove();
            });

            // Animate the old tweets up
            $newTweetsContainer.slideDown(speed, function() {
                $newTweetsContainer.after($newTweetsContainer.children());
                $newTweetsContainer.remove();
                widget.rebindTweetControls();
                
                // Call the callback if there is any
                if(callback != null) { callback.call(widget); }
            });
        }
    },
    
    /**
     * rebindTweetControls - rebind the functionality for the tweet controls (reply, retweet, dm, etc)
     */
    rebindTweetControls:function() {
        var _this = this;
        
        if(_this.isTwubAdmin) {
            $(".tweetRemove").unbind('click').bind('click', {'_this':_this}, _this.tweetRemove);
            if (_this.isLiveEvent) {
                $(".tweetBroadcastAdd").unbind('click').bind('click', {'_this':_this}, _this.broadcastAdd);
            }
        }
        
        $("#" + _this.prefix + "twitterfeed .litnv:not(.pleaseLogIn):not(.RT)").unbind('click').click(function() {
            _this.pause();
        
        	var paramString = $(this).attr('href').split("?")[1];
        	var paramParts = paramString.split("&");
        	var keyValues = new Array();
        	var parts;

        	for(var i=0; i < paramParts.length; i++) {
        		parts = paramParts[i].split("=");
        		keyValues[(parts[0])] = parts[1];
        	}

        	var username = keyValues['status'].substring(1, keyValues['status'].indexOf('%20')) + ' ';
        	var statusID = keyValues['in_reply_to_status_id'];

        	tweetTwubReplyBox_tweetThisTwub.reply(username, statusID, 'R');

        	return false;
        });
        
        $("#" + _this.prefix + "twitterfeed .pleaseLogIn").unbind('click').click(function() {
            _this.pause();
        
        	$(this).html("Please Sign In");
        	return false;
        });

        $("#" + _this.prefix+ "twitterfeed .litnv.RT:not(.pleaseLogIn)").unbind('click').click(function() {
            _this.pause();
        
        	var paramString = $(this).attr('href').split("?")[1];
        	var paramParts = paramString.split("&");
        	var keyValues = new Array();
        	var parts;

        	for(var i=0; i < paramParts.length; i++) {
        		parts = paramParts[i].split("=");
        		keyValues[(parts[0])] = parts[1];
        	}

        	var r = new RegExp(String.fromCharCode(0), 'g');
        	var message = $.trim($.base64Decode(keyValues['status']).replace(/>/g, '=').replace(r,''));
        	var statusID = keyValues['in_reply_to_status_id'];

        	tweetTwubReplyBox_tweetThisTwub.reply(message, statusID, 'RT');

        	//Cancel default action
        	return false;
        });

        $("#" + _this.prefix + "twitterfeed .litnv.DM:not(.pleaseLogIn)").unbind('click').click(function() {
            _this.pause();
        
        	var paramString = $(this).attr('href').split("?")[1];
        	var paramParts = paramString.split("&");
        	var keyValues = new Array();
        	var parts;

        	for(var i=0; i < paramParts.length; i++) {
        		parts = paramParts[i].split("=");
        		keyValues[(parts[0])] = parts[1];
        	}

        	var message = $.trim(keyValues['status'].replace(/%20/g, ' ')) + ' ';
        	var statusID = keyValues['in_reply_to_status_id'];

        	tweetTwubReplyBox_tweetThisTwub.reply(message, statusID, 'DM');

        	//Cancel default action
        	return false;
        });
    },
    
    getLocalTime:function() {
        var d = new Date();
        return Math.round(d.getTime()/1000);
    },
    
    prettyTimeSince:function(secondsPast) {
        if (secondsPast < 0) {
            return '';
        }
        if (secondsPast == 1) {
            return '1 second ago';
        }
        if (secondsPast < 60) {
            return secondsPast + ' seconds ago';
        }
        if (secondsPast < 120) {
            return '1 minute ago';
        }
        if (secondsPast < 3600) {
            return Math.floor(secondsPast / 60) + ' minutes ago';
        }
        if (secondsPast < 7200) {
            return '1 hour ago';
        }
        if (secondsPast < 24*3600) {
            return Math.floor(secondsPast / 3600) + ' hours ago';
        }
        if (secondsPast < 2*24*3600) {
            return '1 day ago';
        }
        return Math.floor(secondsPast / (24*3600)) + ' days ago';
    }
};

var mainFeed_twitterFeedWidget;

var popup;
$(document).ready(function() {
    // Create the TwitterFeedWidget JS object
    mainFeed_twitterFeedWidget = new TwitterFeedWidget('mainFeed_', 'pk2upv', '95339-2695168', '', false, false, [{"feedId":"95339-2695168","hashTag":"#pk2upv","sinceId":1}]);
    
    // Initialize the feed with the first 10 tweets
    mainFeed_twitterFeedWidget.initialize([], []);
    
    });



$(document).ready(function() {
    $("#descriptionWidget_accessDenied").click(function() {
                $("#descriptionWidget_loginNotice").html('<div class="errorBox" style="margin-top:5px;">Please sign in to edit this twub.</div>');
                return false;
    });
});

function updateDescriptionCharLimit()
{
	var text = $('#descriptionWidget_EditText');
	
	var charsLeft = 140-(text.val().length + 1);
	if (charsLeft < 0) {
		charsLeft = 0;
		text.val(text.val().substr(0,139));
	}
	
	$('#descriptionWidget_totalChars').html(charsLeft);
}

function descriptionWidget_EditDescription()
{
        $("#descriptionWidget_loginNotice").html('<div class="errorBox" style="margin-top:5px;">Please join this Twub to edit this info.</div>');
    return false;
	}

function descriptionWidget_HideEdit()
{
	$('#descriptionWidget_TwubDescriptionEdit').hide();
	$('#descriptionWidget_editArea').show();
}

function descriptionWidget_SaveDescription()
{
	$('#descriptionWidget_TwubDescriptionText').html($('#descriptionWidget_EditText').val());
	$('#descriptionWidget_TwubDescriptionTitle').html($('#descriptionWidget_EditTitle').val());
	
	descriptionWidget_HideEdit();
	
	var obj = $('#descriptionWidget_SaveDescriptionButton');
	if ($(obj).attr('rel') == 'create') {
		$('#descriptionWidget_TwubDescription .createLink').hide();
		$('#descriptionWidget_TwubDescription .editLink').show();
		
		// make it say edit now
		$(this).attr('rel','edit');
	}
}







$(document).ready(function()
{
    $(".twubSideBar_iconLink").qtip({
       show: 'mouseover',
       hide: 'mouseout',
       style: {
           name: 'dark',
           tip: {
               corner: 'bottomMiddle',
               size: {
                   width: 8,
                   height: 8
               }
           },
           width: {
               max: 275
           },
           'font-weight':'bold',
           'text-align':'center'
       },
       position: {
           corner: {
               target:'topMiddle',
               tooltip:'bottomMiddle'
           }
       }
    });
    
    $("#twubSideBar_requestConferenceSuite").click(function()
    {
       var ajaxOptions = {
           'url':'http://twubs.com/ajax360/121840-6225920/requestConferenceSuite',
           'type':'POST',
           'data': {
               'currentTwub':'pk2upv'
           },
           'dataType':'html',
           'success':function(data)
           {
               $.facebox(data);
           }
       };
       
       $.ajax(ajaxOptions);
    });
    
    $("#twubSideBar_actionTweetButton").click(function()
    {
        $("#twubSideBar_actionTweet").show();
        $("#twubSideBar_actionEmbed").hide();
        $("#twubSideBar_actionStyle").hide();
    });
    
    $("#twubSideBar_actionStyleButton").click(function()
    {
                $("#twubSideBar_actionTweet").hide();
        $("#twubSideBar_actionEmbed").hide();
        $("#twubSideBar_actionStyle").show();
            });
    
    var myShareThis = SHARETHIS.addEntry({
        title:'Twitter Twub: #pk2upv',
        url:'http://twubs.com/pk2upv'
    },
    {
        button: false, 
        offsetLeft: -120,
        onmouseover: false
    });    
    myShareThis.attachButton($("#twubSideBar_actionShareButton").get(0));

});

$(document).ready(function() {
    $('#twubSideBar_inviteMyFollowers').click(function() {
    	sendInvite();
    });
    
    $('#twubSideBar_CancelInvite').click(function() {
    	$('#twubSideBar_actionTweet').hide();
    });
});

function sendInvite()
{  
	if ($(this).attr("disabled")) return false;
	
	//If user not logged in, display message
			$("#twubSideBar_actionTweetMessage").html('<div class="errorBox">Please sign in to send invite...</div>');
		$(this).attr("disabled", true).attr("value", 'Please sign in');
		return false;
	    	
	$.post("http://twubs.com/ajax360/121840-6225920/sendTweet", {'status': $("#twubSideBar_inviteText").val()}, 
		function(data){
			switch (data.results) {
				case "success":
					//Show Success Message
		    		$("#twubSideBar_actionTweetMessage").fadeOut().html('<div class="successBox">Invite has been sent.</div>').fadeIn();
		    		break;
				case "message":
					$("#twubSideBar_actionTweetMessage").fadeOut().html(data.message).css({'color' : '#999999'}).fadeIn();
					break;
				case "ERROR":
				default:
					$("#twubSideBar_actionTweetMessage").fadeOut().html('Error: Message could not be sent.').css({'color' : 'red'}).fadeIn();
					break;
			}
		}
		, "json");
}
$(document).ready(function() {
	
	$('#twubSideBar_submitAdminRequest').click(function(){
		var name = $('#twubSideBar_name').val(); 
		var email =$('#twubSideBar_email').val();
		
		if (name == '') {
			alert("Please provide your full name.");
			return;
		}
		
		if (email == '') {
			alert("Please provide your e-mail.");
			return;
		}
		
		$.ajax({
			type: 'POST', 
			url: 'http://twubs.com/ajax360/121840-6225920/requestEditor',
			data: 'twubName='+$('#twubName').val()+'&name='+name+'&email='+email+'&phone='+$('#twubSideBar_phone').val(),
	        success: function (data) {
	            $('#twubSideBar_requestAdminContent').html(data);
			}
		});
	});
});
$(document).ready(function() {
    $("#widgetNavigator_title_events").click(function() {
        window.location = 'http://twubs.com/pk2upv/events';
    });
});
$(document).ready(function() {
    $("#widgetNavigator_title_feed").click(function() {
        window.location = 'http://twubs.com/pk2upv/feed';
    });
});
$(document).ready(function() {
    $("#widgetNavigator_title_members").click(function() {
        window.location = 'http://twubs.com/pk2upv/members';
    });
});

var memberRefresh = false;

$(document).ready(function() {
	
	$('#joinTwub').click(function(){JoinTwub();});
	$('#leaveTwub').click(function(){LeaveTwub();});
	
	
	$(document).bind('close.facebox', function() {
	    if(memberRefresh) {
	        location.href = location.href;
	    }
	});
	
});

function JoinTwub()
{    
            $("#loginNotice").html('<div class="errorBox" style="margin-top:5px;">Please sign in to join this Twub.</div>');
        return false;
    }

function LeaveTwub()
{    
    memberRefresh = true;
    
    
    var params = {
        'twub':"pk2upv",
        'prefix':''
    };
    
    $.post("http://twubs.com/ajax360/63260-5177344/leave", params, function(data) {
                $.facebox(data);
            });
    return false;
}
function webLinks_webLinksModule()
{
    var _this = this;
    
    // Grab our DOM instances
    _this.$container = $("#webLinks_container");
    _this.$loginNotice = $("#webLinks_loginNotice");
    
    _this.$editButton = $("#webLinks_editButton");
    _this.$viewButton = $("#webLinks_viewButton");
    
    // Bind our events
    _this.$editButton.bind('click', {'_this':_this}, _this.edit);
    _this.$viewButton.bind('click', {'_this':_this}, _this.view);
}

webLinks_webLinksModule.prototype = {
    edit:function(e)
    {
        var _this = e.data._this;
                
                _this.$loginNotice.html('<div class="errorBox">Please sign in to edit web links.</div>');
            },
    view:function(e)
    {
        var _this = e.data._this;
                
        var ajaxOptions = {
            'url':'http://twubs.com/ajax360/306-3670016/view',
            'type':'POST',
            'data':{
                'twub':'pk2upv',
                'viewPrefix':'webLinks_'
            },
            'dataType':'html',
            'success':function(data) {
                _this.$editButton.show();
                _this.$viewButton.hide();
                _this.$container.html(data);
            }
        };
        
        $.ajax(ajaxOptions);
    }
};

// Create our instance of the web links module when the DOM is ready
var webLinks_webLinks;
$(document).ready(function() 
{
    var webLinks = new webLinks_webLinksModule();
})

$(document).ready(function() {
        $("#twubsTagsWidget_editTags").click(function() {
        $("#twubsTagsWidget_loginNotice").html('<div class="errorBox" style="margin-top:5px;">Please join this Twub to edit tags.</div>');
        return false;
    });
    
    });

function rssFeeds_RssLinksModule()
{
    var _this = this;
    
    // Grab our DOM instances
    _this.$container = $("#rssFeeds_container");
    _this.$loginNotice = $("#rssFeeds_loginNotice");
    
    _this.$editButton = $("#rssFeeds_editButton");
    _this.$viewButton = $("#rssFeeds_viewButton");
    
    // Bind our events
    _this.$editButton.bind('click', {'_this':_this}, _this.edit);
    _this.$viewButton.bind('click', {'_this':_this}, _this.view);
}

rssFeeds_RssLinksModule.prototype = {
    edit:function(e)
    {
        var _this = e.data._this;
                
                _this.$loginNotice.html('<div class="errorBox">Please sign in to edit rss feeds.</div>');
            },
    view:function(e)
    {
        var _this = e.data._this;
                
        var ajaxOptions = {
            'url':'http://twubs.com/ajax360/25-1703936/view',
            'type':'POST',
            'data':{
                'twubName':$('#twubName').val(),
                'viewPrefix':'rssFeeds_'
            },
            'dataType':'html',
            'success':function(data) {
                _this.$editButton.show();
                _this.$viewButton.hide();
                _this.$container.html(data);
            }
        };
        
        $.ajax(ajaxOptions);
    }
};

// Create our instance of the rss links module when the DOM is ready
var rssFeeds_RssLinks;
$(document).ready(function() 
{
    var RssLinks = new rssFeeds_RssLinksModule();
})

$(document).ready(function(){
	$('#adsenseRight_edit').click(function(){
		$.post('http://twubs.com/ajax360/212116-6684672/',{action:'edit',prefix:$('#adsenseRight_prefix').val()},function(data){
			$('#adsenseRight_container').html(data);
		});
	});
});

$(function() {
    hoverHeadshots();
    submitBadHeadshots();
});

//holds the item that is currently being hovered
var hover;

//holds the delay to close something
var hoverout;

//holds a reference to the current object
var _this;

//holds collection of users with bad headshots
var badHeadshots = [];

function hoverHeadshots() {
    $(".TwitterHeadShot").unbind().hover(
        function() {
            //if we are hovering a different headshot...the other window needs to be closed
            //(a hack to close the window if it is left open -- due to the closing delay, if I simply
            //clearTimeout(hoverout), then if I instantly hover on another headshot after I out on 
            //one, the window will stay open because i hovered on another $(".TwitterHeadShot"), and
            //as far as the script is concerned, the window should then remain open, this line stops
            //that behavior)
            if ($("#TwitterHeadShot[username="+$(this).attr("username")+"]").length == 0 && $(".twitterHeadShotInfo").length > 0)
                closeWindow();
            else if ($("#TwitterHeadShot[username="+$(this).attr("username")+"]").length > 0)
                clearTimeout(hoverout);
            
            _this = $(this);
            hover = setTimeout(function(){showInfo(_this);}, 750);
        },
        function() { //de-hover
            //stop any windows from opening when we are not hovering on something
            clearTimeout(hover);
            
            hoverout = setTimeout(function(){
                closeWindow();
            }, 250);
        }
    );
}

function closeWindow() {
    //fade out and remove the info dialog
    $(".twitterHeadshotInfo").fadeOut("fast", function() {
        $(this).remove();
        $(".qtip").remove();
    });
}

function follow(user) {
    $.ajax({
        url: "http://twubs.com/ajax360/63257-5046272/",
        type: "post",
        data: {"action": "follow", "user": user},
        success: function() {
            $(".TwitterHeadshotUserInfo input").hide();
            $(".TwitterHeadshotUserInfo .numFollowers").show();
        }
    });
}

function showInfo(item) {
    //don't show a headshot if one already exists
    if ($("#TwitterHeadShot").length > 0)
        return;
        
    //hover
    guy = $('<div/>').attr("id","TwitterHeadShot").attr("class","twitterHeadshotInfo").attr("username", item.attr("username"));
    guy.append('<div \
            <div id="arrow">&nbsp;</div> \
            <div id="title">'+item.attr("username")+'</div> \
            <div id="content"> \
                <img src="http://static.360hubs.com/images/spinner.gif" style="width: 16px; height: 16px;" /> \
            </div> \
    ');
    
    //add the pop-up box
    item.append(guy);
    
    //get the offset of the headshot
    offset = item.offset();
    
    //make the ajax request for info before we show so it gets a head start
    $.ajax({
        url: "http://twubs.com/ajax360/63257-5046272/",
        type: "post",
        data: {"action": "userInfo", "user": item.attr("rel")},
        success: function(data) {
            display = $(data);
            display.hide();
            guy.find("#content").html(display);
            display.slideDown("medium");
            
            guy.find(".twubTip").each(function() {
                var title = $(this).attr("title");
                var body = $(this).attr("rel");
                
                //make browser tooltips not show up
                $(this).attr("title", "").attr("rel", "");
                
                $(this).unbind().qtip({
                    show: 'mouseover',
                    hide: 'mouseout',
                    content: {
                        text: body,
                        title: {
                            text: title
                        }
                    },
                    position: {
                        adjust: {
                            y: -10,
                            x: 10
                        }
                    },
                    style: {
                        border: {
                            width: 5
                        },
                        tip: {
                            corner: 'leftTop'
                        },
                        name: 'blue',
                        width: 250
                    }
                });
            });
            
            $.ajax({
                url: "http://twubs.com/ajax360/63257-5046272/",
                type: "post",
                data: {"action": "posts", "user": display.attr("rel")},
                success: function(data) {
                    recentTweets = $(data);
                    recentTweets.hide();
                    display.find("#posts #content").html(recentTweets);
                    recentTweets.slideDown("medium");
                }
            });
        }
    });
    
    //fade in the info dialog
    guy
        .css("top", offset.top)
        .css("left", offset.left + item.outerWidth())
        .fadeIn("fast")
    ;
}

function noteBadHeadshot(item) {
    badHeadshots.push(item.closest(".TwitterHeadShot").attr('username'));
}

function submitBadHeadshots() {
    // wait a bit to give page load a chance to fail loading headshots
    setTimeout(
        function() {
            if (badHeadshots.length > 0) {
                $.ajax({
                    url: "http://twubs.com/ajax360/63257-5046272/",
                    type: "post",
                    data: {"action": "updateHeadshots", "userList": ""+badHeadshots}
                })
            }
        },
        5000
    );
}