
function FeedLoad()
{
    $("#feedform").attr("action","javascript:FeedSubmit();");
    $.each(
        $("#pager a"),
        function(i,item)
        {
            item.href = "javascript:Show("+i+");";
        }

    )
    $("#pager a:first span").addClass("active");
}

function FeedSubmit()
{
    $("#cat_f").val($("#cats_f").val());
    $("#id_f").val($("#items_f").val());
    Show(0);
}

function Show(page)
{
    if (page == null)
        {
            page = 0;
        }
    page += 1;
    $.getJSON
        (
            "/feedback/show/",
            {"cat":$("#cat_f").val(), "id":$("#id_f").val() , "page" : page},
            _Show
        );
}

function _Show(json)
{

    var html = "";

    if (json.items != null)
    {
        $.each(json.items,
                function(i,item)
                {
                    html += " <div class=\"SendedReport\">\n\
                        <div class=\"Sender\">"+item.name+"<br /></div>\n\
                        <div class=\"Message\">\n\
                          <div class=\"BLPat\">\n\
                            <div class=\"angle\"><!-- no content --></div>\n\
                            <div class=\"GoodsView\">Мнение о товаре: <a href=\"/good/"+item.item+"\">\n\
                                    "+item.item_name+"</a></div>\n\
                            <div class=\"Text\">"+item.text+"</div>\n\
                            <div class=\"Send\">";

                    if (json.isAdmin == "0")
                        {
                            html += "<a href=\"javascript:OpenFeedbackForm();\">\n\
                                    <img src=\"/img/send_report.gif\" alt=\"Оставить свой отзыв\" />\n\
                                    <span>Оставить свой отзыв</span>";
                        }
                    else
                        {
                              html += "<a href=\"javascript:DeleteFeedback("+item.id+");\">\n\
                                    <img src=\"/img/send_report.gif\" alt=\"Оставить свой отзыв\" />\n\
                                    <span>Удалить отзыв</span>";
                        }


                    html +="</a>\n\
                            </div>\n\
                          </div>\n\
                        </div>\n\
                      </div>";
                }

        );
    }

    $("#reports_container").html(html);
    var pages = parseInt(json.pages, 10);
    var current = parseInt(json.current, 10);
    html = "";
    for (i=0; i < pages; i++)
        {
            html += "<a href=\"javascript:Show(" + i.toString() + ");\"><span class=\"link";
            if (current == i)
                {
                    html += " active";
                }
            html +="\">" +
                (i+1).toString() + "</span></a>";
        }
    $("#pager").html(html);
}

function ChooseCategory()
{
    $("#item").attr("disabled","disabled");
    $("#item_id").val(0);
    $.getJSON(
                "/ajax/category/",
                {"id":$("#cat").val()},
                _ChooseCategory);
}

function _ChooseCategory(json)
{
    var html = "<option value=\"0\" disabled=\"disabled\" selected=\"selected\">Выберите позицию</option>";
    $.each(json,
            function(i,item)
            {
                html += "<option value=\""+item.ID+"\" >"+item.Title+"</option>";
            }
    );
    $("#item").html(html);
    $("#item").removeAttr("disabled");
}

function ChooseCategoryF()
{
    $("#items_f").attr("disabled","disabled");
    $("#id_f").val(0);
    $.getJSON(
                "/ajax/category/",
                {"id":$("#cats_f").val()},
                _ChooseCategoryF);
}

function _ChooseCategoryF(json)
{
    var html = "<option value=\"0\" selected=\"selected\">Без раздела</option>";
    $.each(json,
            function(i,item)
            {
                html += "<option value=\""+item.ID+"\" >"+item.Title+"</option>";
            }
    );
    $("#items_f").html(html);
    $("#items_f").removeAttr("disabled");
}

function ChooseItem()
{
    $("#item_id").val($("#item").val());
}


function DeleteFeedback(id)
{
    $.get(
          "/feedback/delete",
          {
            "feed_id":id
          },
          function(){
            alert('Отзыв удален!');
          }
            );
}

function NewFeedback()
{

    if ($("#item_id").val() == 0)
        {
            alert('Вы должны выбрать товар');
            return;
        }

    if ($("#f_name").val() == "")
        {
            alert('Вы должны указать имя');
            return;
        }

    if ($("#f_text").val() == "")
        {
            alert('Вы должны написать комментарий');
            return;
        }

    $.post("/feedback/add", $("#feed").serialize() , CloseFeedback);

}

function OpenFeedbackForm()
{
    $("#SendReport").css({"visibility" : "visible"});
    $("#SendReport").animate(
                        {"opacity":1}, 200
                    );

}

function OpenReports(i)
{
    $.getJSON(
                "/feedback/good/",
                {"id":i},
                _OpenReports
        );

}

function _OpenReports(json)
{
    if (json.nill == 1)
        {
            alert('К сожалению для данного товара отзывов нет.');
            return;
        }

    var html = "";
        $.each(json.items,
                function(i,item)
                {
                    html += " <div class=\"SendedReport\">\n\
                        <div class=\"Sender\">"+item.name+"<br /><span>"+item.status+"</span></div>\n\
                        <div class=\"Message\">\n\
                          <div class=\"BLPat\">\n\
                            <div class=\"angle\"><!-- no content --></div>\n\
                            <div class=\"Text\">"+item.text+"</div>\n\
                          </div>\n\
                        </div>\n\
                      </div>";
                }

        );

   $(".reports").html(html);
   $("#GoodReports").css({"display" : "block"});
   $("#GoodReports").animate(
                        {"opacity":1}, 200
                    );

}

function CloseReports()
{
       $("#GoodReports").animate(
                        {"opacity":0}, 200
                    );

      var t = setTimeout(function()
                        {
                            $("#GoodReports").css({"display" : "none"});
                        }, 250
        )
}


function CloseFeedback()
{
   $("#SendReport").animate(
                        {"opacity":0}, 200
                    );

   var t = setTimeout(function()
                        {
                            $("#SendReport").css({"visibility" : "hidden"});
                        }, 250
        )
}




