$(document).ready(function(){
    var no_more_to_load = false;
    var wait_loading = false;
    $(window).scroll(function(){
        if (($(window).scrollTop() == $(document).height() - $(window).height()) && !no_more_to_load)
        {
            if(!wait_loading)
            {
                wait_loading = true;
                var last_id = $(".item").last().attr("id");
                last_id = last_id.replace('item_id_', '');
                last_id = parseInt(last_id);

                $('#loading').slideDown('fast', function(){
                    $.post("framework/controllers/load_more_items.php", {'last_id':last_id}, function(data){
                        $('#loading').fadeOut('fast', function(){
                            if (data != "")
                            {
                                $("#item_id_"+last_id).after(data);
                                wait_loading = false;
                                $('.location').fancybox();
                                $('.gallery').nivoSlider({
                                    effect:'slideInLeft',
                                    slices:1,
                                    animSpeed:250,
                                    pauseTime:3000,
                                    startSlide:0,
                                    directionNav:true,
                                    directionNavHide:false,
                                    controlNav:true,
                                    controlNavThumbs:false,
                                    keyboardNav:false,
                                    pauseOnHover:true,
                                    manualAdvance:true,
                                    captionOpacity:0.8,
                                    prevText: '',
                                    nextText: '',
                                    afterLoad: function(){
                                        $('.nivo-controlNav').each(function(){
                                            var thewidth = $(this).width();
                                            thewidth = (thewidth / 2) * -1;
                                            $(this).css('margin-left', thewidth).fadeTo('fast', 0.3);
                                        });
                                    }
                                });

                                $('.gallery').live('mouseenter', function(){
                                    $(this).find('.nivo-controlNav').fadeTo('fast', 1);
                                    $(this).find('.nivo-directionNav').fadeTo('fast', 1);
                                }).live('mouseleave', function(){
                                    $(this).find('.nivo-controlNav').fadeTo('fast', 0.3);
                                    $(this).find('.nivo-directionNav').fadeTo('fast', 0);
                                });
                            }
                            else
                            {
                                $("#item_id_"+last_id).after('<div class="clearfix"></div><div class="notice_box">There are no more items to load.</div>');
                                no_more_to_load = true;
                                wait_loading = false;
                            }
                        });
                    });
                });
            }
        }
    });
});
