
/**
 * jQuery Ready Functions
 */

var objSlideShowApi;

var SLIDESHOW_INTERVAL = 5000;
var SLIDESHOW_PROGRESS = 25;
var SLIDESHOW_FADETIME = 500;

jQuery.MySlideShow = {};
jQuery.MySlideShow.blnPaused = 0;
jQuery.MySlideShow.intTabIndex = 0;
jQuery.MySlideShow.intProgressWidth = 0;

function advanceProgressBar()
{
    if (jQuery.MySlideShow.blnPaused == 0) {

        fltStep = intItemWidth / (SLIDESHOW_INTERVAL / SLIDESHOW_PROGRESS);
        jQuery.MySlideShow.intProgressWidth += fltStep;

        if (jQuery.MySlideShow.intProgressWidth > intListWidth) {
            jQuery.MySlideShow.intTabIndex = 0;
            jQuery.MySlideShow.intProgressWidth = 0;
            objSlideShowApi.next();
        } else if (jQuery.MySlideShow.intProgressWidth > (intItemWidth + 1) * (objSlideShowApi.getIndex() + 1)) {
            objSlideShowApi.next();
        }

        jQuery('#slideshow div.progress').css('width', Math.round(jQuery.MySlideShow.intProgressWidth) + 'px');
    }
}

jQuery(document).ready(function()
{
    // open external links in a new window

    jQuery('a[rel="external"], a[href^="http://"], a[href^="/pdf"], a[href^="/uploads"]').not('.cancel, .fancybox').click(function()
    {
        window.open(jQuery(this).attr('href'));
        return false;
    });


    // hide the flash messenger

    setTimeout(function() {
        jQuery('#flashmessenger').fadeTo('slow', 0.01, function() { 
            jQuery(this).slideUp('fast');
        });
    }, 10000);


    // animate back to top links

    jQuery('a[href="#top"]').click(function() {
        jQuery('html, body').animate({scrollTop: 0}, 'normal');
        return false;
    });


    // captcha

    jQuery('#captcha').focus(function() {
        if (this.value == this.defaultValue) {
            this.value = '';
        }
    });

    jQuery('#captcha').blur(function() {
        if (this.value == '') {
            this.value = this.defaultValue;
        }
    });


    // tabs

    jQuery('ul.tabs').tabs('div.panes > div');


    // zebra stripes

    jQuery('ul.zebra li:nth-child(odd)').addClass('alt');
    jQuery('table tbody.zebra tr:nth-child(even)').addClass('alt');


    // hide share bar for small browser viewports

    if (jQuery(document).width() < 1040) {
        jQuery('#share').hide();
    }

    jQuery(window).resize(function() {
        if (jQuery(document).width() < 1040) {
            jQuery('#share').fadeOut();
        } else {
            jQuery('#share').fadeIn();
        }
    });



    /**
     * Feeds
     */

    jQuery('#blog-feed').load('/feeds/blog');
    jQuery('#facebook-feed').load('/feeds/facebook');


    /**
     * Slideshow
     */

    if (jQuery('#slideshow').length > 0) {

        // dynamic list item widths

        intListWidth = jQuery('#slideshow ol').outerWidth();
        intItemCount = jQuery('#slideshow ol li').length;
        intItemWidth = (intListWidth / intItemCount) - 1;  // minus 1 to account for border
        jQuery('#slideshow ol li a').append('<img src="/images/slideshow/pause.png" alt="Pause" />');
        jQuery('#slideshow ol li').css('width', intItemWidth + 'px');
        jQuery('#slideshow ol li:first-child').css('width', (intItemWidth + 1) + 'px');

        // play and pause

        jQuery('#slideshow ol li img').click(function() {
            if (jQuery.MySlideShow.blnPaused == 1) {
                jQuery.MySlideShow.blnPaused = 0;
                jQuery('#slideshow ol li img').attr('src', '/images/slideshow/pause.png');
                jQuery('#slideshow ol li img').attr('alt', 'Pause');
            } else {
                jQuery.MySlideShow.blnPaused = 1;
                jQuery('#slideshow ol li img').attr('src', '/images/slideshow/play.png');
                jQuery('#slideshow ol li img').attr('alt', 'Play');
            };
        });

        // custom fade effect

        jQuery.tools.tabs.addEffect('fader', function(i, done)
        {
            this.getPanes().fadeOut(SLIDESHOW_FADETIME);
            this.getPanes().eq(i).fadeIn(SLIDESHOW_FADETIME, function() {
                done.call();
            });
        });

        // configure tabs

        objSlideshow = jQuery('#slideshow ol.tabs').tabs('#slideshow ul.panes > li', {
            effect: 'fader',
            fadeOutSpeed: 'normal',
            rotate: true,
            onBeforeClick: function(e, i) {
                if (i != jQuery.MySlideShow.intTabIndex) {
                    intProgressWidth = (i == 0) ? 0 : i * (intItemWidth + 1) + 1;
                    jQuery.MySlideShow.intProgressWidth = intProgressWidth;
                    jQuery('#slideshow div.progress').css('width', Math.round(intProgressWidth) + 'px');
                    jQuery.MySlideShow.intTabIndex = i;
                } else {
                    // prevent click when indexes are the same
                }
            }
        });

        objSlideShowApi = jQuery('#slideshow ol.tabs').data('tabs');

        // start the progress bar
        setInterval('advanceProgressBar()', SLIDESHOW_PROGRESS);
    }


    /**
     * Contact Form
     */

    var intAttempts = 0;

    jQuery('#contact-form').submit(function() {
        if (jQuery('#message').val() == '') {
            if (++intAttempts >= 3) {
                alert('Please enter your message.');
            }
            return false;
        }
    });


    /**
     * Expandable Definition Lists
     */

    jQuery('dl.expandable dd').hide();
    jQuery('dl.expandable dt a').click(function()
    {
        jQuery(this).parent().next().toggle();
        jQuery(this).toggleClass('expanded');
        return false;
    });


    /**
     * Hover states for calendar with iframe
     */

    jQuery('a.target-area, a.target-trigger').mouseover(function(){
      jQuery('a.target-trigger').css({ background : '#eff0ee'});
    });
    jQuery('a.target-area, a.target-trigger').mouseout(function(){
      jQuery('a.target-trigger').css({ background : 'none'});
    });


    /**
     * Continuum Modal
     */

    jQuery('a.continuum, a.owf').fancybox({
        'centerOnScroll': true,
        'hideOnOverlayClick': true,
        'overlayOpacity': 0.85,
        'overlayColor': '#000',
        'padding': 25,
        'showCloseButton': true,
        'transitionIn': 'fade',
        'transitionOut': 'fade'
    });

    /**
     * Winter-Lab Starter-Kit-Form Modal
     */

    jQuery('a.inline').fancybox({
        'centerOnScroll': true,
        'hideOnOverlayClick': true,
        'hideOnContentClick' : false,
        'overlayOpacity': 0.85,
        'overlayColor': '#000',
        'padding': 0,
        'showCloseButton': true,
        'transitionIn': 'fade',
        'transitionOut': 'fade'
    });

});

