/*
 * id: dialog element ID attribute
 * handler (optional): submit handler
 */
function Dialog(id) {
	this.id = id;
	this.element = $("#" + id);
	
	this.setTitle = function(title) {
		this.element.find(".dialog-header").html(title);
	}.bind(this);
	
	this.setContent = function(content) {
		this.element.find(".dialog-content").html(content);
	}.bind(this);
	
	this.setData = function(data) {
		this.element.find("input.dialog-data").val(data);
	}.bind(this);
	
	this.show = function() {
		this.element.fadeIn();
		//this.element.find("input[type=submit]").focus();
		this.element.find("a.close").focus();
		
	}.bind(this);
	
	this.hide = function() {
		this.element.fadeOut();
		$('#overlay').hide();
		return false;
	}.bind(this);
	
	this.move = function(y) {
		if (this.element.css("position") == "absolute") { /* IE6 */
			this.element.css("top", (y - this.element.height() / 2) + "px");
		}
	}.bind(this);
	
	this.open = function(event) {
		if (event) {
			var target = $(event.target);
			if (!target.is("a")) {
				target = target.closest("a");
			}
			this.setData(target.attr("href"));
			
			this.move(target.offset().top + target.height() / 2);
			
			event.preventDefault();
		}
		this.show();
	}.bind(this);
	
	this.close = function(event) {
		if (event) {
			event.preventDefault();
		}
		this.hide();
		$('#overlay').hide();
		return false;
	}.bind(this);
	
	this.handlerCb = null;
	
	this.addHandler = function(handler) {
		this.element.find("input.submit").click(handler);
		this.element.find("button.submit").click(handler);
	}.bind(this);
	
	this.addHandlerWrapper = function(event) {
		var ret = true;
		if ( typeof this.handlerCb == 'function' ) {
			ret = this.handlerCb.call(this,event);
		}
		event.preventDefault();
		if ( ret === false ) {
			return;
		}
		this.close(event);
	}.bind(this);

	this.addDismissHandler = function(handler) {
		this.element.find("input.reset").click(handler);
		this.element.find("button.reset").click(handler);
	}.bind(this);
	
	this.paragraph = function(text) {
		return "<p>" + text.replace(/\n/, "</p><p>") + "</p>";
	};
	
	/*
	 * title: heading title
	 * message: content message
	 * target: positioning element
	 * duration: duration in seconds
	 */
	this.showMessage = function(options) {
		if (options.title) {
			this.setTitle(options.title);
		}
		if (options.message) {
			this.setContent(this.paragraph(options.message));
		}
		
		if (options.target) {
			this.move($(options.target).offset().top);
		}
		
		this.show();
		
		if (options.duration) {
			setTimeout(this.hide, options.duration * 1000);
		}
	}.bind(this);
	
	/*
	 * title: heading title
	 * message: content message
	 * errors: detailed errors
	 * target: positioning element
	 */
	this.showErrors = function(options) {
		if (options.title) {
			this.setTitle(options.title);
		}
		if (options.message) {
			this.setContent(this.paragraph(options.message));
		}
		
		if (options.errors) {
			var errors = options.errors;
			
			var div = this.element.find("div.dialog-errors");
			var list = div.find("ul");
			list.empty();
			
			for (var x in errors) {
				if (errors.hasOwnProperty(x)) {
					list.append("<li>" + errors[x] + "</li>");
				}
			}
			
			div.show();
			div.find(".details a").click(function(event) {
				list.fadeIn();
				$(this).fadeOut();
				event.preventDefault();
			});
		}
		
		if (options.target) {
			this.move($(options.target).offset().top);
		}
		
		this.show();
	}.bind(this);
	
	if (arguments.length > 1) {
		this.handlerCb = arguments[1];
	}
	if (arguments.length > 2) {
		this.addDismissHandler(arguments[2]);
	}
	
	this.addHandler(this.addHandlerWrapper);
	this.element.find(".dialog-close").click(this.hide);
}


/*
 * id: input element ID attribute
 */
function Input(id) {
	this.id = id;
	this.element = $("#" + escapeId(id));
	this.form = this.element.closest("form");
	this.initial = this.element.val();

	this.doOnClear = [];
	this.doOnPrompt = [];
	this.atTwitter = false;
	this.atTwitterName="";
	this.isEmpty = function() {
		return this.element.val().trim().length < 1 || this.element.is(".empty");
	}.bind(this);
	
	this.value = function() {
		return this.element.val();
	}.bind(this);
	
	this.clear = function() {
		if(this.atTwitter==false){
			this.element.val("");
		}else{
			this.element.val("@"+this.atTwitterName+" ");
			
		}
		this.form.removeClass("empty");
		this.element.removeClass("empty");
		this.element.addClass("ready");
		for(var f in this.doOnClear) {
			this.doOnClear[f]();
		}
	}.bind(this);
	
	this.prompt = function() {
		this.element.removeClass("ready");
		this.element.addClass("empty");
		this.form.addClass("empty");
		this.element.val(this.initial);
		
		if (arguments.length < 1) {
			this.blur();
		}

		for(var f in this.doOnPrompt) {
			this.doOnPrompt[f]();
		}
	}.bind(this);
	
	this.focus = function() {
		this.element.focus();
		if(this.atTwitter==true){
			this.element.val(this.element.val()+" ");
		}
	}.bind(this);
	
	this.blur = function() {
		this.element.blur();
	}.bind(this);
	
	this.showTask = function() {
		this.element.val("");
		this.element.attr("disabled", "disabled");
		this.element.removeClass("ready");
		this.element.addClass("input-task");
	}.bind(this);
	
	this.hideTask = function() {
		this.element.removeClass("input-task");
		this.element.removeAttr("disabled");
		this.prompt();
	}.bind(this);
	
	this.onFocus = function(event) {
		if (this.element.is(".empty")) {
			this.clear();
		}
	}.bind(this);
	
	this.onBlur = function(event) {
		if (this.element.val().trim().length <= 0) {
			this.prompt(event);
		}
	}.bind(this);
	
	this.element.focus(this.onFocus);
	this.element.blur(this.onBlur);
}

