﻿function LoadGlobal() {
    $(".rpRootGroup").removeClass("rpRootGroup");
    $(".rpOut").removeClass("rpOut");
    $("#btnSignup").button();
    $("#btnVoteaza").button();

    $("#submeniu1").hide();
    $("#submeniu2").hide();
    $("#submeniu3").hide();
    $("#submeniu4").hide();

    $("#ulServicii").hide();
    $("#ulNewsletter").hide();
    $("#divSondaj").hide();
    $("#divComunica").hide();
    $("#div_comunica").hide();

    $('#div_sondaj').dialog({
        autoOpen: false,
        modal: true,
        title: "Intrebarea zilei",
        width: 600,
        buttons: {
            "DA": function () {
                SetRaspunsSondaj('da');
            },
            "NU": function () {
                SetRaspunsSondaj('nu');
            }
        }
    });
    $('#div_comunica').dialog({
        autoOpen: false,
        modal: true,
        title: "Comunica cu noi",
        width: 600,
        buttons: {
            "Trimite": function () {
                $(this).dialog("close");
            },
            "Renunt": function () {
                $(this).dialog("close");
            }
        }
    });
    $("#div_formular").dialog({
        autoOpen: false,
        modal: true,
        title: "Formular",
        width: 600,
        height: 500
    });

    $('.btnComunica').click(function () {
        $('#div_comunica').dialog('open');
        return false;
    });
    $('#btnVoteaza').click(function () {
        $('#div_sondaj').dialog('open');
        return false;
    });

    $("#txtNewsletter").click(function () { $(this).val(""); })
                               .blur(function () { if ($(this).val() == "") $(this).val($(this).attr("watermark")) });
    $(".keywords").click(function () { $(this).val(""); })
                               .blur(function () { if ($(this).val() == "") $(this).val($(this).attr("watermark")) });
    GetSondaj();
}

function callNewsletterSignup() {
    var email = $("#txtNewsletter").val();
    if (email) {
        newsletterSignUp(email);
    }
}

function showServicii(li) {
    $(li).height(100);
    //li.style.backgroundColor = "White";        
    $("#ulServicii").show();
}
function hideServicii(li) {
    $(li).height(25);
    //li.style.backgroundColor = "#d52323;"
    $("#ulServicii").hide();
}
function showNewsletter(li) {
    $(li).height(50);
    $("#ulNewsletter").show();
}
function hideNewsletter(li) {
    $(li).height(25);
    $("#ulNewsletter").hide();
}
function showSondaj(li) {
    $(li).height(152);
    $("#divSondaj").show();
}
function hideSondaj(li) {
    $(li).height(25);
    $("#divSondaj").hide();
}
function showComunica(li) {
    $(li).height(180);
    $("#divComunica").show();
}
function hideComunica(li) {
    $(li).height(25);
    $("#divComunica").hide();
}

function showSubmeniu(index) {
    $("#submeniu" + index).show();
    for (var i = 1; i <= 4; i++) {
        if (index == i)
            $("#submeniu" + i).show();
        else if (index == 0)
            $("#submeniu" + i).hide();
        else
            $("#submeniu" + i).hide();
    }
}
function showFormular(index) {
    $("#frame_formular").attr("src", "about:blank");
    $("#frame_formular").attr("src", "/formulare/fsi.aspx?Tip=" + index);
    $('#div_formular').dialog('open');
    return false;
}


function newsletterSignUp(email) {
    $("#txtNewsletter").parent(".rpTemplate").append("<img id='imgloading' src='/App_Themes/Default/Img/ajax-loader.gif'/>");

    var memberfilter = new Array();
    memberfilter[0] = "email";

    var abonat = new Object();
    abonat.email = email;

    var jsonText = JSON.stringify(abonat, memberfilter, "\t");
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/service.asmx/NewsletterSignup",
        data: jsonText,
        dataType: "json",
        success: function (msg) {
            alert(msg.d);
            $('#imgloading').remove();
        },
        failure: function (msg) {
            alert(msg);
            $('#imgloading').remove();
        }
    });
}

function GetSondaj() {
    $("#txtSondaj").parent(".rpTemplate").append("<img id='imgloading' src='/App_Themes/Default/Img/ajax-loader.gif'/>");

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/service.asmx/GetSondaj",
        data: "{}",
        dataType: "json",
        success: function (msg) {
            var sondaj = JSON.parse(msg.d);
            $("#txtSondaj").html(sondaj.Mesaj);
            $("#txtIdSondaj").val(sondaj.Id);
            $("#txtSondaj2").html(sondaj.Mesaj);
            $('#imgloading').remove();
        },
        failure: function (msg) {
            alert(msg.d);
            $('#imgloading').remove();
        }
    });
}

function SetRaspunsSondaj(raspuns) {
    $("#txtComentariu").parent("td").append("<img id='imgloading' src='/App_Themes/Default/Img/ajax-loader.gif'/>");

    var memberfilter = new Array();
    memberfilter[0] = "raspuns";
    memberfilter[1] = "comentariu";
    memberfilter[2] = "idsondaj";

    var sondaj = new Object();
    sondaj.raspuns = raspuns;
    sondaj.comentariu = $("#txtComentariu").val();
    sondaj.idsondaj = $("#txtIdSondaj").val();

    var jsonText = JSON.stringify(sondaj, memberfilter, "\t");

    $(".ui-dialog-buttonpane").hide();

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/service.asmx/SetRaspunsSondaj",
        data: jsonText,
        dataType: "json",
        success: function (msg) {
            $("#txtComentariu").parent("td").append("<p>" + msg.d + "</p>");
            $('#imgloading').remove();
        },
        failure: function (msg) {
            alert(msg.d);
            $('#imgloading').remove();
        }
    });
}

jQuery.fn.extend({
    // Based off of the plugin by Clint Helfers, with permission.
    // http://blindsignals.com/index.php/2009/07/jquery-delay/
    delay: function (time, type) {
        time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
        type = type || "fx";

        return this.queue(type, function () {
            var elem = this;
            setTimeout(function () {
                jQuery.dequeue(elem, type);
            }, time);
        });
    }
});
