String.prototype.htmlSpecialChars = function() { return this.replace(/\</g,'&#060;').replace(/\>/g,'&#062;'); }
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

function alternate() {
	$("ul").each(function(){
		$("li:odd", this).not(".even").addClass("odd");
		$("li:even", this).not(".odd").addClass("even");
	});
}

$(document).ready(function() {

	alternate();

	$(".editable.title").editable(
		function(value, settings) {
			if ( value != 'My Drop' ) {
				$("#title").attr('value', value);
			}
			return value;
	 	}, {
	 	onblur   : 'submit',
		type     : 'text',
		tooltip  : 'Click to edit...',
		cssclass : 'editbox'
	});

	extras.load();

	$("#upload_form").submit(function() {
		var options = {
			beforeSubmit:  function() {},
			success:       upload.finished,
			url:           '_ajax/upload',
			dataType:      'json',
			iframe:        true,
			semantic:      true
		};

		upload.id = $("#progress-id").attr('value');
		$("#form-body").hide("normal").fadeOut("normal");
		$("#progress").show("normal").fadeIn("slow");
		upload.timeout = setTimeout('upload.progress()', 800);
		$(".error").each(function() {
			$(this).hide("normal").fadeOut("normal");
		});

		$(this).ajaxSubmit(options);
		return false;
	});

	$("#create_fdorm").submit(function() {
		var options = {
			//beforeSubmit:  function() {$("#error").hide("fast");},
			success:       function (response) {$('#content').html(response)},
			url:           '_ajax/create'
		};

		$(this).ajaxSubmit(options);
		return false;
	});

});

var extras = {
	load : function (){
		$("#description-toggle").click(function (){
			$("#extras").append('<p id="extras-description" class="feild hide corner"><label for="description" class="small">Description</label><textarea name="description" id="description" class="description"></textarea><a href="#" class="close corner">x</a></p>');

			$("#extras #extras-description .close").each(function() {
				$(this).click(function() {
					$("#extras #extras-description").hide("slow").fadeOut("slow");
					$("#description-toggle").show("slow").fadeIn("slow");
					$("#extras #extras-description").remove();
					return false;
				});
			});
			$("#extras #extras-description").show("fast").fadeIn("slow");
			$(this).hide().fadeOut("slow");
			return false;
		});

		$("#password-toggle").click(function (){
			$("#extras").append('<p id="extras-password" class="feild hide corner"><label for="password" class="small">Password</label><input type="password" name="password" id="password" class="password"><a href="#" class="close corner">x</a></p>');

			$("#extras #extras-password .close").each(function() {
				$(this).click(function() {
					$("#extras #extras-password").hide("slow").fadeOut("slow");
					$("#password-toggle").show("slow").fadeIn("slow");
					$("#extras #extras-password").remove();
					return false;
				});
			});
			$("#extras #extras-password").show("fast").fadeIn("slow");
			$(this).hide("slow").fadeOut("slow");
			return false;
		});

		$("#imap-toggle").click(function (){
			$("#extras").append('<p id="extras-imap" class="feild hide corner"><label for="imapserver" class="small">IMAP Server</label><input type="text" name="imapserver" id="imapserver" class="imapserver"><a href="#" class="close corner">x</a><br><label for="imapdomain" class="small">IMAP Domain</label><input type="text" name="imapdomain" id="imapdomain" class="imapdomain"><a href="#" class="close corner">x</a></p>');

			$("#extras #extras-imap .close").each(function() {
				$(this).click(function() {
					$("#extras #imapserver").hide("slow").fadeOut("slow");
					$("#imap-toggle").show("slow").fadeIn("slow");
					$("#extras #extras-imap").remove();
					return false;
				});
			});
			$("#extras #extras-imap").show("fast").fadeIn("slow");
			$(this).hide("slow").fadeOut("slow");
			return false;
		});
	},

	// todo
	toggleButton : function (){
	},

	// todo
	addFeild : function (label, name, type){
		if ( type == 'textarea' ) {
		}
		else {
			$("#extras").append('<p id="extras-'+ name +'" class="feild hide corner"><label for="'+ name +'" class="small">'+ label +'</label><input type="'+ type +'" name="'+ name +'" id="'+ name +'" class="'+ name +'"><a href="#" class="close corner">x</a></p>');
		}
	}
};

var upload = {

	timeout : null,

	id : null,

	progress : function() {
		$.get(
			'_ajax/progress/' + upload.id,
			null,
			function (status){
				// done, cancel, percent, error
				if (status) {
					$("#progress-title").html('Upload Progress');
					$("#progress-text").html(status['pct'] + '%');
					$("#progress-bar").css('width', status['pct'] + "%");
					$("#progress-file").html('<p>Uploading ' + $("#drop_file").attr('value') + ', please wait.</p>');

				}
				else {

				}
			},
			"json"
		);

		upload.timeout = setTimeout("upload.progress()", 200);
	},

	finished : function(response)  {
		clearTimeout( upload.timeout );

		if ( response['error'] ) {
			upload.error(response['error']);
		}
		else if ( response['success'] ) {
			$("#progress-file").html(response['success']);
			$("#progress-title").html('Upload Complete');
			$("#progress-bar").css('width', '0');
			$("#progress-text").html('100%');
			$("#recent-drops").load("_ajax/recent li");
			$("#current-session").show("slow").fadeIn("slow");
			$("#current-session ul").append(response['current']);
			upload.reset();
			alternate();
		}
		else {
			upload.error('Sorry, something went bad, try again.');
		}
	},

	reset : function() {
		$("#upload_form").clearForm();
		$("#my-drop").html('My Drop');
	},

	error : function (errStr) {
		$("#progress").hide("fast");
		$("#error").html('<p class="error hide corner small">' + errStr + '</p>');
		$(".error").each(function() {
			$(this).show("slow").fadeIn("slow");
		});
		$("#form-body").show("slow").fadeIn("slow");
	}
};
