/**
 * rTabs - 
 * Tabbed video auto-rotator with auto resizing
 * of embedded youtube/vimeo videos
 * by Ryan Olson
 * http://thatryan.com
 * 
 * Resizing based off of the script
 * YouTube Embedded Video Resizer
 * by Trevor Fitzgerald
 * http://trevorfitzgerald.com/
 */

 (function($) {


    $.fn.rTabs = function(options) {

        // Find and grab any objects embedded from vimeo or youtube, grab their initial dimensions,
        // find aspect ration, (width/height) and create new dimensions using the enclosing div as the
        // new width for the object.
        $('embed[src*="youtube.com"],embed[src*="vimeo.com"]').each(function() {
            aspect = $(this).attr('width') / $(this).attr('height');
            newWidth = $(this).parent().parent().width();
            newHeight = Math.round(newWidth / aspect);
            $(this)
            .attr('width', newWidth)
            .attr('height', newHeight)
            .parent()
            .attr('width', newWidth)
            .attr('height', newHeight);
        });

        // Setting default options for the rotator plugin, these can be overridden when calling the function
        var options = $.extend({
            fade: null,
            tabHeading: null,
            interval: null,
            startTab: 1,
            tabList: 'tab-list'
        },
        options);

        // Find out how many divs, (videos) were included and create a tab list of them all
        return this.each(function() {

            var $this = $(this),

            divCount = $this.children('div').length,
            tabContainer = '<ul id="' + options.tabList + '">';

            $this
            .children('div:not(:nth-child(' + options.startTab + '))')
            .hide()
            .end()
            .hover(function() {
                //When we hover over the tab list of videos, add a class to highlight it
                $this.addClass('hovered');
            },
            function() {
                //mouse moved, time to remove highlighting!
                $this.removeClass('hovered');

            });

            // Name the tabs, using the default tabHeading if user does not specify a tag other than default
            for (var i = 1; i <= divCount; i++) {

                if (options.tabHeading) {
                    var heading =
                    $this.children('div:nth-child(' + i + ')')
                    .find(options.tabHeading)
                    .filter(':first')
                    .text();

                    if (heading !== '') tabContainer += '<li><a href="#" rel="' + i + '">' + heading + '</a></li>';
                    else tabContainer += '<li><a href="#" rel="' + i + '">' + i + '</a></li>';
                }
                else {
                    tabContainer += '<li><a href="#" rel="' + i + '">' + i + '</a></li>';
                }
            };
            tabContainer += '</ul>';
            $(options.tabHeading).remove();
            $this.prepend(tabContainer);

            var $container = $('#' + options.tabList);

            $container
            .find('li:nth-child(' + options.startTab + ')')
            .addClass('tab-selected')
            .end()
            .find('a')
            .click(function() {
                var $a = $(this),
                num = $a.attr('rel');
                if ($this
                .children('div')
                .eq(num - 1)
                .is(':visible')
                ) return false;

                $container
                .find('li.tab-selected')
                .removeClass('tab-selected');

                $a.parent().addClass('tab-selected');

                // Set the fading effect if user chooses it
                /*
	TODO add fade in and out options...
*/
                if (options.fade) {
                    $this
                    .children('div')
                    .hide()
                    .eq(num - 1)
                    .fadeIn(options.fade);
                }
                else {
                    $this
                    .children('div')
                    .hide()
                    .eq(num - 1)
                    .show();
                }
                return false;
            });

            // If options set to auto change tabs, get the interval and trigger a click event to move to next tab
            if (options.interval) {
                var i = options.startTab;

                setInterval(function() {

                    if (!$this.hasClass('hovered')) {
                        $container
                        .find('a')
                        .eq(i)
                        .trigger('click');
                        i++;
                        if (i === divCount) i = 0;
                    }

                },
                options.interval)
            }

        });
        //end each
    }

})(jQuery);