/* generates a email form */
var OverlaidLocation =
{
	archetype:	null,
	button_style:'overlaid-location',
	id: 'div_overlaid_location',
	pe:null
	
};

/* extend */
Object.extend(OverlaidLocation,Overlaid);
// overwrite the container style
OverlaidLocation.container_style='';

OverlaidLocation.show=function()
{
	// positioning
	this.position();
	
	// cancel button
	$$('#'+$(this.id).identify()+' .boxy-cancel').each(function(item) {
		Event.observe(item,"click",this.hide.bind(this));
	}.bind(this));
	
	// use google for long/lat
	LongLatGUI.init($('set_location'), {});
	LongLatGUI.addListener(this);
	
	LongLatGUI.select=function()
	{
		if(!this.suggestions)
			return;
		
		var selected = this.shell.down('.'+this.selected_class);
		var i = (selected) ? selected.id.replace(this.item_class,'') : 0;
		
		var location = this.suggestions[i];

		var longitude = this.longitude(location);
		var latitude = this.latitude(location);
		$('location_value').update(this.address(location));
		
		$$(".distance").each(function(item){
			
			var href = item.readAttribute("href");
			var regex = /location=.*&long=-?\d{1,3}\.\d+&lat=-?\d{1,3}\.\d+/;
			var replacement = "location="+$('location_value').innerHTML+"&long="+longitude+"&lat="+latitude;
			
			item.setAttribute("href",(regex.test(href))?href.replace(regex,replacement):href+"&"+replacement);
		});
		
		// if distance is already selected, 
		// reload the page w/ the new zip code search
		$('location_value').up().siblings().each(function(item){
			var down = item.down("a");
			if(!down)
				return;
			var span = item.down("span");
			down.show();
			span.hide();
			if(down.hasClassName('on'))
				document.location = down.href;
		});
		
		// all done
		this.hide();
		this.broadcastMessage('hide');
		this.removeListeners();
		this.destroy();
	};
	
	$('set_location').focus();

};

OverlaidLocation.hide=function()
{
	if ($(this.id)) $(this.id).remove();
	Overlay.hide();
	Overlay.removeListener(this);
	
	// handle the auto complete
	LongLatGUI.removeListener(this);
	LongLatGUI.destroy();
};


OverlaidLocation.update_location=function(longitude, latitude)
{
	$$(".distance").each(function(item){
		var href = item.readAttribute("href");
		var regex = /long=[-]\d{1,3}.\d+&lat=[-]\d{1,3}.\d+/;
		var replacement = "long="+longitude+"&lat="+latitude;
		item.setAttribute("href",(regex.test(href))?href.replace(regex,replacement):href+"&"+replacement);
	});
};

OverlaidLocation.position=function()
{	
	// positioning
	var offset = $(this.button).cumulativeOffset();
	var top=(offset.top-60);
	var left=(offset.left+60);
	
	$(this.id).setStyle({zIndex:Overlay.zIndex+1,position:'absolute',top:top+"px",left:left+"px",visibility:'visible'});
};

OverlaidLocation.process=function(item)
{
	Event.observe(item,"click",function(){
				
		Overlay.show(this);
		
		// the button that was clicked
		this.button = item;
		
		this.archetype=$(item.readAttribute('rel'));

		new Ajax.Request(this.archetype.action+"/"+this.id,{
			method:this.archetype.method,
			parameters:this.archetype.serialize(),
			onComplete:this.show.bind(this)
		});
	}.bind(this));
};
/*
*/
Event.observe(window,"load",function()
{
	InializeAsBroadcaster(LongLatGUI);
	$$('.'+OverlaidLocation.button_style).each(OverlaidLocation.process.bind(OverlaidLocation));	
});