// HomeFinder Script Document

//Defines the buttons fournd in homefinder
var HF_buttons = new Class({
	
	options: {
		btnOver				: 'ccc',	
		btnNorm				: 'fff',
		btnClicked		: '333',
		btnCantUse		: '000',		
		btnClassId		: false,		
		id						: false,
		onClick				: function(eventType, element){ 										//fired when click is pushed
											var value = element.getText();											//get value of button
											value = value.trim();	
											HomeFinder.update(element.id,eventType,value);	//pass button - select/deslect - value
										},			
		onRollOut			: Class.create()
	},
//----------------------------------------------------------------------------		
	initialize: function(initVal) {
		this.setOptions(this.options, initVal);		
		this.buttons = $$(this.options.btnClassId);		
		this.setUpButtons();
	},
//----------------------------------------------------------------------------		
	select: function(element){
		element.addClass('selected');					
		element.removeEvents('mouseout');					
		element.removeEvents('mouseover');
		element.effect.start({'background-color': element.clickedState, 'color': 'ffffff'});
		this.fireEvent('onClick',['select',element]);    //fire onClick event -> defined above
	},
//----------------------------------------------------------------------------
	deselect: function(element){
		element.removeClass('selected');			//remove class clickedState leave button in rolloverstate.  
		element.effect.start({'background-color': element.overState, 'color': '000000' });		//return to over state
		
		element.addEvent('mouseover', function(){ 				//add overstate and mouseout
			element.effect.start({'background-color': element.overState, 'color':'000000'});
		}.bind(this));
		
		element.addEvent('mouseout', function(){ 
			element.effect.start({	'background-color': element.normalState, 'color': '000000'});  
		}.bind(this));
		
		this.fireEvent('onClick',['deselect',element]);    //fire onClick event -> defined above
	},
//----------------------------------------------------------------------------
	deactivate: function(element){
		if(element.hasClass('active')){
			element.removeClass('acitve');
		}
		if(element.hasClass('selected')){
			element.removeClass('selected');
		}
		element.addClass('deactivated');
		element.removeEvents();	
		element.effect.start({'background-color': element.normalState, 'color' : '000000', 'opacity' : .2});	
	},
//----------------------------------------------------------------------------
	activate: function(element){
		if(element.hasClass('deactivated')){
			element.removeClass('deactivated');
		}
		element.addClass('active');
		
		this.addMouseEvents(element);
					//remove class clickedState leave button in rolloverstate.  
		element.effect.start({ 'background-color' : element.normalState, 'color' : '000000', 'opacity' : 1});		//return to over state
	
	},
//----------------------------------------------------------------------------
	addMouseEvents: function(element){
		element.addEvent('mouseover', function(){ 
			element.effect.start({	'background-color': element.overState, 'color': '000000'});
		}.bind(this));
		
		element.addEvent('mouseout', function(){ 
			element.effect.start({	'background-color': element.normalState, 'color': '000000'}); 
		}.bind(this));
		
		element.addEvent('click', function(){ 														 
			if (!element.hasClass('selected')) { this.select(element); }		//if it isn't selected, then select
			else { this.deselect(element); }														//elese deselect it																				 
		}.bind(this));
	},
//----------------------------------------------------------------------------		
	setUpButtons: function(){
		this.buttons.each(function(button, i){
			//alert(this.options.id);
			button.id = this.options.id;
			button.overState = this.options.btnOver;
			button.clickedState = this.options.btnClicked;
			button.deactivate = this.options.btnCantUse;
			button.normalState = this.options.btnNorm;
			button.effect = new Fx.Styles(button, {wait:false, duration: 300});
			this.addMouseEvents(button);
		}.bind(this));
	},
	//----------------------------------------------------------------------------		
	update: function(array){
		this.buttons.each(function(el, i){
			if((array[i] == 0) && (!el.hasClass('deactivated'))){		//make sure it isnt already inactive
				this.deactivate(el);
			}
			if((array[i] == 1) && (el.hasClass('deactivated'))){
				this.activate(el);
			}
		}.bind(this));	
	}
//----------------------------------------------------------------------------	
});
HF_buttons.implement(new Events);
HF_buttons.implement(new Options);
