﻿$(document).ready(function () {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);


});

function addQuote(nick, text) {
    $(".jq_ForumMessage").val($(".jq_ForumMessage").val() + "[quote nick=" + nick + "][/quote]");
}

function EndRequestHandler() {
    //setFriendsUI('<%=FriendListMode%>');
    setGbMsg();
    watermark("#ctl00_ctl00_MasterContentPlaceHolder_MainContentPlaceHolder_txtFacebook", "Full url like http://facebook.com/ola.johansson")
    watermark("#ctl00_ctl00_MasterContentPlaceHolder_MainContentPlaceHolder_txtTwitter", "@olaj, olaj or http://twitter.com/olaj")
}

function getForumAndGroup() {
    var url = document.location.href;

    if (url.indexOf("/forum")) {
        var urlSegments = url.split("/");
        var values = new Array(urlSegments[4], urlSegments[5]);
        return values;
    }

    return null;
}

function toogleFriendList() {
    imgsrc = $("#toogleFriends").attr("src");
    matches = imgsrc.match(/open/);

    if (matches) {
        mode = 'close';
    }
    else {
        mode = 'open';
    }

    //setFriendsUI(mode);


    $.ajax({
        type: "POST",
        url: '/Services/AjaxServices.asmx/RememberFriends',
        data: "{mode:'" + mode + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
        }
    });
}

$.timer(90000, function (timer) {
    setGbMsg();
});

function setGbMsg() {

    var uid = $("#hdnId").val();

    $.ajax({
        type: "POST",
        url: '/Services/AjaxServices.asmx/GetNotifiers',
        data: "{userId:'" + uid + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            var a = result.d.split(",");
            if (a[0] > 0) {
                $("#aMsg").attr("style", "font-weight: bold");
                $("#aMsg").attr("class", "msg_on");
            }
            if (a[1] > 0) {
                $("#aGb").attr("style", "font-weight: bold");
                $("#aGb").attr("class", "gb_on");
            }


            $("#aMsg").text(a[0]);
            $("#aGb").text(a[1]);
            document.title = 'whoa.nu | ' + a[1] + '/' + a[0];
        }
    });
}

function ToogleGroup(ForumGroupId) {

    ToogleUI(ForumGroupId);

    $.ajax({
        type: "POST",
        url: '/Services/AjaxServices.asmx/RememberGroups',
        data: "{forumGroupId:'" + ForumGroupId + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}

function ToogleUI(ForumGroupId) {
    $("#aToogleGroup_" + ForumGroupId).toggleClass("forumGroup_open");
    $("#aToogleGroup_" + ForumGroupId).toggleClass("forumGroup_closed");
    $("#forumgroup_" + ForumGroupId).toggleClass("forumGroup_open");
    $("#forumgroup_" + ForumGroupId).toggleClass("forumGroup_closed");

    ToggleSidebar();
}

function watermark(ctl, watermark, persist) {

    if ($(ctl).val() == "") {
        $(ctl).val(watermark);
        $(ctl).css("color", "grey");
    }

    $(ctl).focus(function () {
        if (this.value == watermark) {
            this.value = "";
            this.css("color", "grey");
        }
    }).blur(function () {
        if (this.value == "") {
            this.value = watermark;
            this.css("color", "black");
        }
    }).click(function () {
        if (this.value.indexOf(watermark) > -1) {
            this.value = "";
            this.css("color", "black");
        }
    });
}


