﻿var errors = [];

$(function() {
    renderErrors();

    $("#create-group-link").click(createNewGroup);

    $("#sign-in :text").bind('keydown', 'return', function(evt) {
        $("#sign-in button").trigger("click");
        return false;
    });

    $(".chunk").each(function(i, el) {
        $(el).hover(function() {
            $(".edit-chunk", this).show();
        }, function() {
            $(".edit-chunk", this).hide();
        }).css("position", "relative");
    });
});

function updateChunk(id, html) {
    var editLink = $(".edit-chunk", id).clone(true);
    $(id).html(html).append(editLink);
}

function createNewGroup(e) {
    e.preventDefault();
    var d = {};
    d.name = prompt("Enter a name for your group");
    if (d.name == null) return;
    
    $.getJSON("/handlers/ajax.ashx/CreateGroup", d, function(json) {
        switch (json.result) {
            case "ERROR":
                alert(json.message);
                break;
            case "OK":
                window.location.href = "/my-groups.aspx?id=" + json.groupId;
                break;
        }
    });
}



function renderErrors(omitSummary) {
	if (errors.length > 0) {
		$.each(errors, function(i, e) {
			highlightError(e);
		});

		if (!omitSummary) {
			var html = "<ul class=\"errors\">";
			$.each(errors, function(i, e) {
				html += "<li>"+ e.message +"</li>";
			});
			html += "</ul>";
			
			$("#content").prepend(html);
		}
	}
}

function highlightError(e) {
	if (!e.c) return;
	var el = $("#"+ e.c).addClass("error");
	$("label[for='"+ e.c +"']").addClass("error");
}

function confirmDelete() {
	return confirm("Are you sure you want to delete this?");
}

//
// String.Format implementation
//
String.prototype.Format = function(format, args) {
    var result = format;
    for (var i = 1; i < arguments.length; i++) {
        result = result.replace(new RegExp('\\{' + (i - 1) + '\\}', 'g'), arguments[i]);
    };
    return result;
};
//
// string.EndsWith implementation
//
String.prototype.EndsWith = function(suffix, ignoreCase) {
    if (!suffix) return false;
    if (suffix.length > this.length) return false;
    if (ignoreCase) {
        if (ignoreCase == true) {
            return (this.substr(this.length - suffix.length).toUpperCase() == suffix.toUpperCase());
        };
    };
    return (this.substr(this.length - suffix.length) === suffix);
};

//
// string.StartWith implementation
//
String.prototype.StartWith = function(prefix, ignoreCase) {
    if (!prefix) return false;
    if (prefix.length > this.length) return false;
    if (ignoreCase) {
        if (ignoreCase == true) {
            return (this.substr(0, prefix.length).toUpperCase() == prefix.toUpperCase());
        };
    };
    return (this.substr(0, prefix.length) === prefix);
};

//
// string.Trim implementation
//
String.prototype.Trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};

//
// String.IsNullOrEmpty implementation
//
String.prototype.IsNullOrEmpty = function() {
    if (this) {
        if (typeof (this) == 'string') {
            if (this.length > 0)
                return false;
        };
        if (this != null)
            return false;
    };
    alert("true");

    return true;
};

String.prototype.urlEncode = function() {
    return encodeURIComponent(this);
};
