// PLlightbox.js - A jQuery Plugin
(function($) {
    $.fn.PLlightbox = function(options) {
        var opts = $.extend({}, $.fn.PLlightbox.defaults, options);
        // ie detection for plugins to access
        var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
        $('body').append('<div id="lightbox"></div>')
        $('#lightbox').append('<div class="box2 box-lb"><div class="box-top"><div class="box-interior"></div></div><div class="box-middle"><div class="box-interior"></div></div><div class="box-bottom"><div class="box-interior"></div></div></div><a class="close" href="#" title="close"></a>');

        return this.each(function() {                         
            var $link = $(this);
            var href = $(this).attr('href');
            var type = ''
            
            if($link.hasClass('image')){
                $link.append('<span class="openLb" />');
            } else if ($link.hasClass('video')){
                $link.append('<span class="openLbv" />');
            };
            $link.click(function(){
                //showMask();
                var top = $(document).scrollTop() + 40;
                if(top <= 140){top = 140}
                $('#lightbox').css('top',top);
                getContent($link, href, type, top);             
                return false;
            });
            $('#lightbox > .close, .mask').click(function(){
                closeLB($link);
                return false
            });
        });
        function showMask(){
            $('.mask').css({'display':'block','z-index':21}).fadeTo(200,0.5);
        };
        function hideMask(){
            $('.mask').fadeOut(300, function(){ //.animate({'delay':'1'},150)
                $(this).css({'z-index':10, 'opacity':0});
            });
        };
        function getContent($link, href, type, top){
            if (type == ''){
                type = getHREF($link, href, type);
            }
            if(href.match('#')){
                href = href.slice(0, href.lastIndexOf('#'))
            };
            $('#lightbox').css({'display':'block','opacity':0})
            $content = $('#lightbox').find('.box-middle > .box-interior');
            $content.append('<div id="tempContainer"></div>');
            
            if (type != 'error'){
                showMask();
                $content.find('#tempContainer').load(href+' #'+type, function (responseText, textStatus) {
                    if(type == 'gallery'){
                        var iW = $content.find('img').width();
                        var gH = $content.find('#gallery').height();
                        
                        if (iW <= 440){
                            $(this).find('.imgContainer').css('margin-left',220-iW/2)
                            iW = 440;
                        } else if (iW >= 890) {
                            iW = 890;
                        };
                        $content.css({'overflow':'hidden'});
                        $('#lightbox').css({'width':iW+48, 'marginLeft':-(iW+48)/2, 'height':gH+40});
                        prevNext($content, type, iW, gH);
                        $(this).find('a.prev').css({'left':-66});
                        $(this).find('a.next').css({'right':-66});
                    } else if (type == 'video'){
                        if ($content.find('#body-copy').length > 0) {
                            document.getElementById('tempContainer').innerHTML += '<p class="transcript"><a href="'+href+'">View a transcript</a></p>';
                        }
						$('#tempContainer').append('<div class="clear"></clear>');
                        var vW = $content.find('#video').width();
                        var vH = $content.find('#video').height();
                        $.getScript('/media/scripts/swfobject.js');
                        $.getScript('/media/scripts/videos.js', function(){
                            var videoID = href.split('/')[2];
                            ace.videos.player.init('video', videoID);
                            $('#lightbox').css({'width':vW+48, 'marginLeft':-(vW+48)/2, 'height':vH+40});
                        });
                    };
                    emptyTextBox();
                    var lbH = $('#lightbox').height();
                    var docH = $('.mask').height();
                    if(top+lbH > docH){
                        $('.mask').height(top+lbH+40);
                    }

                    
                    $('#lightbox').animate({'delay':1},150).animate({'opacity':1},300);
                });
            };// end if not error
        };
        function closeLB($link){
            hideMask();
            //$link.focus();
            $('#lightbox').fadeOut(200, function(){
                $(this).css({'top':'', 'width':'', 'marginLeft':'', 'height':''});
                $(this).find('.box-middle > .box-interior').css({'width':'', 'height':''});
                $(this).find('.box-middle > .box-interior').empty()
            });
        };
        
        function getHREF($link, href, type){
            if($link.hasClass('twitter')){
                type = 'util_twitter';
            } else if ($link.hasClass('rss')){
                type = 'util_rss';
            } else if ($link.hasClass('newsletter')){
                type = 'util_newsletter';
            } else if ($link.hasClass('image')){
                type = 'gallery';
            } else if ($link.hasClass('video') || $link.hasClass('videoText')){
                type = 'video';
            } else {
                type = 'error';
                alert('error - missing type class')
            };
            return type;
        };
        
        
        function prevNext($content, type, iW, gH) {
            
            $content.find('.prev, .next').click(function(){
                var href = $(this).attr('href');
                
                $('#lightbox').fadeOut(200, function(){
                    $(this).css({'top':'', 'width':'', 'marginLeft':'', 'height':''});
                    $(this).find('.box-middle > .box-interior').css({'width':'', 'height':''});
                    $(this).find('.box-middle > .box-interior').empty();
                    type = 'gallery';
                    
                    var top = $(document).scrollTop() + 40;
                    if(top <= 140){top = 140}
                    $('#lightbox').css('top',top);

                    getContent($(this), href, type)
                    
                });
                return false
            });
            
            $content.find('.imgContainer').bind("mouseenter", function(e){
                $(this).find('a.prev').animate({'left':0},200);
                $(this).find('a.next').animate({'right':0},200);
            }).bind("mouseleave", function(e){
                $(this).find('a.prev').animate({'left':-66},200);
                $(this).find('a.next').animate({'right':-66},200);
            });
        };
    }; // end plugin PLlightbox
// end of closure
})(jQuery);

$.fn.PLlightbox.defaults = {
    //duration: 800
};
