/**
 * Rdzenny javascript Hologramu
 * @author m.augustynowicz
 * @package hologram
 */

if (typeof($)!='function')
{
    // jQuery not loaded.
    var hg  = function () { return function(){return hg['exception'].f;} };
    hg.debug = false;
}
else
{

    // dummy function for when firebug is not available
    if (typeof(console)!='object')
      console = {};
    if (typeof(console.log)!='function')
        console.log = function() {}
    console.log('hg.core initiation');

    /**
     * does a magic trick
     *
     * @author m.augustynowicz
     *
     * @param nazwa funkcji do wywolania
     * @return funkcja
     */
    function hg(f)
    {
        console.log('hg(',f,') called, debug =', hg.debug);
        try
        {
            if (typeof(hg[f].f)!='function')
            {
                var path = hg[f].js;
                if (path.charAt(0)!='/')
                    path = hg.include_path + path;
                if (hg.debug)
                {
                    $('head').append('<script id="hg__'+path+'" src="'+path+'"></script>');
                }
                else
                {
                    $.ajax({
                        type: 'GET',
                        url: path,
                        async: false,
                        success: function(data) {
                            eval(data);
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            var msg = '$.ajax error: ';
                            if (undefined!=textStatus)
                                msg += textStatus;
                            if (undefined!=errorThrown)
                                msg += errorThrown;
                            throw msg;
                            this;
                        }
                    });
                }
            }
            return hg[f].f;
        }
        catch(e)
        {
            if (hg[f]==undefined)
                hg[f] = {};
            return hg[f].f = function()
                {
                    console.log('hg(',f,') failed:', e);
                    if (f!='exception')
                        hg('exception')(e);
                    return function() { return null; }
                }
        }
    }

    /*
     * Domyslne ustawienia obiektu hg
     */

    // bazowa sciezka do skryptow, uzywana jesli js _nie_ zaczyna sie od '/'
    hg.include_path = '/jscripts/';
    // tryb debug
    hg.debug = false;


} // if $ is object

hg['exception'] = {f:function(e) {
    hg.console = document.getElementById('hg_messages');
    if (typeof(hg.console)=='undefined' || hg.console == null)
    {
        hg.console = document.createElement('div');
        hg.console.setAttribute('id', 'hg_messages');
        // FIXME position:fixed versus IE
        hg.console.setAttribute('style', 'position:fixed; top:0; right:0; padding: 1ex; background-color: pink; border: silver solid thin; -moz-border-radius: 0 0 0 1ex; opacity: .9;');
        document.getElementsByTagName('body')[0].appendChild(hg.console);
    }
    var e_text = hg.debug ? e : '<!-- '+e+' -->';
    hg.console.innerHTML += '<p>damn. some failure happened. '+e_text+'</p>';
    setTimeout(function(){
	if (hg.console.lastChild)
	    hg.console.removeChild(hg.console.lastChild);
	console.log(hg.console.childNodes);
	if (!hg.console.childNodes.length)
	    hg.console.parentNode.removeChild(hg.console);
    }, 3000);
}};

//
// some helper functions
//

hg.isset = function(a) { return typeof(a)!='undefined' && a!=null; }

/**
 * Check if given funcion is present
 * @author m.augustynowicz
 *
 * @param string function name
 * @return boolean
 */
hg.present = function(name) {
    if (typeof hg[name] == 'undefined')
        return false;
    return (typeof hg[name].f != 'undefined' || typeof hg[name].js != 'undefined');
}

