(function ($) {
    $.fn.contentFixedDetect = function (options) {
        //settings are default to add appliedClass when above yScrollPoint
        var settings = $.extend({
            appliedClass: "unscrolled", //default for at top..
            useScrollPoint: true, //use point on page, false to use the selectors (this) outerheight
            yScrollPoint: 200, //point at which you want to check for scrolling
            scrollTimeOut: 250, //timeout to check scroll pos,
            YOffSet: 0, //optional offset for position
            elementToApplyClass: "body", //the element that you want the appliedClass added or removed from
            addAppliedClassAbovePoint: true, // true : remove the applied class if scrolling is above the yScrollPoint, false for below.
            classToApplyToSelector: "" //use this option if you need specific classes added to the selector if @elementToApplyClass is not the selector.
        }, options, true),
        doc = document.documentElement;
        

        return this.each(function () {

           var wasScrolled = false,
            $slector = $(this),
            selectorHeight;
        
        function init() {
            /*run checks right on init just to make sure all the right classes are set if user ends up in the middle of the page on start due to reload or #*/
            if (settings.useScrollPoint) {
                setTimeout(checkYScrollPoint, settings.scrollTimeOut);
            } else {
                //were using the selectors outer height and optional offset.
                setTimeout(CheckYFromOuterHeight, settings.scrollTimeOut);
            }
            /*add listener to watch the scroll */
            $(window).scroll(function () {
                if (!wasScrolled) {
                    wasScrolled = true;
                    //check to see if were using scroll point.
                    if (settings.useScrollPoint) {
                        setTimeout(checkYScrollPoint, settings.scrollTimeOut);
                    } else {
                        //were using the selectors outer height and optional offset.
                        setTimeout(CheckYFromOuterHeight, settings.scrollTimeOut);
                    }
                }
            });

            //add resize for selector height detect.
            if (!settings.useScrollPoint) {
                selectorHeight = $slector.outerHeight();
                $(window).resize(function () {
                    selectorHeight = $slector.outerHeight();
                    setTimeout(CheckYFromOuterHeight, settings.scrollTimeOut);
                });
            }
        }

        //check against scroll position
        function checkYScrollPoint() {
            var ypos = getYPosition();
            if (settings.addAppliedClassAbovePoint) {
                //add class if were above the point
                if (ypos >= (settings.yScrollPoint + settings.YOffSet)) {
                    $(settings.elementToApplyClass).removeClass(settings.appliedClass);
                    //remove specific class to selector if given
                    if (settings.classToApplyToSelector !== "") {
                        $slector.removeClass(settings.classToApplyToSelector);
                    }
                }
                else {
                    $(settings.elementToApplyClass).addClass(settings.appliedClass);
                    //add specific class to selector if given
                    if (settings.classToApplyToSelector !== "") {
                        $slector.addClass(settings.classToApplyToSelector);
                    }
                }
            } else {
                //add class if were below below point
                if (ypos <= settings.yScrollPoint + settings.YOffSet) {
                    $(settings.elementToApplyClass).removeClass(settings.appliedClass);
                    //remove specific class to selector if given
                    if (settings.classToApplyToSelector !== "") {
                        $slector.removeClass(settings.classToApplyToSelector);
                    }
                }
                else {
                    $(settings.elementToApplyClass).addClass(settings.appliedClass);
                    //add specific class to selector if given
                    if (settings.classToApplyToSelector !== "") {
                        $slector.addClass(settings.classToApplyToSelector);
                    }
                }
            }
            //force check.
            wasScrolled = false;
        }

        //check from seectors outerheight 
        function CheckYFromOuterHeight() {
            var ypos = getYPosition();
            if (settings.addAppliedClassAbovePoint) {
                //remove class if were above the point
                if (ypos >= (selectorHeight + settings.YOffSet)) {
                    //past scrolled point so add the scrolled class
                    $(settings.elementToApplyClass).removeClass(settings.appliedClass);
                    //remove specific class to selector if given
                    if (settings.classToApplyToSelector !== "") {
                        $slector.removeClass(settings.classToApplyToSelector);
                    }
                } else {
                    //above scrolled class so remove the class.
                    $(settings.elementToApplyClass).addClass(settings.appliedClass);
                    //add specific class to selector if given
                    if (settings.classToApplyToSelector !== "") {
                        $slector.addClass(settings.classToApplyToSelector);
                    }
                }
            } else {
                //remove class if were below below point
                if (ypos <= (selectorHeight + settings.YOffSet)) {
                    //remove class to element spciefied
                    $(settings.elementToApplyClass).removeClass(settings.appliedClass);
                    //remove specific class to selector if given
                    if (settings.classToApplyToSelector !== "") {
                        $slector.removeClass(settings.classToApplyToSelector);
                    }
                }
                else {
                    $(settings.elementToApplyClass).addClass(settings.appliedClass);
                    //add specific class to selector if given
                    if (settings.classToApplyToSelector !== "") {
                        $slector.addClass(settings.classToApplyToSelector);
                    }

                }
            }
            //force check.
            wasScrolled = false;
        }

        //get y offset.
        function getYPosition() {
            return window.pageYOffset || doc.scrollTop;
        }
        //run onload to set classes etc classes added only only if the user ends up lower on the page..
        init();

        });
       
    };
})(jQuery);
