function updateFeedback() 
{
    $("a.feedback_show").on("click", function() 
    {
        //var to = $(this).parents("div.persona").find("div.persona-name > a").text();
        $("#feedback_errors").html('');
        $("#feedback_to").val($(this).attr('title'));
        $("#feedback_act").val($(this).attr('type'));
        $("#feedback_sport").val($(this).attr('rel'));
        $("#feedback").modal({ autoRefresh: true, autoSize: true, autoResize: true, autoPosition: true, overlayClose: true, closeClass: "feedback_close", onOpen: function(dialog) {
                dialog.overlay.fadeIn('slow', function () {
                    dialog.container.slideDown('fast', function () {
                        dialog.data.fadeIn('slow');
                    });
                });
            }
        });

        $("#feedback_send").click(function() 
        {
            var valid = false;
            
            $("#feedback_errors").html("").hide();
            if ($("#feedback_message").val() != '' && $("#feedback_email").val() != '' && $("#feedback_name").val() != '' )
                valid = true;
            else {
                $("#feedback_errors").append('<ul class="red">');
            }

            if ($("#feedback_message").val() == '') {
                $("#feedback_message").focus();
                $("#feedback_errors").children("ul").append("<li>введите Ваше сообщение.</li>");
            }
            if ($("#feedback_title").val() == '') {
                $("#feedback_title").focus();
                $("#feedback_errors").children("ul").append("<li>введите тему сообщения.</li>");
            }
            if ($("#feedback_email").val() == '') {
                $("#feedback_email").focus();
                $("#feedback_errors").children("ul").append("<li>введите корректный адрес электронной почты.</li>");
            }
            if ($("#feedback_name").val() == '') {
                $("#feedback_name").focus();
                $("#feedback_errors").children("ul").append("<li>введите Ваше имя.</li>");
            }

            if (valid) {
                $.get("/feedback/?" + $("#feedback form").serialize(), function(data) {
                    if (data == "OK") {
                        $("#feedback").html("<h3>Сообщение успешно отправлено!</h3>");
                        setTimeout(function() {
                            $.modal.close();
                        }, 1000);
                    }
                    else {
                        $("#feedback_errors").html(data);
                        $("#feedback_errors").show();
                    }
                });
            }
            else {
                $("#feedback_errors").append("</ul>");
                $("#feedback_errors").show();
            }
            
            return false;
        });
        
        return false;
    });
}

$(function() 
{
    $(window).bind('resize',function(){
        initScroll();
    });

    var centerPanel = 'center-member';
    var totalPanels = $("#scroll-members").children().size();

    var timeout_a = 200;
    var timeout_b = 300;
    var timeout_c = 0;

    $("#scroll-members").data("moving", false);

    var curPanel = 1;

    var centerValue;
    var center;

    function initScroll() {
        centerValue = parseInt(($(".members").width() - $(".member").width())/2, 10);
        center = parseInt(($(window).width() - $(".member").width())/2, 10);
        $(".center-member").css('left', centerValue);
    }

    function addScrollNav() {
        var i, skip;
        var code = '';
        for (i = 1; i <= totalPanels; i++) {
            code += '<li class="scroll-nav" id="scroll-nav-' + i + '"></li>';
        }
        $("#scroll-nav").html(code);
        $("#scroll-nav-" + curPanel).addClass("active");
        $(".scroll-nav").click(function() {
            var go_to = $(this).attr("id").replace("scroll-nav-", "");
            skip = go_to - curPanel;
            if (skip < 0) stir(false, Math.abs(skip))
            else stir(true, skip);
        })
    }

    function setScroollNav(num) {
        $("#scroll-nav li").removeClass("active");
        $("#scroll-nav-" + num).addClass("active");
    }

    function align(num) {
        if (num > totalPanels) num = 1;
        if (num < 1) num = totalPanels;
        return num;
    }

	function getRandomInt(min, max)
	{
	    return Math.floor(Math.random() * (max - min + 1)) + min;
	}

    function stir(direction, skip) {

        for (i = 1; i <= skip; i++) {

            if (!$("#scroll-members").data("moving")) {
                $("#scroll-members").data("moving", true);

                var next = direction ? align(curPanel + 1) : align(curPanel - 1);

                $("#member" + next).css("left", direction ? center + 2000 : center - 2000);

                $("#member" + curPanel)
                    .removeClass(centerPanel)
                    .animate({
                            "left" : direction ? center - 2000 : center + 2000
                        },
                        (skip > 1) ? timeout_c : timeout_a,
                        function() {
                            $(this).hide();
                        }
                );
                $("#member" + next)
                    .show()
                    .animate({
                            "left" : centerValue
                        },
                        (skip > 1) ? timeout_c : timeout_b,
                        function() {
                            $("#scroll-members").data("moving", false);
                    })
                    .addClass(centerPanel);

                $("#m_bg" + next).fadeIn(skip > 1 ? 0 : 'slow');
                $("#m_bg" + curPanel).fadeOut(skip > 1 ? 0 : 'slow');
                curPanel = next;
                setScroollNav(curPanel);
            }
        }
    }

    var r = getRandomInt(1, totalPanels);
    $("#member" + r).show().addClass("center-member");
    curPanel = r;
    $("#m_bg" + r).css({'display': 'block'});
    initScroll();
    $(".m-ar-left").bind('click', (function() { stir(false, 1) }));
    $(".m-ar-right").bind('click', (function() { stir(true, 1) }));
    addScrollNav();
    setScroollNav(r);

    $("#show_members").click(function() {
        $(this).addClass("active");
        $("#show_grantee").removeClass("active");
        $(".members").show();
        $(".grantee-container").hide();
    });
    $("#show_grantee").click(function() {
        $(this).addClass("active");
        $("#show_members").removeClass("active");
        $(".grantee-container").show();
        $(".members").hide();
    });
})

$(document).ready(function()
{
	var scrollSpeed = 200; // pixels per second
	
	$('.partners .jsc_scroll_left').mouseover(function(){
		scrollClients('left',scrollSpeed);
	}).mouseout(function(){
		$('.partners .jsc_scrolls').stop();
	}).mousedown(function(){
		scrollClients('left',scrollSpeed*3);
	}).mouseup(function(){
		scrollClients('left',scrollSpeed);
	});

	$('.partners .jsc_scroll_right').mouseover(function(){
		scrollClients('right',scrollSpeed);
	}).mouseout(function(){
		$('.partners .jsc_scrolls').stop();
	}).mousedown(function(){
		scrollClients('right',scrollSpeed*3);
	}).mouseup(function(){
		scrollClients('right',scrollSpeed);
	});

    $(".shine_image").mouseover(function () {
        //alert('mousein');
        $(".image_bg", this).hide();
        $(".image_colored", this).show();
    });
    $(".shine_image").mouseout(function () {
        //alert('mouseout');
        $(".image_bg", this).show();
        $(".image_colored", this).hide();
    });
});

function scrollClients(direction,curScrollSpeed){
	$('.partners .jsc_scrolls').stop();
	var scrollPH = $('.partners .jsc_scrolldiv');
	var scrollDiv = $('.partners .jsc_scrolls');
	var scrollDiv_width = scrollDiv.width();
	var scrollPH_width = scrollPH.width();
	var currentPosition = scrollDiv.position().left;
	if (direction=='left'){
		var finalPosition = 20;
	} else {
		var finalPosition = -(scrollDiv_width-scrollPH_width+30);
	}
	var pixelsToGo = Math.abs(finalPosition-currentPosition);
	var goTime = Math.round((pixelsToGo/curScrollSpeed)*1000);

	//console.debug({scrollDiv_width:scrollDiv_width});
	//console.debug({scrollPH_width:scrollPH_width});
	//console.debug({currentPosition:currentPosition});
	//console.debug({finalPosition:finalPosition});
	//console.debug({pixelsToGo:pixelsToGo});
	//console.debug({goTime:goTime});

	$('.partners .jsc_scrolls').animate(
		{left:+(finalPosition)+'px'},
		{
		  duration: goTime,
		  easing:'linear'
		}
	);
}

var aniDuration = 3000;
function animate_bg(el){
		//console.debug(el);
		$(el).animate({backgroundPosition: '(50% 0px)'},aniDuration,'linear',function(){
			$(el).animate({backgroundPosition: '(50% -64px)'},aniDuration,'linear',function(){
				animate_bg(el);
			});
		});
}
