/*
NIGallery - jQuery Neoinfo ltd Gallery Plugin
Author: 		Ivan Vidovic
Version:		1.1 (September 29, 2010)
Documentation: 	http://www.neoinfo.hr/jquery/gallery/
*/
(function ($) {
    $.fn.ni_gallery = function (options) {
        var opts = $.extend($.fn.ni_gallery.defaults, options);

        var panel;
        var gallery;
        var current_displayed_element;
        var current_displayed_element_ID;
        var prev_icon;
        var next_icon;

        function moveToNextElement() {
            //function that moves forward on next img in gallery. 
            //If it's last img it will start from begining
            if (current_displayed_element.next(".ni-gallery-element").length == 0) {
                current_displayed_element = gallery.first();
            } else {
                current_displayed_element = current_displayed_element.next(".ni-gallery-element");
            }
        }

        function moveToPrevElement() {
            //function that moves back on previous img in gallery.
            //If it's first img it will move to last img in gallery
            if (current_displayed_element.prev(".ni-gallery-element").length == 0) {
                current_displayed_element = gallery.last();
            } else {
                current_displayed_element = current_displayed_element.prev(".ni-gallery-element");
            }
        }

        function nextPreview() {
            //This function hides current img and shows next img	

            prev_icon.unbind('click');
            next_icon.unbind('click');

            current_displayed_element.fadeOut(function () {
                moveToNextElement();
                current_displayed_element.fadeIn(function () {
                    bindClickPrevNext();
                });
            });
        }

        function bindClickPrevNext() {
            prev_icon.click(function () {
                prevPreview();
            });
            next_icon.click(function () {
                nextPreview();
            });
        }

        function prevPreview() {
            //This function hides current img and shows previous img		

            prev_icon.unbind('click');
            next_icon.unbind('click');

            current_displayed_element.fadeOut(function () {
                moveToPrevElement();
                current_displayed_element.fadeIn(function () {
                    bindClickPrevNext();
                });
            });
        }

        function initDimensions() {
            //set panel dimensions same as dimensions of gallery img
            panel.css("width", opts.width);
            panel.css("height", opts.height);
            if (opts.imgSize == "fit") {
                gallery.find(".ni-gallery-element-img").attr("width", opts.width);
                gallery.find(".ni-gallery-element-img").attr("height", opts.height);
            } else if (opts.imgSize == "leave") {
                gallery.find(".ni-gallery-element-img").each(function () {
                    var myImg = $(this);
                    myImg.css("position", "absolute");
                    myImg.css("top", opts.height - myImg.outerHeight());
                    myImg.css("left", opts.width - myImg.outerWidth() - 8);
                });
            }
        }

        function initText() {
            //set position of text to display
            gallery.find(".ni-gallery-element-text").each(function () {
                var text = $(this);
                var padding = 40;
                if (opts.textWidth > 0 && text.text().length > 5) {
                    text.css("width", opts.textWidth - padding);
                }
                if (opts.textHeight > 0 && text.text().length > 5) {
                    text.css("height", opts.textHeight - padding);
                }

                if (text.text().length < 5) {
                    text.css("padding", 0);
                }

                //decide where to position text X cord
                if (opts.textXPosition == "left") {
                    text.css("left", 0);
                } else if (opts.textXPosition == "center") {
                    text.css("left", opts.width / 2 - text.outerWidth() / 2);
                } else if (opts.textXPosition == "right") {
                    text.css("left", opts.width - text.outerWidth());
                } else {
                    text.css("left", 0);
                }
                //decide where to position text Y cord
                if (opts.textYPosition == "top") {
                    text.css("top", 0);
                } else if (opts.textYPosition == "center") {
                    text.css("top", opts.height / 2 - text.outerHeight() / 2);
                } else if (opts.textYPosition == "bottom") {
                    text.css("top", opts.height - text.outerHeight());
                } else {
                    text.css("top", 0);
                }
            });
        }

        function initPrevNextItems() {
            //setup of back and forward icons
            next_icon = panel.find(".ni-gallery-element-next");
            if (next_icon.length == 0) {
                panel.append("<div class='ni-gallery-element-next'></div>");
                next_icon = panel.find(".ni-gallery-element-next");
            }
            prev_icon = panel.find(".ni-gallery-element-prev");
            if (prev_icon.length == 0) {
                panel.append("<div class='ni-gallery-element-prev'></div>");
                prev_icon = panel.find(".ni-gallery-element-prev");
            }

            var img = current_displayed_element.find(".ni-gallery-element-img");

            var newTop = opts.height / 2 - next_icon.outerHeight() / 2;
            var newLeft = opts.width - next_icon.outerWidth(); //img.position().left + img.outerWidth() - next_icon.outerWidth();

            prev_icon.css("top", newTop);
            prev_icon.css("left", opts.prevIconLeft); //img.position().left);

            next_icon.css("top", newTop);
            next_icon.css("left", newLeft - 14);

            bindClickPrevNext();
        }

        return this.each(function () {

            //init all objects			
            panel = $(this);
            gallery = $(this).find(".ni-gallery-element");
            current_displayed_element = gallery.first();

            initDimensions();
            initText();
            initPrevNextItems();

            gallery.hide();
            current_displayed_element.show();
            current_displayed_element_ID = window.setInterval(nextPreview, opts.interval);

            panel.hover(
				function () {
				    window.clearInterval(current_displayed_element_ID);
				    next_icon.show();
				    prev_icon.show();
				},
				function () {
				    current_displayed_element_ID = window.setInterval(nextPreview, opts.interval);
				    next_icon.hide();
				    prev_icon.hide();
				}
			);
        });
    };

    $.fn.ni_gallery.defaults = {
        interval: 4000, // interval of slide change
        width: 1140, // width of gallery container
        height: 550, // height of gallery container
        textXPosition: "left", // left, center, right - position of text
        textYPosition: "top", // top, center, bottom - position of text
        textHeight: 0, // if not 0 it will be fixed size of area where text is displayed
        textWidth: 0, // if not 0 it will be fixed size of area where text is displayed
        prevIconLeft: 0, // position of previous icon
        imgSize: "leave" // leave, fit - if you use leave it will do nothing with smaller imgs than gallery,
        // and bigger imgs they will be scaled  to gallery
        //when fit is used imgs will be resized to fit gallery
    };
})(jQuery);
