$(document).ready(function() {

  if ($("#left_container").length > 0 && $("#right_container").length > 0) {
    var right_width = $("#right_container").outerWidth();
    var total_width = $(window).width() - right_width - 60;
    $("#left_container").width(total_width);
  }
  
  $("div.centered_box, div.index_box").each(function() {
    var width = $(this).outerWidth();
    var height = $(this).outerHeight();
    $(this).css("margin-left", -(width / 2)).css("margin-top", -(height / 2));
  });
  
  $("div.index_box").hide();
  $("a#about_link").toggle(function() {
  	 $("div.index_box").fadeIn();	
  }, function() {
  	 $("div.index_box").fadeOut();
  });
  
  $("a").live("mouseover", function() {
  	if (!$(this).hasClass("post") && ($(this).text().indexOf("?") > 0) && $(this).text().lastIndexOf("?") == ($(this).text().length - 1)) {
  		var newText = $(this).text().substring(0, $(this).text().length - 1) + "!";
  		$(this).text(newText);
  	}
  }).live("mouseout", function() {
  	if (!$(this).hasClass("post") && ($(this).text().indexOf("!") > 0) && $(this).text().lastIndexOf("!") == ($(this).text().length - 1)) {
  		var newText = $(this).text().substring(0, $(this).text().length - 1) + "?";
  		$(this).text(newText);
  	}
  });
  
  $(".section_header").attr("open", false).append("<div class='indicator'></div>").next(".section_content").hide();
  $(".section_header").hover(function() {
    if ($(this).attr("open") == "true")
      var newText = "-";
    else
      var newText = "+";
  	$(this).children(".indicator").text(newText);
  }, function() {
    var newText = "";
  	$(this).children(".indicator").text(newText);
  }).click(function() {
    if ($(this).attr("open") == "true") {
      $(this).next(".section_content").stop().slideUp("fast");
      $(this).attr("open", false);
      var newText = "+";
  	  $(this).children(".indicator").text(newText);
    }
    else {
      $(".section_header[open='true']").trigger("click").trigger("mouseout");
      $(this).next(".section_content").stop().slideDown("fast");
      $(this).attr("open", true);
      var newText = "-";
  	  $(this).children(".indicator").text(newText);
    }
    return false;
  });
  $(".section_header").eq(0).trigger("click").trigger("mouseout");
  
  $("form").each(function() {
  	$(this).find(".submit").click(function() {
  		$(this).parents("form").submit();
  	});
  	
  	$(this).find("input").keydown(function(event) {
  		if (event.keyCode == 13) {
  			$(this).parents("form").submit();
  		}
  	});  
  });
  
  /*$(".content_header li").click(function() {
  	if ($(this).hasClass("selected")) {
  		$(".content_header li").fadeIn("fast");
  	}
  	else {
  		var that = $(this);
  		$(".content_header li").removeClass("selected").hide();
  		$(this).addClass("selected").show();
  		that.parent().prepend(that);
  	}
  	
  });*/
  
  $("#follow_link").click(function() {
  	var follow_url = $(this).attr("href");
  	var follow_text = $("#follow_text").text();
  	var follow_html = $(this).html();
  	var that = $(this);
  	$(this).stop().fadeOut();
  	$.get(follow_url, '', function(successful) {
		if (successful) {
			if (follow_url.indexOf("unfollow") >= 0) {
				follow_url = follow_url.replace("unfollow", "follow");
				follow_html = follow_html.replace("Unfollow", "Follow");
				follow_text = follow_text.replace("are ", "are not ");
			}
			else {
				follow_url = follow_url.replace("follow", "unfollow");
				follow_html = follow_html.replace("Follow", "Unfollow");
				follow_text = follow_text.replace("are not ", "are ");
			}
			that.html(follow_html);
			that.attr("href", follow_url);
			$("#follow_text").text(follow_text);
			that.stop().fadeIn();
		}
		else {
			alert("?");
			that.fadeIn();
		}
	});
	return false;
  });
  
  $("#add_user_tag").click(function() {
  	var new_tag = $("#new_user_tag").val();
  	if (new_tag != "") {
  		$.get("/add_user_tag.php?tag=" + new_tag, '', function(new_tag_id){
  			if (new_tag_id > 0) {
  				$("#user_tag_container").append("<div id='user_tag" + new_tag_id + "' class='user_tag'>" + new_tag + "<div class='delete_tag'>x</div></div>");
  				$("#no_tags").hide();
  			}
  		});
  		$("#new_user_tag").val("");
  	}  
  });
  
  $("#new_user_tag").keydown(function(event) {
  	if (event.keyCode == 13) {
  		$("#add_user_tag").trigger("click");	
  	}
  });
  
  $(".delete_tag").live("click", function() {
  	var tag_id = $(this).parent().attr("id").replace("user_tag", "");
  	$.get("/delete_user_tag.php?tag=" + tag_id, '', function(data){
  		//alert(data);
  	});
  	$(this).parent().fadeOut();
  });
  
  $("#user_search").click(function() {
  	var query = $("#user_query").val();
  	if (query != "") {
  		$(this).html("<img src='/img/ajax-loader.gif' />");
  		$.get("/user_search.php?query=" + query, '', function(search_results){
  			var users = [];
  			eval("users = " + search_results);
  			fillUsers(users);
  			$("#content_title").html("Search results for: " + query + "<div class='remove_search'>x</div>");
  			$("#user_search").html("Go");
  		});	
  	}
  });
  
  $("#user_query").keydown(function(event) {
  	if (event.keyCode == 13) {
  		$("#user_search").trigger("click");	
  	}
  });
  
  $("#post_search").click(function() {
  	var query = $("#post_query").val();
  	if (query != "") {
  		$(this).html("<img src='/img/ajax-loader.gif' />");
  		$.get("/post_search.php?query=" + query, '', function(search_results){
  			var posts = [];
  			eval("posts = " + search_results);
  			$("div.container").html("");
  			fill(posts);
  			$("#content_title").html("Search results for: " + query + "<div class='remove_search'>x</div>");
  			$("#post_search").html("Go");
  		});	
  	}
  });
  
  $("#post_query").keydown(function(event) {
  	if (event.keyCode == 13) {
  		$("#post_search").trigger("click");	
  	}
  });
  
  $(".remove_search").live("click", function() {
  	$("#content_title").html("Recent posts from users you follow.");
  	$("div.container").html("");
  	$("#post_query, #user_query").val("");
  	$.get("/retrieve_from_followers.php", '', function(search_results){
  			var posts = [];
  			eval("posts = " + search_results);
  			fill(posts);
  		});	
  });
  
  $("#save_user_info").click(function() {
  	var email = $("#edit_email").val();
  	var location = $("#edit_location").val();
  	var bio = $("#edit_bio").val();
  	$.post("/edit_user_info.php", {'email':'"' + email + '"','location':'"' + location + '"','bio':'"' + bio + '"'}, function(results){
  			if (results != "") {
  				$("#edit_user_info_error").html(results);
  			}
  			else {
  				$("#edit_user_info_error").text("Success!");
  				$("#current_email").text(email);
  				$("#current_location").text(location);
  				$("#current_bio").text(bio);
  			}
  	});	
  });
  
  $("#open_edit").click(function() {
	$("#edit_personal_info").fadeIn();
  	$("#veil").width($(window).width()).height($(window).height()).fadeIn();
  	return false;
  });
  
  $("#close_edit").click(function() {
	$("#edit_personal_info").fadeOut();
  	$("#veil").fadeOut();
  	return false;
  });
  
  $("#veil").click(function() {
  	$(this).fadeOut();
  	$(".centered_box").fadeOut();
  });
  
  $(".user_link").live("click", function(evnt) {
  	var e = (window.event) ? window.event : evnt;
	e.cancelBubble = true;
	window.location.href = $(this).attr("href");
	return false;
  });
});