// JavaScript Document

$.fn.roundbox = function() {
		//alert($(this).html());
	var top = $("<div class='roundbox-top'><div></div></div>");
	var bottom = $("<div class='roundbox-bottom'><div></div></div>");
	var content = $("<div class='content'>" + $(this).html() + "</div>");
	
	var roundin = $("<div class='roundbox-inside'></div>");
	
	$(this).html("");
	top.appendTo(roundin);
	content.appendTo(roundin);
	bottom.appendTo(roundin);
	roundin.appendTo($(this));	
	$(this).addClass('roundbox');

};

$.fn.indices = function() {
	$(this).addClass("indices");
	var titles = $(this).siblings("h2");
	if(titles.length > 0) {
		for(i = 0; i < titles.length; i++) {
			var titleanchor = $("<a name='title" + i +  "'></a>");
			//$(titleanchor).attr("name", "title" + i);
			$(titleanchor).insertBefore(titles[i]);
			var toTop = $("<div id='backtotop'><a href='#indices'>↑Top</a></div>");
			$(toTop).insertAfter(titles[i]);
		}
		var caption = $("<span id='caption'><a name='indices'/>----Contents----</span>");
		caption.appendTo($(this));
		var buttonhide = $("<span id='buttonhide'>Click to Hide</span>");
		var titlelist = $("<ul id='titlelist'></ul>");
		titlelist.appendTo($(this));
		for(i = 0; i < titles.length; i++) {			
			var titleitem = $("<li><a href='#title" + i + "'>" + $(titles[i]).text() + "</a></li>");
			titleitem.appendTo(titlelist);
		}
		buttonhide.appendTo($(this));
		//buttonhide.hide();
		//titlelist.hide();
	}
	$(caption).bind("click", function () {
									   if($(titlelist).is(":hidden")) {
										   $(titlelist).show();
										   $(buttonhide).show();
									   }
									   else {
										   $(titlelist).hide();
   										   $(buttonhide).hide();
									   }
									   });
	$(buttonhide).bind("click", function() {										 
										   $(titlelist).hide();
   										   $(buttonhide).hide();
										 });
	$(caption).hover(function() {
							  $(this).css("cursor","pointer");
							  $(this).css("color","red");
							  },
					function() {
						$(this).css("color", "#000099");
					});
	$(buttonhide).hover(function() {
								 $(this).css("text-decoration", "underline");
								 $(this).css("cursor", "pointer");
								 },
						function() {
								 $(this).css("text-decoration", "none");
								}
						);
};

// JavaScript Document
$.fn.slideNote = function() {
	
	$(this).addClass("note");
	$(this).children().filter("h3").click(function() {
														   var note = $(this).next("div");
														   if($(note).is(":hidden")) {
																		 $(note).slideDown("fast");
															}
															else {
																$(note).slideUp("fast");
															}
																		 
														   }
														   );
	$(this).children().filter("h3").hover(
										  function() {
											  $(this).css("cursor", "pointer");
											  $(this).css("color", "red");
											  },
										  function() {
											  $(this).css("color", "white");
											  }
										  );
}

$.fn.hline = function() {
		$(this).each(function(){
			$(this).addClass("compact");
			$(this).addClass("h-line");
			var bodypart = $(this).find("tbody");
			var rows = bodypart.find("tr");
			for(i = 0; i < rows.length; i++) {
				if(i == 0) {
					$(rows[i]).children("td").each(function() {
															if($(this).attr("rowspan") + i == rows.length) {
																$(this).addClass("bottom");
															}
															else {
																$(this).addClass("top");
															}
															});
				}
				else if(i == rows.length - 1) {
					$(rows[i]).children("td").each(function() {
															$(this).addClass("bottom");
															});
				}
				else {
					$(rows[i]).children("td").each(function() {
															if($(this).attr("rowspan") + i == rows.length) {
																$(this).addClass("bottom");
															}
															else {
																$(this).addClass("middle");
															}
															});
				}
			}
	  });
};