/*
 * Veil 1.0
 *
 * Author: Ryan Seddon
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 */

(function($) {
    $.fn.veil = function(options) {

        var defaults = {
            speed: 1000,
            hideContent: true,
            hideIndex: "none",
            showIndex: "none",
            showThisManyItems: "",
            itemsAreWhat: "",
            sectionFooter: ".section-footer",
            showThisManyItemsTopClass: "top-listing",
            showThisManyItemsHideClass: "hide-listing",
            showThisManyItemsButton: ".show-all",
            hideRevealedItemsButton: ".hide-extra",
            activeClass: "active-section",
            hoverClass: "hover-section"
        };
        var options = $.extend(defaults, options);

        // Checks to see if anchor link is in url
        if (window.location.hash) {
            var itemQS = window.location.hash.substring(1, location.hash.length);
            itemQS = itemQS.match(/\eq\d{1,}/).join("");
            itemQSDigitExtract = parseInt(itemQS.replace(/eq/, ""));
        }
		
        $(options.sectionFooter).show();

        return this.each(
			function(itemIndex) {
			    var title = $(this)
			    var content = $(this).next();
			    var contentlistings = content.find(options.itemsAreWhat);
			    
			    if (options.showThisManyItems != "" && typeof options.showThisManyItems == "number") {
			        $(contentlistings).each(function(itemIndex) {
			            if (itemIndex < options.showThisManyItems) {
			                $(this).addClass(options.showThisManyItemsTopClass);
			            } else {
			                $(this).addClass(options.showThisManyItemsHideClass);
			            }
			        });

			        $(options.showThisManyItemsButton).click(function(event) {
			            $(this).parent().parent().parent().find(options.itemsAreWhat).css({ display: "block" });
			            $(this).parent().css({ display: "none" });
			            $(this).parent().next().css({ display: "block" });
			            event.preventDefault();
			        });

			        $(options.hideRevealedItemsButton).click(function(event) {
			            $(this).parent().parent().parent().find(options.itemsAreWhat).removeAttr("style");
			            $(this).parent().css({ display: "none" });
			            $(this).parent().prev().css({ display: "block" });
			            event.preventDefault();
			        });
			    }

			    if (typeof options.hideIndex == "number") {
			        // Error handling to make sure showIndex and hideIndex aren't both specified
			        if (typeof options.showIndex == "number") { throw new Error("showIndex and hideIndex cannot be expressed in the same function, please use only one parameter"); }
			        
			        title.addClass(options.activeClass);
			        if (typeof itemQSDigitExtract == "number") {
			            if ($(this).attr('id') == itemQS) {
			                title.addClass(options.activeClass);
			                content.show();
			                // Anchor works out before all content is hidden/shown this makes anchor visible again
			                window.location = window.location.href;
			            } else {
			                title.removeClass(options.activeClass);
			                content.hide();
			            }
			        } else {
			            if (itemIndex == options.hideIndex) {
			                title.removeClass(options.activeClass);
			                content.hide();
			            }
			        }
			    }
			    else if (typeof options.showIndex == "number") {
			        // Error handling to make sure showIndex and hideIndex aren't both specified
			        if (typeof options.hideIndex == "number") { throw new Error("showIndex and hideIndex cannot be expressed in the same function, please use only one parameter"); }

			        content.hide();
			        if (typeof itemQSDigitExtract == "number") {
			            if ($(this).attr('id') == itemQS) {
			                title.addClass(options.activeClass);
			                content.show();
			                // Anchor works out before all content is hidden/shown this makes anchor visible again
			                window.location = window.location.href;
			            }
			        } else {
			            if (itemIndex == options.showIndex) {
			                title.addClass(options.activeClass);
			                content.show();
			            }
			        }
			    } else {
			        (options.hideContent) ? content.hide() : content.show();
			    }

			    title.click(function(event) {
			        title.toggleClass(options.activeClass);
			        content.slideToggle(options.speed);
			        event.preventDefault();
			        if(title.hasClass("active-section")){
						title.find(".section-extras span.sh-label").text("[hide links]");	
					} else {
						title.find(".section-extras span.sh-label").text("[show links]");	
					}
			    }).hover(function() {
			        title.toggleClass(options.hoverClass);
			    }, function() {
			        title.toggleClass(options.hoverClass);
			    });
			}
		);
    };
})(jQuery);
