﻿$(document).ready(function () {

    $('.size a').click(function () {
        var ourText = $('div.body');

        var currFontSize = ourText.css('fontSize');
        var finalNum = parseFloat(currFontSize, 10);
        var stringEnding = currFontSize.slice(-2);
        
        if (this.className == 'larger') {
            finalNum *= 1.2;
        }
        else if (this.className == 'smaller') {
            finalNum /= 1.2;
        }
        ourText.css('fontSize', finalNum + stringEnding);
    });


    var readyToRate = true;

    $.ajaxSetup({
        cache: false,
        type: "GET",
        timeout: 30000
    });

    function ShowRate(item, data, action) {
        switch (data) {
            case "yes":
                item.qtip("api").updateContent("شما قبلاً رای مثبت داده اید", false);
                item.qtip("api").updateWidth(150);
                item.qtip("show");
                if (data == action) {
                    item.children("em").remove();
                    item.append("<em>[شما]</em>");
                }
                break;
            case "no":
                item.qtip("api").updateContent("شما قبلاً رای منفی داده اید", false);
                item.qtip("api").updateWidth(150);
                item.qtip("show");
                if (data == action) {
                    item.children("em").remove();
                    item.append("<em>[شما]</em>");
                }
                break;
            case "d":
                item.qtip("api").updateContent("شما قبلاً رای داده اید", false);
                item.qtip("api").updateWidth(120);
                item.qtip("show");
                break;
            default:
                if (data != "") {
                    item.html(data);
                    item.qtip("api").updateContent("رای شما ثبت شد", false);
                    item.qtip("api").updateWidth(100);
                    item.children("em").remove();
                    item.append("<em>[شما]</em>");
                    item.qtip("show");
                }
                break;
        }

        item.parent().eq(0).children(".waiting").remove();
        item.unbind("ajaxStart");
        item.show("fast");

        readyToRate = true;

    }

    function SetRate(item, action) {

        readyToRate = false;

        var commentId = item.parents("div.comment").eq(0).find("em").eq(0).text();

        item.ajaxStart(function () {
            item.hide();
            item.parent().eq(0).append("<div class='waiting'>&nbsp;</div>");
        });

        $("div#holder").load(
                    "/CommentRate.aspx?action=" + action + "&id=" + commentId,
                    function (data) {
                        ShowRate(item, data, action);
                    }
                );

    }

    $(".yes").click(function () {
        if (readyToRate) {
            SetRate($(this), "yes");
        }
    });

    $(".no").click(function () {
        if (readyToRate) {
            SetRate($(this), "no");
        }
    });

    $("div.no[title]").qtip({
        show: {
            delay: 0,
            solo: true,
            when: {
                target: false,
                event: 'mouseover'
            },
            effect: function () {
                $(this).stop(true, true).show();
            }
        },
        hide: {
            fixed: true,
            delay: 0,
            when: {
                target: false,
                event: 'mouseout'
            },
            effect: function () {
                $(this).stop(true, true).hide();
            }
        },
        position: {
            adjust: { x: 8, y: -1, mouse: true },
            corner: {
                target: "topLeft",
                tooltip: "bottomLeft"
            }
        },
        style: {
            border: { width: 0, radius: 0, color: "#F75223" },
            padding: "2px 4px 4px 4px",
            margin: "0px",
            color: "#fff",
            "font-size": "12px",
            "background-color": "#F75223",
            tip: { corner: "bottomLeft", color: false, size: { x: 4, y: 4} }
        }
    });

    $("div.yes[title]").qtip({
        show: {
            delay: 0,
            solo: true,
            when: {
                target: false,
                event: 'mouseover'
            },
            effect: function () {
                $(this).stop(true, true).show();
            }
        },
        hide: {
            fixed: true,
            delay: 0,
            when: {
                target: false,
                event: 'mouseout'
            },
            effect: function () {
                $(this).stop(true, true).hide();
            }
        },
        position: {
            adjust: { x: 8, y: -1, mouse: true },
            corner: {
                target: "topLeft",
                tooltip: "bottomLeft"
            }
        },
        style: {
            border: { width: 0, radius: 0, color: "#5DC815" },
            padding: "2px 4px 4px 4px",
            margin: "0",
            color: "#fff",
            "font-size": "12px",
            "background-color": "#5DC815",
            tip: { corner: "bottomLeft", color: false, size: { x: 4, y: 4} }
        }
    });

});
