/*
 * Class: ScriptLoader
 *     Загружает указанные скрипты из директории scripts.
 */
var ScriptLoader = {

    request: null,

    loaded: {},

    load: function() {
        for (var i = 0, len = arguments.length; i < len; i++) {
            var filename = arguments[i];
            if (!this.loaded[filename]) {
                if (!this.request) {
                    this.request = window.XMLHttpRequest ? new XMLHttpRequest : (window.ie ? new ActiveXObject('Microsoft.XMLHTTP') : null);
                }
                if (!this.request) return false;
                this.loaded[filename] = true;
                this.request.open('get', 'scripts/'+filename, false);
                this.request.send(null);
                if (this.request.status == 200) {
                    this.globalEval(this.request.responseText);
                }
            }
        }
    },

    globalEval: function(code) {
        if (window.execScript) window.execScript(code, 'javascript');
        else window.eval(code);
    }
}

// Словарь для хранения языкозависимых констант и их переводов.
var Dictionary = {};

/*
 * Улучшения:
 *   - Проверка уже загруженных стилей;
 *   - Загрузка стилей из директории stylesheets.
 */
Asset.loaded = { css: {} };
Asset.css = function(source, properties) {
    if (!Asset.loaded.css[source]) {
        Asset.loaded.css[source] = true;
    	return Asset.create('link', {
    		'rel': 'stylesheet', 'media': 'screen', 'type': 'text/css', 'href': 'stylesheets/'+source
    	}, properties, true);
    }
    return false;
}


Object.toQueryString = function(source){
	var queryString = [];
	for (var property in source) {
	    if ($type(source[property]) == 'array') {
	        for (var i = 0; i < source[property].length; i++) {
	            queryString.push(encodeURIComponent(property)+'='+encodeURIComponent(source[property][i]));
	        }
	    }
	    else {
	        queryString.push(encodeURIComponent(property)+'='+encodeURIComponent(source[property]));
	    }
	}
	return queryString.join('&');
};

Element.implement({

    // Для единообразия.
    removeProperty: function(property){
        this.removeAttribute(property);
        return this;
    },

    // Добавлена поддержка multiple select.
    getValue: function(){
		switch (this.getTag()){
			case 'select':
                if (this.multiple){
                    var values = [];
                    $A(this.getElements('option')).each(function(option){
                        if (option.selected) values.push(option.value);
                    });
                    return values;
                }
                else if (this.selectedIndex != -1){
					var opt = this.options[this.selectedIndex];
					return opt.value || opt.text;
				}
				break;
			case 'input': if (!(this.checked && ['checkbox', 'radio'].test(this.type)) && !['hidden', 'text', 'password'].test(this.type)) break;
			case 'textarea': return this.value;
		}
		return false;
	},

    // Добавлена поддержка multiple select.
    toObject: function(){
		var obj = {};
		$$(this.getElementsByTagName('input'), this.getElementsByTagName('select'), this.getElementsByTagName('textarea')).each(function(el){
			var name = $(el).name;
			var value = el.getValue();
			if ((value !== false) && name){
                obj[name] = value;
            }
		});
		return obj;
	}
});

