(function ($) {

    $.fn.Slideshow = function () {

        var self = $(this);

        //vars
        var itemWidth = self.find(".pictures ul li").css("width").replace("px", ""),
       	numItems = self.find(".pictures ul li").length,
       	listWidth = numItems * itemWidth,
       	lastItemScroll = (numItems * itemWidth) - itemWidth,
       	index = 1,
       	time = 4000,
       	timeout,
		showHide = true,
		cycle = function () {
		    if (self.find(".navigation ul li.active").next().length) {
		        self.find(".navigation ul li.active").next().trigger("click");
		    } else {
		        self.find(".navigation ul li:eq(0)").trigger("click");
		    }
		},
		handleTimeout = function () {
		    clearTimeout(timeout);
		    timeout = setTimeout(cycle, time);
		},
		manageNavigationClass = function (element) {
		    element.parent().find("li").removeClass("active");
		    element.addClass("active");
		}

        // [begin] inicializar ------------------------------------>

        if (self.find(".pictures ul li.active").length == 0) {
            self.find(".pictures ul").css({ "width": listWidth });
            showHide = false;
        }

        if (self.attr("id") == 'product-slideshow') {
            self.find(".pictures ul").append('<li style="position:absolute; z-index:2; top:0; left:0; width:520px; height:390px; background:#FFFFFF;">');
        }

        if (numItems == 1) {
            self.find(".navigation").hide(); 
        } else {
            timeout = setTimeout(cycle, time);
        }

        // [end] inicializar ------------------------------------>

        //arrows
        self.find(".arrow").click(function () {
            if ($(this).is(".right")) {
                cycle();
            } else if ($(this).is(".left")) {
                if (self.find(".navigation ul li.active").prev().length) {
                    self.find(".navigation ul li.active").prev().trigger("click");
                } else {
                    self.find(".navigation ul li:eq(" + (self.find(".navigation ul li").length - 1) + ")").trigger("click");
                }
            }
        });

        self.find(".pictures ul li").hover(function () {
            clearTimeout(timeout);
        }, function () {
            timeout = setTimeout(cycle, time);
        });

        self.find(".navigation ul li").click(function () {

            var element = $(this);

            if (showHide == true) {

                self.find(".pictures ul li.active").fadeOut("slow").removeClass("active").addClass("inactive");


                self.find(".pictures ul li:eq(" + element.index() + ")").fadeIn(1500).removeClass("inactive").addClass("active");

                self.find(".enlarge ul li.active").removeClass("active").addClass("inactive");
                self.find(".enlarge ul li:eq(" + element.index() + ")").removeClass("inactive").addClass("active");
                handleTimeout();
            } else {
                self.find(".pictures ul").animate({ "left": "-" + (itemWidth * element.index()) + "px" }, function () {
                    handleTimeout();
                });
            }

            self.find(".subtitles ul li.active").removeClass("active");
            self.find(".subtitles ul li:eq(" + element.index() + ")").addClass("active");

            index = element.index() + 1;

            manageNavigationClass(element);

        });

    };

})(jQuery);
