// JavaScript Document

(function($){
    $.fn.glow = function(options) {
        var defaults = {
			duration : 500,
			glowColor : '#FFF',
			dimColor  : '#AAA',
			activeClass : 'active',
			glowWidth   : 2,
			glowGrad    : 20,
			fine        : 10
			
        };
        var o = $.extend(defaults, options);
		var p = $(this),t=o.duration,sh=o.glowWidth,shgrad=o.glowGrad,steps=o.fine,active,col_1=o.glowColor,col_2=o.dimColor;
		return p.each(function(){
		var l=0,idg,idd,mouseleft;
		var x = this;
		$(x).mouseover(mover)
		$(x).mouseout(mout)
		$(x).click(function(){
			if($(this).hasClass('active')) return;
			p.each(function()
			{
				if($(this).hasClass('active'))
				 $(this).toggleClass('active').mouseout();
			})
			$(x).toggleClass('active');
		})
		
		function mover(){
			mouseleft=false;
			clearInterval(idg);
			idg=glow(this);
		}
		function mout() {
			mouseleft=true;
			if(l>t/3)
			{
				clearInterval(idd);
				idd=dim();
			}
		}
		
		function glow(){
			if(l<0) l =0;
			return window.setInterval(function(){
			$(x).css({"text-shadow" : 
			""+ l*sh/t +"px 0px "+ l*shgrad/t +"px "+col_1+
			",-"+ l*sh/t +"px 0px "+ l*shgrad/t +"px  "+col_1+
			",0px "+ l*sh/t +"px "+ l*shgrad/t +"px  "+col_1+
			",0px -"+ l*sh/t +"px "+ l*shgrad/t +"px  "+col_1,
				"color" : '#FFF'});
			l+=t/steps;
			if(mouseleft&&l>t/2){ 
				window.clearInterval(idg);
				window.clearInterval(idd);
				idd = dim(); }
			if(l>=t){
				window.clearInterval(idg);
			}
			},t/steps);
		}
		
		function dim(){
			if(!$(x).hasClass('active'))
			{
			return window.setInterval(function()
			{
				l-=t/steps;
					$(x).css({"text-shadow" : 
					""+ l*sh/t +"px 0px "+ l*shgrad/t +"px  "+col_2+
					",-"+ l*sh/t +"px 0px "+ l*shgrad/t +"px  "+col_2+
					",0px "+ l*sh/t +"px "+ l*shgrad/t +"px  "+col_2+
					",0px -"+ l*sh/t +"px "+ l*shgrad/t +"px  "+col_2,
					"color" : '#AAA'
					});
				if(l<=0||!mouseleft)
				{
					$(x).css({"color" : "#AAA"});
					window.clearInterval(idd);
				}
			},t/steps);
			
			}
		}
		})
		
	}
})(jQuery);
