/* 
 *  Elaine Marshall Scripts
 *  Site by New Media Campaigns
 */

$(document).ready(function($) {
    
    // Open external links in a new window
	$('a[href^="http://"]').attr("target", "_blank");
	
	// Clear textboxes on focus
    $('.autoclear').autoClear();
    
	// Setup the nav drop-downs
	$('#nav').dropDown();
	
	// Cycle home page images
	$('#homeFeature ul').cycle({
		timeout:8000
	});
	
});


// Clears the default text when an input receives
// focus and reinstates it if it is left blank
(function($) {
 
    $.fn.autoClear = function() {	
        return this.each(function() {
            $(this).focus(function() {
                if( this.value == this.defaultValue ) {
                    this.value = "";
                }
            })
            .blur(function() {
                if( !this.value.length ) {
                    this.value = this.defaultValue;
                }
            });
        });
    };
	
})(jQuery);


// Sets up an elements's children as drop-down menus
(function($) {

	$.fn.dropDown = function(options) {
	
		// build main options before element iteration
		var opts = $.extend({}, $.fn.dropDown.defaults, options);

		// iterate each matched element
		return this.each(function() {
			menu = $(this);
			
			// Show the submenus on click
			menu.children('li:has(ul)').hover(
				function() {
					$(this)
						.addClass(opts.active_class)
						.children('ul').animate(opts.show, opts.show_speed);
				},
				function() {
					$(this)
						.removeClass(opts.active_class)
						.children('ul').animate(opts.hide, opts.hide_speed);
				}
			).children('ul').hide();
		});
	};

	// Default options
	$.fn.dropDown.defaults = {
		show: {opacity: 'show'}, 	// Effect to use when showing the sub-menu
		show_speed: 300,			// Speed of the show transition
		hide: {opacity: 'hide'}, 	// Effect to use when hiding the sub-menu
		hide_speed: 200,			// Speed of the hide transition
		active_class: 'open'		// Class to give open menu items
	};

})(jQuery);