﻿(function($) {
    var params = new Array;
    var items = new Array;
    var curIndex = new Array;

    $.fn.easyslider = $.fn.EasySlider = function(options) {

        $.changeIndex = function(index, el) {
            if (index < items[el.id].length - 1) { curIndex[el.id] = index + 1 }
            else curIndex[el.id] = 0;
        };

        $.prevIndex = function(index, el) {
            var prev = index - 1 >= 0 ? index - 1 : items[el.id].length - 1;
            return prev;
        }

        $.transitionCall = function(el) {
            curIndex[el.id] = 0;
            if (items[el.id].length > 1)
                curIndex[el.id] = 1;

            if (params[el.id].delay != 0) {
                setInterval(function() {
                    $.Effect(el);
                    $.changeIndex(curIndex[el.id], el);
                }, params[el.id].delay);
            }
            else {
                curIndex[el.id] = 0;
                $.ChangeTime(el);
            }
        }

        $.ChangeTime = function(el) {
            $.Effect(el);
            setTimeout(function() { $.ChangeTime(el); }, params[el.id].TimeInSlider[curIndex[el.id]]);
            $.changeIndex(curIndex[el.id], el);
        }

        $.Effect = function(el) {
            if (params[el.id].effect == 'fade') {
                $(items[el.id][curIndex[el.id]]).fadeIn(params[el.id].fadeTime);
                $(items[el.id][$.prevIndex(curIndex[el.id], el)]).fadeOut(params[el.id].fadeTime);
            }
            else if (params[el.id].effect == 'slidel2roverlap') {
                $(items[el.id][curIndex[el.id]]).css('left', -params[el.id].width);
                $(items[el.id][curIndex[el.id]]).show();
                var liWidth = 0;
                if ($(items[el.id][curIndex[el.id]]).is('a'))
                    liWidth = $(items[el.id][curIndex[el.id]]).children().width();
                else
                    liWidth = items[el.id][curIndex[el.id]].width;
                $(items[el.id][curIndex[el.id]]).animate({ left: parseInt(params[el.id].width / 2) - parseInt(liWidth / 2) }, 1000);
                $(items[el.id][$.prevIndex(curIndex[el.id], el)]).animate({ left: params[el.id].width }, 1000);
            }
            else if (params[el.id].effect == 'slidel2shift') {
                $(items[el.id][curIndex[el.id]]).css('z-index', 2);
                $(items[el.id][$.prevIndex(curIndex[el.id], el)]).css('z-index', 1);
                $(items[el.id][curIndex[el.id]]).css('left', -params[el.id].width);
                $(items[el.id][curIndex[el.id]]).css('opacity', 1);
                $(items[el.id][curIndex[el.id]]).show();
                var liWidth = 0;
                if ($(items[el.id][curIndex[el.id]]).is('a'))
                    liWidth = $(items[el.id][curIndex[el.id]]).children().width();
                else
                    liWidth = items[el.id][curIndex[el.id]].width;
                $(items[el.id][curIndex[el.id]]).animate({ left: parseInt(params[el.id].width / 2) - parseInt(liWidth / 2) }, 1000);
                $(items[el.id][$.prevIndex(curIndex[el.id], el)]).animate({ opacity: 0 }, 1000);
            }
            else if (params[el.id].effect == 'slideu2doverlap') {
                $(items[el.id][curIndex[el.id]]).css('top', -params[el.id].height);
                $(items[el.id][curIndex[el.id]]).show();
                var liHeight = 0;
                if ($(items[el.id][curIndex[el.id]]).is('a'))
                    liHeight = $(items[el.id][curIndex[el.id]]).children().height();
                else
                    liHeight = items[el.id][curIndex[el.id]].height;
                $(items[el.id][curIndex[el.id]]).animate({ top: parseInt(params[el.id].height / 2) - parseInt(liHeight / 2) }, 1000);
                $(items[el.id][$.prevIndex(curIndex[el.id], el)]).animate({ top: params[el.id].height }, 1000);
            }
            else if (params[el.id].effect == 'slideu2dshift') {
                $(items[el.id][curIndex[el.id]]).css('z-index', 2);
                $(items[el.id][$.prevIndex(curIndex[el.id], el)]).css('z-index', 1);
                $(items[el.id][curIndex[el.id]]).css('top', -params[el.id].height);
                $(items[el.id][curIndex[el.id]]).css('opacity', 1);
                $(items[el.id][curIndex[el.id]]).show();
                var liHeight = 0;
                if ($(items[el.id][curIndex[el.id]]).is('a'))
                    liHeight = $(items[el.id][curIndex[el.id]]).children().height();
                else
                    liHeight = items[el.id][curIndex[el.id]].height;
                $(items[el.id][curIndex[el.id]]).animate({ top: parseInt(params[el.id].height / 2) - parseInt(liHeight / 2) }, 1000);
                $(items[el.id][$.prevIndex(curIndex[el.id], el)]).animate({ opacity: 0 }, 1000);
            }
            else {//none
                $(items[el.id][$.prevIndex(curIndex[el.id], el)]).hide();
                $(items[el.id][curIndex[el.id]]).show();
            }
        }

        $.GetNewSize = function(imgHeight, imgWidth, divHeight, divWidth) {
            liNewH = 0;
            liNewW = 0;
            liScaleWidth = 0;
            liScaleHeight = 0;
            liScaleWidth = imgWidth / divWidth;
            liScaleHeight = imgHeight / divHeight;
            if (liScaleWidth > liScaleHeight) {
                liFittedHeight = imgHeight / liScaleWidth;
                liTmp = imgHeight / liFittedHeight;

                liNewH = imgHeight / liTmp;
                liNewW = imgWidth / liScaleWidth;
            }
            else {
                liFittedWidth = imgWidth / liScaleHeight;
                liTmp = imgWidth / liFittedWidth;
                liNewH = imgHeight / liScaleHeight;
                liNewW = imgWidth / liTmp;
            }
            var newSize = {};
            newSize.Height = liNewH;
            newSize.Width = liNewW;
            return newSize;
        }

        $.init = function(el) {
            items[el.id] = new Array();
            params[el.id] = $.extend({}, $.fn.easyslider.defaults, options);

            $.each($('#' + el.id + ' img'), function(i, item) {
                var NewSize = $.GetNewSize(item.height, item.width, params[el.id].height, params[el.id].width);

                items[el.id][i] = $(item).parent().is('a') ? $(item).parent() : item;
                $(item).css({
                    'width': NewSize.Width,
                    'height': NewSize.Height
                });
                $(items[el.id][i]).css({
                    'top': parseInt(params[el.id].height / 2) - parseInt(NewSize.Height / 2),
                    'left': parseInt(params[el.id].width / 2) - parseInt(NewSize.Width / 2)
                });
            });
            $.transitionCall(el);
        }

        this.each(
            function() { $.init(this); }
        );
    }

    // default values
    $.fn.easyslider.defaults = {
        width: 600, // width of slider panel
        height: 400, // height of slider panel
        delay: 3000, // delay between images in ms
        fadeTime: 1000, // time of fading in and out
        TimeInSlider: [], //display time of each image
        effect: 'none'
    };
})(jQuery);
