﻿registerNS("Tegud.Utilities");
registerNS("Tegud.Utilities.RegEx");

Tegud.Utilities.RegEx.Email = new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$");

/*
* JavaScript Pretty Date
* Copyright (c) 2008 John Resig (jquery.com)
* Licensed under the MIT license.
*/

// Takes an ISO time and returns a string representing how
// long ago the date represents.
Tegud.Utilities.PrettyDate = function (time) {
    var date = new Date((time || "")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);

    if (isNaN(day_diff))
        return;
    else if (day_diff >= 31)
        return date.getDate() + Tegud.Utilities.GetDaySuffix(date.getDate()) + " " + Tegud.Utilities.GetMonthName(date.getMonth()) + " " + date.getYear();
    else if (day_diff < 0)
        return "just now";

    return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor(diff / 60) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor(diff / 3600) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil(day_diff / 7) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if (typeof jQuery != "undefined")
    jQuery.fn.prettyDate = function () {
        return this.each(function () {
            var date = Tegud.Utilities.PrettyDate(this.title);
            if (date)
                jQuery(this).text(date);
        });
    };

// jQuery.support.transition
// to verify that CSS3 transition is supported (or any of its browser-specific implementations)
$.support.transition = (function () {
    var thisBody = document.body || document.documentElement,
        thisStyle = thisBody.style,

    support = thisStyle.WebkitTransition !== undefined || thisStyle.MozTransition !== undefined || thisStyle.OTransition !== undefined || thisStyle.transition !== undefined;

    return support;
})();

Tegud.Utilities.ReplaceAll = function (input, replaceString, withString) {
    while (input.indexOf(replaceString) > -1)
        input = input.replace(replaceString, withString);

    return input;
}

Tegud.Utilities.GetMonthNumber = function (monthName) {
    if ((monthName || "") == "")
        return;

    if (monthName == "Janurary")
        return 1;

    if (monthName == "February")
        return 2;

    if (monthName == "March")
        return 3;

    if (monthName == "April")
        return 4;

    if (monthName == "May")
        return 5;

    if (monthName == "June")
        return 6;

    if (monthName == "July")
        return 7;

    if (monthName == "August")
        return 8;

    if (monthName == "September")
        return 9;

    if (monthName == "October")
        return 10;

    if (monthName == "November")
        return 11;

    if (monthName == "December")
        return 12;
}

Tegud.Utilities.GetMonthName = function (month) {
    if (month == 0)
        return "Janurary";
    if (month == 1)
        return "Feburary";
    if (month == 2)
        return "March";
    if (month == 3)
        return "April";
    if (month == 4)
        return "May";
    if (month == 5)
        return "June";
    if (month == 6)
        return "July";
    if (month == 7)
        return "August";
    if (month == 8)
        return "September";
    if (month == 9)
        return "October";
    if (month == 10)
        return "November";
    if (month == 11)
        return "December";
}

Tegud.Utilities.GetDaySuffix = function (day) {
    if (day == 1 || day == 21 || day == 31)
        return "st";

    if (day == 2 || day == 22)
        return "nd";

    if (day == 3 || day == 23)
        return "rd";

    return "th";
}

Tegud.Utilities.GetMousePosition = function (e, offset) {
    var posx = 0;
    var posy = 0;

    if (!e) var e = window.event;

    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX + document.body.scrollLeft
			                + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
			                + document.documentElement.scrollTop;
    }

    return {
        X: posx + (offset.X || 0),
        Y: posy + (offset.Y || 0)
    };
}

$(function () {
    $.extend($.fn.disableTextSelect = function () {
        return this.each(function () {
            if ($.browser.mozilla) {//Firefox
                $(this).css('MozUserSelect', 'none');
            } else if ($.browser.msie) {//IE
                $(this).bind('selectstart', function () { return false; });
            } else {//Opera, etc.
                $(this).mousedown(function () { return false; });
            }
        });
    });
    $('.noSelect').disableTextSelect(); //No text selection on elements with a class of 'noSelect'
});

$.fn.reverse = [].reverse; 
