
//  This is where the magic happens, like on Cribs, except nowhere near as vile.
(function ($, window, document) {
    var H, // <head>
        B; // <body>

    //  Is any of this actually going to work?
    if (typeof jQuery === 'undefined') {
        return; // Hot Christ! jQuery isn't available!
    }

    //  --  Instantiate just ONE jQuery object, use this to find other elements
    //      rather than creating a new jQuery object for each selector
    $.root = new jQuery.prototype.init(document);

    //	--	Make Rocket Go Now
    $.root.ready(function () {
        //  Cache <head> and <body> elements
        H = $.root.find('head');
        B = $.root.find('body');

        //  Invoke plugins, yo -> B.find('#element').functionName({ ... });
        B.find('.video_embed').videoEmbed();
        B.find('.top_banners').homeBanners();
        B.find('.map_container').networkMap();

        //  Other bindings and DOM modifications and such
        B.find('.home_articles article:nth-child(3n+1)').addClass('new_row');
    });



    //	--	Hey buddy I got your custom jQuery plugins right here...

    //  Home page slideshow thing
    $.prototype.homeBanners = function () {
        return this.each(function () {
            var container = $(this);

            container.find('article').bind('click', function () {
                var article = $(this),
                    link    = article.find('h1 a'),
                    href    = link.attr('href');

                if (href) {
                    window.location = href;
                }

                return false;
            });

            container.cycle({
                timeout: 10000,
                speed:   250,
                sync:    true
            });
        });
    };

    //  Network Map
    $.prototype.networkMap = function () {
        return this.each(function () {
            var container = $(this),
                image     = container.find('img'),
                region    = image.data('region'),
                areas     = container.find('area');

            image.attr('src', '/img/trans.png');

            if (region === 'national') {
                region = 'default';
            }

            container.css('background-image', "url('/img/network_map/" + region + ".png')");

            areas.each(function () {
                var area   = $(this),
                    region = area.data('region'),
                    image  = $('<img alt="" class="map_state ' + region + '" height="485" src="/img/network_map/' + region + '.png" width="378" />');

                image.bind('load', function () {
                    image.css('opacity', 0).appendTo(container);
                });
            }).bind({
                mouseover: function () {
                    var area   = $(this),
                        region = area.data('region'),
                        image  = container.find('img.' + region);

                    image.stop().animate({opacity: 1}, 100);
                },
                mouseleave: function () {
                    var area   = $(this),
                        region = area.data('region'),
                        image  = container.find('img.' + region);

                    image.stop().animate({opacity: 0}, 100);
                }
            });
        });
    };



    //  General video embedder thinger
    $.prototype.videoEmbed = function () {
        return this.each(function () {
            var video  = $(this),
                config = {};

            if (!$.flash.available) {
                return;
            }

            config.params = {
                allowfullscreen:    true,
                allowscriptaccess: 'always',
                wmode:             'transparent'
            };

            config.swf       = video.data('swf');
            config.height    = video.data('height');
            config.width     = video.data('width');
            config.wmode     = 'transparent';
            config.flashvars = {};

            if (video.data('vars')) {
                $.each(video.data('vars'), function (key, value) {
                    config.flashvars[key] = value;
                });
            }

            video.empty().flash(config);
        });
    };

}(jQuery, this, this.document));
//  --  FIX UP; LOOK SHARP!

