var timer;
$j = jQuery.noConflict();
$j(function ($) {
    $('#background_tout img').eq(0).css('z-index', 100);
    $('#hero').css('top', $('#hero').parent().outerHeight() * -1);
    //$('#nav_wrapper').parent().height($('#nav_wrapper').parent().outerHeight()).css({ overflow: 'hidden' }).end().css({ top: $('#nav_wrapper').parent().outerHeight() * -1, position: 'absolute' });
    $j('#nav_wrapper')
	    .parent().css({
		    overflow: 'hidden',
		    height: $j('#nav_wrapper').outerHeight(),
		    width: $j('#nav_wrapper').outerWidth(),
		    position: 'relative'
	    }).end()
	    .css({ top: $j('#nav_wrapper').parent().outerHeight() * -1, position: 'absolute', left:'0px', height:422 });
    timer = window.setTimeout('hero.init()', 1500);

    $j('.tout').hometout();
});
var handle = {
    container: null,
    container_height: 0,
    handles: null,
    current: null,
    init: function () {
        handle.container = $j('<div id="hero_handle_container" />');
        //handle.container.css({ paddingTop: '35px' });
        handle.handles = jQuery.extend({}, hero.hero_handles);
        handle.handles = hero.hero_handles.clone();
        handle.current = handle.handles.eq(0);
        handle.handles.removeClass('hero_handle').addClass('hero_handle_wrapper').appendTo(handle.container);
        handle.container.appendTo(hero.hero_touts.parent());
        //handle.container.appendTo($('.hero_handle'));
        handle.container.fadeOut(function () {
            $j(this).css({ zIndex: 1000 });
        }).hide();
        handle.container.mouseleave(function () { handle.container.stop(true, true).fadeOut('fast'); });
        handle.handles.eq(0).addClass('off_screen');

        /*$j('#hero .hero_tout').find('.hero_handle').focus(function () {
        $j('#hero_handle_container').children().eq(0).focus();
        console.debug($j(this).html());
        });*/

        handle.handles.mousedown(function () {
            handle.handle_click($j(this));
            handle.container.hide();
            return false;
        });
    },
    handle_click: function ($elm) {
        //Stop user from clicking while animation is running
        //handle.container.children().eq(0).focus();
        var index = handle.container.children().index($elm);
        hero.hero_handles.removeClass('on').addClass('off').eq(index).removeClass('off').addClass('on');

        hero.hero_touts.eq(index).css({ zIndex: 0, opacity: 1 });
        hero.hero_current.addClass('off').removeClass('on').stop(true, true).animate({ opacity: 0 }, hero.anim_speed, 'easeInOutSine', function () {

            handle.handles.removeClass('off_screen').eq(index).addClass('off_screen');
            hero.hero_current.css({ zIndex: 0 });
            hero.hero_current = hero.hero_touts.eq(index);
            hero.hero_current.css({ zIndex: 1 });
            hero.hero_current.removeClass('off').addClass('on');
        });


        handle.current = handle.handles.eq(index);
    }
};
var hero = {
    anim_speed: 600,
    touts_container: null,
    hero_touts: null,
    hero_handles: null,
    handles_container: null,
    hero_current: null,
    init: function () {
        window.clearTimeout(timer);
        $j('#background_tout img').eq(0).stop(true, true).fadeOut('slow', function () {

            hero.hero_touts = $j('#hero .hero_tout');
            hero.hero_handles = hero.hero_touts.find('.hero_handle');

            handle.init();


            hero.hero_touts.css({ position: 'absolute', top: 0, left: 0, zIndex: 0 });
            hero.hero_current = hero.hero_touts.eq(0).css({ zIndex: 1 });

            hero.hero_handles.addClass('off').mouseenter(function () {
                hero.handle_click($j(this));
                return false;
            }).keyup(function () {
                hero.handle_click($j(this));
                return false;
            });

            hero.hero_handles.click(function () {
                handle.container.stop(true, true).fadeOut('fast');
            });
            hero.hero_touts.each(function (i) {
                if (i == 0) {
                    $j(this).addClass('on');
                    hero.hero_handles.eq(i).addClass('on');
                } else {
                    $j(this).addClass('off').css({ opacity: 0 });
                }
            });

            //Start up animation
            $j('#hero').show().stop(true, true).animate({ top: 0 }, hero.anim_speed, 'easeInOutExpo');
            /*$j('#nav_wrapper').animate({ top: 0 }, hero.anim_speed, 'easeInOutExpo', function () {
            $j(this).parent().css('overflow', 'visible');
            $j(this).css({ position: 'relative' });
            });*/
            $j('#nav_wrapper').show().stop(true, true).animate({ top: 0 }, hero.anim_speed, 'easeInOutExpo', function () {
                $j(this).parent().css('overflow', 'visible');
            });
        });
    },
    handle_click: function ($elm) {
        handle.container.stop(true, true).fadeIn(hero.anim_speed, 'easeOutExpo', function () {
            $j(this).children().eq(0).focus();
        });
    }
};


// HOME TOUT
///////////////////////////////////////
(function ($) {

    var methods = {
        init: function (options) {
            var defaults = {
                min: 6
            };
            var options = $.extend(defaults, options);


            return this.each(function () {
                var obj = $(this);
                var container_height = obj.find('p').height();
                obj.find('p').hide().height(1);
                obj.find('.tout_content').find('.more').wrap('<div class="more_container" />');
                var handles = obj.find('.more_container');
                handles.css({ height: 44 })
			        .children().css({ margin: '10px 20px 0 0' }).end()
			        .parent().parent().mouseover(function () {
			            $(this).find('p').stop(true).show().animate({ height: container_height }, 500);
			        });
                obj.mouseleave(function () {
                    //$j(this).find('p').slideUp();
                    $(this).find('p').stop(true).animate({ height: 1 }, 500, function () {
                        $(this).hide();
                    });
                });

            });
        }
    };
    $.fn.hometout = function (method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};
