var Webolution = {};

Webolution.Utils = {};

/*
 * This is a copy of YUI's isArray function the only reason for the clone is that
 * Webolution needs to have this function without dependencies.
 *
 * @method isArray
 * @static
 * @param  {Object} o subject
 *
 */
Webolution.Utils.isArray = function(o) {
    return Object.prototype.toString.apply(o) === '[object Array]';
};
Webolution.Utils.isFunction = function(o) {
    return Object.prototype.toString.apply(o) === '[object Function]';
};


Webolution.Utils.isObject = function(o) {
    return (o && (typeof o === 'object' || Webolution.Utils.isFunction(o))) || false;
};

/*
 * Checks if a class or a set of classes is available.
 *
 * @method isClassAvailable
 * @static
 * @param  {Mixed} cls
 */
Webolution.Utils.isClassAvailable = function(cls) {
    if (!Webolution.Utils.isArray(cls)) {
        cls = [cls];
    }
    var clsparts;
    var avail = true;
    var i,j,tclass;

    for (j=0;j<cls.length;j++) {
        clsparts = cls[j].split('.');
        tclass = window;
        for (i=0;i<clsparts.length;i++) {
                tclass = tclass[clsparts[i]]
            if (!Webolution.Utils.isObject(tclass)) {
                avail = false;
                break;
            }
        }
        if (!avail) {
            break;
        }
    }
    return avail;
};

/*
 * Dynamicly loads an external script.
 *
 * @method loadScript
 * @static
 * @param  {String} scriptsrc script location
 *
 */
Webolution.Utils.loadScript = function(scriptsrc) {

    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = scriptsrc;
    head.appendChild(script);
}

/*
 * Dynamicly loads a style sheet.
 *
 * @method loadStyleSheet
 * @static
 * @param  {String} stylesheet style location
 *
 */
Webolution.Utils.loadStyleSheet = function(stylesheet) {

    var head = document.getElementsByTagName('head')[0];
    var style = document.createElement('link');
    style.type = 'text/css';
    style.rel= 'stylesheet';
    style.href = stylesheet;
    head.appendChild(style);
};
/*
 * Executes a function when a class or a set of classes is available.
 *
 * @method onClassAvailable
 * @static
 * @param  {Mixed} cls style location
 * @param  {Function} func style location
 */
Webolution.Utils.onClassAvailable = function(cls,func) {
    var checker = function() {
        if (Webolution.Utils.isClassAvailable(cls)) {
            func();
        } else {
            window.setTimeout(checker, 100);
        }
    }
    checker();
};

/*
 * Loads external Json
 *
 * @method loadJson
 * @static
 * @param  {String} url Json location
 * @param  {Function} func callback
 * @param  {String} mode Execution mode
 */
Webolution.Utils.loadJSON = function(url,func,mode) {
    if (mode==null) {
        mode = 'nocheck';
    }
    var load = function() {
        try {
        var transaction = YAHOO.util.Connect.asyncRequest('GET', url,
        {
            success: function(o) {
                try {
                    func(YAHOO.lang.JSON.parse(o.responseText));
                }
                catch (e) {
                    alert('Webolution.Utils.loadJSON::'+e);
                }
            },
            failure: function(o) {
                alert('Webolution.Utils.loadJSON::Connection Failed');
            }
        },
        null
        );
        } catch (e) {
            alert('Webolution.Utils.loadJSON::'+e);
        }
    }

    switch (mode) {
        case 'check':
            if (Webolution.Utils.isClassAvailable(['YAHOO.util.Connect','YAHOO.lang.JSON'])) {
                load();
            } else {
                alert('Webolution.Utils.loadJSON::Requirments to run this function are not met.');
            }
            break;
        case 'onload':
            Webolution.Utils.onClassAvailable(['YAHOO.util.Connect','YAHOO.lang.JSON'],load);
            break;
        default:
            //nocheck
            load();
            break;
    }

};
