﻿/// <reference path="jquery-1.4.1.min-vsdoc.js" />
var lc = null;
$(document).ready(function () {
    $("#prCheckerInput").focus();
    SetDefaultButton();
    RefreshLastCheck();
});
function RefreshLastCheck() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/ReturnLastCheck",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d == "") {
                setTimeout(RefreshLastCheck, 2000);
            }
            else {
                var lastCheck = eval('(' + msg.d + ')');
                if (lc == null) {
                    lc = lastCheck.url;
                }
                else if (lc != lastCheck.url) {
                    lc = lastCheck.url;
                    AddNewRow(lastCheck);
                }
                setTimeout(RefreshLastCheck, 2000);
            }
        },
        error: function (e) {
        }
    });
}
function AddNewRow(row) {
    var newRow = "<div class=\"lastChechRow\">";
    newRow += "<div class=\"lsUrl\">" + row.url + "</div>";
    newRow += "<div class=\"lsGif\"><img src=\"img/" + row.pagerank + ".gif\" /></div>";
    newRow += "<div class=\"lsPR\">" + row.pagerank + "</div>";
    newRow += "</div>";

    $("#lastCHecksPanel").append(newRow);

    $("#lastCHecksPanel div:first").slideUp("fast", function () { $(this).remove() });
}
function SetDefaultButton() {
    $("input").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $("#prChekerButton").click();
            return false;
        }
    });
}
function prChekerButtonAction() {
    if ($("#prCheckerInput").val() == "") {
        $("#prCheckerReloadGifDiv").remove();
        $("#warningBar").remove();
        $("#prCheckerTable").after("<div id=\"warningBar\" style=\"height:28px; border:1px solid #DD3C10; background-color:#FFEBE8; margin-top:10px; line-height:28px;\">Lütfen bir url girdiniz.</div>");
        $("#prChekerButton").removeAttr("disabled");
        $("#prCheckerInput").removeAttr("disabled");
        $("#prCheckerInput").focus();
        return false;
    }
    $("#prChekerButton").attr("disabled", "true");
    $("#prCheckerInput").attr("disabled", "true");
    $("#prCheckerReloadGifDiv").remove();
    $("#warningBar").remove();
    $("#prCheckerTable").after("<div id=\"prCheckerReloadGifDiv\" style=\"height:40px;\"><img id=\"\" style=\"margin-top:16px;\" src=\"img/d1.gif\" /></div>");
    $.ajax({
        type: "POST",
        url: "Default.aspx/PR",
        data: "{'url':'" + $("#prCheckerInput").val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d === "-1") {
                $("#prCheckerReloadGifDiv").remove();
                $("#warningBar").remove();
                $("#prCheckerTable").after("<div id=\"warningBar\" style=\"height:28px; border:1px solid #DD3C10; background-color:#FFEBE8; margin-top:10px; line-height:28px;\">Geçersiz url girdiniz.</div>");
                $("#prChekerButton").removeAttr("disabled");
                $("#prCheckerInput").removeAttr("disabled");
                $("#prCheckerInput").focus();
                return false;
            } else {
                var siteName = $("#prCheckerInput").val();
                $("#prCheckerSiteName").html(siteName);
                $("#prCheckerSitePic").removeAttr("src");
                $("#prCheckerSitePic").attr("src", "img/" + msg.d + ".gif");
                $("#prCheckerResultNo").html(msg.d);
                $("#prChekerButton").removeAttr("disabled");
                $("#prCheckerReloadGifDiv").html("<img id=\"\" style=\"margin-top:16px;\" src=\"img/s1.gif\" />");
                $("#prCheckerInput").removeAttr("disabled");

                $("#prCheckerInput").val('');
                $("#prCheckerInput").focus();

                return false;
            }
        },
        error: function (e) {
            $("#prCheckerReloadGifDiv").remove();
            $("#warningBar").remove();
            $("#prCheckerTable").after("<div id=\"warningBar\" style=\"height:28px; border:1px solid #DD3C10; background-color:#FFEBE8; margin-top:10px; line-height:28px;\">Hata ile karşılaşıldı.</div>");
            $("#prChekerButton").removeAttr("disabled");
            $("#prCheckerInput").removeAttr("disabled");
        }
    });
}
