(function($) {

    // custom moduler slider plug-in
    // syntax: $(selector).moduleSlider();
    $.fn.moduleSlider = function() {
        return this.each(function() {

            var wrapper = $(this),
				shell = wrapper.parent().parent(),
				controls = shell.find(".controls"),
				list,
				listItems,
				dots;

            wrapper.jcarousel({
                wrap: "circular",
                scroll: 1,
                initCallback: carousel_initCallback,
                buttonNextHTML: "<a class='jNext' href='#slider-next' id='slider-next'>Next</a>",
                buttonPrevHTML: "<a class='jPrev' href='#slider-previous' id='slider-previous'>Previous</a>"
            });

            function carousel_initCallback(c) {
                list = controls.find("ol");
                listItems = list.find("li");
                dots = listItems.find("a");

                list.width(($(dots[0]).width() * dots.length) + "px");

                dots.click(function(e) {
                    e.preventDefault();

                    var self = $(this);
                    dots.removeClass("active");
                    self.addClass("active");
                    c.scroll($.jcarousel.intval(self.text()));
                });
            }

            shell.find(".jcarousel-container .jPrev")
				.click(function(e) {
				    e.preventDefault();
				    var self = $(this),
						currentIndex = parseInt(dots.filter(".active").text()) - 1,
						newItem;

				    dots.removeClass("active");

				    if (currentIndex === 0) {
				        newItem = dots.filter(":last");
				    } else {
				        newItem = dots.filter(":eq(" + (currentIndex - 1) + ")");
				    }

				    newItem.addClass("active");

				})
				.appendTo(controls);

            shell.find(".jcarousel-container .jNext")
				.click(function(e) {
				    e.preventDefault();
				    var self = $(this),
						currentIndex = parseInt(dots.filter(".active").text()) - 1,
						newItem;

				    dots.removeClass("active");

				    if (currentIndex === (dots.length - 1)) {
				        newItem = dots.filter(":first");
				    } else {
				        newItem = dots.filter(":eq(" + (currentIndex + 1) + ")");
				    }

				    newItem.addClass("active");

				})
				.appendTo(controls);

        });
    };

    // custom moduler slider plug-in
    // syntax: $(selector).pollSlider();
    $.fn.pollSlider = function() {
        return this.each(function() {

            var wrapper = $(this),
	            ul = wrapper.find("ul");

            ul.jcarousel({
                wrap: "circular",
                scroll: 1,
                initCallback: function() {
                    var hiddenPrev = wrapper.find(".jcarousel-prev"),
			            hiddenNext = wrapper.find(".jcarousel-next"),
			            realPrev = wrapper.find(".ppoll-prev"),
			            realNext = wrapper.find(".ppoll-next");

                    realPrev.click(function(e) {
                        e.preventDefault();
                        hiddenPrev.trigger("click");
                    });

                    realNext.click(function(e) {
                        e.preventDefault();
                        hiddenNext.trigger("click");
                    });
                }
            });

        });
    };

    // custom 2 or 3 tabbed module plugin
    // syntax: $(selector).tabbedModule();
    $.fn.tabbedModule = function() {
        return this.each(function() {

            var root = $(this),
			    links = root.find(".module-tabs li a"),
			    groups = root.find(".module-groups").children("div"),
			    blank = groups.filter(".blank"),
			    h = 0,
			    w = blank.parent().parent().width();

            groups.each(function() {
                h = Math.max(h, $(this).height());
            }).height(h + "px").width(w + "px");

            blank
			    .css("z-index", "50")
			        .parent()
			            .height(h + "px");

            links.click(function(e) {
                e.preventDefault();

                var self = $(this),
				    i = links.index(self);

                if (!self.hasClass("active")) {
                    links.removeClass("active");
                    self.addClass("active");
                    groups.css("visibility", "hidden").css("z-index", "10");
                    groups.filter(":eq(" + i + ")").css("visibility", "visible").css("z-index", "100");
                    blank.css("z-index", "50").css("visibility", "visible");
                }
            });

        });
    }

    // custom drop cap applier will add <span class="dropCap"></span> around the 1st letter of the passed selector
    $.fn.DropCap = $.fn.dropCap = $.fn.dropcap = function() {
        return this.each(function() {
            var self = $(this);

            if (self.length > 0) {
                var raw = self.html(),
					first = $("<span></span>").addClass("dropCap").text(raw.charAt(0));
                // if the browser is IE, add a class to allow some additional css adjustment
                if (!$.support.opacity) {
                    first.addClass("ie");
                }

                if (first.html() != "&lt;") {
                    self.html(raw.substring(1)).prepend(first);
                }
            }
        });
    }

})(jQuery);
