﻿//*************************************************************************************
// File     : braingnat.js
// Version  : 0.1.2
// Requires : jquery.js (version 1.2.6+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : July 29, 2008
// Modified : December 11, 2008
// Purpose  : BrainGnat - A collection of handy solutions and functions for problems and 
//            desired functionality for client websites by Mindfly.
//*************************************************************************************

/* List of methods */

// 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.
// randomBackgroundImage- Add a randomly selected background image from a provided array to the provided element.
// randomImage          - Set the source of the provided image tag to the a randomly selecte image from the provided array.

var BrainGnat = function() {
    return {
        version: "0.1.2",
        test: function() {
            alert('BrainGnat version ' + BrainGnat.version + ' is loading correctly.');
        },
        setContentHeight: function(verticalOffset) {
            if (!verticalOffset) {
                verticalOffset = 0;
            }
            var minContentHeight = $(window).height() - ($('#branding').height() + $('#site_info').height() + verticalOffset);
            if ($('#content').height() < minContentHeight) {
                $('#content').height(minContentHeight);
            }
        },
        slideshow: {
            fadein: function(elem, imageList, slideDuration, fadeSpeed, current) {
                var listSize = imageList.length;
                if (!current || current >= listSize) current = 0;
                if (!slideDuration) slideDuration = 5000;
                if (!fadeSpeed) fadeSpeed = 1000;
                $(elem + " img").attr("src", imageList[current]);
                if (current >= (listSize - 1)) {
                    $(elem).css("background", "transparent url(" + imageList[0] + ") no-repeat");
                } else {
                    $(elem).css("background", "transparent url(" + imageList[current + 1] + ") no-repeat");
                }
                $(elem + " img").animate({ opacity: "1" }, slideDuration).animate({ opacity: "0.01" }, fadeSpeed, function() { $(this).css("opacity", "1"); fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current + 1) });
            },
            ajaxLoadCrossfade: function(file, wrapper, slideDuration, fadeSpeed) {
                if (!slideDuration) slideDuration = 5000;
                if (!fadeSpeed) fadeSpeed = 1000;
                $.get(file, function(data) { fadeinSlideshow(wrapper, data.split(" "), slideDuration, fadeSpeed); });
            }
        },
        stickyList: function() {
            $('.stickyList > li').bind('mouseover', function() {
                $('.stuck').removeClass('stuck');
                if ($(this).children('ul').children().length > 0) {
                    $(this).addClass('stuck');
                }
            });
            $('.stickyList li ul').bind('mouseout', function() {
                $(this).parent().removeClass('stuck');
            });
        },
        randomBackgroundImage: function(elem, imageList) {
            var i = Math.floor(Math.random() * imageList.length);
            $(elem).css({ 'background-image': 'url(' + imageList[i] + ')' });
        },
        randomImage: function(elem, imageList) {
            var i = Math.floor(Math.random() * imageList.length);
            $(elem).attr('src', imageList[i]);
        }
    }
} ();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Pre-Singleton Functions for pre-0.1.0 Backwards Compatibility 
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function setContentHeight(verticalOffset) {
    if (!verticalOffset) {
        verticalOffset = 0;
    }
    var minContentHeight = $(window).height() - ($('#branding').height() + $('#site_info').height() + verticalOffset);
    if ($('#content').height() < minContentHeight) {
        $('#content').height(minContentHeight);
    }
} 

function fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current) {
    var listSize = imageList.length;
    if (!current || current >= listSize) current = 0;
    if (!slideDuration) slideDuration = 5000;
    if (!fadeSpeed) fadeSpeed = 1000;
    $(elem + " img").attr("src", imageList[current]);
    if (current >= (listSize - 1)) {
        $(elem).css("background", "transparent url(" + imageList[0] + ") no-repeat");
    } else {
        $(elem).css("background", "transparent url(" + imageList[current + 1] + ") no-repeat");
    }
    $(elem + " img").animate({ opacity: "1" }, slideDuration).animate({ opacity: "0.01" }, fadeSpeed, function() { $(this).css("opacity", "1"); fadeinSlideshow(elem, imageList, slideDuration, fadeSpeed, current + 1) });
}

function ajaxLoadCrossfade(file, wrapper, slideDuration, fadeSpeed) {
    if (!slideDuration) slideDuration = 5000;
    if (!fadeSpeed) fadeSpeed = 1000;
    $.get(file, function(data) { fadeinSlideshow(wrapper, data.split(" "), slideDuration, fadeSpeed); });
}

function stickyList() {
    $('.stickyList > li').bind('mouseover', function() {
        $('.stuck').removeClass('stuck');
        if ($(this).children('ul').children().length > 0) {
            $(this).addClass('stuck');
        }
    });
}