/*

The script makes a slider icon slide over a series of elements when they are moused-over.

Attributes:
The element over which the slider should slide must have the custom attribute hlslider 
with 3 space-delimited values.  The first value is the id of the slider that should
slide over the moused-over elements.  The second two values are adjustments to the X
and Y positioning of the slider respectively for that element.
Optionally, the slider itself can have the hloffsets custom attribute set, which takes
two space-delimited values.  They are respectively X and Y global offsets that are
applied in addition to the previously described offsets.

requires Prototype 1.6+
*/

var HighlightSlider = {
	init: function() {
		
		var oThis = this;
		Element.descendants(document.body).each( function(elem) {
			if(elem.readAttribute('hlslider') != null) {

				var configs = elem.readAttribute('hlslider').split(" ");
				elem.hls = $(configs[0]);
				elem.hlxoffset = parseInt(configs[1]);
				elem.hlyoffset = parseInt(configs[2]);
				elem.hlfreeze = configs[3];
				
				Event.observe(elem,'mouseover', oThis.handleMOver.bind(elem));
				Event.observe(elem,'mouseout', oThis.handleMOut.bind(elem));
				Event.observe(elem,'click', oThis.click.bind(elem));
				
				if(!elem.hls.moveToPos) {


					Object.extend(elem.hls,oThis.sliderMethods);
					elem.hls.init();
									
				}

			}
		})
		
	},
	handleMOver: function() {
		this.hls.moveToPos(this.offsetLeft + this.hlxoffset,  this.offsetTop + this.hlyoffset);
	},
	handleMOut: function() { 
		this.hls.moveToPos(this.hls.baseLeft - this.hls.initLeft, this.hls.baseTop - this.hls.initTop);
	},
	click: function() {
				
		if(this.hlfreeze === "false")
			return;
				
		this.hls.clicked = true;
	},
	sliderMethods: {
		init: function() {
			var offsets_raw = this.readAttribute('hlsoffets');
			
			if(offsets_raw) {
				var offsets = offsets_raw.split(" ");
				this.initLeft = parseInt(offsets[0]);
				this.initTop = parseInt(offsets[1]);
			} else {
				this.initLeft = 0;
				this.initTop = 0;
			}
		
			this.baseTop = this.offsetTop;
			this.baseLeft = this.offsetLeft;
		},
		moveToPos: function(xdest,ydest) {
			
			if(this.clicked)
				return;
			
			/*
			this.style.left = xdest + this.initLeft + 'px';
			this.style.top = ydest + this.initTop + 'px';
			
			return;
			*/

			//if(this.baseTop === undefined)
			//	this.makeDef(xdest,ydest);

			if(this.PE)
				this.PE.stop();

			this.AX = this.offsetLeft;
			this.AY = this.offsetTop;
			
			this.BX = xdest + this.initLeft;
			this.BY = ydest + this.initTop;
			
			this.DX = this.BX - this.AX;
			this.DY = this.BY - this.AY;
			
			this.step = 0;
			
			this.PE = new PeriodicalExecuter(this.dostep.bind(this),0.05);
		},
		dostep: function() {

			this.style.left = this.AX + (this.step / this.steps) * this.DX + 'px';
			this.style.top = this.AY + (this.step / this.steps) * this.DY + 'px';
		
			if(this.step >= this.steps)
				this.PE.stop()
			
			this.step++;
			
		},
		steps: 5
	}
	
}


Event.observe(window,'load',function() {

	HighlightSlider.init();

});
