// JavaScript Document
HW.onload(function(){HW.SavingsTool.init();});

HW.SavingsTool = {
	
	container: null,
	
	setDropdowns: function(){
		var paramString = window.location.search	
		if(paramString){
			var inputs = _$$('select', this.container);
			for(var i = 1; i <= inputs.length; i++){
				try{
					var regExp = new RegExp('ipt0'+i+'=.{1}');
					//HW.log(paramString.match(regExp)[0].split('=')[1]);
					var index = parseInt(paramString.match(regExp)[0].split('=')[1]);
					HW.log(index);
					inputs[i-1].selectedIndex = index;
				}catch(e){
					throw new Error('Failed to set selectedIndex');	
				}
			}
		}
	},
	
	init: function(){
		var obj = this;
		this.container = $('SA_FilterModule');
		var hints = _$$('.SA_iptInfo', this.container);
		hints.each(function(hint){hint.style.display = 'none';});
		var labels = _$$('label img', this.container);
		
		this.setDropdowns();

		function deactivate(index){
			var iframe = labels[index].parentNode.getElementsByTagName('iframe')[0]								  
			if(iframe) labels[index].parentNode.removeChild(iframe);								  
			hints[index].style.display = 'none';			
		}
		
		function deactivateDelay(index){
					labels[index].timer = setTimeout(function(){
							deactivate(index);
						}, 200);				
		}

		function activate(index){	
			for(var x = 0; x < hints.length; x++){
				if(hints[index] != hints[x]) deactivate(x);
			}		
			if(hints[index].style.display == 'none'){
				hints[index].style.display = 'block';						
				var offsetWindowTop = 0;
				var offsetWindowLeft = 0;
				var elem = labels[index];
				while(elem != document.getElementsByTagName('body')[0]){
					offsetWindowTop+=elem.offsetTop;
					offsetWindowLeft+=elem.offsetLeft;
					elem = elem.offsetParent;
				}						
				hints[index].setStyle({top: (offsetWindowTop - hints[index].offsetHeight)+'px', left: (offsetWindowLeft - 10)+'px'});	
				//hack for IE6. Iframe between tool tip and form element
				labels[index].parentNode.appendChild(
						HW.setStyle(document.createElement('iframe'), {
						top: (offsetWindowTop - hints[index].offsetHeight + 1)+'px', 
						left: (offsetWindowLeft - 10)+'px',
						height: (hints[index].offsetHeight-13)+'px',
						width: (hints[index].offsetWidth)+'px',
						border: 'none',
						position: 'absolute',
						zIndex: '1'
					}))
			}
		}	
		function activateDelay(index){
			clearTimeout(labels[index].timer);
			labels[index].timer2 = setTimeout(function(){activate(index);}, 500);
		}
		for(var i = 0; i < labels.length; i++){
			(function(){
				var index = i;
				labels[index].bind('click', function(){activate(index)}, false);
				labels[index].bind('mouseover', function(){
						activateDelay(index);
					}, false);

				labels[index].bind('mouseout', function(){
						deactivateDelay(index);
						clearTimeout(labels[index].timer2);
					}, false);
				hints[index].bind('mouseover', function(){
						if(labels[index].timer){
							clearTimeout(labels[index].timer);
						}
					}, false);
				hints[index].bind('mouseout', function(){
						deactivateDelay(index);												   
					}, false);
			})();
		};
	}
}
