﻿(function ($)
{
    $.fn.slideTabs = function (b)
    {
        var c = {
            tabsList: 'ul.tabs',
            viewContainer: 'div.view_container',
            btnNext: 'a.next',
            btnPrev: 'a.prev',
            tabClass: 'tab',
            tabActiveClass: 'active',
            viewActiveClass: 'active_view',
            btnDisabledClass: 'disabled',
            orientation: 'horizontal',
            slideLength: 694,
            offsetTL: 0,
            offsetBR: 0,
            tabsEasing: '',
            tabsAnimTime: 300,
            tabsScroll: true,
            tabSaveState: false,
            contentAnim: 'slideH',
            contentEasing: 'easeInOutExpo',
            contentAnimTime: 600,
            autoHeight: false,
            autoHeightTime: 0,
            autoplay: false,
            autoplayInterval: 4000
        },
		conf = $.extend(true, {},
		c, b);
        return this.each(function ()
        {
            var a = new SlideTabs($(this), conf);
            a.init()
        })
        alert('1');
    };
    function SlideTabs(c, d)
    {
        var e = c.find(d.tabsList),
		$a = e.children('li').find('a'),
		$content = c.find(d.viewContainer).css('overflow', 'hidden'),
		$prev = c.find(d.btnPrev).click(function ()
		{
		    f.prev(val);
		    return false
		}),
		$next = c.find(d.btnNext).click(function ()
		{
		    f.next(val);
		    return false
		}),
		$tab,
		$activeTab,
		$li,
		$lastElem,
		$view,
		$activeView,
		val = {},
		margin = 0;
        this.init = function ()
        {
            if (d.orientation == 'horizontal')
            {
                val.func = 'outerWidth';
                val.obj = 'left';
                val.attr = 'marginLeft'
            }
            else
            {
                val.func = 'outerHeight';
                val.obj = 'top';
                val.attr = 'marginTop'
            }
            f.init();
            if (d.autoplay == true)
            {
                autoplay.init()
            }
        };
        var f = {
            animated: '#' + c.attr('id') + ' ' + d.tabsList + ':animated',
            init: function ()
            {
                f.posActive();
                f.bind()
            },
            bind: function ()
            {
                e.delegate('li a.' + d.tabClass, 'click', function ()
                {
                    f.click(this);
                    return false
                });
                if ($.fn.mousewheel && d.tabsScroll == true)
                {
                    e.mousewheel(function (a, b)
                    {
                        (b > 0) ? f.prev(val) : f.next(val);
                        return false
                    })
                }
            },
            posActive: function ()
            {
                f.getActive();
                content.showActive();
                $lastElem = e.children('li:last');
                $activeTab = $activeTab.parent('li');
                if (($lastElem[val.func](true) + $lastElem.position()[val.obj]) > d.slideLength)
                {
                    val.elemD = $activeTab[val.func](true);
                    val.elemP = $activeTab.position()[val.obj];
                    if (val.elemP > d.slideLength)
                    {
                        margin += (val.elemD + (val.elemP - d.slideLength));
                        margin = (margin + d.offsetBR)
                    } else if ((val.elemP + val.elemD) > d.slideLength)
                    {
                        margin += (val.elemD - (d.slideLength - val.elemP));
                        margin = (margin + d.offsetBR)
                    } else
                    {
                        margin = (margin - d.offsetTL)
                    }
                    e.css(val.attr, - +margin);
                    f.showButtons()
                }
            },
            showButtons: function ()
            {
                if (e.children("li:first").position()[val.obj] == (0 + d.offsetTL))
                {
                    $prev.addClass(d.btnDisabledClass)
                } else if (($lastElem.position()[val.obj] + $lastElem[val.func](true)) == (d.slideLength - d.offsetBR))
                {
                    $next.addClass(d.btnDisabledClass)
                }
                $prev.show();
                $next.show()
            },
            click: function (a)
            {
                $tab = $(a);
                if ($(content.animated).length || $tab.hasClass(d.tabActiveClass))
                {
                    return false
                }
                $li = $tab.parent('li');
                f.setActive();
                if (d.autoplay == true)
                {
                    val.index = $tab.parent().index();
                    autoplay.setInterval()
                }
                val.elemP = $li.position();
                val.activeElemP = $activeTab.position();
                f.slideClicked(val);
                $activeView = $content.children('div.' + d.viewActiveClass).removeClass(d.viewActiveClass);
                $view = $content.children('div#' + $tab.attr('hash')).addClass(d.viewActiveClass);
                if (d.autoHeight == true)
                {
                    content.adjustHeight()
                }
                if (d.contentAnim.length > 0)
                {
                    content[d.contentAnim](val)
                } else
                {
                    $activeView.hide();
                    $view.show()
                }
            },
            getActive: function ()
            {
                if ($.cookie)
                {
                    var a = $.cookie(c.attr('id'))
                }
                if (a)
                {
                    e.children('li').find('.' + d.tabActiveClass).removeClass(d.tabActiveClass);
                    $activeTab = e.find('a#' + a).addClass(d.tabActiveClass)
                } else
                {
                    $activeTab = e.children('li').find('.' + d.tabActiveClass);
                    if (!$activeTab.length)
                    {
                        $activeTab = e.find('a:first').addClass(d.tabActiveClass)
                    }
                    f.saveActive($activeTab)
                }
            },
            setActive: function ()
            {
                $activeTab = e.children('li').find('a.' + d.tabActiveClass).removeClass(d.tabActiveClass);
                $tab.addClass(d.tabActiveClass);
                f.saveActive($tab)
            },
            saveActive: function (a)
            {
                if (d.tabSaveState == true)
                {
                    $.cookie(c.attr('id'), a.attr('id'))
                }
            },
            slideClicked: function (a)
            {
                a.elemP = a.elemP[a.obj];
                a.elemD = $li[a.func](true);
                a.nextElemPos = ($li.next().length == 1) ? $li.next().position()[a.obj] : 0;
                if (a.elemP < (0 + d.offsetTL))
                {
                    a.elemHidden = (a.elemD - a.nextElemPos);
                    margin = (margin - (a.elemHidden + d.offsetTL));
                    $next.removeClass(d.btnDisabledClass)
                } else if ((a.elemD + a.elemP) > (d.slideLength - d.offsetBR))
                {
                    margin += (a.elemD - (d.slideLength - (a.elemP + d.offsetBR)));
                    $prev.removeClass(d.btnDisabledClass)
                }
                f.animate()
            },
            prev: function (a)
            {
                if ($(f.animated).length)
                {
                    return false
                }
                e.children('li').each(function ()
                {
                    $li = $(this);
                    //a.elemP = $li.position()[a.obj];
                    a.elemP = a.elemD;
                    if (a.elemP >= (0 + d.offsetTL))
                    {
                        a.elemHidden = ($li.prev()[a.func](true) - a.elemP);
                        margin = ((margin + a.elemHidden) - d.offsetTL);
                        $li = $li.prev();
                        f.animate();
                        return false
                    }
                });
                $next.removeClass(d.btnDisabledClass)
            },
            next: function (a)
            {
                if ($(f.animated).length)
                {
                    return false
                }
                e.children('li').each(function ()
                {
                    $li = $(this);
                    a.elemD = $li[a.func](true);
                    a.elemP = $li.position()[a.obj];
                    if ((a.elemD + a.elemP) > (d.slideLength - d.offsetBR))
                    {
                        a.elemHidden = (d.slideLength - a.elemP);
                        margin += ((a.elemD) + d.offsetBR);
                        f.animate();
                        return false
                    }
                });
                $prev.removeClass(d.btnDisabledClass)
            },
            animate: function ()
            {
                if (d.orientation == 'horizontal')
                {
                    e.animate({ 'marginLeft': - +margin }, d.tabsAnimTime, d.tabsEasing)
                }
                else
                {
                    e.animate({ 'marginTop': - +margin }, d.tabsAnimTime, d.tabsEasing)
                }
                f.setButtonState()
            },
            setButtonState: function ()
            {
                if ($li.is(':first-child'))
                {
                    $prev.addClass(d.btnDisabledClass)
                }
                else if ($li.is(':last-child'))
                {
                    $next.addClass(d.btnDisabledClass)
                }
            }
        },
		content = {
		    animated: '#' + c.attr('id') + ' :animated',
		    showActive: function ()
		    {
		        $view = $content.children($activeTab.attr('href')).addClass(d.viewActiveClass);
		        $content.children('div').css('position', 'absolute').not('div.' + d.viewActiveClass).hide();
		        if (d.autoHeight == true)
		        {
		            $content.css('height', $view.height()).parent().css('height', 'auto')
		        }
		    },
		    adjustHeight: function ()
		    {
		        if (d.autoHeightTime > 0)
		        {
		            $content.animate({
		                'height': $view.height()
		            },
					d.autoHeightTime)
		        } else
		        {
		            $content.css('height', $view.height())
		        }
		    },
		    fade: function ()
		    {
		        $activeView.fadeOut(d.contentAnimTime, function ()
		        {
		            $view.fadeIn(d.contentAnimTime)
		        })
		    },
		    slideH: function (a)
		    {
		        a.wh = c.outerWidth(true);
		        content.setSlideValues(a);
		        $activeView.animate({ 'left': a.animVal }, d.contentAnimTime, d.contentEasing);
		        $view.css({ 'display': 'block', 'left': a.cssVal }).animate({ 'left': '0px' }, d.contentAnimTime, d.contentEasing, function ()
		        {
		            $activeView.css('display', 'none')
		        })
		    },
		    slideV: function (a)
		    {
		        a.wh = c.outerHeight(true);
		        content.setSlideValues(a);
		        $activeView.animate({
		            'top': a.animVal
		        },
				d.contentAnimTime, d.contentEasing);
		        $view.css({
		            'display': 'block',
		            'top': a.cssVal
		        }).animate({
		            'top': '0px'
		        },
				d.contentAnimTime, d.contentEasing, function ()
				{
				    $activeView.css('display', 'none')
				})
		    },
		    setSlideValues: function (a)
		    {
		        if (a.elemP > a.activeElemP[a.obj])
		        {
		            a.animVal = -a.wh;
		            a.cssVal = a.wh
		        } else
		        {
		            a.animVal = a.wh;
		            a.cssVal = -a.wh
		        }
		    }
		},
		autoplay = {
		    init: function ()
		    {
		        val.index = 0;
		        autoplay.setInterval()
		    },
		    setInterval: function ()
		    {
		        clearInterval(val.intervalId);
		        val.intervalId = setInterval(function ()
		        {
		            autoplay.play()
		        },
				d.autoplayInterval)
		    },
		    play: function ()
		    {
		        val.index++;
		        if (val.index == $a.length)
		        {
		            val.index = 0
		        }
		        $($a[val.index]).trigger('click')
		    }
		}
    }
})(jQuery);
