
$(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/twubseventsinfo';
			}
            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() {
	$("#13_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() {

    $("#eventRegistration_submitButton").click(function() {
        $("#eventRegistration_form").submit();
    });
    
    $("#eventRegistration_form").submit(function() {
        
        var fail = false;
        
        $(".eventRegistration_inputRequired").each(function() {
            var $this = $(this);
            
            var $row = $this.parents(".eventRegistration_formRow");
            var $label = $row.find(".eventRegistration_label");
            
            if($this.val().length == 0) {
                $label.addClass("eventRegistration_labelError");
                fail = true;
            } else {
                $label.removeClass("eventRegistration_labelError");
            }
        });
        
        if(fail) {
            $("#eventRegistration_message").html('<div class="errorBox">One or more required fields were left empty.</div>');
            return false;
        }
        
        $("#eventRegistration_submitBusy").show();
        
        var ajaxOptions = {
            'url':'http://twubs.com/ajax360/84903-5636096/',
            'type':'POST',
            'data':$("#eventRegistration_form input, #eventRegistration_form textarea").serialize(),
            'success':function(data) {
                $("#eventRegistration_form").hide();
                $("#eventRegistration_submitMessage").html(data);
            }
        };
        
        $.ajax(ajaxOptions);
        
        return false;
    });

});
$(document).ready(function() {
    $("#widgetNavigator_title_").click(function() {
        window.location = 'http://twubs.com/';
    });
});
$(document).ready(function() {
    $("#widgetNavigator_title_twubsearch").click(function() {
        window.location = 'http://twubs.com/twubsearch';
    });
});
$(document).ready(function() {
    $("#widgetNavigator_title_twubscontact").click(function() {
        window.location = 'http://twubs.com/twubscontact';
    });
});
$(document).ready(function() {
    $("#widgetNavigator_title_twubseventsinfo").click(function() {
        window.location = 'http://twubs.com/twubseventsinfo';
    });
});
$(document).ready(function() {
	$('#definitionWidget_showHow').click(function() {
		$('#selectHow').click();
	});
});

$(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
    );
}