﻿jQuery(function($) {
    /*
    $.fn.extend({ handleScroll: function(callback) {

        return this.each(function() {
            var context = this;

            $(window).scroll(function(e) {
                var offset = $(context).offset();
                var current = $(window).scrollTop();


                if (current > offset.top - $(window).height() && current < offset.top + $(context).height()) {
                    var position = Math.round((offset.top - current - $(window).height()) / ($(context).height() + $(window).height()) * -10000) / 100;

                    callback.call(context, position);
                }
            });
        });

    }
    });

    $('.wrapper-bg').handleScroll(function(e) {
        var position = $(this).height() * e;
        $('.wrapper-bg').css({ 'backgroundPosition': 'right ' + (e*5) + 'px' });
    }); */

    (function searchField() {
        $('.search-block .search-field').focus(function() {
            $('.search-block .button.btn-4').addClass('focused')
        });
        $('.search-block .search-field').blur(function() {
            $('.search-block .button.btn-4').removeClass('focused')
        });
    })();

    (function adRotater() {
        var slideWrapper = $('.slideWrapper'),
			slideCount = $('.slides').length,
			slideWidth = $('.slides').outerWidth(),
			containerWidth = slideCount * slideWidth,
			timerDuration = 4000,
			slideSpeed = 800,
			currentObject = 0;

        slideWrapper.css({ 'width': +containerWidth });

        function rotate() {
            currentObject++;
            if (currentObject == slideCount) {
                currentObject = 0;
            }

            var moveSlide = (currentObject * slideWidth) * -1
            slideWrapper.animate({ 'left': moveSlide }, { duration: slideSpeed });

            timer();
        }

        function timer() {
            var rotateTimer = setTimeout(rotate, timerDuration);


            /*
            $('.slides').hover(function(){
            clearTimeout(rotateTimer);
            }, function() {
            var rotateTimer2 = setTimeout(rotate, timerDuration);
            });
            */
        }
        timer();

    })();
});







