﻿/*
*	Name:			my.blank.plugin.js
    Purpose:        Shell to use for the plugins we build
*/

(function ($) {
    // DONT FORGET TO NAME YOUR PLUGIN
    jQuery.fn.icgslider = function (options, i) {
        if (this.length > 1) {
            var a = new Array();
            this.each(
                function (i) {
                    a.push($(this).icgslider(options, i));
                });
            return a;
        }
        var opts = $.extend({}, $().icgslider.defaults, options);

        var slideCount, slideHeight, slideWidth;

        /* PUBLIC FUNCTIONS */

        this.Destroy = function (reInit) {
            var $container = this;
            var reInit = (reInit != undefined) ? reInit : false;
            $(container).removeData('myPlugin'); // this removes the flag so we can re-initialize
        };

        this.Update = function (options) {
            opts = null;
            opts = $.extend({}, $().ryaccordion.defaults, options);
            this.Destroy(true);
            return this.Create();
        };

        this.Create = function (iteration) {
            var $container = $(this);

            if ($container.data('icgslider') == true)
                return this;
            var totalWidth = 0, $wrapper = $container.wrap('<div class="' + opts.wrapperClass + '" />').parent(), actualSlideWidth = null;
            slideCount = $container.children(opts.contentElement).size();
            slideHeight = $container.outerHeight(false);
            slideWidth =  $container.outerWidth(false) / opts.itemsToDisplay;

            $wrapper.css({ position: 'relative', overflow: 'hidden', height: slideHeight, width: $container.outerWidth(false) });

            // oh my god i hate you IE
            if ($container.css('padding-left') == 'auto')
                $container.css('padding-left', 0);
            if ($container.css('padding-right') == 'auto')
                $container.css('padding-right', 0);
            if ($container.css('margin-left') == 'auto')
                $container.css('margin-left', 0);
            if ($container.css('margin-right') == 'auto')
                $container.css('margin-right', 0);

            $container.children(opts.contentElement).each(function (index) {
                var $slide = $(this);
                $slide.addClass('slide-' + (index + 1) + ' ' + opts.contentClass);
                if (slideWidth == null)
                    slideWidth = $slide.width();
                if (actualSlideWidth == null)
                    actualSlideWidth = slideWidth - (parseInt($slide.css('padding-left')) + parseInt($slide.css('padding-right'))) - (parseInt($slide.css('margin-left')) + parseInt($slide.css('margin-right')));

                $slide.css({ display: 'block', width: actualSlideWidth, height: slideHeight, overflow: 'auto', position: 'absolute', left: (index * slideWidth) + parseInt($slide.css('margin-left')) });

                if (index + 1 < opts.startItem + opts.itemsToDisplay &&
                    index + 1 >= opts.startItem) {
                    $slide.addClass(opts.enabledClass);
                }
                totalWidth += $slide.outerWidth();
            });

            $container.addClass(opts.containerClass).css({ left: 0, overflow: 'hidden', position: 'relative', width: (actualSlideWidth * slideCount) * 2, height: $wrapper.outerHeight() });

            if (slideCount > opts.itemsToDisplay) {
                if (opts.renderNext)
                    $('<a href="#" class="' + opts.nextButtonClass + '">Next</a>').appendTo($wrapper).bind('click.rySlider.onNextClick', function () { nextSlide($container, opts.itemsToDisplay, true); return false; });
                if (opts.renderPrevious)
                    $('<a href="#" class="' + opts.prevButtonClass + '">Previous</a>').appendTo($wrapper).bind('click.rySlider.onPrevClick', function () { previousSlide($container, opts.itemsToDisplay, true); return false; });
            }
            else
                $wrapper.addClass(opts.notEnoughChildrenClass);
                
            $container.data('icgslider', true);
            return this;
        };


        // Worker function to figure out whether a child element is 'shown' or not
        function addRemoveShowClass($item, $container) {
            if ($item.offset().left < $container.parent().offset().left || ($item.offset().left + $item.outerWidth(true)) > ($container.parent().offset().left + $container.parent().outerWidth()))
                $item.removeClass(opts.enabledClass);
            else
                $item.addClass(opts.enabledClass);
        }


        // scrolls by item, not by page.
        function nextSlide($container, itemsToDisplay, stopAutoplay) {
            if ($container.children('.' + opts.contentClass + ':animated').size() > 0
                || $container.parent().find('.' + opts.containerClass + ':animated').size() != 0)
                return false;

            var newPage = $.data($container, 'currentPage');
            var stopAutoplay = (stopAutoplay != true) ? false : true;
            var itemsToDisplay = (itemsToDisplay == null) ? opts.itemsToDisplay : itemsToDisplay;

            $container.children(opts.contentElement + ':gt(' + (itemsToDisplay * 2) + ')').css('visibility', 'hidden').siblings().css('visibility', 'visible');

            // Slide container over
            $container.animate({ left: '-=' + (slideWidth * itemsToDisplay) },
                                  { duration: opts.speed, easing: opts.easingMethod, complete: function () {
                                      $container.children('.' + opts.contentClass).each(function () {
                                          addRemoveShowClass($(this), $container);
                                      });
                                      // reset stuff
                                      $container.css({ left: 0 });
                                      $container.children(opts.contentElement + ':lt(' + itemsToDisplay + ')').appendTo($container);
                                      $container.children('.' + opts.contentClass).css('left', function (index) {
                                          return (index * slideWidth) + parseInt($(this).css('margin-left'));
                                      });
                                      // change current page
                                  }
                                  });
        }

        function previousSlide($container, itemsToDisplay, stopAutoplay) {
            if ($container.children('.' + opts.contentClass + ':animated').size() > 0
                || $container.parent().find('.' + opts.containerClass + ':animated').size() != 0)
                return false;

            var itemsToDisplay = (itemsToDisplay == null) ? opts.itemsToDisplay : itemsToDisplay;
            var stopAutoplay = (stopAutoplay != true) ? false : true;


            // shift container left to fit cells
            $container.css({ left: ((itemsToDisplay * slideWidth) * -1) });
            // prepend cells from end to start
            $container.children(opts.contentElement).slice(itemsToDisplay * -1).prependTo($container);
            // reset left values
            $container.children('.' + opts.contentClass).css('left', function (index) {
                return (index * slideWidth) + parseInt($(this).css('margin-left'));
            });
            // scroll right
            $container.children(opts.contentElement + ':lt(' + (itemsToDisplay * 2) + ')').css('visibility', 'hidden').siblings().css('visibility', 'visible');
            $container.animate({ left: '+=' + (slideWidth * itemsToDisplay) },
                { duration: opts.speed, easing: opts.easingMethod, complete: function () {
                    $container.children('.' + opts.contentClass).each(function () { addRemoveShowClass($(this), $container); });
                }
                });
        }


        // Finally
        return this.Create(i);
    };

    // DONT FORGET TO NAME YOUR DEFAULTS WITH THE SAME NAME
    jQuery.fn.icgslider.defaults = {
        speed: 500,
        startItem: 1,
        itemsToDisplay: 1,
        renderNext: true,
        renderPrevious: true,
        easingMethod: 'easeOutQuad',
        contentClass: 'slider_content',
        wrapperClass: 'slider_wrapper',
        containerClass: 'ryslider',
        nextButtonClass: 'next_button',
        prevButtonClass: 'previous_button',
        notEnoughChildrenClass: 'notEnoughChildren',
        enabledClass: 'active',
        disabledClass: 'disabled',
        contentElement: 'div',
        beforeSlideFunction: null,
        afterSlideFunction: null,
        beforeResetFunction: null,
        afterResetFunction: null,
        beforeCreateFunction: null,
        afterCreateFunction: null,
        beforeDestroyFunction: null,
        afterDestroyFunction: null
    };
})(jQuery);
