var Sands = {};
Sands.preload_images = function () {
	Sands._preloaded = [];
	$('img').each(function () {
		var self = this;
		var img = new Image();
		img.onload = function () {
			$(self).bind('mouseover', Sands.image_swap);
			$(self).bind('mouseout', Sands.image_swap);
		}
		img.src = this.src.replace(/\.(gif|png|jpg)/, "-over.$1");
	});
}
Sands.image_swap = function (event) {
	if (event.type == 'mouseover') {
		this.src = this.src.replace(/\.(gif|png|jpg)/, "-over.$1");
	} else {
		this.src = this.src.replace('-over', '');
	}
}
Sands.init_form_hints = function () {
	/* Initialize display of <span class="hint">...</span> elements in form
	 * fields
	 */
	$('.hint').each(function () {
		var hint = this;
		var label = $(this).closest('label')[0];
		var input;
		if ($(label).attr('for')) {
			input = $('#' + $(label).attr('for')).eq(0);
		} else {
			input = $(label).find('input').eq(0);
		}
		if (input.length == 0) {
			return;
		}

		input.wrap('<div style="display: inline; position: relative"></div>');
		input.before(hint);
		$(hint).css({
			position: 'absolute',
			left: '3px',
			top: '3px'
		});
		if (input[0].value) {
			$(this).hide();
		}
		if (!input[0].disabled) {
			$(hint).bind('click', function() { $(hint).hide(); input.focus(); });
			$(input).bind('click', function() { $(hint).hide(); });
			$(input).bind('focus', function() { $(hint).hide(); });
			$(input).bind('blur', function () { if (this.value == '') { $(hint).show() } });
		}
	});
}

// Initialize row hover effect on search results
Sands.init_hover_styles = function () {
	$('#SearchResults tr.e, #SearchResults tr.o').each(function () {
		$(this).bind('mouseover', function () { $(this).addClass('hover') })
		$(this).bind('mouseout', function () { $(this).removeClass('hover') })
	});
}

// Focus the first input box in the content area.
Sands.focus_first = function () {
	$('#Content input.error, #Content select.error')
		.add('#Content input[type=text], #Content select')
		.eq(0)
		.focus();
}
$(Sands.preload_images);
$(Sands.init_form_hints);
$(Sands.init_hover_styles);
//$(Sands.focus_first);
