/* 
 *  Wood Wise 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();
    
    $('#nav').menuify();
    
    $('#rotator').rotate(500);
    
    $('#gallery').portfolio();
    
    $('a[href=#popup]').colorbox({
            transition: 'none',
            inline: true,
            href:"#popup",
            width: "350px"
    });
    
});


// Clears the default text when an input receives
// focus and reinstates it if it is left blank
(function($) {
 
    $.fn.portfolio = function() {	
        return this.each(function() {
            var gallery = $(this);
            var mainImg = $('#mainPhoto', gallery);
            var thumbs = $('#mainThumbs li a', gallery);
            
            // Pre-load each of the big images
            thumbs.each(function() {
                // Insert the image
                var bigImage = $('<div class="imgWrapper"><img src="' + $(this).attr('href') + '" alt="" /></div>')
                                    .appendTo('#mainPhoto')
                                    .hide()
                                    .css('position', 'absolute');
                
                // Setup the click handler
                $(this).click(function() {
                    $('#mainPhoto .imgWrapper').not(bigImage).fadeOut(500);
                    bigImage.fadeIn(500);
                    
                    thumbs.removeClass('current');
                    $(this).addClass('current');
                    return false;
                });
            }).filter(':first').click();
        });
    };
	
})(jQuery);


// 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);


// Show and hide submenus
(function($) {
 
	$.fn.menuify = function() {
		
		// iterate each matched element
		return this.each(function() {
			menu = $(this);
			
			// Show the submenus on click
			menu.children('li:has(ul)').hover(
                function() {
                    $(this).children('ul').fadeIn(50);
                },
                function() {
                    $(this).children('ul').fadeOut(50);
                }
			).children('ul').hide();
		});
	};
	
})(jQuery);


// Fades from one image to another
(function($) {
 
    $.fn.rotate = function(time) {	
        return this.each(function() {
            list = $(this);
            items = list.children();
            
            items.css('position', 'absolute').not(':first').hide();

            // Set a timer for the next fade
            var t = setTimeout('$.fn.rotate.startFade()', 5000);
        });
    };
    
    $.fn.rotate.startFade = function() {
        // Get the current and the next image
        var current = items.filter(':visible');
        var next = current.next()
        var next = (next.length) ? next : items.filter(':first');
        
        // Do the fade
        current.fadeOut(1200);
        next.fadeIn(1200);
        
        // Set a timer for the next fade
        var t = setTimeout('$.fn.rotate.startFade()', 5000);
    }
	
})(jQuery);