/* Author: 

*/

(function(window, document, $) {
    var trailerVid = 'http://www.youtube.com/v/_xgWKaBXvHE?fs=1&amp;hl=en_US';
    var root = $(document).ready(function() {
        var win = $(window), body = root.find('body'),
            winW = win.width(), winH = win.height(),
            overlayIndex = 0, thumbnails, imgCache = {},
            obg = $('<div id=overlay-bg>').appendTo('body'),
                o = $('<div id=overlay><div id=overlay-content></div><div id=overlay-next class=overlay-btn><a href=#>Next</a></div><div id=overlay-prev class=overlay-btn><a href=#>Previous</a></div><a href=# id=overlay-close title="Click to close or press ESC">close</a>').appendTo('body').css({
                    left: winW/2,
                    top: winH/4
                }),
                closeOverlay = function(ev) {
                    ev.stopPropagation();
                    if((ev.which && ev.which == 27) || ev.type == 'click') {
                        o.fadeTo(500, 0, function() {
                            o.hide().find(':first').flash().remove();
                        });
                        obg.fadeTo(500, 0, function() {
                            obg.hide();
                        }).unbind('click.close-overlay');
                        root.unbind('keydown.close-overlay').unbind('keydown.navigate-overlay');
                        return false;
                    }
                    return true;
                }, openOverlay = function(w, h, innerHTML, paginate) {
                    o.show()[(paginate ? 'add' : 'remove')+'Class']('overlay-paginate').animate({
                        height: h,
                        left: w > winW ? 10 : ((winW-w)/2)-10,
                        opacity: 1,
                        top: h > winH ? 10 : ((winH-h)/4)-10,
                        width: w
                    }, 500, function() {
                        innerHTML.fadeTo && innerHTML.fadeTo(400, 1, function() {
                            o.addClass('preloaded');
                        });
                    }).find(':first').html(innerHTML);
                    obg.show().bind('click.close-overlay', closeOverlay).height(body.height()).fadeTo(500, .6);
                    root.bind('keydown.close-overlay', closeOverlay);
                    root.bind('keydown.navigate-overlay', navigateOverlay);
                }, navigateOverlay = function(ev) {
                    var origIndex = overlayIndex;
                    $(this).trigger('blur');
                    if((ev.which && ev.which == 37) || $(this).parent().attr('id') == 'overlay-prev') {
                        if(--overlayIndex < 0) overlayIndex = thumbnails.length-1;
                    }
                    if((ev.which && ev.which == 39) || $(this).parent().attr('id') == 'overlay-next') {
                        if(++overlayIndex == thumbnails.length) overlayIndex = 0;
                    }
                    if(origIndex != overlayIndex) {
                        root.unbind('keydown.navigate-overlay');
                        o.find(':first').find(':first').fadeTo(300, 0, function() {
                            thumbnails.eq(overlayIndex).trigger('click');
                        });
                        return false;
                    }
                    return true;
                };
        root.find('a[href^="http"]:not([href*="' + window.location.host + '"][target="_blank"])').attr('target', '_blank');
        $.getScript('http://hungryforjustice.com/js/mylibs/jquery.swfobject.js', function() {
            Modernizr.addTest('flashavailable', function() { return $.flash.available; });
            if($.flash.available) {
                var params = '?fs=1&amp;hl=en_US';
                $('#trailer').find('a').bind('click', function() {
                    var link = $(this), w = link.data('overlay-w'), h = link.data('overlay-h'), href = this.href, videohref = href.replace('watch?v=', 'v/')+params;
                    w = w == '100%' ? win.width()-20 : w;
                    h = h == '100%' ? win.height()-40 : h;
                    openOverlay(w, h, $.flash.create({
                        allowFullScreen: true,
                        allowScriptAccess: 'always',
                        height: h,
                        swf: videohref,
                        width: w
                    }));
                    return false;
                });
                root.find('a.clip').bind('click', function() {
                    var href = this.href, videohref = href.replace('watch?v=', 'v/')+params;
                    openOverlay(720, 429, $.flash.create({
                        allowFullScreen: true,
                        allowScriptAccess: 'always',
                        height: 429,
                        swf: videohref,
                        width: 720
                    }));
                    return false;
                })
                $('#overlay-close').bind('click', closeOverlay);
            }
        });
        thumbnails = root.find('a.thumbnail').bind('click', function() {
            var href = this.href;
            if( ! imgCache[href]) {
                o.removeClass('preloaded');
                imgCache[this.href] = $('<img>', {
                    src: this.href,
                    css: {
                        opacity: 0
                    }
                }).bind('load', function() {
                    openOverlay(this.width, this.height, imgCache[href], true);
                });
            } else {
                var img = imgCache[href][0];
                openOverlay(img.width, img.height, imgCache[href], true);
            }
            overlayIndex = thumbnails.index(this);
            return false;
        });
        root.find('div.overlay-btn').bind('mouseenter', function() {
            $(this).fadeTo(500, 1);
        }).bind('mouseleave', function() {
            $(this).fadeTo(500, 0);
        }).find('a').bind('click', navigateOverlay);
    });
})(window, window.document, jQuery);
