﻿//*************************************************************************************
// File     : braingnat.js
// Version  : 0.3.0
// Requires : jquery.js (version 1.3.2+)
// Author   : Kyle Weems (ksw), Janae Wiedmaer (jlw)
// Origin   : mindfly.com
// Created  : July 29, 2008
// Modified : May 12, 2009
// Purpose  : BrainGnat - A collection of handy solutions and functions for problems and 
//            desired functionality for client websites by Mindfly.
//*************************************************************************************

/* List of methods */

// 0.0.1
// setContentHeight 	- Adjusts height of page content to make it fit in browser.
// 0.0.5
// stickyList			- Gives list items the class 'stuck' when they are hovered over and in lists with the '.stickyList' class.
// 0.1.0
// slideshow.fadein  	- Loops a series of images in a slideshow.
// slideshow.ajaxLoadCrossfade 	- Ajax call to a document that lists the images for use in a slideshow
// 0.1.4
// 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.
// 0.1.5
// html5Shiv            - Fixes IE to support CSS styling of HTML5 elements
// 0.2.0
// setTabs              - Adds tab functionality to given section where tabs have class 'tab' and content of tabs has class 'tabbedContent'
// 0.3.0
// Google               - New namespace for Google mashups
// Google.map           - New namespace for Google maps mashups
// Google.map.load(elem)- Loads elem (defined by css selecor). If selector is for more than one element, only selects the first one.
// Google.map.addMarker(latlng,info) - Loads point defined by latlng and adds a marker. If info has a value, puts it in an info bubble.
// Google.map.addMarkerByAddress(address,info) - loads point defined by address and adds a marker. If info has a value, puts it in a info bubble.
// Google.map.setCenterByAddress(address) - sets the center of map to a provided address.
// Google.map.loadFromHCard(card, map, zoom) - Loads a map, gets the address from an hcard and sets a marker there, and if zoom is provided, zooms to that level.

var BrainGnat = function() {
    return {
        version: "0.2.0",
        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, frontElem) {
                var listSize = imageList.length;
                if (!current || current >= listSize) current = 0;
                if (!frontElem) frontElem = "img";
                if (!slideDuration) slideDuration = 5000;
                if (!fadeSpeed) fadeSpeed = 1000;
                if (frontElem == "img") {
                    $(elem + " " + frontElem).attr("src", imageList[current]);
                } else {
                    $(elem + " " + frontElem).css("background-image", "url(" + 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 + " " + frontElem).animate({ opacity: "1" }, slideDuration).animate({ opacity: "0.01" }, fadeSpeed, function() { $(this).css("opacity", "1"); BrainGnat.slideshow.fadein(elem, imageList, slideDuration, fadeSpeed, current + 1, frontElem) });
            },
            ajaxLoadCrossfade: function(file, wrapper, frontElem, slideDuration, fadeSpeed) {
                // This function makes use of the .NET files: imageList.aspx, imageList.aspx.vb which can be found in proofs.
                if (!slideDuration) slideDuration = 5000;
                if (!fadeSpeed) fadeSpeed = 1000;
                if (!frontElem) frontElem = "img";
                $.get(file, function(data) { var imageList = data.split("|"); imageList.pop(); BrainGnat.slideshow.fadein(wrapper, imageList, slideDuration, fadeSpeed, 0, frontElem); });
            }
        },
        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]);
        },
        html5Shiv: function() {
            var shivstring = "section|article|aside|header|footer|nav|dialog|figure|audio|video|embed|m|meter|time|canvas|command|datagrid|details|datalist|datatemplate|rule|nest|event-source|output|progress";
            var shiv = shivstring.split('|');
            for (i = 0; i < shiv.length; i++) {
                document.createElement(shiv[i]);
            }
        },
        setTabs: function(elem) {
            $(elem + ' .tab').bind('click', function() {
                $(elem + ' .tab').removeClass('active');
                $(elem + ' .tabbedContent').removeClass('show');
                var tabClasses = $(this).attr('class').split(' ');
                for (i = 0; i < tabClasses.length; i++) {
                    if (tabClasses[i] != 'tab') {
                        $(elem + ' .tabbedContent.' + tabClasses[i]).addClass('show');
                    }
                }
                $(this).addClass('active');
            });
        },
        Google: {
            map: {
                // Using BrainGnat.Google.map requires a MAP API Key included before this script in the project.
                load: function(elem) {
                    if (GBrowserIsCompatible()) {
                        var domElem = $(elem).get(0);
                        BrainGnat.Google.map.data = new GMap2(domElem);
                        BrainGnat.Google.map.data.addControl(new GLargeMapControl());
                        BrainGnat.Google.map.data.addControl(new GMapTypeControl());
                    } else {
                        alert('Error: BGGM1 - There was an issue loading the Google map.');
                    }
                },
                addMarker: function(latLng, info) {
                    var marker = new GMarker(latLng);
                    BrainGnat.map.data.addOverlay(marker);
                    if (!info) { } else {
                        marker.openInfoWindowHtml(info);
                    }
                },
                addMarkerByAddress: function(address, info) {
                    geocoder = new GClientGeocoder();
                    geocoder.getLatLng(address, function(point) {
                        if (!point) {
                            alert('Error: BGGM2 - Provided address does not exist.');
                        } else {
                            var marker = new GMarker(point);
                            BrainGnat.Google.map.data.addOverlay(marker);
                            if (!info) { } else {
                                marker.openInfoWindowHtml(info);
                            }
                        }
                    });
                },
                setCenterByAddress: function(address, zoom) {
                    if (!zoom) {
                        zoom = 13;
                    }
                    geocoder = new GClientGeocoder();
                    geocoder.getLatLng(address, function(point) {
                        if (!point) {
                            alert('Error: BGGM2 - Provided address does not exist.');
                        } else {
                            BrainGnat.Google.map.data.setCenter(point, zoom);
                        }
                    });
                },
                loadFromHCard: function(card, map, zoom) {
                    if (!zoom) {
                        zoom = 13;
                    }
                    BrainGnat.Google.map.load(map);
                    var address = "";
                    address += $(card + ' .street-address').text();
                    address += ' ' + $(card + ' .locality').text();
                    address += ', ' + $(card + ' .region').text();
                    address += ' ' + $(card + '. postal-code').text();
                    BrainGnat.Google.map.setCenterByAddress(address, zoom);
                    BrainGnat.Google.map.addMarkerByAddress(address, address);
                },
                data: null
            }
        }
    }
} ();


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Pre-Singleton Functions for pre-0.1.0 Backwards Compatibility 
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Modified May 12, 2009 by JLW to identify the header and footer rather than the branding and site_info
function setContentHeight(verticalOffset) {
    if (!verticalOffset) {
        verticalOffset = 0;
    }
    var minContentHeight = $(window).height() - ($('#header').height() + $('.footer').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');
        }
    });
}