// Copyright 2010 OverInteractive Media Inc. All rights reserved.

/*global $, DR, document, window */

/**
 * Miscellaneous utilities.
 */
DR.extend('Util', {
    /**
     * Converts a UNIX timestamp to a JavaScript Date object.
     *
     * @param {Number}  seconds A UNIX timestamp.
     * @return {Date} The date object.
     */
    dateFromUnixTimestamp: function(seconds) {
        var monthsShort = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.',
                           'Jul.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];

        var d = new Date(seconds * 1000);
        return monthsShort[d.getMonth()] + ' ' + d.getDate() + ', ' +
            d.getFullYear();
    },

    /**
     * Converts a name into a string that can be safely used as ID and class
     * names.
     *
     * @param {String}  name    The element name.
     * @return {String} The ID-friendly string.
     */
    niceName: function(name) {
        if (!name) {
            return '';
        }

        // Replace spaces with dashes; strip periods and colons
        return name
            .replace(/\s+/g, '-') // Replace spaces with dashes
            .replace(/\.+/g, '')  // Strip periods
            .replace(/:+/g, '');  // Strip colons
    },

    /**
     * Check that we can set cookies (DRA-66).
     * If not, attempt to set the cookie through iframe/form post means.
     *
     * @return {Boolean} Whether there's cookie support.
     */
    checkCookies: function(cookieVal) {
        return document.cookie.indexOf(cookieVal) >= 0;
    },

    /**
     * Gets the values from a form.
     *
     * @param {Element} form    The form to get values from.
     * @return {Object} The form's values.
     */
    getFormValues: function(form) {
        var values = {};

        $.each($(form).serializeArray(), function(i, field) {
            values[field.name] = field.value;
        });

        return values;
    },

    /**
     * Does a full redirect going to //apps.facebook.com/appname/path
     *
     * @param {String}  url The URL to load.
     */
    loadUrl: function(url) {
        window.top.location = DR.Facebook.getFullPath(url);
    }
});

