﻿$(document).ready(function() {

    $("#tipsa_button").click(function() {

        var data = {};
        $("#tipsa_dialog :input").each(function() {
            data[$(this).attr("name")] = $(this).val() == null || $(this).val() == "" ? $(this).text() : $(this).val();
        });

        if (data['text'] == null || data['text'] == '') {
            alert("Ange text");
            return;
        }

        $(this).attr("disabled", "disabled");

        $.ajax({ url: "/inc/Tipsa.ashx",
            data: data,
            success: function(d) {
                $("#tycktill_button").attr("disabled", "");
                if (d == "") {
                    alert("Tipset är mottaget. Tack så mycket!");
                    $("#tycktill_dialog").dialog('close');
                }
                else
                    alert("Ett fel har uppstått. Var god prova senare");
            },
            error: function(d) {
                alert("Ett fel har uppstått. Var god prova senare");
                $("#tycktill_button").attr("disabled", "");
            },
            type: "POST",
            dataType: "text",
            contentType: "application/x-www-form-urlencoded;charset=utf-8"
        });
    });

    $("#tycktill_button").click(function() {

        var data = {};
        $("#tycktill_dialog :input").each(function() {
            data[$(this).attr("name")] = $(this).val() == null || $(this).val() == "" ? $(this).text() : $(this).val();
        });

        if (data['text'] == null || data['text'] == '') {
            alert("Ange text");
            return;
        }

        $(this).attr("disabled", "disabled");

        $.ajax({ url: "/inc/Tipsa.ashx",
            data: data,
            success: function(d) {
                $("#tycktill_button").attr("disabled", "");
                if (d == "") {
                    alert("Tipset är mottaget. Tack så mycket!");
                    $("#tycktill_dialog").dialog('close');
                }
                else
                    alert("Ett fel har uppstått. Var god prova senare");
            },
            error: function(d) {
                alert("Ett fel har uppstått. Var god prova senare");
                $("#tycktill_button").attr("disabled", "");
            },
            type: "POST",
            dataType: "text",
            contentType: "application/x-www-form-urlencoded;charset=utf-8"
        });
    });

    $("a#tipsa").click(function() {

        $("#player").css({ visibility: 'hidden' });
        $("#tipsa_dialog").dialog({ bgiframe: true, modal: true, draggable: false, title: 'Tipsa 24Norrbotten', autoOpen: true, closeOnEscape: true, beforeclose: function() {
            $("#player").css({ visibility: 'visible' });
        }
        }).dialog('open');
        return false;

    });

    $("a#tycktill").click(function() {
        $("#player").css({ visibility: 'hidden' });
        $("#tycktill_dialog").dialog({ bgiframe: true, modal: true, draggable: false, title: 'Lämna synpunkter till 24Norrbotten', autoOpen: true, closeOnEscape: true, beforeclose: function() {
            $("#player").css({ visibility: 'visible' });
        }
        }).dialog('open');
        return false;
    });

});