// Simon Willison's addLoadEvent, stolen from Jeremy Keith's book 'DOM scriptin'
// buy it--it's good stuff
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
		window.onload = func;	
	else
		window.onload = function() {
			oldonload();
			func();
		}
}

// image captions
// hot stuf!! Get an image's alt text and place it below the image as a caption.
// number all the images nicely. Completely unobtrisive! I rule!
function addImageCaptions(numbering, label, class, limit_width) {
	
	// first check if all the necessary functions are available
	// if not: quit immediatly for this browser sucks
	if (!document.createElement) 		return false;
	if (!document.createTextNode)		return false;
	if (!document.getElementById)		return false;
	if (!document.getElementsByTagName)	return false;
	
	// check the function arguments and apply default values
	if (!numbering)		numbering 	= true;			// by default apply numbering
	if (!label)			label 		= 'Image';		// by default use the label 'image'
	if (!class)			class 		= 'captioned';	// by default take all images with classname 'captioned'
	if (!limit_width)	limit_width = true;		// by default don't make the caption exactly as wide as the image itself
	
	// find all images with classname CLASS--they need to be pimped
	// then process all images to make sure that all images-to-be-pimped
	// actually have an ALT-text set. Without ALT-text there's no caption...
	// We also check for the right classname
	var images = document.getElementsByTagName('img');
	var images_to_caption = Array();
	if (!images)	return false;
	for ( var i = 0 ; i < images.length; i++) {
		if ( images[i].nodeType == 1 && images[i].getAttribute('alt')) {
			if ( !class || (class && images[i].className.indexOf(class) != -1 ) )
				images_to_caption[i] = images[i];
		}
	}
	images = images_to_caption;

	// now we've got all the images we're looking for we are going to loop through
	// them all and perform the following actions:
	// 1. extract the ALT-text
	// 2. finalize the caption. e.g. : <span class="image_caption" id="caption_image_1"><strong>image 1:</strong> my caption</span>
	// 3. limit the width of the caption if applicable
	// 4. insert the caption after the image
	var image_number = 0;
	for ( var i = 0; i < images.length; i++) {
		if ( !images[i] )
			continue;
		else
			image_number++;

		// 1
		var caption_text = images[i].getAttribute('alt');

		// 2
		var caption_label = label + ' ' + image_number + ': ';
		var caption = document.createElement('span');
		caption.className = 'image_caption';
		caption.setAttribute('id', 'caption_' + label + '_' + image_number);
		var lbl = document.createElement('strong');
		lbl.appendChild(document.createTextNode(caption_label));
		caption.appendChild(lbl);
		caption.appendChild(document.createTextNode(caption_text));
		
		// 3
		if ( limit_width ) {
			caption.style.width = '' + images[i].getAttribute('width') + 'px';
		}

		// 4
		if ( images[i].parentNode.lastChild == images[i])
			images[i].parentNode.appendChild(caption);
		else
			images[i].parentNode.insertBefore(caption, images[i].nextSibling);
	}
	
	// finished! I rule!
}

// display text in the searchbox when there is no default text,
// and make it disappear when the user wants to enter text into
// the text box
function smartSearchBox() {
	if ( !document.getElementById)	return false;
	
	var search_box = document.getElementById('query');
	if ( !search_box)	return false;
	
	default_value = search_box.getAttribute('value');
	if ( default_value.length < 1 ) {
		search_box.setAttribute('value', 'Search this site...');
		search_box.onfocus = function() {
			this.setAttribute('value', '');	
		}
		search_box.onblur = function() {
			this.setAttribute('value', 'Search this site...');
		}
	}
}

addLoadEvent(addImageCaptions);
addLoadEvent(smartSearchBox); 