$.fn.imgFbBox = function(option)
{	

    /* option */
    option = jQuery.extend({
        htmlFbBox: '<div id="fb-window"><div id="fb-close">Zavřít</div><div id="fb-paging">paging</div><h2 id="fb-title"></h2><p id="fb-desc"></p><div id="fb-img-box"></div><div id="fb-gallery"></div><div class="break"></div><div id="fg-gallery-info"></div></div>',
        showTitle: false,
        showDesc: false,
        showGallery: true,
        showPaging: false, // TODO
        listNumber: 5,
        customClass: ''
    }, option);
    
    /* HELP */   
    var firstRound = true;
    
    var createOverlay = function showWindow(){
        var overlay = document.createElement('div');
        $(overlay).attr('id', 'fb-box-overlay').bind('click', hideBox).appendTo('body');
        $(overlay).each(overlayHeight);
    }
    
    var overlayHeight = function overlayHeight(){
        $('#fb-box-overlay').css({'height': $(document).height(), 'min-height': '100%'});
        setTimeout(overlayHeight, 200); 
    }
    
    var hideBox = function hideBox(){
        $('#fb-box-overlay, #fb-window').empty().remove();
        firstRound = true; 
    }
    
    var showWindow = function showWindow(){
        $('#fb-box-overlay').after(option.htmlFbBox);
        $('#fb-window').attr('class', option.customClass).each(showOption);
        $('#fb-gallery').each(createGallery);
        $('#fb-gallery').each(actualyObject);
        $('#fb-close').click(hideBox);
    }
    
    var actualyObject = function actualyObject(){
        var fbTitle = $('#fb-gallery li.active img').attr('alt');
        var fbDesc = $('#fb-gallery li.active img').attr('title');
        $('#fb-img-box').empty();
        $('#fb-title').empty().append(fbTitle);
        $('#fb-desc').empty().append(fbDesc);
        var imgPreloader = document.createElement('img');
        $(imgPreloader).bind('load', showImg);
        $(imgPreloader).attr({'src': $('#fb-gallery li.active a').attr('href'), 'alt':' '});
        $('#fg-gallery-info').text($('#fb-gallery li').index($('#fb-gallery li.active'))+1 + ' / ' + $('#fb-gallery li').size());
    }
    
    var showImg = function showImg(){
        this.onload = null;
        if (firstRound == false){$(this).hide();}
        $('#fb-img-box').empty().append($(this));
        var imgWinH = $('#fb-img-box').height()
        var imgH = $(this).height()
        // Todo Přepínání na centrování obrázku nebo animacni zvětšování;
        //$('#fb-img-box').animate({'height': imgH}, 300);
        if (firstRound == true){$(this).css('margin-top', (imgWinH - imgH)/2 );}
        else{ $(this).css('margin-top', (imgWinH - imgH)/2 ).fadeIn(700);}
        firstRound = false;
        $(this).each(boxPosition);
    }
    
    var createGallery = function createGallery(){
        $('.' + thisFbClass).clone()
                            .appendTo('#fb-gallery');
        $('#fb-gallery').each(function(){
        	$('a', this).contents().not("[nodeType=1]").remove();
            $(this).wrapInner('<ul></ul>');
            $('a', this).wrap('<li></li>');
            $('li', this).eq(thisFbIndex).addClass('active');
            $('#fb-gallery').prepend('<div id="fb-prev">Předchozí</div>');
            $('#fb-gallery').append('<div id="fb-next">Další</div>');
            $('#fb-prev', this).click(prevGallery);
            $('#fb-next', this).click(nextGallery);
            if($('#fb-gallery li.active').is(':first-child') == true){$('#fb-prev').addClass('fb-prev-disable').removeClass('fb-prev-hover');}
            else{$('#fb-prev').removeClass('fb-prev-disable').hover(function(){if($(this).hasClass('fb-prev-disable') == false ){$(this).addClass('fb-prev-hover')}}, function(){$(this).removeClass('fb-prev-hover')});}
            if($('#fb-gallery li.active').is(':last-child') == true){$('#fb-next').addClass('fb-next-disable').removeClass('fb-next-hover');}
            else{$('#fb-next').removeClass('fb-next-disable').hover(function(){if($(this).hasClass('fb-next-disable') == false ){$(this).addClass('fb-next-hover')}}, function(){$(this).removeClass('fb-next-hover')});}
            $('li.active', this).each(rotateGallery);
            $('li', this).children('a').click(changeImg);
        });  
    }
    
    var prevGallery = function prevGalery(){
        $('#fb-gallery li.active').prev().children('a').trigger('click');
        
    }
    var nextGallery = function nextGalery(){
        $('#fb-gallery li.active').next().children('a').trigger('click');
        
    }
    
    var rotateGallery = function rotateGalery(){
        $('#fb-gallery li').hide();
        var startIndex = thisFbIndex - ((option.listNumber - 1) / 2) + ((((option.listNumber - 1)) % 2)/2);
        var finishIndex = thisFbIndex + ((option.listNumber - 1) / 2) + ((((option.listNumber - 1)) % 2)/2);
        if(startIndex<0){
            finishIndex = finishIndex - (startIndex);
            startIndex = 0;
            if(finishIndex > $('#fb-gallery .' + thisFbClass).size() - 1){
                finishIndex = $('#fb-gallery .' + thisFbClass).size() - 1;
            }
        }
        if(finishIndex > $('#fb-gallery .' + thisFbClass).size() - 1){
            var help = finishIndex - ($('#fb-gallery .' + thisFbClass).size() - 1)
            startIndex = startIndex - (help);
            finishIndex = $('#fb-gallery .' + thisFbClass).size() - 1;
            if(startIndex<0){
                startIndex = 0;
            }
        }
        for(i = startIndex; i <= finishIndex; i++){
            $('#fb-gallery li').eq(i).show();
        }
    }
    
    var changeImg = function changeImg(){
        // TODO vyresit klik na stejnou polozku
        $('#fb-gallery li').removeClass('active');
        $(this).parent().addClass('active');
        /* nav */
        if($('#fb-gallery li.active').is(':first-child') == true){$('#fb-prev').addClass('fb-prev-disable').removeClass('fb-prev-hover');}
        else{$('#fb-prev').removeClass('fb-prev-disable').hover(function(){if($(this).hasClass('fb-prev-disable') == false ){$(this).addClass('fb-prev-hover')}}, function(){$(this).removeClass('fb-prev-hover')});}
        if($('#fb-gallery li.active').is(':last-child') == true){$('#fb-next').addClass('fb-next-disable').removeClass('fb-next-hover');}
        else{$('#fb-next').removeClass('fb-next-disable').hover(function(){if($(this).hasClass('fb-next-disable') == false ){$(this).addClass('fb-next-hover')}}, function(){$(this).removeClass('fb-next-hover')});}
        thisFbIndex = $('#fb-gallery .' + thisFbClass).index(this);
        /* function */
        $(this).each(actualyObject);
        $(this).each(rotateGallery);
        return false;
    }
    
    var showOption = function showOption(){
        if(option.showTitle == false){$('#fb-title').hide()}
        if(option.showDesc == false){$('#fb-desc').hide()}
        if(option.showGallery == false){$('#fb-gallery').hide()}
        if(option.showPaging == false){$('#fb-paging').hide()}
    }
    
    $(this).click(function(){
        thisFbClass = $(this).attr('class');
        thisFbIndex = $('.' + thisFbClass).index(this);
        $(this).each(createOverlay);
        $(this).each(showWindow);
        $(this).each(boxPosition);
        return false;
    });
    
    var boxPosition = function boxPosition(){
        var widthPage = $('body').innerWidth();
        var widthFbGallery = $('#fb-window').innerWidth();
        var topPos = $(document).scrollTop();
        $('#fb-window').css({'left': (widthPage - widthFbGallery) / 2, 'top': topPos + 40 + 'px'});
    };
};
