/// <reference path="jquery-vsdoc.js" />
// braingnat.js 0.5 | requires jquery 1.4.2+ | made by Kyle Weems of Mindfly Web Studio (http://mindfly.com/)
// Created Jul 29, 2008 | Last Modified Oct 14, 2010
var bg = {
    version: "0.5.0",
    test: function() {
        alert('BrainGnat version ' + bg.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"); bg.slideshow.fadein(elem, imageList, slideDuration, fadeSpeed, current + 1, frontElem) });
        },
        ajaxLoadCrossfade: function(file, wrapper, frontElem, slideDuration, fadeSpeed, random) {
            // 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";
            if (!random || random == null) random = false;
            $.get(file, function(data) {
                var imageList = data.split("|");
                imageList.pop();
                if (random == false) {
                    bg.slideshow.fadein(wrapper, imageList, slideDuration, fadeSpeed, 0, frontElem);
                } else {
                    var newList = new Array();
                    var j = 0;
                    while (imageList.length > 0) {
                        var i = Math.floor(Math.random() * imageList.length);
                        newList[j] = imageList[i];
                        j++;
                        if (imageList.length > 1) {
                            for (k = i; k < (imageList.length - 1); k++) {
                                imageList[k] = imageList[k + 1];
                            }
                        }
                        imageList.pop();
                    }
                    bg.slideshow.fadein(wrapper, newList, slideDuration, fadeSpeed, 0, frontElem);
                }
            });

        },
        loadViaSpan: function(url) {
            if ($('span:contains("bg-gallery")').length > 0) {
                var h = $('span:contains("bg-gallery")').html();
                var attributes = h.split(" ");
                var dir = "";
                var elem = "";
                var dur = 5000;
                var fade = 1000;
                var random = true;
                for (i = 0; i < attributes.length; i++) {
                    var key = attributes[i].split("=")[0];
                    var value = attributes[i].split("=")[1];
                    switch (key) {
                        case "dir":
                            dir = value;
                            break;
                        case "element":
                            elem = value;
                            break;
                        case "duration":
                            dur = value * 1;
                        case "fade":
                            fade = value * 1;
                        case "random":
                            if (value == "false") {
                                random = false;
                            }
                            break;
                        default:
                            break;
                    }
                }
                bg.slideshow.ajaxLoadCrossfade(url + dir, elem, null, dur, fade, random);
                $('span:contains("bg-gallery")').remove();
            }
        },
        listFade: function(list, slideDuration, fadeSpeed) {
            if ($(list + ' li.current').length < 1) {
                $(list + ' li:last-child').addClass('current');
            } else {
                $(list + ' .fade').removeClass('fade');
                var i = $(list + ' .current').index(list + ' li');
                if (i > 0) {
                    $(list + ' .current').addClass('fade');
                    $(list + ' .fade').animate({ opacity: 0 }, fadeSpeed);

                } else {
                    $(list + ' li:last-child').animate({ opacity: 1 }, fadeSpeed, function() {
                        $(this).siblings().css({ opacity: 1 });
                    });
                }
                if (i > 0) {
                    i--;
                } else {
                    i = $(list + ' li').length;
                }
                $(list + ' .current').removeClass('current');
                $(list + ' li:eq(' + i + ')').addClass('current');
            }
            var t = setTimeout("bg.slideshow.listFade('" + list + "', " + slideDuration + ", " + fadeSpeed + " );", (fadeSpeed + slideDuration));

        },
        panelSlide: function(panel, control, defaultPos, activePos) {
            bg.slideshow.psPanel = panel;
            bg.slideshow.psControl = control;
            bg.slideshow.psDefault = defaultPos;
            bg.slideshow.psActive = activePos;
            $(panel + ':first-child').addClass('current');
            $(control + ':first-child').addClass('active');
            var li = null;
            for (i = 0; i < $(panel).length; i++) {
                li = $(panel + ':eq(' + i + ')');
                if (!li.hasClass('current')) {
                    li.css('left', defaultPos + 'px');
                }
            }
            $(control + ' a').live('click', function() {
                var p = bg.slideshow.psPanel;
                var c = bg.slideshow.psControl;
                var dp = bg.slideshow.psDefault;
                var da = bg.slideshow.psActive;
                if ($(p + '.new').length < 1) {
                    var i = $(c + ' a').index(this);
                    var old_i = $(p).index($(p + '.current'));
                    if (i != old_i) {
                        clearTimeout(t);
                        $(c + '.active').removeClass('active');
                        $(this).parent().addClass('active');
                        var li = $(p + ':eq(' + i + ')');
                        li.addClass('new');
                        li.animate({ left: '0px' }, 1000, 'swing', function() {
                            $(p + '.current').css('left', dp + 'px');
                            $(p + '.current').removeClass('current');
                            $(p + '.new').addClass('current');
                            $(p + '.new').removeClass('new');
                            t = setTimeout("bg.slideshow.panelSlideAuto();", 10000);
                        });
                    }
                    $(this).blur();
                }
                return false;
            });
            t = setTimeout("bg.slideshow.panelSlideAuto();", 10000);
        },
        panelSlideAuto: function() {
            var p = bg.slideshow.psPanel;
            var control = bg.slideshow.psControl;
            var dp = bg.slideshow.psDefault;
            var da = bg.slideshow.psActive;
            var c = $(p).index($(p + '.current'));
            var n = c + 1;
            if (n == $(p).length) {
                n = 0;
            }
            $(control + '.active').removeClass('active');
            $(control + ':eq(' + n + ')').addClass('active');
            c = $(p + ':eq(' + c + ')');
            n = $(p + ':eq(' + n + ')');
            n.addClass('new');
            n.animate({ left: da + 'px' }, 1000, 'swing', function() {
                c.css('left', dp + 'px');
                c.removeClass('current');
                n.addClass('current');
                n.removeClass('new');
                t = setTimeout("bg.slideshow.panelSlideAuto();", 10000);
            });
        },
        galleryListFader: function(list, duration, fade) {
            if (!duration) {
                duration = 5000;
            }
            if (!fade) {
                fade = 1000;
            }
            var l = $(list);
            bg.slideshow.glfList = list;
            bg.slideshow.glfDuration = duration;
            bg.slideshow.glfFade = fade;
            l.before("<div id='fader'><img src='' alt=''/><span class='caption'>&nbsp;</span>");
            $('#fader').before("<div id='faderWrapper'></div>");
            $('#faderWrapper').append($('#fader'));
            $('#faderWrapper').append($(list));
            $(list + ' li:first-child').addClass('current');
            $('#fader img').attr('src', $(list + ' .current img').attr('src'));
            $('#fader .caption').html($(list + ' .current img').attr('alt'));
            $('#fader .caption').css({ top: (454 - $('#fader .caption').outerHeight()) + 'px' });
            $('#fader').css('background', "transparent url(" + $(list + ' .current + li img').attr('src') + ") no-repeat left top");
            $(list + ' img').click(function() { bg.slideshow.galleryListFaderClick($(bg.slideshow.glfList + ' img').index($(this))); });
            bg.slideshow.glfTimeout = setTimeout("bg.slideshow.galleryListFaderAuto();", duration);
        },
        galleryListFaderAuto: function() {
            $('#fader img').animate({ opacity: 0 }, bg.slideshow.glfFade, function() {
                var list = bg.slideshow.glfList;
                var oc = $(list + ' li.current');
                var i = $(list + ' li').index(oc);
                i++;
                if (i > ($(list + ' li').length - 1)) {
                    i = 0;
                }
                oc.removeClass('current');
                $(list + ' li:eq(' + i + ')').addClass('current');
                $('#fader img').attr('src', $(list + ' .current img').attr('src'));
                $('#fader img').css({ opacity: 1 });
                $('#fader .caption').html($(list + ' .current img').attr('alt'));
                $('#fader .caption').css({ top: (454 - $('#fader .caption').outerHeight()) + 'px' });
                if (i < ($(list + ' li').length - 1)) {
                    $('#fader').css('background', "transparent url(" + $(list + ' .current + li img').attr('src') + ") no-repeat left top");
                } else {
                    $('#fader').css('background', "transparent url(" + $(list + ' li:first-child img').attr('src') + ") no-repeat left top");
                }
                bg.slideshow.glfTimeout = setTimeout("bg.slideshow.galleryListFaderAuto();", bg.slideshow.glfDuration);
            });
        },
        galleryListFaderClick: function(i) {
            var list = bg.slideshow.glfList;
            clearTimeout(bg.slideshow.glfTimeout);
            $(list + ' .current').removeClass('current');
            $(list + ' li:eq(' + i + ')').addClass('current');
            $('#fader img').attr('src', $(list + ' .current img').attr('src'));
            $('#fader .caption').html($(list + ' .current img').attr('alt'));
            $('#fader .caption').css({ top: (454 - $('#fader .caption').outerHeight()) + 'px' });
            $('#fader').css('background', "transparent url(" + $(list + ' .current + li img').attr('src') + ") no-repeat left top");
            bg.slideshow.glfTimeout = setTimeout("bg.slideshow.galleryListFaderAuto();", bg.slideshow.glfDuration);
        },
        psDefault: null,
        psActive: null,
        psPanel: null,
        psControl: null,
        glfList: null,
        glfDuration: null,
        glfFade: null,
        glfTimeout: null
    },
    fields: {
        preset: new Array(),
        clearOnFocus: function() {
            var f = $('input[type="text"]');
            var counter = 0;
            for (i = 0; i < f.length; i++) {
                bg.fields.preset[counter] = $('input[type="text"]:eq(' + i + ')').attr('value');
                counter++;
            }
            f = $('textarea');
            for (i = 0; i < f.length; i++) {
                bg.fields.preset[counter] = $('textarea:eq(' + i + ')').attr('value');
                counter++;
            }
            $('input[type="text"]').focus(function() {
                var v = bg.fields.preset[$('input[type="text"]').index($(this))];
                if ($(this).attr('value') == v) {
                    $(this).attr('value', '');
                }
            });
            $('input[type="text"]').blur(function() {
                var v = bg.fields.preset[$('input[type="text"]').index($(this))];
                if ($(this).attr('value') == '') {
                    $(this).attr('value', v);
                }
            });
            $('textarea').focus(function() {
                var v = bg.fields.preset[($('input[type="text"]').length + $('textarea').index($(this)))];
                if ($(this).attr('value') == v) {
                    $(this).attr('value', '');
                }
            });
            $('textarea').blur(function() {
                var v = bg.fields.preset[($('input').length + $('textarea').index($(this)))];
                if ($(this).attr('value') == '') {
                    $(this).attr('value', v);
                }
            });
        }
    },
    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');
        });
    }
}
var BrainGnat = bg;

