var Carousel =
{
    init: function() {
        var max_height = 0;

        var $carousel = $('#carousel');

        var $slides = $carousel.find('section');

        var indicator_html = '<nav><ul>';

        var num, height;

        if ($slides.size() < 2) {
            return false;
        }

        $slides.each(function(i) // loop slides
        {
            num = i + 1;

            indicator_html += '<li><a href="#">' + num + '</a></li>'; // add one li to indicator ul per slide

            height = $(this).outerHeight();

            //console.log(height);

            if (height > max_height) // set max height
            {
                max_height = height;
            }
        });

        indicator_html += '</ul></nav>';

        var $indicator_html = $(innerShiv(indicator_html));

        $indicator_html.find('li:first').addClass('sel');

        $carousel.append($indicator_html); // append indicator to carousel

        $slides.css({ height: max_height, position: 'absolute' });

        $carousel.css('height', max_height + 40).find('section').not(':first').hide(); // set min-height to carousel based on the tallest slide

        this.init_slide(false); // start sliding

        $indicator_html.delegate('a', 'click', function() {
            return false;

            var $this = $(this);

            var index = $this.parent('li').index();

            Carousel.init_slide(index);

            return false;
        });
    },

    clicked: false,

    slide_timer: '',

    init_slide: function(index) {
        var $slider_nav = $('#carousel nav');

        var $current_indicator = $slider_nav.find('li.sel a');

        var timeout = 6000;

        if (index) {
            Carousel.clicked = true;

            Carousel.slide();
        }
        else {
            Carousel.slide_timer = setTimeout(function() // flash indicator, then fade out current slide and fade in next the one. Then run this func again.
            {
                Carousel.slide(index);
            }, timeout);
        }
    },

    slide: function(index) {
        if (Carousel.clicked) {
            if (index === false) {
                return false;
            }
        }

        var $carousel = $('#carousel');

        var $slider_nav = $carousel.find('nav');

        var $slide_current = $carousel.find('section:visible'); // get current slide

        var $slide_to_show = $slide_current.next('section'); // grab the next one to show

        if (!$slide_to_show.size()) // if no next one, start from beginning
        {
            $slide_to_show = $carousel.find('section:first');
        }

        if (index) {
            $slide_to_show = $carousel.find('section:eq(' + index + ')');
        }

        $slider_nav.find('li').removeClass('sel');

        setTimeout(function() {
            $slider_nav.find('li').eq($slide_to_show.index()).addClass('sel');

            $slide_current.fadeOut();
            $slide_to_show.fadeIn(function() {
                Carousel.clicked = false;
                clearTimeout(Carousel.slide_timer);

                Carousel.init_slide();

            });

        }, 200);
    }
};
$(document).ready(function() {    
    $(function() {

        Carousel.init();

        var page = window.location.pathname;
    });
});
// http://jdbartlett.github.com/innershiv | WTFPL License
window.innerShiv = (function() { var d, r; return function(h, u) { if (!d) { d = document.createElement('div'); r = document.createDocumentFragment(); /*@cc_ond.style.display = 'none'@*/ } var e = d.cloneNode(true); /*@cc_ondocument.body.appendChild(e); @*/e.innerHTML = h.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); /*@cc_ondocument.body.removeChild(e); @*/if (u === false) return e.childNodes; var f = r.cloneNode(true), i = e.childNodes.length; while (i--) f.appendChild(e.firstChild); return f } } ());
