﻿$().ready(function() {
    // animation
    jQuery.fn.fadeToggle = function(speed, easing, callback) {
        return this.animate({ opacity: 'toggle' }, speed, easing, callback);
    };

    // ajax
    $.ajaxSetup(
	{
	    dataType: "json"
	});

    (function($) {
        m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"': '\\"',
            '\\': '\\\\'
        },
    $.toJSON = function(value, whitelist) {
        var a,          // The array holding the partial texts.
            i,          // The loop counter.
            k,          // The member key.
            l,          // Length.
            r = /["\\\x00-\x1f\x7f-\x9f]/g,
            v;          // The member value.

        switch (typeof value) {
            case 'string':
                return r.test(value) ? '"' + value.replace(r, function(a) {
                    var c = m[a];
                    if (c) {
                        return c;
                    }
                    c = a.charCodeAt();
                    return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
                }) + '"' :
                '"' + value + '"';

            case 'number':
                return isFinite(value) ? String(value) : 'null';

            case 'boolean':
            case 'null':
                return String(value);

            case 'object':
                if (!value) {
                    return 'null';
                }
                if (typeof value.toJSON === 'function') {
                    return $.toJSON(value.toJSON());
                }
                a = [];
                if (typeof value.length === 'number' &&
                    !(value.propertyIsEnumerable('length'))) {
                    l = value.length;
                    for (i = 0; i < l; i += 1) {
                        a.push($.toJSON(value[i], whitelist) || 'null');
                    }
                    return '[' + a.join(',') + ']';
                }
                if (whitelist) {
                    l = whitelist.length;
                    for (i = 0; i < l; i += 1) {
                        k = whitelist[i];
                        if (typeof k === 'string') {
                            v = $.toJSON(value[k], whitelist);
                            if (v) {
                                a.push($.toJSON(k) + ':' + v);
                            }
                        }
                    }
                } else {
                    for (k in value) {
                        if (typeof k === 'string') {
                            v = $.toJSON(value[k], whitelist);
                            if (v) {
                                a.push($.toJSON(k) + ':' + v);
                            }
                        }
                    }
                }
                return '{' + a.join(',') + '}';
        }
    };

    })(jQuery);

    //forms
    $("a.submit").click(function() {
        $(this).parents("form:first").submit();
        return false;
    });

    $("form").submit(function() {
        $(this).find("input.watermarked").val("").removeClass("watermarked");
        return true;
    });

    // Enable submit on Enter key
    $("form").append("<input class=\"_submit\" type=\"submit\" value=\".\" style=\"height: 0; width: 0; border: 0; margin: 0; padding: 0; display: inline; opacity: 0;\" />");

    // Tjek for submit links, and fake click on it instead of submitting (for the sake of backwards compatibility)
    $("form a.submit").each(function(i, item) {
        $(item).parents("form:first").find("input._submit").click(function() {
            $(item).click();
            return false;
        });
    });


    //page
    $(".editable").each(function(i, item) {
        $(item).click(function() {
            editPanel = $("#" + $(this).attr("id") + "-edit");
            editable = $(this);

            $(editable).hide();
            $(editPanel).show();
            $(editPanel).find("input[type=text], textarea").eq(0).focus();

            $(editPanel).find(".cancel").click(function() {
                $(editable).show();
                $(editPanel).hide();
                return false;
            });
        });

        if ($(item).attr("title").length == 0)
            $(item).attr("title", "Click to edit");
    });

    // Guestbook
    $("a.delete-tagwall-message").click(function() {
        row = $(this).parent().parent();

        $.get($(this).attr("href"), function(data) {
            if (data.Success) {
                $(row).fadeOut("fast", function() {
                    $(row).find("~ tr").eq(0).remove();
                    $(row).find("~ tr").eq(0).remove();
                    $(row).find("~ tr").eq(0).remove();

                    nextRow = $(row).find("~ tr").eq(0);

                    if ($(nextRow).find("td.replies").size() > 0) {
                        $(nextRow).remove();
                    }

                    $(row).remove();
                });
            }
        });

        return false;
    });

    $("a.replyto-tagwall-message").click(function() {
        $("#replyto-message span").text($(this).parent().find(".tagwall-message-authorname").text() + "'s");

        href = $(this).parent().find("a.replyto-tagwall-message").attr("href");

        $("input#replyid").val(href);
        //		$("input#replyid").val(href.substring(1));

        $("#replyto-message").show("fast");
        $("#comment").focus();

        return false;
    });

    $("a#cancel-tagwall-reply").click(function() {
        $("input#replyid").val("");
        $("#replyto-message").hide("fast");

        return false;
    });


    // Promt

    $.fn.promt = function(isSelfClosing, timeout) {
        $.blockUI(
		{
		    message: $(this),
		    centerY: false,
		    css:
			{
			    position: "absolute",
			    top: "50px",
			    left: ($(window).width() - 538) / 2 + "px",
			    margin: 0,
			    padding: 0,
			    width: "538px",
			    overflow: "hidden",
			    border: "none",
			    cursor: "default",
			    backgroundColor: "transparent"
			},
		    overlayCSS:
			{
			    backgroundColor: "#000",
			    opacity: "0.7"
			}
		});

        $._currentPromt = $(this);

        $('html, body').animate({ scrollTop: 0 }, "fast");
        if (isSelfClosing.toLowerCase() == 'true')
            setTimeout("$('.promt a.close').trigger('click')", timeout);

        return $(this);
    }

    $(".promt a.close").click(function() {
        $.unblockUI();
        $._currentPromt = null;
        return false;
    });

    $(document).keyup(function(e) {
        if ($._currentPromt != null) {
            if ($._currentPromt.length > 0 && e.keyCode == 13) {
                $($._currentPromt).find("a.close").eq(0).click();
            }
        }

    });

    // Updates (temp)

    $("#home-updates td a, #main-user-updates td a").attr("style", "");


    //mouseover list
    $("td.list").mouseover(function() {
        $(this).addClass("active");
    });

    $("td.list").mouseout(function() {
        $(this).removeClass("active");
    });


});




