
/**
 * Framework
 *
 * @copyright Copyright (c) 2011, ecto.lt
 * @author    Benas Valančius <benas@ecto.lt>
 * @package   Framework
 *
 * $Id: framework.js 1313 2011-01-01 21:39:44Z noen $
 */

var ecto = {};

/**
 * Aler dialog
 *
 * @param string message - Message
 */
ecto.alert = function(message, msgTitle, msgButton)
{
    var ranId = 'dialog_'+ ecto.randomCode(16);
    $('<div id="'+ ranId +'" title="'+ (msgTitle !== undefined ? msgTitle : _('alertTitle')) +'"><div class="confirmContent">'+ message +'</div></div>').dialog({
        width : 350,
        template : {
            classTitle : 'redNotice'
        },
        buttons : [
            {
                title : (msgButton !== undefined ? msgButton : _('alertButton')),
                action : function()
                {
                    $('#'+ ranId).dialog('remove');
                    return false;
                },
                template : {
                    className : 'icon_ok'
                }
            }
        ]
    });
    $('#'+ ranId).dialog('open');
    $('#'+ ranId +' .icon_ok').focus();
};

/**
 * Confirm dialog
 *
 * @param string message   - Message
 * @param string callback  - Callback function
 * @param string msgTitle  - Title
 * @param string msgOk     - Button OK text
 * @param string msgCancel - Button Cancel text
 */
ecto.confirm = function(message, callback, msgTitle, msgOk, msgCancel)
{
    var ranId = 'dialog_'+ ecto.randomCode(16);
    $('<div id="'+ ranId +'" title="'+ (msgTitle !== undefined ? msgTitle : _('confirmTitle')) +'"><div class="confirmContent">'+ message +'</div></div>').dialog({
        width : 350,
        template : {
            classTitle : 'redNotice'
        },
        buttons : [
            {
                title : (msgOk !== undefined ? msgOk : _('confirmOk')),
                action : function()
                {
                    if(typeof(callback) == 'string')
                        eval(callback);
                    else
                        callback();

                    $('#'+ ranId).dialog('remove');
                    return false;
                },
                template : {
                    className : 'icon_ok'
                }
            },
            {
                title : (msgCancel !== undefined ? msgCancel : _('confirmCancel')),
                action : function()
                {
                    $('#'+ ranId).dialog('remove');
                    return false;
                },
                template : {
                    className : 'icon_cancel'
                }
            }
        ]
    });
    $('#'+ ranId).dialog('open');
    $('#'+ ranId +' .icon_ok').focus();
};

/**
 * Set cookie
 *
 * @param string  name  - Cookie name
 * @param string  value - Cookie value
 * @param integer time  - Duration
 */
ecto.setCookie = function(name, value, time)
{
    if(time)
    {
        var date = new Date();
        date.setTime(date.getTime()+(time*1000));
        var expires = '; expires='+ date.toGMTString();
    }
    else
        var expires = '';

    document.cookie = name +'='+ value + expires +'; path=/';
};

/**
 * Get cookie value
 *
 * @param string name - Cookie name
 */
ecto.getCookie = function(name)
{
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
};

/**
 * Delete cookie
 *
 * @param string name - Cookie name
 */
ecto.removeCookie = function(name)
{
    this.setCookie(name,'',-1);
};

/**
 * Toggle item status
 *
 * @param object e    - Link
 * @param array  data - Styles
 * @param string path - Ajax path
 */
ecto.toggleItem = function(e, data, path)
{
    var status = 1;

    if($(e).hasClass(data[0]))
        status = 0;

    new Ajax.Post(
    {
        url     : path,
        data    : {status:status},
        success : function(res)
        {
            if(res)
            {
                if(status === 0)
                {
                    $(e).removeClass(data[0]);
                    $(e).addClass(data[1]);
                }
                else
                {
                    $(e).removeClass(data[1]);
                    $(e).addClass(data[0]);
                }
            }
        }
    });
};

ecto.randomCode = function(length)
{
    var chars = 'abcdefghijklmnopqrstuvwxyz';
    var code = '';
    for(x = 0; x < length; x++)
    {
        i = Math.floor(Math.random() * chars.length);
        code += chars.charAt(i);
    }
    return code;
};
