var MapAnimator = Class.create();
MapAnimator.prototype = {
	initialize: function(p_aOptions) {
	    this.m_sImgMapId = Object.isString(p_aOptions.ImgMapId) ? p_aOptions.ImgMapId : 'Map';
	    this.m_sImgId = Object.isString(p_aOptions.StateClass) ? p_aOptions.StateClass : 'stateStar';
	    this.m_sMapContainer = Object.isString(p_aOptions.MapContainer) ? p_aOptions.MapContainer : 'dealerLocatorCnt';
	    this.m_aMapItems = $(this.m_sImgMapId).childElements();
	    this.m_eImg = $(this.m_sImgId);
	    this.m_sMapContainerEm = $(this.m_sMapContainer);
	    this.m_sMapContainerOffset = this.m_sMapContainerEm.cumulativeOffset();
	    this.m_sStateBox = Object.isString(p_aOptions.StateBox) ? p_aOptions.StateBox : 'dealerLocState';
	    this.m_eStateBox = $(this.m_sStateBox);
	    this.m_sStateRowId = Object.isString(p_aOptions.StateRowId) ? p_aOptions.StateRowId : 'stateDivRow';
	    this.animating = false;
	    this.selectedState = "";
	    
	   var i;
		for(i=0;i<this.m_aMapItems.length;i++){
			Event.observe( this.m_aMapItems[i], 'click', this._onMapStateClick.bindAsEventListener( this, this.m_aMapItems[i] ) );
		}
		
	},
	
	_onMapStateClick : function(p_event, mapItem){
		 p_event.stop();
		 if(this.animating) return false;
		 this.m_eImg.setStyle({display: 'none'});
		 this.selectedState = mapItem.readAttribute('href');
		 new Effect.Move(this.m_eImg, {
		 								beforeStart: function() {this.animating = true;}.bind(this),
		 								duration: .01, 
		 								x: p_event.pointerX()-this.m_sMapContainerOffset[0]-25, 
		 								y: p_event.pointerY()-this.m_sMapContainerOffset[1]-25, 
		 								mode: 'absolute',
										afterFinish: function(){
																new Effect.Grow(this.m_eImg, {
																			duration:.01,
																			afterFinish: function(){
																									this._sendToState(p_event);
																									this._matchId(this.m_eStateBox, this.selectedState);
																									}.bind(this)
																			
																		});
																}.bind(this)
									  });
	},
	
	_sendToState : function(p_event){
		var fxArray = new Array();
		var setDur = 1.5;
		var stateOffset = this.m_eStateBox.cumulativeOffset();
		
		fxArray[0] = new Effect.Puff(this.m_eImg, {sync: true});
		fxArray[1] = new Effect.Move(this.m_eImg, {sync: true, x: stateOffset[0]-this.m_sMapContainerOffset[0], y: stateOffset[1]-this.m_sMapContainerOffset[1], mode: 'absolute'});
		
		new Effect.Parallel(fxArray,{duration: setDur, 
									 afterFinish: function(){
															new Effect.Highlight($(this.m_sStateRowId), {endcolor: '#99CCFF', startcolor: '#003366',
																										 afterFinish: function(){
																										 				this.animating = false;
																										 			  }.bind(this)});
															}.bind(this)});
	},
	
	_matchId : function(em, val){
		var i;
		for(i=0;i<em.options.length;i++){
			if(em[i].value == val){
				em[i].selected = true;
			}
		}
	}
};