﻿/**
 * 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;


    /*
     * Definicje funkcji
     */

    hg['alert'] = {f:function(a) { alert(a); }};
    
    //OCA
    hg['login'] = {js:'hg.login.js'};
    hg['buy_object'] = {js:'hg.buy_object.js'};
    hg['add_subpage'] = {js:'hg.add_subpage.js'};
    hg['edit_categories'] = {js:'hg.edit_categories.js'};
    hg['edit_text'] = {js:'hg.edit_text.js'};
    hg['edit_textarea'] = {js:'hg.edit_textarea.js'};
    hg['edit_select'] = {js:'hg.edit_select.js'};
    hg['ifreplace_val'] = {js:'hg.ifreplace_val.js'};
    hg['notice_box'] = {js:'hg.notice_box.js'};
    hg['error_box'] = {js:'hg.error_box.js'};
    hg['send_msg'] = {js:'hg.send_msg.js'};
    hg['share'] = {js:'hg.share.js'};
    hg['send_xmas_msg'] = {js:'hg.send_xmas_msg.js'};
    hg['hideFlashes'] = {js:'hg.flashtoggler.js'};
    hg['unhideFlashes'] = {js:'hg.flashtoggler.js'};
    hg['prompt'] = {js:'hg.prompt.js'};
    hg['confirm'] = {js:'hg.prompt.js'};
} // 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);
    }
    if (hg.debug)
        hg.console.innerHTML += '<p title="'+escape(e)+'">damn. some failure happened.</p>';
    else
	hg.console.innerHTML += '<p>damn. some failure happened.</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; }


