/*
 * @author Arnaud Métral <arnaud.metral@evonux.com>
 * @version 1.0
 * @copyright Evonux 2009
 *
 * Evonux.Control.js
 *
 */

Evonux.Control = {}

/*
 * Display message 
 */
Evonux.Control.showMsg = function (prefix, text)
{    
    $('msg-' + prefix).innerHTML = text;
    $('msg-box-' + prefix).style.display = "block";
}

/*
 * Disable all inactive tabs in panel
 */
Evonux.Control.showTab = function (num, len)
{
    for (var i = 0; i < len; i++)
    {
	$('tab_' + i).removeClass ("active");
	$('pane_' + i).removeClass ("active");
    }
    $('tab_' + num).addClass ("active");
    $('pane_' + num).addClass ("active");
}

/*
 * Update checkbox hidden value
 */
Evonux.Control.updateCheckbox = function (chk)
{
    var		name;

    name = chk.name.substring (0, chk.name.length - 4);
    $(name).value = (chk.checked ? '1' : '0');
}

/*
 * User logout if message confirmed
 */
Evonux.Control.logout = function (msg)
{
    if (confirm (msg))
    {
	Evonux.Page.get ('module=user&action=logout');
    }
}

/*
 * Eval text images (GD)
 */
Evonux.Control.evalText = function (el, font)
{
    var     text = el.get ("text");
    var     fg_colors = el.getStyle ('color').hexToRgb (true);
    var     bg_colors = el.getStyle ('border-top-color').hexToRgb (true);
    var     fg_color = fg_colors[0] + "-" + fg_colors[1] + "-" + fg_colors[2];
    var     bg_color = (bg_colors ? bg_colors[0] + "-" + bg_colors[1] + "-" + bg_colors[2] : '255-255-255');
    var     size = el.getStyle ('font-size');     
    var	    img = new Element ('img', { 'class' : 'gdimg', 'src' : U_ROOT + '/?module=tools&action=getText&text=' + text + '&size=' + size + '&fg_color=' + fg_color + '&bg_color=' + bg_color + '&font=' + font});

    img.replaces(el);
}

/*
 * Update text images (GD)
 */
Evonux.Control.updateText = function ()
{
    $$('h1,.gd').each (function (el)
	{
	    Evonux.Control.evalText (el, 1);
	});
}

/*
 * Get a radio group value
 */
Evonux.Control.getRadioValue = function (group)
{
    var		len = group.length;

    for (var i = 0; i < len; i++)
    {
	if (group[i].checked)
	    {
		return group[i].value;
	    }
    }

    return 0;
}

/*
 * Swap 2 items contents
 */

Evonux.Control.swapContents = function (div1, div2)
{
    var buffer = $(div1).innerHTML;
    $(div1).innerHTML = $(div2).innerHTML;
    $(div2).innerHTML = buffer;
}

/*
 * Make blink 
 */
Evonux.Control.blink = function (div, timer)
{
    if (!$(div))
    return;

    clearInterval (timers[timer]);
    $(div).style.display = ($(div).style.display == "inline-block" ? "none" : "inline-block");
    timers[timer] = setInterval ("Evonux.Control.blink ('" + div + "', " + timer + ");", $(div).style.display == "inline-block" ? 1000 : 100);
}

/*
 * Upload file
 */

Evonux.Control.submitFile = function ()
{
    var		frm = $('frm-file');

    frm.submit ();
}

/*
 * Launch action from settings
 */
Evonux.Control.launchOperation = function (prefix, link, msg)
{
    if (confirm (msg))
    {
	Evonux.Page.get (link, 'msg-' + prefix, function () { $('msg-box-' + prefix).style.display = 'block'; });
    }
}

/*
 * Enlarge picture
 */
Evonux.Control.enlarge = function (el)
{
    var		big = $('visual');

    if (el.width > el.height)
	{
	    big.style.width = "257px";
	    big.style.height = "auto";
	}
    else
	{
	    big.style.height = "257px";
	    big.style.width = "auto";
	}
    big.src = el.src;
}

/*
 * Bind picture event to grid
 */
Evonux.Control.bindGrid = function ()
{
    $$('table.grid tr td.small img').each (function (el)
	{
	    el.addEvent ('mouseenter', function () { Evonux.Control.enlarge (el); });
	});
}

