js.module('ajb');

ajb = 
{
	last_clicked : false,
	down : function(e)
	{
		if(!$(e)) return;
		if(!$A(e, 'abtype')) return;

		if(!ajb.canClick(e)) return;

		visual.setClass(e, 'ab_'+$A(e, 'abtype')+'d');
	}
	,
	up : function(e)
	{
		if(!$(e)) return;
		if(!$A(e, 'abtype')) return;

		if(!ajb.canClick(e)) return;

		visual.setClass(e, 'ab_'+$A(e, 'abtype'));
	}
	,
	click : function(e)
	{
		if(!$(e)) return;
		if(!ajb.canClick(e)) return;

		this.last_clicked = $A(e, 'id');

		var exjs = ajb.canExecJs(e); 
		
		//This will stop fading and blinking
		$(e).fade_count=20;
		$(e).blink_count=20;
		
		if(ajb.canDisable(e))
			ajb.disable(e);

		if(exjs)
		{
			$(e).f = new Function($A(e, 'clickact'));
			$(e).f();
		}
		
		if($A(e, 'is_form'))
		{
			for(var n in document.forms)
			{
				if(document.forms[n].name=='form_'+$A(e, 'id'))
				{
					document.forms[n].submit();
					break;
				}
			}
		}
	}
	,
	disable : function(e)
	{
		if(!$(e)) return;

		$A(e, 'canclick', 'false');
		if($A(e, 'abtype'))
			visual.setClass(e, 'ab_dis');
	}
	,
	enable : function(e)
	{
		if(e==undefined)
			e=this.last_clicked;

		if(!$(e)) return;
		
		$A(e, 'canclick', 'true');
		if($A(e, 'abtype'))
			visual.setClass(e, 'ab_'+$A(e, 'abtype'));
	}
	,
	fade : function(e)
	{
		if(!$(e)) return;
		
		if($(e).in_fade) return;
		
		$(e).fade_count = 0;
		$(e).fade_dir = false;
		$(e).fade = new Fx.Morph(e, { duration:250 }).set({ 'opacity' : 0.5 });
		$(e).fade_to = visual.getClass(e);
		visual.setClass(e,'ab_yellow');
		
		$(e).fade_func = function ()
		{
			if(this.fade_count<5)
			{
				this.fade.start( { 'opacity' : this.fade_dir?0.5:1 } ).chain( function() { $(e).fade_func(); } );
				this.fade_count++;
				this.fade_dir = !this.fade_dir;
			}
			else
			{
				this.in_fade=false;
				this.fade.set( { 'opacity' : 1 } );
				visual.setClass(e,$(e).fade_to);
			}
		}
		
		$(e).in_fade = true;
		$(e).fade_func();
	}
	,
	blink : function(e, to)
	{
		if($(e).blink_interval) return;
	
		$(e).blink_func = function()
		{
			if(this.blink_count>7)
			{
				clearInterval(this.blink_interval);
				this.blink_interval = false;
				this.blink_count = 0;
				visual.setClass(this, this.blink_orig_class);
				return;
			}

			visual.setClass(this, visual.getClass(this)==this.blink_orig_class?'ab_yellow':this.blink_orig_class);

			$(e).blink_count++;
		}
		
		$(e).blink_count = 0;
		$(e).blink_orig_class = visual.getClass(e);
		$(e).blink_interval = setInterval(function() { $(e).blink_func() }, 150);
	}
	,
	setText : function(e, text)
	{
		if(!$(e)) return;
		
		var children = $(e).getElementsByTagName('span');
		if(children.length > 1) return;
		
		$$(children[0], text);
	}
	,
	setClickAct : function(e, act)
	{
		if(!$(e)) return;
		
		$A(e,'clickact', act);
		
		if(act)
		{
			$A(e, 'canclick', 'true');
			ajb.setType(e, 'nor');
		}
		else
		{
			$A(e, 'canclick', 'false');
			ajb.setType(e, 'dis');
		}
	}
	,
	setOldType : function(e, type)
	{
		if(!$(e)) return;
		
		$A(e, 'old_abtype', type);
	}
	,
	setActive : function(e)
	{
		if(!$(e)) return;
		
		if($A(e, 'ab_active')!='true')
			ajb.setOldType(e, $A(e,'abtype'))
		
		$A(e, 'ab_active', 'true');
		
		ajb.setType(e, 'act');
	}
	,
	setNotActive : function(e)
	{
		if(!$(e)) return;
		if(!$A(e, 'old_abtype')) return;
		
		ajb.setType(e, $A(e, 'old_abtype'));
	}
	,
	toggleActive : function(e, on)
	{
		if(!$(e)) return;
		
		if(on)
			ajb.setActive(e);
		else
			ajb.setNotActive(e);
	}
	,
	setType : function(e, type)
	{
		if(!$(e)) return;

		visual.setClass(e, 'ab_'+type);
		$A(e, 'abtype', type);
	}
	,
	canClick : function(e)
	{
		if(!$(e)) return false;

		return $A(e, 'is_form') || ($A(e, 'clickact') && ($A(e, 'canclick')=='true' || $A(e, 'canclick')=='1' || $A(e, 'canclick')==null || $A(e, 'canclick')==undefined));
	}
	,
	canExecJs : function(e)
	{
		if(!$(e)) return false;

		return $A(e, 'clickact') && ($A(e, 'canclick')=='true' || $A(e, 'canclick')=='1' || $A(e, 'canclick')==null || $A(e, 'canclick')==undefined);
	}
	,
	canDisable : function(e)
	{
		if(!$(e)) return false;

		return $A(e, 'clickdis')=='true'  || $A(e, 'clickdis')=='1';
	}
}