// Добавить эффект:
// uid - универсальный id
// param - {current: 0, end: 100, type:'alpha', step: 1, special: 'visibility'}
function effect_class() {

this.effect_array = new Array();
this.interval = 10;
this.block = false;
this.timeout_id = undefined;

// Добавить в таблицу объектов новый с привязаным эфффектом
this.add_effect = function (object_uid, param) 
{
    object = select_object(object_uid);
    var num = this.effect_array.length;
    
    // Ищем наличие такого эффекта для этого объекта
    var effect_result = new Array();
    
    var effect_array = this.effect_array;
    var effect_array_length = effect_array.length;
   	// Просеяли эффекты
    for(var i=0; i<effect_array_length;i++) {
        if(object!=effect_array[i]['object'] || param.type!=effect_array[i]['param']['type'])
            effect_result[effect_result.length]=effect_array[i];
    }
    // Внесли новый  
    effect_result[effect_result.length] = this.start_effect(object, param);
    this.effect_array = effect_result;
    
    this.set_interval();
}
// Обработать эффект - сделать старт обработку
this.start_effect = function (object, param)
{
	var new_row = new Array();
    new_row['object'] = object;
    new_row['param'] = param;
	
	this.make_effect(object, param);
    
    return new_row;
}
// Интервал перезапуска эффектов
this.set_interval = function ()
{
    clearTimeout(this.timeout_id);
    this.timeout_id = setTimeout("effect.compile_effect()", this.interval);
}

// Обработать эффект
this.compile_effect = function ()
{
	if (this.block) {
		this.set_interval();
		return false;
	}
	else
		this.block = true; // Залочили
	
    var effect_array = this.effect_array;
    var effect_array_length = effect_array.length;
    
    if (effect_array_length!=0) {
        var effect_array_result= new Array();
        for(var i=0; i<effect_array_length;i++)
        {
            var effect_row = effect_array[i];
            
            effect_row['param']['current'] = (effect_row['param']['current']) + (effect_row['param']['step'] || 1);
     
            if (effect_row['param']['current']>effect_row['param']['end']) {
                effect_row['param']['current'] = effect_row['param']['end'];
            }
            // Применить эффект
            this.make_effect(effect_row['object'], effect_row['param']);
            // Если не закончен, то сохранить в таблице
            if (effect_row['param']['current']<effect_row['param']['end'])
                effect_array_result[effect_array_result.length] =  effect_row;
        }
        if (effect_array_result.length!=0)
            this.set_interval();
    }
    this.effect_array = effect_array_result;
	this.block = false;
}

// 
this.make_effect = function(object, effect_param)
{
    switch(effect_param.type) {
        case "alpha":
                this.effect_set_visibility(object, effect_param.current, effect_param.special);
            break;
        case "size":
                this.effect_set_size(object, effect_param.current, effect_param.special);
            break;
    }
}

// Выставить видимость объекту
this.effect_set_size = function (object, value, special)
{
	special = special || "height";
			
	if (value>100) 
        value = 100;
    else if(value<0) 
        value = 0;
      
	if(object.style.overflow!='hidden')
		object.style.overflow='hidden';
		
	if(value!=0 && object.style.display=='none') {
		object.style.visibility = 'hidden';
		object.style.display = 'block';
		var make_visible = true;
	}
      
    if(special=='height')
     	var cur_value =  object.scrollHeight; 
    else
     	var cur_value =  object.scrollWidth; 
     		
	cur_value = (cur_value/100)*value;
	
	if(special=='height')
    	object.style.height = cur_value+"px";
    else
    	object.style.width = cur_value+"px";
    	
    if(make_visible)
    	object.style.visibility = 'visible';
    if(value==0) {
		object.style.display = 'none';
	}
}

// Выставить видимость объекту
this.effect_set_visibility = function (object, value, special)
{
	special = special || "";
	
    if (value>100) 
        value = 100;
    else if(value<0) 
        value = 0;
       
           
    if (value == 0 && special) {
		if(special=="visibility")
			object.style.visibility = "hidden";
		if(special=="display")	
			object.style.display = "none";
    }
    else if (special) {
		if(special=="visibility") {
			if(object.style.visibility == "hidden")
				object.style.visibility = "visible";
		}
		if(special=="display") {
			if(object.style.display == "none")
				object.style.display = "";
		}
    }
    
    object.style.opacity = value/100; 
    object.style.filter = 'alpha(opacity=' + value + ')';
}

}
var effect = new effect_class();
