/*!
* Site methods
*/
(function ($) {
	
	 //$("#ContainerGaleria").find("a").lightBox();

    $.fn.lightBox = function (settings) {
        // Settings to configure the jQuery lightBox plugin how you like
        settings = jQuery.extend({
            // Configuration related to overlay
            overlayBgColor: '#003966', 	// (string) Background color to overlay; inform a hexadecimal value like: #RRGGBB. Where RR, GG, and BB are the hexadecimal values for the red, green, and blue values of the color.
            overlayOpacity: 0.9, 	// (integer) Opacity value to overlay; inform: 0.X. Where X are number from 0 to 9
            // Don´t alter these variables in any way
            imageArray: [],
            activeImage: 0
        }, settings);

        var jQueryMatchedObj = this;
        function _initialize() {
            _start(this, jQueryMatchedObj);
            return false;
        }

        function _start(objClicked, jQueryMatchedObj) {
            $('embed, object, select').css('visibility', 'hidden');

            _set_interface();

            // Zera as variaveis globais
            settings.imageArray.length = 0;
            settings.activeImage = 0;

            // Uma imagem ou várias?
            if (jQueryMatchedObj.length == 1) {
                $("#lightbox-nav-btnPrev,#lightbox-nav-btnNext").css("visibility", "hidden");
            }
            var i = jQueryMatchedObj.length,
            t = 0,
            href = "",
            title = "";

            while (i--) {
                href = jQueryMatchedObj[t].getAttribute('href')
                title = jQueryMatchedObj[t].getAttribute('title'); ;


                if (href == objClicked.getAttribute('href')) {
                    settings.activeImage = t;
                }

                settings.imageArray.push([href, title]);
                ++t;
            }

            _set_image_to_view();
        }

        function _set_interface() {
            // Definição de variaveis
            var RefBody = $(document.body),
            RefDocument = $(document);

            // Adiciona HTML
            RefBody.append("<div id='jquery-overlay'></div><div id='jquery-lightbox'><div class='lightbox'><div class='left' style='width:226px;'><img src='images/logo_lb.png' alt='Nutrin' width='226' height='70' rel='conteudo' /></div ><div class='right'><a id='lightbox-secNav-btnClose' href='#'><img src='images/lightbox_btn_fechar.jpg' alt='Fechar' /></a></div><div class='clear'></div><div class='lightbox_navigation'><div id='lightbox-nav-btnPrev' style='visibility: hidden' class='left'><a href='#'><img src='images/lightbox_btn_left.jpg' alt='Anterior' /></div><div id='lightbox-container-image-box' class='left'><img id='lightbox-image' alt='Nome da notícia' /></div><div id='lightbox-loading' style='float: left; width:790px;height:466px;'><table style='width:100%;height:100%;'><tr><td style='vertical-align:middle;'><img style='width: 16px; height: 16px;' src='images/loader.gif' /></td></tr></table></div><div id='lightbox-nav-btnNext' class='right'><a href='#'><img src='images/lightbox_btn_right.jpg' alt='Próximo' /></a></div><div class='clear'></div></div></div></div>");


            var RefOverlay = $('#jquery-overlay'),
            RefLightbox = $('#jquery-lightbox'),
            RefBtnPrev = $('#lightbox-nav-btnPrev'),
            RefBtnNext = $('#lightbox-nav-btnNext');

            //Posiciona overlay e Lightbox, além de mostra-los e definir o evento click no overlay
            RefOverlay.css({
                backgroundColor: settings.overlayBgColor,
                opacity: settings.overlayOpacity,
                width: RefDocument.width(),
                height: RefDocument.height()
            }).fadeIn().click(function () {
                _finish();
            });

            RefLightbox.css({
                top: RefDocument.scrollTop() + 8,
                left: RefDocument.scrollLeft()
            }).show().click(function () {
                _finish();
            });

            //Define a paginação e seus eventos
            RefBtnNext.click(function (e) {
                ++settings.activeImage;
                _set_image_to_view();

                e.preventDefault();
                e.stopPropagation()
            });

            RefBtnPrev.click(function (e) {
                --settings.activeImage;
                _set_image_to_view();

                e.preventDefault();
                e.stopPropagation()
            });

            // Evento click do botão close. Fecha o lightbox
            $('#lightbox-secNav-btnClose').click(function () {
                _finish();
                return false;
            });

            // Quando a janela for redimensionada, muda o estilo do lightbox
            window.onresize = function () {
                RefOverlay.css({
                    backgroundColor: settings.overlayBgColor,
                    opacity: settings.overlayOpacity,
                    width: RefDocument.width(),
                    height: RefDocument.height()
                });

                RefLightbox.css({
                    top: RefDocument.scrollTop() + 8,
                    left: RefDocument.scrollLeft()
                });
            }
        }

        function _set_navigation() {
            var RefBtnPrev = $('#lightbox-nav-btnPrev'),
            RefBtnNext = $('#lightbox-nav-btnNext');

            if (settings.activeImage != 0) {
                RefBtnPrev.css("visibility", "visible");
            }
            else {
                RefBtnPrev.css("visibility", "hidden");
            }

            if (settings.activeImage != (settings.imageArray.length - 1)) {
                RefBtnNext.css("visibility", "visible");
            }
            else {
                RefBtnNext.css("visibility", "hidden");
            }
        }

        function _set_image_to_view() {
            var img = new Image();
            _set_navigation();

            $('#lightbox-loading').show();
            $('#lightbox-image').hide()
            img.src = settings.imageArray[settings.activeImage][0];

            if (img.nodeType === 1 && img.tagName.toLowerCase() === "img" && img.src !== "") img.complete || img.readyState == 4 ? _show_image(img) : $(img).bind("load", function () {
                _show_image(img)
            })
        }

        function _show_image(img) {

            setTimeout(function () {
                $('#lightbox-loading').hide();
                $('#lightbox-image').fadeIn().attr("src", img.src);
            }, 3000);
        }

        function _finish() {
            $('#jquery-lightbox').remove();
            $('#jquery-overlay').fadeOut(function () { $('#jquery-overlay').remove(); });
            // Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
            $('embed, object, select').css({ 'visibility': 'visible' });
        }

        // Return the jQuery object for chaining. The unbind method is used to avoid click conflict when the plugin is called more than once
        return this.unbind('click').click(_initialize);
    };
})(jQuery);
