/* stop IE6 flicker */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}

// Return true if browser is Internet Explorer and version 6 or earlier
$.IE6Below = function() {
	if ($.browser.msie) {
		try {
			if (ScriptEngineMajorVersion() <= 5 && ScriptEngineMinorVersion() < 7) { // ie 6- only
				return true;
			}
		} catch(err) { return false; };
		return false;
	} else {
		return false;
	};
};

// Return true if browser is Safari 2.0 or earlier
$.Safari2Below = function() {
	if ($.browser.safari) {
		var kitName = "applewebkit/";
		var tempStr = navigator.userAgent.toLowerCase();
		var pos		  = tempStr.indexOf(kitName);
		var isAppleWebkit = (pos != -1);
		var kitVersion = 0		
		var kitVersion = tempStr.substring(pos + kitName.length,tempStr.length);
				kitVersion = kitVersion.substring(0,kitVersion.indexOf(" "));
				kitVersion = kitVersion.substring(0,3);
		return (kitVersion < 420) ? true : false;
	};
};

// add default text functionality to input fields
$(function(){ // (executes when the dom is ready)
	$('.input-default-text').each(function(i) {
		var defaultValue = $(this).attr('value');
		$(this).focus(function () { if (this.value == defaultValue) this.value = ''; this.style.color = '#333333' });
		$(this).blur(function () { if (this.value == '') { this.value = defaultValue; this.style.color = '#666666' }});
	});
});



// ** Rotating backgrounds on home page **
// **                                   **
$(function(){ // (executes when the dom is ready)
	
	var initialized = false;
	var current_image_number = 0;
	var rotating_images = new Array();
	var initial_rotation_delay = 5000;
	var rotation_delay = 5000;
	var animation_speed = 2000;

	$.rotateBgImages = function() {
		// if we haven't initialized let's do that now
		if (! initialized)
		{			
			// find ".background" images and add them to our array
			$("#fade").each(function(i) {
				rotating_images[i] = this;
				$(this).css("z-index",300-i);
				if (i>0) $(this).css("visibility","hidden");
			});
			initialized = true;
			setTimeout('$.rotateBgImages()', initial_rotation_delay);
			return;
		}
		// if the first image doesn't exist, just exit
		if (! rotating_images[0]) return;
		// if the first image has downloaded, continue
		if (rotating_images[0].complete || rotating_images[0].complete == undefined)
		{
			// set the next image we should load
			var next_image_number = current_image_number + 1;
			if (next_image_number >= rotating_images.length) next_image_number = 0;
			// has this image been loaded
			if (rotating_images[next_image_number].complete || rotating_images[next_image_number].complete == undefined)
			{
				// ok, switch to this new image
				$(rotating_images[current_image_number]).fadeOut(animation_speed);
				$(rotating_images[next_image_number]).css("visibility","visible").fadeIn(animation_speed);
				// set the Find More link to the rel attribute of the image (so images can link to different pages)				
				$("#fade").attr("href",$(rotating_images[next_image_number]).attr("rel"));
				// change the current image to this one
				current_image_number = next_image_number;
			};
			setTimeout('$.rotateBgImages()', rotation_delay);
		}
		else
		{
			// not downloaded yet, check back later
			setTimeout("$.rotateBgImages()", rotation_delay);
		};
	};
	
	// start rotation
	$.rotateBgImages();
});
