jQuery(function($){

    window.UserData                    = {};
    window.UserData.publicity          = [];
    window.timeInterval                = setInterval(nextPublicity, 5000);
    window.UserData.currentImgageIndex = 0;

    for (var i = 0; i < config_publicity.length; i++){
        window.UserData.publicity[i] = new Image();
        window.UserData.publicity[i].src = '/img/publicity/'+ config_publicity[i];
    }

    nextPublicity();

    function nextPublicity(){

        clearInterval(window.timeInterval)
        var curInd = window.UserData.currentImgageIndex + 1;

        if(curInd == config_publicity.length ){
            window.UserData.currentImgageIndex = 0
            curInd = 0;
        }
        else{
            window.UserData.currentImgageIndex = curInd;
        }

        var imagePath = '/img/publicity/' + config_publicity[curInd]

        $('.publicity-b-image').fadeTo(200,0.2, function(){
            $(this).html('<img src="'+imagePath + '" alt="" />').fadeTo(200, '1')
        })
        window.timeInterval = setInterval(nextPublicity, 6000)
    }
// карусель на главной странице
    $('.main-image-navigation-link').click(function(){
        var clkLink = $(this).attr("img");
        var currentLink = $('.in-image').html()[38]
        if ( clkLink !== currentLink )
            $('.in-image').animate({
                opacity :0.2
            },500, '', function(){
                $('.in-image').animate({
                    opacity:1
                },500)
                .html('<img src="/img/main_image/'+clkLink+'" alt="" />')
            })
		return false;
    });
// голосование
    $(".ratio-b").hover(function(){
        $(".ratio-b").bind('mousemove',function(e){
            var nedDistance =e.pageX - $(".ratio-b").offset().left;
                $(".active-ratio").css({
                    width: nedDistance
                });

        });
    }, function(){
                w = ($("#rating").val()*16)+'px';
                $('.active-ratio').css({
                    width: w
                });
    })

    $(".ratio-b").click(function(e){

	   obj =  $(this).attr("obj");
      div_star = $(this);
         //width = e.pageX - $(this).offset().left;
	  vote1 = (e.pageX - $(this).offset().left)/16;
          vote1 = Math.round(vote1*2)/2; //округляем с точностью 0,5
          if (obj == undefined)
	  {
	  	// если не определён id объекта, значит нажимаем на странице отзыва
	  	//$("#ratio-width").val(width);
	  	$("#rating").val(vote1);
                $("#vote").html(vote1);
	  }
	})


});

function review_form_show()
{
	$("#review_form_button").hide();
	$("#review_form").show();
}

function send_review()
{
    var name = $("#user_name").val();
    var message = $("#message").val();
    var title = $(".center-conten-title").html();
    var reg=/\+/g;
    name = name.replace(reg, "%2B");
    message = message.replace(reg, "%2B");
    title = title.replace(reg, "%2B");

    $.post(
    "/ajax_review.php",
    {
        action: "send_review",  
        rating: $("#rating").val(),
        name: name,
        message: message,
        id_object: $("#id_object").val(),
        id_section: $("#id_section").val(),
        title: title,
        captcha: $("#captcha").val(),
        hash: $("#hash").val()
    },
    function(data)
    {
        $("#reviews_errors").html(data.text);
        if (data.error == 0)
        {
            $("#review_form").hide();
        }
    },
    "json"
    );
}

function show_review(page)
{
    $.post(
    "/ajax_review.php",
    {
        action: "show_review",  
        id_object: $("#id_object").val(),
        page: page
    },
    function(data)
    {
        if (data.error == 0)
        {
            $("#reviews").html(data.text);
            $(".read-reviews-link").hide();
        }
    },
    "json"
    );
}
