/**
 * @author      DucthuanX
 * @link        http://www.ducthuan.info
 * @copyright   (c) 2007 Nguyen Duc Thuan <me at ducthuan dot info>
 */
/**
 * Get instance of an element by id
 * @param {String} id
 * @return {HTMLElement}
 */
function $(id){
    return document.getElementById(id);
}

/**
 * Get array of elements by tag name
 * @param {String} tagName
 * @return {Array}
 */
function els(tagName){
    return document.getElementsByTagName(tagName);
}

/**
 * Check an array for an existing item
 * @param {Object} item
 * @return {Boolean}
 */
Array.prototype.isExisting = function(item){
    for (var itemCounter = 0, arrayItem; arrayItem = this[itemCounter]; itemCounter++) {
        if (arrayItem == item) {
            return true;
        }
    }
    return false;
};

/**
 * Ducthuans Library object
 */
var Ducthuans = new function(){
    this._loadedScripts = [];
    this._baseUrl = '';
    this._handlers = [];
    this._handlers['winLoad'] = [];
    this._handlers['winUnload'] = [];
    
    var scripts = els('script');
    
    for (var scriptCounter = 0, script; script = scripts[scriptCounter]; scriptCounter++) {
        if (script.src.match(/(\/Ducthuans\.js)$/)) {
            this._baseUrl = script.src.replace(/(Ducthuans\.js).*$/, '');
            this.baseUrl = this._baseUrl;
            return;
        }
    }
};

/**
 * Adds event handler
 * @param {String} event
 * @param {Object} handler
 */
Ducthuans.addHandler = function(event, handler){
    this._handlers[event].push(handler);
};

/**
 * Executes handlers
 * @param {String} event
 */
Ducthuans.runHandlers = function(event){
    var object = this;
    for (var hCounter = 0, handler; handler = this._handlers[event][hCounter]; hCounter++) {
        if ('function' == typeof handler) {
            handler.call(object);
        }
        else {
            eval(handler);
        }
    }
};

/**
 * Generates a random string
 * @param {Number} length
 * @param {String} prefix
 * @param {String} source
 * @return {String}
 */
Ducthuans.genRandomString = function(length, prefix, source){
    if (!prefix) {
        prefix = '';
    }
    
    if (!source) {
        source = '1234567890qwertyuiopasdfghjklzxcvbnm';
    }
    
    if (length < 0) {
        length = 0;
    }
    
    var output = prefix;
    var charCounter = 0;
    
    while (output.length < length) {
        var randomNum = Math.round(Math.random() * (source.length - 1));
        output += source.substring(randomNum, randomNum + 1);
    }
    
    return output;
};

/**
 * Loads list of Ducthuans component scripts into HTML document
 * @param {String} componentList separate by comma
 */
Ducthuans.load = function(componentList){
    componentArray = componentList.split(',');
    for (var componentCounter = 0, componentName; componentName = componentArray[componentCounter]; componentCounter++) {
        var fullFilePath = this._baseUrl + 'Ducthuans_' + componentName + '.js';
        this.include(fullFilePath);
    }
};

/**
 * Includes list of custom scripts
 * @param {String} scriptList separate by comma
 */
Ducthuans.include = function(scriptList){
    var scriptArray = scriptList.split(',');
    for (var scriptCounter = 0, scriptSource; scriptSource = scriptArray[scriptCounter]; scriptCounter++) {
        if (false == this._loadedScripts.isExisting(scriptSource)) {
            var script = document.createElement('script');
            // if script source had an absolute location then we load it,
            // else we add the base url before
            script.src = scriptSource.match(/^(https?:\/\/|\/)/) ? scriptSource : this._baseUrl + scriptSource;
            script.type = 'text/javascript';
            els('head')[0].appendChild(script);
            this._loadedScripts.push(script.src);
        }
    }
};

window.onload = function(){
    Ducthuans.runHandlers('winLoad');
};

window.onunload = function(){
    Ducthuans.runHandlers('winUnload');
};
