/**
 * The cartografur js library.
 * 
 * Copyright 2009 cartografur.
 * Created by Jeff Verkoeyen.
 * jverkoey@gmail.com
 * twitter.com/featherless
 */
Fur.MapViewer=function(elementIDs){this._elementIDs=elementIDs;this._elements={};for(var key in this._elementIDs){if(key!='canvas'){this._elements[key]=$(this._elementIDs[key]);}else{this._elements[key]=$('#'+this._elementIDs[key]);}}
this._hover_info=$('#floating-info');if(this._hover_info.length==0){this._elements.canvas.after('<div id="floating-info" style="display: none"></div>');this._hover_info=$('#floating-info');}
this._map_list=null;$(document).unbind('mousemove').mousemove(function(event){var feature=this._hovered_feature;if(feature&&feature.info.name){this._hover_info.html(feature.info.name);this._hover_info.show();this._hover_info.css({left:event.clientX+5,top:event.clientY+5});}else{this._hover_info.hide();}}.bind(this));this.init();this._filter_timer=setTimeout(this._filter_tick.bind(this),this.tick);this._dead=false;};Fur.MapViewer.prototype={min_zoom:14,tick:1000*60,init:function(){this._map=null;this._format=null;this._map_list=null;this._map_cache={};this._selected_feature=null;this._hovered_feature=null;this._elements.map_list.empty();this._elements.selection.hide();},reset:function(){if(this._map){this._map.destroy();}
this._elements.canvas.empty();this.init();},initWithLatLon:function(latlon){var width=0.001;var bounds=new OpenLayers.Bounds(latlon.lon-width,latlon.lat-width,latlon.lon+width,latlon.lat+width);this.initWithBounds(bounds);},initWithBounds:function(bounds){this.reset();this._bounds=bounds.clone();this._bounds.transform(Fur.OpenLayersMap.display_projection,Fur.OpenLayersMap.data_projection);this.create_map();this.request_map_list();this.check_map_visibility();},create_map:function(){var options=Fur.OpenLayersMap.createGoogleMapOptions();options.numZoomLevels=30;options.maxResolution=206543.0339;this._map=new OpenLayers.Map(this._elementIDs.canvas,options,{'buffer':4});var gstreets=new OpenLayers.Layer.Google("Streets",{sphericalMercator:true});var gsat=new OpenLayers.Layer.Google("Satellite",{type:G_SATELLITE_MAP,sphericalMercator:true});this._map.addLayers([gstreets,gsat]);this._map.zoomToExtent(this._bounds,true);this._map.events.on({moveend:this.request_map_list.bind(this),zoomend:this.check_map_visibility.bind(this)});this._map.addControl(new OpenLayers.Control.LayerSwitcher());this._selectFeature=null;var out_options={'internalProjection':options.projection,'externalProjection':options.displayProjection};this._format=new OpenLayers.Format.WKT(out_options);this._typeahead=new Fur.Control.TypeAhead(this._elementIDs.type_ahead,this._elementIDs.dropdown,this._map,this);},request_map_list:function(){if(this._dead){return;}
if(this._map.getZoom()>=this.min_zoom){var bounds=this._map.getExtent();bounds.transform(Fur.OpenLayersMap.data_projection,Fur.OpenLayersMap.display_projection);$.ajax({type:'GET',url:'/maps/withinbounds',dataType:'json',data:{bounds:bounds},success:this._map_list_received.bind(this)});}},load:function(id){if(this._map_cache[id]==undefined){$.ajax({type:'GET',url:'/maps/load',dataType:'json',data:{id:id},success:this.map_loaded.bind(this)});this._map_cache[id]=null;}},_map_list_received:function(result,textStatus){if(this._dead){return;}
if(result.succeeded){if(null!=this._map_list&&this._map_list.length==result.maps.length){var map_hash={};for(var i=0;i<this._map_list.length;++i){map_hash[this._map_list[i]]=true;}
var any_difference=false;for(var i=0;i<result.maps.length;++i){if(map_hash[result.maps[i].id]==undefined){any_difference=true;break;}
delete map_hash[result.maps[i].id];}
for(var key in map_hash){any_difference=true;break;}
if(!any_difference){return;}}
this._typeahead.new_map_list(result.maps);this._map_list=[];this._elements.map_list.empty();var html=[];html.push('<ul>');for(var i=0;i<result.maps.length;++i){var id=result.maps[i].id;this._map_list.push(id);html.push('<li class="',id,'">');if(this._map_cache[id]==undefined){html.push('Loading map...');}else{html.push(this._map_cache[id].getName());this._typeahead.load_map(this._map_cache[id]);}
html.push('</li>');this.load(id);}
html.push('</ul>');this._elements.map_list.html(html.join(''));if(result.maps.length){this._elements.no_maps.hide();this._elements.map_list_parent.show();}else{this._elements.map_list_parent.hide();this._elements.no_maps.show();}
var t=this;$(this._elementIDs.map_list+' li').click(function(){var id=$(this).attr('class');if(t._map_cache[id]!=undefined){var map=t._map_cache[id];t._map.panTo(map._bounds.getCenterLonLat());}});}},_update_info:function(){if(this._hovered_feature||this._selected_feature){var feature=this._selected_feature||this._hovered_feature;var html=[];if(feature.info.name){html.push('<div class="name">',feature.info.name,'</div>');}
if(feature.info.phone){html.push('<div class="phone">',Fur.StringSanitization.phone_number(feature.info.phone),'</div>');}
if(feature.info.hours!=null){html.push('<div class="hours">',Fur.StoreHours.render_short_hours(feature.info.hours),'</div>');}
this._elements.selection_info.empty();if(html.length){this._elements.selection_info.html(html.join(''));}else{this._elements.selection_info.html('Your selection doesn\'t seem to have any useful information.');}
this._elements.selection.show();}else{this._elements.selection.hide();}},selectfeature:function(feature){this._selectFeature.clickFeature(feature);this._map.panTo(feature.geometry.getBounds().getCenterLonLat());},featureselected:function(event){switch(event.event_type){case'click':this._selected_feature=event.feature;break;case'hover':this._hovered_feature=event.feature;break;}
this._update_info();},featureunselected:function(event){switch(event.event_type){case'click':if(event.feature==this._selected_feature){this._selected_feature=null;}
case'hover':if(event.feature==this._hovered_feature){this._hovered_feature=null;}
break;}
this._update_info();},check_map_visibility:function(event){if(this._dead){return;}
var show=this._map.getZoom()>=this.min_zoom;for(var key in this._map_cache){var map=this._map_cache[key];if(map){map.getLayer().setVisibility(show);}}
if(show){this._elements.map_info.show();this._elements.cloud_view.hide();}else{this._elements.map_info.hide();this._elements.cloud_view.show();}},_filter_tick:function(){for(var key in this._map_cache){var map=this._map_cache[key];if(map){this._update_filters(map);map.getLayer().redraw();}}
this._filter_timer=setTimeout(this._filter_tick.bind(this),this.tick);},_update_filters:function(map){var date=new Date();var hour=date.getHours();var minute=date.getMinutes();var ampm;if(hour<12){ampm='am';}else{hour-=12;ampm='pm';}
var day_of_the_week=date.getDay();var military=Fur.StringSanitization.ampm_to_military(hour,minute,ampm);var filtered=map.filterByHours(day_of_the_week,military);for(var i=0;i<filtered.closed.length;++i){var feature=filtered.closed[i];feature.state=Fur.CartografurMap.CLOSED;}
for(var i=0;i<filtered.open.length;++i){var feature=filtered.open[i];feature.state=Fur.CartografurMap.OPEN;}},map_loaded:function(result,textStatus){if(this._dead){return;}
if(result.succeeded){var data=result.map_data;var map=new Fur.CartografurMap(data.id,data.location,new OpenLayers.Bounds(data.left,data.bottom,data.right,data.top),result.poly_data,this._format);this._map_cache[data.id]=map;this._typeahead.load_map(map);var listElm=$(this._elementIDs.map_list+' .'+data.id);if(listElm){listElm.html(map.getName());}
this._update_filters(map);this._map.addLayer(map.getLayer());map.getLayer().events.on({featureselected:this.featureselected.bind(this),featureunselected:this.featureunselected.bind(this)});if(this._selectFeature==null){this._selectFeature=new OpenLayers.Control.SelectFeature(map.getLayer(),{multiple:false,hover:false,hover_and_pick:true});this._map.addControl(this._selectFeature);this._selectFeature.activate();}else{this._selectFeature.addLayer(map.getLayer());}}}};