From e0d72d5795a5f0896231f0efb0fe0372be24f188 Mon Sep 17 00:00:00 2001
From: Shish Moom <shish@shishnet.org>
Date: Tue, 7 Jul 2009 07:40:59 -0700
Subject: [PATCH] get rid of shimmie's JS lib, use jquery

---
 ext/index/theme.php             |   7 +-
 ext/upload/theme.php            |   9 ++-
 lib/jquery.form-defaults.js     |  41 ++++++++++
 lib/shimmie.js                  | 136 --------------------------------
 themes/default/layout.class.php |  17 ++--
 5 files changed, 63 insertions(+), 147 deletions(-)
 create mode 100644 lib/jquery.form-defaults.js
 delete mode 100644 lib/shimmie.js

diff --git a/ext/index/theme.php b/ext/index/theme.php
index 5408bf85..4b73dfc5 100644
--- a/ext/index/theme.php
+++ b/ext/index/theme.php
@@ -76,9 +76,14 @@ EOD;
 		$h_index = "<a href='".make_link()."'>Index</a>";
 		$h_next = ($page_number >= $total_pages) ? "Next" : "<a href='".make_link("post/list$query/$next")."'>Next</a>";
 
-		$h_search_string = count($search_terms) == 0 ? "Search" : html_escape(implode(" ", $search_terms));
+		$h_search_string = html_escape(implode(" ", $search_terms));
 		$h_search_link = make_link();
 		$h_search = "
+			<script>
+			$(document).ready(function() {
+				$(\"#search_input\").DefaultValue(\"Search\");
+			});
+			</script>
 			<p><form action='$h_search_link' method='GET'>
 				<input id='search_input' name='search' type='text'
 						value='$h_search_string' autocomplete='off' />
diff --git a/ext/upload/theme.php b/ext/upload/theme.php
index a67642f0..03ca2737 100644
--- a/ext/upload/theme.php
+++ b/ext/upload/theme.php
@@ -85,15 +85,20 @@ class UploadTheme extends Themelet {
 			if($i == 0) $style = ""; // "style='display:visible'";
 			else $style = "style='display:none'";
 			$upload_list .= "<input accept='image/jpeg,image/png,image/gif' size='10' ".
-				"id='data$i' name='data$i' $style onchange=\"showUp('data".($i+1)."')\" type='file'>\n";
+				"id='data$i' name='data$i' $style onchange=\"$('#data".($i+1)."').show()\" type='file'>\n";
 		}
 		$max_size = $config->get_int('upload_size');
 		$max_kb = to_shorthand_int($max_size);
 		// <input type='hidden' name='max_file_size' value='$max_size' />
 		return "
+			<script>
+			$(document).ready(function() {
+				$(\"#tag_input\").DefaultValue(\"tagme\");
+			});
+			</script>
 			<form enctype='multipart/form-data' action='".make_link("upload")."' method='POST'>
 				$upload_list
-				<input id='tagBox' name='tags' type='text' value='tagme' autocomplete='off'>
+				<input id='tag_input' name='tags' type='text' autocomplete='off'>
 				<input type='submit' value='Post'>
 			</form>
 			<div id='upload_completions' style='clear: both;'><small>(Max file size is $max_kb)</small></div>
diff --git a/lib/jquery.form-defaults.js b/lib/jquery.form-defaults.js
new file mode 100644
index 00000000..7ef5a9fe
--- /dev/null
+++ b/lib/jquery.form-defaults.js
@@ -0,0 +1,41 @@
+jQuery.fn.DefaultValue = function(text){
+    return this.each(function(){
+		//Make sure we're dealing with text-based form fields
+		if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
+			return;
+		
+		//Store field reference
+		var fld_current=this;
+		
+		//Set value initially if none are specified
+        if(this.value=='') {
+			this.value=text;
+		} else {
+			//Other value exists - ignore
+			return;
+		}
+		
+		//Remove values on focus
+		$(this).focus(function() {
+			if(this.value==text || this.value=='')
+				this.value='';
+		});
+		
+		//Place values back on blur
+		$(this).blur(function() {
+			if(this.value==text || this.value=='')
+				this.value=text;
+		});
+		
+		//Capture parent form submission
+		//Remove field values that are still default
+		$(this).parents("form").each(function() {
+			//Bind parent form submit
+			$(this).submit(function() {
+				if(fld_current.value==text) {
+					fld_current.value='';
+				}
+			});
+		});
+    });
+};
\ No newline at end of file
diff --git a/lib/shimmie.js b/lib/shimmie.js
deleted file mode 100644
index 3b26466e..00000000
--- a/lib/shimmie.js
+++ /dev/null
@@ -1,136 +0,0 @@
-var defaultTexts = new Array();
-
-window.onload = function(e) {
-	var sections=get_sections();
-	for(var i=0;i<sections.length;i++) toggle(sections[i]);
-
-	initGray("search_input", "Search");
-	initGray("commentBox", "Comment");
-	initGray("tagBox", "tagme");
-	
-	// if we're going to show with JS, hide with JS first
-	pass_confirm = byId("pass_confirm");
-	if(pass_confirm) {
-		pass_confirm.style.display = "none";
-	}
-}
-
-
-function initGray(boxname, text) {
-	var box = byId(boxname);
-	if(!box) return;
-
-	var clr = function () {cleargray(box, text);};
-	var set = function () {setgray(box, text);};
-
-	addEvent(box, "focus", clr, false);
-	addEvent(box, "blur",  set, false);
-
-	if(box.value == text) {
-		box.style.color = "#999";
-		box.style.textAlign = "center";
-	}
-	else {
-		box.style.color = "#000";
-		box.style.textAlign = "left";
-	}
-}
-
-function cleargray(box, text) {
-	if(box.value == text) {
-		box.value = "";
-		box.style.color = "#000";
-		box.style.textAlign = "left";
-	}
-}
-function setgray(box, text) {
-	if(box.value == "") {
-		box.style.textAlign = "center";
-		box.style.color = "gray";
-		box.value = text;
-	}
-}
-
-function showUp(elem) {
-	e = document.getElementById(elem)
-	if(!e) return;
-	e.style.display = "";
-//	alert(e.type+": "+e.value);
-	if(e.value.match(/^http|^ftp/)) {
-		e.type = "text";
-		alert("Box is web upload");
-	}
-}
-
-
-/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
-*                              LibShish-JS                                  *
-\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-
-function addEvent(obj, event, func, capture){
-	if (obj.addEventListener){
-		obj.addEventListener(event, func, capture);
-	} else if (obj.attachEvent){
-		obj.attachEvent("on"+event, func);
-	}
-}
-
-
-function byId(id) {
-	return document.getElementById(id);
-}
-
-
-function getHTTPObject() { 
-	if (window.XMLHttpRequest){
-		return new XMLHttpRequest();
-	}
-	else if(window.ActiveXObject){
-		return new ActiveXObject("Microsoft.XMLHTTP");
-	}
-}
-
-function ajaxRequest(url, callback) {
-	var http = getHTTPObject();
-	http.open("GET", url, true);
-	http.onreadystatechange = function() {
-		if(http.readyState == 4) callback(http.responseText);
-	}
-	http.send(null);
-}
-
-
-/* get, set, and delete cookies */
-function getCookie( name ) {
-	var start = document.cookie.indexOf( name + "=" );
-	var len = start + name.length + 1;
-	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
-		return null;
-	}
-	if ( start == -1 ) return null;
-	var end = document.cookie.indexOf( ";", len );
-	if ( end == -1 ) end = document.cookie.length;
-	return unescape( document.cookie.substring( len, end ) );
-}
-	
-function setCookie( name, value, expires, path, domain, secure ) {
-	var today = new Date();
-	today.setTime( today.getTime() );
-	if ( expires ) {
-		expires = expires * 1000 * 60 * 60 * 24;
-	}
-	var expires_date = new Date( today.getTime() + (expires) );
-	document.cookie = name+"="+escape( value ) +
-		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
-		( ( path ) ? ";path=" + path : "" ) +
-		( ( domain ) ? ";domain=" + domain : "" ) +
-		( ( secure ) ? ";secure" : "" );
-}
-	
-function deleteCookie( name, path, domain ) {
-	if ( getCookie( name ) ) document.cookie = name + "=" +
-			( ( path ) ? ";path=" + path : "") +
-			( ( domain ) ? ";domain=" + domain : "" ) +
-			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
-}
-
diff --git a/themes/default/layout.class.php b/themes/default/layout.class.php
index d6723ff2..4b3b39c5 100644
--- a/themes/default/layout.class.php
+++ b/themes/default/layout.class.php
@@ -87,15 +87,16 @@ EOD;
 			<script>
 			$(document).ready(function() {
 				$(\"#$i-toggle\").click(function() {
-					$(\"#$i\").slideToggle(\"slow\");
-					if($(\"#$i\").is(\":hidden\")) {
-						$.cookie(\"$i-hidden\", true);
-					}
-					else {
-						$.cookie(\"$i-hidden\", false);
-					}
+					$(\"#$i\").slideToggle(\"slow\", function() {
+						if($(\"#$i\").is(\":hidden\")) {
+							$.cookie(\"$i-hidden\", 'true', {path: '/'});
+						}
+						else {
+							$.cookie(\"$i-hidden\", 'false', {path: '/'});
+						}
+					});
 				});
-				if($.cookie(\"$i-hidden\")) {
+				if($.cookie(\"$i-hidden\") == 'true') {
 					$(\"#$i\").hide();
 				}
 			});