$(document).ready(function(){
	$(".login_btn").live("click", function(){
		$.facybox({ div: "#login"});
		
		Cufon.replace('h2', {
						textShadow: '#ccc 1px 1px'
		});
		
		return false;
	});
	
	$(".area:first").show();
	
	$(".search_tab").live("click", function(){
		$(".search_tab").each(function(){
			$(this).removeClass("active");
		})
		
		var id = $(this).attr("href");
		
		$(this).addClass('active');
		
		$(".area").hide();
		
		$(id).show();
		
		return false;
	})
	
	$("input[type='text']").live("focus", function(){
		if($(this).hasClass("waterMark"))
		{
			$(this).removeClass("waterMark");
			$(this).val("");
		}
	});
	

					
	$(".hearts").live("click", function(){
		
		var id = $(this).attr("id").split("_");
		var h = $(this).attr("data");
		var coolness = this;
		if (id[1] == "")
		{
			var tct = "Something went horribly wrong. Please refresh the page and try again.";

			$.notifyBar({
				html: tct,
				delay: 2000,
				animationSpeed: "normal"
			  });
		}
		else
		{
			var args = "public_id="+id[1]+"&prev="+h;
			$.ajax({
				url: "/shares/like",
				type: "POST",
				data: args,
				dataType: "json",
			    error: function(html)
				{
					var tct = "Something went horribly wrong. Please refresh the page and try again.";

					$.notifyBar({
						html: tct,
						delay: 2000,
						animationSpeed: "normal"
					  });
				},
				success: function(html)
				{
					if (html.result == "success")
					{
						$(coolness).attr("data", html.msg);
						var txt = '<img src="/images/hearts.png" height="16" width="16" alt="Like"> ' + html.msg;
						$(coolness).html(txt);
					}
					else
					{
						$.notifyBar({
							html: html.msg,
							delay: 2000,
							animationSpeed: "normal"
						  });
					}
				}
			});
			
		}
		
		return false;
	})
	
	
});

function login()
{
	$(".bottom").hide();
	$(".loader").show();
	
	var args = $(".login_form:last").serialize();
	
	$.ajax({
		url: "/session/new",
		type: "POST",
		data: args,
		dataType: "json",
	    error: function(html)
		{
			$(".loader").hide();
			$(".bottom").show();
			
			
			$(".alertBad").html("<p>Something went wrong. Please try again.</p>");
			$(".alertBad").fadeIn("slow");
		},
		success: function(html)
		{
			if (html.result == "success")
			{
				window.location.href = "/home";
			}
			else if (html.result == "tokens")
			{
				window.location.href = "/account/authorize";
			}
			else if(html.result == "auth")
			{
				$(".loader").hide();
				$(".bottom").show();
				$(".alertBad").html("<p>"+html.msg+"</p>");
				$(".alertBad").fadeIn("slow");
			}
			else
			{
				$(".loader").hide();
				$(".bottom").show();
				$(".alertBad").html("<p>"+html.msg+"</p>");
				$(".alertBad").fadeIn("slow");
			}
		}
	});
	
	return false;
}
