﻿// ###############   CAROUSEL #####################
(function ($) {
    $.fn.extend({
        //plugin name - tai_carousel
        tai_carousel: function (options) {

            //Settings list and the default values
            var defaults = {
                divContent: "#jq_advContent", // place content id
                eachBt: "jq_advItem", // place class to look for buttons
                eachItem: "car-item", // place class to look for content items
                firstItem: 0, //set first item to show
                nItem: 4, // set number of items
                step: 208, // step width for each item content
                timer: 4000, // set timer for the loop animation
                loop: false // just do one loop
            };


            var options = $.extend(defaults, options);

            return this.each(function () {
                var o = options;

                //Assign current element to variable, 
                var obj = $(this);
                var content = $(o.divContent);
                var eachBt = $('.' + o.eachBt);
                var eachItem = $('.' + o.eachItem);
                var firstItem = o.firstItem;
                var activeItem = firstItem;
                var nItem = o.nItem - 1;

                // set default Position
                var objFirst = $('.jq_advItem[pos=' + firstItem + ']'); // first showed item
                var pos = -(objFirst.attr('pos') * o.step); // set position
                var objActive = objFirst;

                content.css('left', pos);
                objActive.addClass('active');


                // SET NUMBER OF ITEMS
                var n = content.find(eachItem).size();

                /*
                obj.filter(function (index) {
                return index == $('.' + o.eachBt, this).index > n;
                }).hide();
                */

                function doAnim() {
                    // set the new active
                    activeItem = (activeItem < nItem) ? activeItem + 1 : 0;
                    objActive = $('.jq_advItem[pos=' + activeItem + ']');
                    //alert(activeItem);
                    pos = -($('.jq_advItem[pos=' + activeItem + ']').attr('pos') * o.step); // set position
                    obj.find(eachBt).removeClass('active'); // remove all
                    objActive.addClass('active');
                    // DO THE ANIMATION
                    content.animate({
                        "left": pos
                    }, "slow");
                }

                // Recursive loop
                var nLoop = 0;
                var loopsiloop = function () {
                    if(!o.loop)
                    {
                        nLoop ++;
                    }
                    if(nLoop <= o.nItem)
                    {
                        tid = setTimeout(function () {
                            doAnim();
                            loopsiloop();

                        }
                        , o.timer);
                    }
                }


                function abortTimer() { // to be called when you want to stop the timer
                    clearTimeout(tid);

                }
                loopsiloop();

                // workaround for focus and blur browser
                /*
                $(function () {
                    $(window).blur(function () {
                        abortTimer();
                    });
                    
                    $(window).focus(function () {
                        loopsiloop();
                    });
                });
                */


                var userAgent = navigator.userAgent.toLowerCase();

                // Figure out what browser is being used
                jQuery.browser = {
                    version: (userAgent.match(/.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/) || [])[1],
                    chrome: /chrome/.test(userAgent),
                    safari: /webkit/.test(userAgent) && !/chrome/.test(userAgent),
                    opera: /opera/.test(userAgent),
                    msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
                    mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
                };

                
                // if chrome fire loop (if not, the loop is fired with the window.focus)
                
                /*if (jQuery.browser.chrome)
                    loopsiloop();
                    
                */

                


                obj.find(eachBt).click(function () {
                    abortTimer();

                    activeItem = parseInt($(this).attr('pos')); // set active Item
                    pos = -($(this).attr('pos') * o.step);

                    obj.find(eachBt).removeClass('active');
                    $(this).addClass('active');
                    content.animate({
                        "left": pos
                    }, "slow");



                });

            });
        }
    });
})(jQuery);
