/* Copyright (c) 2009 you-vs-me.com
 *-----------------------------------------------------*/
 
$(document).ready(function() {
	$(".login-dialog").click(function(){
		if($("#overlay").css("display") == "none") {
			login();
		} else {
			closeOverlay();
		}
		
		return false;
	});
	
	/* Ajax Functions */
	function login() {
		var params = '';
	 	
		jQuery.ajax({
			url: '/app/ajax/login',
			type: 'GET',
			data: params,
			complete: function(transport) {
				var response = transport.responseText || "no response";
				
				openOverlay(response);
				$("#username:visible:first").focus();
			}
		});
	}
	
	$(".search-button").toggle(function() {
		$("#topic-form").slideUp("fast");
		$("#search-form").slideDown("fast");
	}, function() {
		$("#search-form").slideUp("fast");
		$("#topic-form").slideDown("fast");
	});
	
	function search() {
		var params = '';
		
		jQuery.ajax({
			url: '/app/ajax/search',
			type: 'GET',
			data: params,
			complete: function(transport) {
				var response = transport.responseText || "no response";
				
				$('#topic').html(response);
			}
		});
	}
	
	$(".follow-user").bind("click", followUser);
	$(".un-follow-user").bind("click", unFollowUser);
	
	function followUser() {
		var params = 'follow_user_id=' + $(this).attr("id");
		
		jQuery.ajax({
			url: '/app/ajax/follow-user',
			type: 'GET',
			data: params,
			complete: function(transport) {
				var response = transport.responseText || "no response";
				
				$('#follow').html(response);
				$(".un-follow-user").bind("click", unFollowUser);
			}
		});
	}
	
	function unFollowUser() {
		var params = 'follow_user_id=' + $(this).attr("id");
		
		jQuery.ajax({
			url: '/app/ajax/un-follow-user',
			type: 'GET',
			data: params,
			complete: function(transport) {
				var response = transport.responseText || "no response";
				
				$('#follow').html(response);
				$(".follow-user").bind("click", followUser);
			}
		});
	}
	
	$(".delete-shout").click(function() {
		deleteShout($(this).attr("id"));
	});
	
	function deleteShout(ids) {
		var params = 'ids=' + ids;
		
		jQuery.ajax({
			url: '/app/ajax/delete-shout',
			type: 'GET',
			data: params,
			complete: function(transport) {
				var response = transport.responseText || "no response";
				
				if(response == 'SUCCESS') {
					openOverlay("This shout has been removed");
					$('#' + ids).remove();
				}
			}
		});
	}
	
	$(".load-follow-info").click(function() {
		loadInfo($(this).attr("id"), 'followed');
	});
	
	$(".load-network-info").click(function() {
		loadInfo($(this).attr("id"), 'networked');
	});
	
	function loadInfo(ids, div) {
		var params = 'ids=' + ids + '&div=' + div;
		
		jQuery.ajax({
			url: '/app/ajax/load-info',
			type: 'GET',
			data: params,
			complete: function(transport) {
				var response = transport.responseText || "no response";
				
				$('#' + div).html(response);
			}
		});
	}
	
	$(".load-voter-info").click(function() {
		loadVoterInfo($(this).attr("id"), 'votes');
		return false;
	});
	
	function loadVoterInfo(userId, div) {
		var params = 'user_id=' + userId + '&div=' + div;
		
		jQuery.ajax({
			url: '/app/ajax/load-voter',
			type: 'GET',
			data: params,
			complete: function(transport) {
				var response = transport.responseText || "no response";
				
				$('#' + div).html(response);
			}
		});
	}
	
	$(".comment-submit").click(function() {
		var string = $("#shouttext").val();
		
		if(!string.length) {
			openOverlay('You can not submit an empty shout.');
			return false;
		} else if(string.length > 255) {
			openOverlay('Hey. Your shout is a little long! Please shorten it and try again.');
			return false;
		}
	});
});