/*
This is based on the ClickScroller Plugin as follows:
jQuery Version:				jQuery 1.3.2
Plugin Name:				ClickScroller V 1.0
Plugin by: 					Jeff Waterfall: http://www.threeformed.com	
License:					ClickScroller is licensed under a Creative Commons Attribution 3.0 Unported License
Read more about this license at --> http://creativecommons.org/licenses/by/3.0/			
*/
(function($) {
    $.fn.clickScroll = function(options) {
        // setup default settings
        var defaults = {
            speed: 400,
            easing: 'linear',
            imageContainer: '.FeaturePagesScroller',
            lessBtn: '.clickscroll-less',
            moreBtn: '.clickscroll-more',
            btnFadeSpeed: 100,
            autoHideNav: true,
            horizontal: false,
            hideNavIfNotNeccasery: true
        },
    	settings = $.extend({}, defaults, options);

        return this.each(function() {
            var obj = $(this);
            var itemContainer = obj.find(settings.imageContainer + ':first');
            var lessBtn = obj.find(settings.lessBtn + ':first');
            var moreBtn = obj.find(settings.moreBtn + ':first');
            var thumbs_scroll_interval = false;

            // Variables
            var frameHeight = itemContainer.innerHeight();
            var frameWidth = itemContainer.innerWidth();
            var listHeight = itemContainer.children('ul').innerHeight();
            var listWidth = 0;

            if (settings.horizontal) { // Get the width and apply it
                setListWidth();
            }

            var itemCount = itemContainer.children('ul').children('li').size();
            var itemHeight = listHeight / itemCount;
            var steps = frameHeight / itemHeight;
            var groupHeight = Math.ceil(listHeight / steps);
            //var groupWidth = Math.ceil(listWidth / steps);
            //var groupWidth = 250;
            var groupWidth = frameWidth - 50;
            var step = 0;
            var targ = 0;

            setControlStates();

            if (listHeight > frameHeight) {
                itemContainer.show();
            } else {
                if (settings.autoHideNav) {
                    moreBtn.hide();
                }
            }

            lessBtn.click(function() {
                return false; // prevents from page jump (event when buttons are disabled)
            });

            moreBtn.click(function() {
                return false; // prevents from page jump (event when buttons are disabled)							   
            });

            var scrollMore = function() {
                stopScrolling();
                step++;
                if (settings.horizontal) {
                    targ -= groupWidth;

                    //avoid scrolling over the edges
                    if (targ < frameWidth - listWidth) {
                        targ = frameWidth - listWidth;
                    }

                    itemContainer.children('ul').stop(true).animate({ 'left': targ }, settings.speed, settings.easing);
                } else {
                    targ -= groupHeight;
                    itemContainer.children('ul').stop(true).animate({ 'top': targ }, settings.speed, settings.easing);
                }

                setControlStates();

                return false;
            }
            moreBtn.bind('click', scrollMore);

            var startScrolling = function() {
                var direction = 'left';
                if ($(this).is(settings.lessBtn)) {
                    direction = 'right';
                };

                thumbs_scroll_interval = setInterval(
                    function() {
                        //var left = imageContainer.scrollLeft() + 1;
                        if (direction == 'left') {
                            targ--;
                            if (targ < frameWidth - listWidth) {
                                targ = frameWidth - listWidth;
                            }
                        } else {
                            targ++;
                            if (targ > 0) {
                                targ = 0;
                            }
                        }

                        itemContainer.children('ul').animate({ 'left': targ }, 0);
                        setControlStates();
                    },
                    10
                  );
                $(this).css('opacity', 1);
            }

            var stopScrolling = function() {
                clearInterval(thumbs_scroll_interval);
                $(this).css('opacity', 0.6);
            }

            moreBtn.hover(startScrolling, stopScrolling);
            lessBtn.hover(startScrolling, stopScrolling);

            var scrollLess = function() {
                stopScrolling();
                step--;
                if (settings.horizontal) {
                    targ += groupWidth;

                    //avoid scrolling over the edges                    
                    if (targ > 0) {
                        targ = 0;
                    }
                    itemContainer.children('ul').stop(true).animate({ 'left': targ }, settings.speed, settings.easing);
                } else {
                    targ += groupHeight;
                    itemContainer.children('ul').stop(true).animate({ 'top': targ }, settings.speed, settings.easing);
                }

                setControlStates();

                return false;
            }
            lessBtn.bind('click', scrollLess);

            function setListWidth() {
                itemContainer.children('ul').children('li').each(function(i) {
                    listWidth += $(this).outerWidth(true);
                });
                itemContainer.children('ul').width(listWidth);
            }

            function setControlStates() {
                //alert(areControlsNeeded())
                if (!areControlsNeeded()) {
                    lessBtn.hide();
                    moreBtn.hide();
                } else {
                    if (isPanelAtRightEdge()) {
                        if (settings.autoHideNav) {
                            moreBtn.fadeOut(settings.btnFadeSpeed);
                        }
                        moreBtn.unbind('click', scrollMore);
                    }

                    if (isPanelAtLeftEdge()) {
                        if (settings.autoHideNav) {
                            lessBtn.fadeOut(settings.btnFadeSpeed);
                        }
                        lessBtn.unbind('click', scrollLess);
                    }

                    if (!isPanelAtRightEdge() && !isPanelAtLeftEdge()) {
                        lessBtn.bind('click', scrollLess);
                        lessBtn.fadeIn(settings.btnFadeSpeed);
                        moreBtn.bind('click', scrollMore);
                        moreBtn.fadeIn(settings.btnFadeSpeed);
                    }
                }
            }

            function isPanelAtRightEdge() {
                return targ <= frameWidth - listWidth;
            }

            function isPanelAtLeftEdge() {
                return targ >= 0;
            }

            function isContentBiggerThanFrame() {
                if (settings.horizontal) {
                    return listWidth > frameWidth;
                } else {
                    return listHeight > frameHeight;
                }
            }

            function areControlsNeeded() {
                return !settings.hideNavIfNotNeccasery || isContentBiggerThanFrame()
            }
        }); // END: return this

        // returns the jQuery object to allow for chainability.  
        return this;
    };
})(jQuery);
