/*
 * utils.js
 *
 * Copyright (C) 2006 - OS3 srl - http://www.os3.it
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
// Try to be compatible with other browsers
// Only use firebug logging when available
if ( typeof console == 'undefined' ) 
{
	console = new Object;
	console.trace = function() {};
	console.log = function() {};
	console.debug = function() {};
	console.info = function() {};
	console.warn = function() {};
	console.error = function() {};
	console.time = function() {};
	console.timeEnd = function() {};
	console.count = function() {};
}

// PUBLIC: OS3_JSLIB_BASE
var OS3_JSLIB_BASE = "";

// Used by the L10n module
var _os3_locale = {};

// PUBLIC: os3_locale_set
function os3_locale_set ( module_name, words )
{
	_os3_locale [ module_name ] = words;
}

// PUBLIC: os3_get_offset_top
function os3_get_offset_top ( elm ) 
{
  	var o_top    = elm.offsetTop;
  	var o_parent = elm.offsetParent;

  	while ( o_parent )
	{
		if ( o_parent.style.position == 'absolute' ) return o_top;
    		o_top += o_parent.offsetTop;
    		o_parent = o_parent.offsetParent;
  	}
 
  	return o_top;
}

// PUBLIC: os3_get_offset_left
function os3_get_offset_left ( elm ) 
{
	var o_left = elm.offsetLeft;
  	var o_parent = elm.offsetParent;

  	while ( o_parent )
	{
		if ( o_parent.style.position == 'absolute' ) return o_left;
    		o_left += o_parent.offsetLeft;
    		o_parent = o_parent.offsetParent;
  	}
 
  	return o_left;
}

// PUBLIC: append_script_file
function append_script_file ( script_file )
{
	var head = document.getElementsByTagName ( "head" ) [ 0 ];
	var new_script = document.createElement ( "script" );

	new_script.src = script_file;
	head.appendChild ( new_script );
}

// PUBLIC: append_css_file
function append_css_file ( css_file, id, as_first )
{
	var head = document.getElementsByTagName ( "head" ) [ 0 ];
	var new_css = document.createElement ( "link" );

	new_css.href = css_file;
	new_css.type = "text/css";
	new_css.rel  = "stylesheet";
	if ( id ) new_css.id = id;

	if ( as_first )
		head.insertBefore ( new_css, head.firstChild );
	else
		head.appendChild ( new_css );
}

// PUBLIC: os3_get_max
function os3_get_max ()
{
	if ( ! arguments.length ) return 0;

	var m = 0;
	var t;

	for ( t = 0; t < arguments.length; t ++ )
		if ( m < arguments [ t ] ) m = arguments [ t ];

	return m;
}

// PUBLIC: os3_get_min
function os3_get_min ()
{
	if ( ! arguments.length ) return 0;

	var m = arguments [ 0 ];
	var t;

	for ( t = 1; t < arguments.length; t ++ )
		if ( m > arguments [ t ] ) m = arguments [ t ];

	return m;
}

// PUBLIC: printfire
function printfire ( txt )
{
	console.warn ( "Still using printfire...." );
	console.debug ( txt );
}

// PUBLIC: os3_debug_obj
function os3_debug_obj ( o )
{
	if ( ! o ) return;
	var s = o.debugDump ( true );

	console.debug ( s );
}

// PUBLIC: os3_get_padding_width
function os3_get_padding_width ( el )
{
	var oldw = el.clientWidth;
	var neww;

	el.style.width = "0px";
	neww = el.clientWidth;

	el.style.width = (oldw - neww) + "px";

	return neww;
}

// PUBLIC: os3_get_padding_height
function os3_get_padding_height ( el )
{
	var oldh = el.clientHeight;
	var newh;

	el.style.height = "0px";
	newh = el.clientHeight;

	el.style.height = (oldh - newh) + "px";

	return newh;
}

// PUBLIC: os3_get_window_size
function os3_get_window_size ()
{
	var s = document.createElement ( "div" );
	s.style.position = "absolute";
	s.style.bottom = "0px";
	s.style.right  = "0px";
	s.style.width  = "1px";
	s.style.height  = "1px";
	s.style.visibility = "hidden";
	document.body.appendChild ( s );

	var w, h;
	w = os3_get_offset_left ( s ) + s.clientWidth;
	h = os3_get_offset_top ( s ) + s.clientHeight;

	document.body.removeChild ( s );

	return { "width": w, "height": h };
}

// PUBLIC: os3_create_element
function os3_create_element ( tag, name, parent )
{
	if ( ! parent ) parent = document.body;

	// if ( document.ActiveXObject ) tag = '<' + tag + ' name="' + name + '">';
	if ( document.all ) tag = '<' + tag + ' name="' + name + '">';

	var e = document.createElement ( tag );
	e.style.position = 'absolute';
	e.style.top = "0px";
	e.style.right = "0px";
	e.id = name;
	e.name = name;
	parent.appendChild ( e );
	
	return e;
}

// PUBLIC: os3_is_defined
function os3_is_defined ( name )
{
	eval ( "var is_def = ( typeof ( " + name + " ) != 'undefined' );" );

	return is_def;
}

// PUBLIC: os3_date2str
// Mode:
//
//	0 - YYYY MM DD	( default )
//	1 - DD MM YYYY
// 	2 - MM DD YYYY
function os3_date2str ( date, mode )
{
	var s = new String ( date );
	var v = s.match ( new RegExp ( "([0-9]{4}).([0-9]{1,2}).([0-9]{1,2})" ) );

	if ( ! mode ) mode = 0;

	switch ( mode )
	{
		case 1:
			s = v [ 3 ] + "-" + v [ 2 ] + "-" + v [ 1 ];
			break;
		case 2:
			s = v [ 2 ] + "-" + v [ 3 ] + "-" + v [ 1 ];
			break;

		default:
			s = v [ 1 ] + "-" + v [ 2 ] + "-" + v [ 3 ];
	}

	return s;
}

// PUBLIC: os3_wait_def
// Aspetta che un determinato oggetto 'name' sia stato
// definito. 
// Nel caso sia stato deinito, chiama la funzione specificata da cback
// passandole 'vars' come parametro
function os3_wait_def ( name, cback, vars )
{
        // console.debug ( "Wait Def...: " + name );
	if ( ! os3_is_defined ( name ) ) setTimeout ( function () { os3_wait_def ( name, cback, vars ); }, 50 );
        else cback ( vars );
}

// PUBLIC: os3_set_opacity
//
// Setta la trasparenza dell'oggetto ``e``
// con il valore di alpha ``val``.
// ``val`` deve essere compreso tra 0 e 100
//
// Restituisce il valore di ``val`` assegnato all'oggetto
function os3_set_opacity ( e, val )
{
        if ( val > 100 ) val = 100;
        if ( val < 0 ) val = 0;

        if ( e.filters )
        {
                if ( ! e.filters.alpha ) e.style.filter = "alpha(opacity=" + val + ")";
                e.filters.alpha.opacity = val;
        } else e.style.MozOpacity = val / 100;

	return val;
}

// PUBLIC: os3_call_func
// Chiama una funzione passando un array di parametri
function os3_call_func ( func_name, this_arg, arg_array )
{
	var i, l = arg_array.length;
	var s = "this_arg." + func_name + " ( ";
	
	for ( i = 0; i < l; i++ )
	{
		if ( i > 0 ) s += ", ";
		s += "arg_array[" + i + "]";
	}

	s += " );";

	return eval ( s );
}

/*
	PUBLIC: _

	This function is used by the L10n module.
	USAGE:

		_ ( mod_name, str )

	mod_name - Module name.
	str	 - String to be localized
*/
function _ ( mod_name, str )
{
	var mod = _os3_locale.get ( mod_name, Array() );
	return mod.get ( str, str );
}

// PUBLIC: $
function $ ( name )
{
	return document.getElementById ( name );
}

// PUBLIC: add_slashes
function add_slashes ( s )
{
	return s.replace ( "'", "\\'" ).replace ( '"', '\\"' );
}


function isEmpty(o) 
{
    	var i, v;

    	if ( isObject ( o ) ) 
	{
        	for ( i in o ) 
		{
        		v = o[i];
            		if ( isUndefined ( v ) && isFunction ( v ) ) return false;
            	}
    	}

	return true;
}

function isAlien ( a )     { return isObject ( a ) && typeof a.constructor != 'function'; }
function isArray ( a )     { return isObject ( a ) && a.constructor == Array; }
function isBoolean ( a )   { return typeof a == 'boolean'; }
function isFunction ( a )  { return typeof a == 'function'; }
function isNull ( a )      { return a === null; }
function isNumber ( a )    { return typeof a == 'number' && isFinite ( a ); }
function isObject ( a )    { return ( a && typeof a == 'object' ) || isFunction ( a ); }
function isString ( a )    { return typeof a == 'string'; }
function isUndefined ( a ) { return typeof a == 'undefined'; } 
