$(document).ready(function() {
	// Input Field from AndyStratton.com
    $("input.populate, textarea.populate").each(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    });
    $("input.populate, textarea.populate").focus(function() {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        }
    });
    $("input.populate, textarea.populate").blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    });
	
	// Gallery Info Hovers
	$(".gallery li a").hover( function() {
		$(this).contents("div.info").fadeIn("normal");
	}, function() {
		$(this).contents("div.info").fadeOut("fast");
	});
	
	// Sidebar Browse Gallery
	var menuHeight = new Array();
	var height;
	var index;
	$("#sidebar .browse ul:gt(0)").each( function() {
		height = $(this).height();
		index = $("#sidebar .browse ul").index(this);
		menuHeight[index] = height;
		$(this).height(20);
	});
	$("#sidebar .browse label").toggle( 
	function() {
		index = $("#sidebar .browse ul").index($(this).next("ul"));
		height = menuHeight[index];
		$(this).next("ul").animate({height: height}, 500);
		$(this).children("span").addClass("expanded");
	},
	function() {
		$(this).next("ul").animate({height: '20'}, 500);
		$(this).children("span").removeClass("expanded");
	});
	
	// Rating Stars
	$(".star-holder.select .star.select").mouseenter( function() {
		$(this).prevAll(".select").add(this).css({backgroundPosition: '0px 0px'});
		$(this).nextAll(".select").css({backgroundPosition: '0px -16px'});
	});
	$(".star-holder").mouseleave( function() {
		$(this).children(".select").css({backgroundPosition: '0px -16px'});
		$(this).children(".selected").css({backgroundPosition: '0px 0px'});
	});
	
	// Tags List
	$("ul#tags-list li").not("ul#tags-list li:last").append(",&nbsp;");
	
	// UI Image Lightbox
	$(".lightbox").lightBox({
		imageLoading: 'http://www.wowuigallery.com/my-templates/wowuigallery/images/lightbox-ico-loading.gif',
		imageBtnClose: 'http://www.wowuigallery.com/my-templates/wowuigallery/images/lightbox-btn-close.gif',
		imageBtnPrev: 'http://www.wowuigallery.com/my-templates/wowuigallery/images/lightbox-btn-prev.gif',
		imageBtnNext: 'http://www.wowuigallery.com/my-templates/wowuigallery/images/lightbox-btn-next.gif',
		imageBlank: 'http://www.wowuigallery.com/my-templates/wowuigallery/images/lightbox-blank.gif'
	});
   
	// Gallery Init - set the first gallery image to be selected and height of containing ul to fit image
	$(".uiGallery li:first").addClass("selected");

	// Gallery Manager - swap images when a thumbnail is clicked
	$(".uiThumbnails li").click( function() {
		var thumbIndex = $(".uiThumbnails li").index(this);
		var galleryIndex = $(".uiGallery li").index( $("li.selected") );
		
		if (thumbIndex != galleryIndex)
		{	
			// fade out currently selected image and remove selected and first class
			$(".uiGallery li:eq(" + galleryIndex + ")").fadeOut();
			$(".uiGallery li:eq(" + galleryIndex + ")").removeClass("selected");
			$(".uiGallery li:eq(" + galleryIndex + ")").removeClass("first");
			
			// fade in new image and add selected class
			// if browser is Opera begin to animate height of containing ul to fit new image
			// we check because Opera will return incorrect height before the new image fades in
			$(".uiGallery li:eq(" + thumbIndex + ")").fadeIn(400, function() {
				if ( isOpera() ) {
					$(".uiGallery").animate({height: $(".uiGallery li:eq(" + thumbIndex + ")").height()});
				}
			});
			$(".uiGallery li:eq(" + thumbIndex + ")").addClass("selected");
			
			// animate height of containing ul for all other browsers
			// this looks smoother than doing it after the new image fades in
			if ( !isOpera() )
				$(".uiGallery").animate({height: $(".uiGallery li:eq(" + thumbIndex + ")").height()});
		}
	});
	
	// Post Form Validation
	jQuery.validator.messages.required = "";
	jQuery.validator.messages.email = "";
	$("#postform").validate();
	$("#loginForm form").validate();
	$("#passwordRecoveryForm form").validate();
	$("#contactForm").validate();
});

function isOpera() {
	if (navigator.userAgent.indexOf('Opera') > -1)
		return true;
	else
		return false;
}


