$(function () {

    /*######################################### Карусель на главной странице  #########################################*/

    $(".next, .prev").text("");
    img = $(".list_img");
    flag_carousel = 1; // во время анимации события не перебивали сами себя
	
    width_frame = $(".list_img img:first").attr("width"); // ширина кадра

    var delay = 8000; // задержка у автоматической прокрутки
	
    var speed = 3000; // скорость прокрутки
	
    //var flag_carousel = 1; // во время анимации события не перебивали сами себя

    function carousel() {

        if (window.flag_carousel == 1) {

            scrollImg();

        }

    }

    function scrollImg() {
        window.flag_carousel = 0;
        img.find("img:first").animate({
            'margin-left': -width_frame
        }, speed, 'easeInOutExpo', removeImg

        );

    }

    function removeImg() {
        //

        if (parseInt(img.find("img:first").css('margin-left')) < 0) {


            img.find("img:first").appendTo($(".list_img"));
            img.find("img:last").css("margin-left", "auto");
            //setTimeout(carousel, delay);            
            window.flag_carousel = 1;
        }

    }



    function scrollImgRight() {
        window.flag_carousel = 0;

        img.find("img:last").css("margin-left", -width_frame);
        img.find("img:last").prependTo($(".list_img"));

        img.find("img:first").animate({
            'margin-left': 0
        }, speed - 1000, 'easeInOutExpo', function () {

            window.flag_carousel = 1;

        });



    }

    function scrollImgLeft() {
        window.flag_carousel = 0;

        img.find("img:first").animate({
            'margin-left': -width_frame
        }, speed - 1000, 'easeInOutExpo', function () {

            img.find("img:first").appendTo($(".list_img"));
            img.find("img:last").css("margin-left", "auto");

            window.flag_carousel = 1;

        });



    }

    setInterval(carousel, delay);



    $(".next").click(function () {
 

        if (window.flag_carousel == 1) 
		scrollImgLeft();
        return false;

    });

    $(".prev").click(function () {

        if (window.flag_carousel == 1) scrollImgRight();

        return false;
    });





    // галерея картинок 
    length_img = parseInt($(".gallery_block .img_cell").css("width"));
    length_gallery = length_img * $(".gallery_block .img_cell").length;

    left_x = 0;  // координата блока который перетаскиваем
    width_gallery = 960;

    left_stop = 0;
    right_stop = width_gallery - length_gallery + 10;

    removeGallery = 0;

    num = 1;  // на какое кол-во пикселей передвигается галерея(скорость)
    section = 12 // на этих промежутках меняется скорость прокрутки

    count = 0;


    $(".mini_gallery").mouseover(function (e) {

        //e.preventDefault();

        mouse_X = e.pageX;
        gallery_x = $(".mini_gallery").css("left");
        mouse_coord = ($(document).width() / 2);


        x1 = mouse_X - (mouse_coord - (width_gallery / 2));
        x2 = (width_gallery / section);
        x3 = Math.ceil(x1 / x2);
        num = Math.abs(x3 - (section / 2)) + 1;


        if (mouse_X < mouse_coord) {

            removeRight();

        }

        if (mouse_X > mouse_coord) {

            removeLeft();

        }


    });


    $(".mini_gallery").mouseout(function () {
        clearInterval(removeGallery);
    });




    function removeLeft() {

        if (left_x >= right_stop) {

            $(".gallery_block").css("left", left_x = left_x - num);

            clearInterval(removeGallery);

            removeGallery = setInterval(removeLeft, 0);

        }

    }

    function removeRight() {

        if (left_x < left_stop) {

            $(".gallery_block").css("left", left_x = left_x + num);

            clearInterval(removeGallery);

            removeGallery = setInterval(removeRight, 0);

        }

    }

    /*##################################################################################*/

    /*######################################### Инкремент, декремент на карточке товара  #########################################*/


    $(".increment").click(function () {

        sum = parseInt($(this).parent(".num_item").children("input").attr("value"));

        $(this).parent(".num_item").children("input").attr("value", (sum + 1));

        return false;

    })

    $(".decrement").click(function () {

        sum = $(this).parent(".num_item").children("input").attr("value");

        if (sum > $("#min_count_value").val()) {

            $(this).parent(".num_item").children("input").attr("value", (sum - 1));

        }

        return false;

    })



    /*##################################################################################*/

    /*######################################### Ванасии скрыть/показать  #########################################*/


    $("a.vacancy").click(function () {

        $(".requirement").hide();
        // $(".requirement").slideUp();
        //$(this).next(".requirement").slideDown(150);
        $(this).next(".requirement").fadeIn(350);

        return false;

    })

    /*##################################################################################*/

    /*######################################### Авторизация  #########################################*/


    $(".authorization_login").click(function () {

        $(".authorization_forma").fadeIn(200);
        return false;

    })

    $(".authorization_close a").click(function () {

        $(".authorization_forma").hide();
        return false;

    })







});
