﻿//*************************************************************************************
// File     : braingnat.js
// Version  : 0.0.7
// Requires : jquery.js (version 1.2.6+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : July 29, 2008
// Modified : October 3, 2008
// Purpose  : BrainGnat - A collection of handy solutions and functions for problems and 
//            desired functionality for client websites by Mindfly.
//*************************************************************************************

/* List of components */

// setContentHeight 	- Adjusts height of page content to make it fit in browser.
// fadeinSlideshow  	- Loops a series of images in a slideshow.
// ajaxLoadCrossfade 	- Ajax call to a document that lists the images for use in a slideshow
// stickyList			- Gives list items the class 'stuck' when they are hovered over and in lists with the '.stickyList' class.

// setContentHeight 
//
// Called by the page load or resize. If the document is too short to fill the window, it resizes the content to
// ensure that it is tall enough.
function setContentHeight(verticalOffset) {
    if(!verticalOffset) {
        verticalOffset = 0;
    }
	var minContentHeight = $(window).height() - ($('#branding').height() + $('#site_info').height() + verticalOffset);
	if($('#content').height() < minContentHeight) {
		$('#content').height(minContentHeight);
	}
} // end of function setContentHeight()


// fadeinSlideshow(elem as string, imageList as Array, optional slideDuration as Integer, optional fadeSpeed as Integer, optional current as Integer)
//
// This function does a loop of images that fade from one to another. The imageloop goes to a
// wrapper "elem" (usually a div) and an image in the wrapper (these should have the same dimensions
// and overlap one another. The fade loops through all the urls listed in the array ImageList.
function fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current) {
    // get the length of the image array.
    var listSize = imageList.length;
    // If there's no current image selected, or the value is out of the range of the
    // slideshow, then set the current image to zero.
    if (!current || current >= listSize) current = 0;
    // If there's no slide duration set, set it to 5 seconds.
    if (!slideDuration) slideDuration = 5000;
    // If there's no fade speed set, set it to 1 second.
    if (!fadeSpeed) fadeSpeed = 1000;
    // Set the image's source to the current image's url.
    $(elem + " img").attr("src", imageList[current]);
    // If the current element is at the maximum of the element size, then set the
    // wrapper's background (aka, the next image) to the first image.
    if (current >= (listSize - 1)) {
        $(elem).css("background", "transparent url(" + imageList[0] + ") no-repeat");
    } else {
        // If not, set the next image in the list to the background of the wrapper.
        $(elem).css("background", "transparent url(" + imageList[current + 1] + ") no-repeat");
    }
    // Hold the current image for a period of time equal to slideDuration. Once that's done, then
    // fade the current image's opacity until the background image shows. Once that is done, then
    // call this same function again with the next image in line.
    $(elem + " img").animate({ opacity: "1" }, slideDuration).animate({ opacity: "0.01" }, fadeSpeed, function() { $(this).css("opacity", "1"); fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current + 1) });
} // end of function fadeinSlideshow()



// ajaxLoadCrossfade(file as String, wrapper as String, optional slideDuration as Integer, optional fadeSpeed as Integer)
//
// Does an ajax call to the file, and then loads the results to the crossfade function in the element 'wrapper'.
function ajaxLoadCrossfade(file, wrapper, slideDuration, fadeSpeed) {
    if (!slideDuration) slideDuration = 5000;
    if (!fadeSpeed) fadeSpeed = 1000;
    $.get(file, function(data) { fadeinSlideshow(wrapper, data.split(" "), slideDuration, fadeSpeed); });
} // end of function ajaxLoadCrossfade()


// stickyList()
//
// Makes the list items in the menus tagged with the class 'stickyList' acquire the class 'stuck' when
// hovered over. The list items lose the class 'stuck' when the mouse hovers over a different list item.
function stickyList() {
    $('.stickyList > li').bind('mouseover', function(){
        $('.stuck').removeClass('stuck');
        $(this).addClass('stuck');
     } );

} // end of function stickyMenus()

