var DoSomething = {
		
	DSO : {},
	timer : 0,
		
	buildList : function(dest, ListObj) {

		var thisObj = this;

		$.each(ListObj, function(i) {
			// Create verbs ul
			$("#" + dest).append("<ul class='DS_Menu DS_VerbList' id='DSM_V" + i +"'></ul>");
			$.each(this, function(j) {
				var vO = this;
				var sA = this.nouns[0].action;
				$("#" + dest).append("<li class='DS_Option' id='DSO_V"+ j +"'>" + this.verb + "</li>");
				$("#DSO_V"+ j).appendTo("ul#DSM_V"+ i);
				$("#DSO_V" + j).click(function() {
					if (!($(this).attr("id") == ($("ul#DSM_V"+ i + " li:first-child").attr("id")))) {
						thisObj.replaceGo(sA);
						thisObj.setNounList(j, dest, vO.nouns);
					}
					thisObj.toggleListOptions(this);
					thisObj.setPreposition(vO.preposition);
				});
				$("ul.DS_Menu li:not(:first-child)").hide();
			});
		});
		
	},

	embed : function(DSO, dest) {
		this.DSO = DSO;
		this.buildList(dest, DSO);
		this.setDefaults(dest, DSO);
	},
	
	moveGo : function() {
		$("#DS_Go").insertAfter("ul.DS_NounList"); // check after() examples
	},
	
	removeAllNounLists : function() {
		$("ul.DS_NounList").remove();
		$("ul.DS_NounList li.DS_Option").remove();
	},

	replaceGo: function(action) {
		var thisObj = this;
		$("#DS_Go").remove();
		$("ul.DS_NounList").parent().append("<div id='DS_Go'></div>");
		$("#DS_Go").click(function(){
			action();
		});

	},
	
	setDefaults : function (dest, ListObj) {
		this.setPreposition(ListObj.verbs[0].preposition);
		this.setNounList(0, dest, ListObj.verbs[0].nouns);
		this.replaceGo(ListObj.verbs[0].nouns[0].action);		
		var thisObj = this;
		
		$("ul.DS_Menu li:first-child").each(function() {
			thisObj.toggleDropdownIcons(this, "closed");
		});
		
		this.setSnapbackEvents();
	},
	
	setNounList : function(id, dest, ListObj) {
		this.removeAllNounLists();
		
		var thisObj = this;
		
		$("#" + dest).append("<ul class='DS_Menu DS_NounList DSO_V" + id + "' id='DSM_N" + id +"'></ul>");

		
		$.each(ListObj, function(k) {
			$("#" + dest).append("<li class='DS_Option' id='DSO_N"+ id + "_" + k +"'>" + this.noun + "</li>");
			$("#DSO_N" + id + "_" + k).appendTo("#DSM_N" + id);
			var sA = this.action;
			$("#DSO_N" + id + "_" + k).click(function() {
				thisObj.toggleListOptions(this);
				thisObj.replaceGo(sA);
			});
		});
		$("ul#DSM_N" + id + " li:not(:first-child)").hide();
		thisObj.toggleDropdownIcons($("ul#DSM_N" + id + " li:first-child"), "closed");
		thisObj.moveGo();
		thisObj.setSnapbackEvents();
	},
	
	setSnapbackEvents : function () {
	
		var thisObj = this;
		
		$(".DS_Menu").unbind("mouseover");
		$(".DS_Menu").unbind("mouseout");
		
		$(".DS_Menu").mouseover(function() {
			clearTimeout(this.timer);
		});

		$(".DS_Menu").mouseout(function() {
			if ($(this).hasClass("DS_OpenMenu")) {
				clearTimeout(this.timer);
				var thisId = $(this).attr("id");
				this.timer = setTimeout(function() {
					thisObj.toggleList($("#"+thisId +" li:first-child"));
				}, 2000);
			} else {
				clearTimeout(this.timer);
			}
		});
	},
	
	setPreposition: function(prep) {
		$("#DS_Preposition").remove();
		$("ul.DS_VerbList").after("<div id='DS_Preposition'>" + prep + "</div>");
	},
	
	toggleList : function($d) {
	
		var $thisId = $($d).attr("id");
		var $parentId = ($($d).parent().attr("id"));

		if ($($d).parent().hasClass("DS_OpenMenu")) {
			$($d).parent().removeClass("DS_OpenMenu");
			$($d).siblings().fadeOut("fast");
			this.toggleDropdownIcons($d, "closed");
		} else {
			$($d).parent().addClass("DS_OpenMenu");
			$("#" + $parentId + " li:not(#"+$thisId+")").fadeIn("fast");
			this.toggleDropdownIcons($d, "open");
		}
	},

	toggleListOptions: function($d) {
		var $thisId = $($d).attr("id");
		var $parentId = ($($d).parent().attr("id"));
		$("#" + $($d).attr("id")).insertBefore("#" + $($d).siblings(":first").attr("id"));
		this.toggleList($d);
	},
	
	toggleDropdownIcons: function($d, state) {
		var $firstChild = $($d).parent().children(":first");
		if ($($d).parent().hasClass("DS_VerbList")) {
			$("#VerbListIcon").remove();
			$firstChild.prepend("<div id='VerbListIcon'></div>");
			if (state == "open") {
				$("#VerbListIcon").addClass("ListIconOpen");
			} else {
				$("#VerbListIcon").addClass("ListIconClosed");
			}
		} else {
			$("#NounListIcon").remove();
			$firstChild.prepend("<div id='NounListIcon'></div>");
			if (state == "open") {
				$("#NounListIcon").addClass("ListIconOpen");
			} else {
				$("#NounListIcon").addClass("ListIconClosed");
			}
		}
		
	}

}