var currentImage;
var currentImageID;
var DEFJSONURL = "/do.php";
$(document).ready(init);

function init(){
	setPos();
	
	$("#topmenubutton").click(function(){
		$("#expanded_toolbar_form").toggle();
	});
	
	$("#loginbutton").click(clickLogin);
	$("#forgot_password").click(forgotPassword);
	$("#save_button").click(updateUserSettings);	
	
}

function saveCurrentImage() {
	
}

function deleteCurrentImage(id_,sender){
	if (confirm("Delete this pic?")) {
		$("#image_toolbar").hide();
		$.getJSON(DEFJSONURL,
				{
					a:"deleteimage",
					id:id_
				}, 
				function(d) {	
					
					if (d.result == "ok") {
						$(sender).hide();
						
					}
				}
			);
		//alert(currentImageID);
	} else {
		$("#image_toolbar").hide();
	}
}

function onClickImage(imageid,sender) {
	$(".image_toolbar").css("visibility","hidden");
	
	$(sender).children(".image_toolbar").css("visibility","visible")
			
	/*currentImage = sender;
	currentImageID = imageid;
	var toolbox = $("#image_toolbar");
	toolbox.show();
	var pos = $(sender).offset();
	toolbox.css("left", pos.left)
	toolbox.css("top", pos.top);*/
}


function setPos() {
    $('#pics').masonry({ columnWidth: 12,animate:true });
}

function updateUserSettings() {
	var url = $("#url").val();
	var email = $("#email").val();
	var password = $("#password").val();	
	loginStatus("updating...");
	$.getJSON(DEFJSONURL,
		{
			a:"updateusersettings",
			e:email,
			u:url,
			p:password
		}, 
		function(d) {	
			loginStatus(d.result);
			
			
			$("#url").val(d.u);
			$("#email").val(d.e);
			$("#password").val(d.p);		
		}
	);
	
}

function onBlurImageCaption(v,id_,sender) {
	$.getJSON(DEFJSONURL,
			{
				a:"updatecaption",
				id:id_,
				value:v
			}, 
			function(d) {	
				$(sender).parent().css("visibility","hidden");
			}
		);
}

function forgotPassword() {
	var email = $("#email").val();
	loginStatus("Sending...");
	if (email){
		
		$.getJSON(DEFJSONURL,{a:"sendpassword",e:email}, function(d){
			loginStatus(d.result);
		});
	} else {
		loginStatus("Fill in your emailaddress and click again.");
	}
}

function logout() {
	$.getJSON(DEFJSONURL,{a:"logout"}, function(d){
		location.href="/";
		return true;
	});
}

function clickLogin() {
	var email_ = $("#email").val();
	var password_ = $("#password").val();
	var fileToSave = $("#f").val();
	
	if (email_ == "" || password_ == "") {
		loginStatus("Please enter email and password.");
		return false;
	}
	$.getJSON(
				DEFJSONURL,
				{
						a:"login",
						email:email_,
						password:password_
				}, 
	function(JSONData){
		//alert(JSONData.auth);
		switch(JSONData.auth){
			case "correct":
				var DEFDOMAIN = "http://symbolicbehavior.com/";
				loginStatus("Welcome back :)");
				if (fileToSave) {
					location.href= DEFDOMAIN+"?f=" + fileToSave;
				} else {
					location.href = DEFDOMAIN;
					
				}
				break;
				
			case "incorrect":
				loginStatus("I'm afraid I cannot do that.");
				break;
			
			case "needsconfirmation":
				loginStatus("Please confirm first by clicking the link in the email.");
				break;
				
			case "new":
				loginStatus("Creating new user...");
				$.getJSON(
						DEFJSONURL,
						{
								a:"createuser",
								e:email_,
								p:password_
						}, function(d){
						
							loginStatus(d.result);
						}
				);
				
			/*	if (fileToSave) {
					location.href="?f=" + fileToSave;
				} else {
				}*/
				break;
			
		}
		
	});
}


function loginStatus(s){
	with($("#status_message")) {
		if (!s || s == "") {
			s = "no message";
		}
		html(s);
		show();
	}
}


