/**
 * The cartografur js library.
 * 
 * Copyright 2009 cartografur.
 * Created by Jeff Verkoeyen.
 * jverkoey@gmail.com
 * twitter.com/featherless
 */
Fur.OpenLayersMap={data_projection:new OpenLayers.Projection("EPSG:900913"),display_projection:new OpenLayers.Projection("EPSG:4326"),createGoogleMapOptions:function(){return{projection:Fur.OpenLayersMap.data_projection,displayProjection:Fur.OpenLayersMap.display_projection,units:"m",numZoomLevels:18,maxResolution:156543.0339,maxExtent:new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508.34)};}};Fur.StoreHours={days:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],render_short_hours:function(hours){if(hours==null){return'No hours listed.';}else if(hours==''){return'Open 24/7';}else{return Fur.StoreHours.render_hours(Fur.StoreHours.parse_hours_data(hours)).join('');}},render_hours:function(schedule){var html=[];html.push('<table><tbody>');for(var i=0;i<7;++i){html.push('<tr><th>',Fur.StoreHours.days[i],'</th><td>');if(schedule[i]==null){html.push('<span>Open</span>');}else if(schedule[i].length==0){html.push('<span>Closed</span>');}else{for(var i2=0;i2<schedule[i].length;++i2){html.push('<span>');html.push(Fur.StringSanitization.military_to_ampm(schedule[i][i2].start));if(schedule[i][i2].start!=schedule[i][i2].end){html.push('-');html.push(Fur.StringSanitization.military_to_ampm(schedule[i][i2].end));}
html.push('</span>');if(i2<schedule[i].length-1){html.push(', ');}}}
html.push('</td></tr>');}
html.push('</tbody></table>');return html;},parse_hours_data:function(hours){if(hours==null){return[[],[],[],[],[],[],[]];}
hours=hours||'';var short_form_to_index={s:0,m:1,t:2,w:3,th:4,f:5,sa:6};var schedule=[null,null,null,null,null,null,null];function grab_day(i){var day=null;if(i<hours.length-1){day=hours.substr(i,2);if(typeof short_form_to_index[day]=='undefined'){day=null;}}
if(!day){day=hours[i];}
if(typeof short_form_to_index[day]=='undefined'){return-1;}
return day;}
var i=0;while(i<hours.length){var day=grab_day(i);if(day==-1){break;}
var start_index=short_form_to_index[day];var end_index=start_index;i+=day.length;if(i>=hours.length){schedule[start_index]=[];break;}
if(hours[i]=='-'){i++;if(i>=hours.length)break;var next_day=grab_day(i);if(next_day==-1){break;}
end_index=short_form_to_index[next_day];i+=next_day.length;if(i>=hours.length){for(var day_index=start_index;day_index<=end_index;++day_index){schedule[day_index]=[];}
break;}}
if(end_index<start_index){break;}
do{if(/[0-9]/.test(hours[i])){var hyphen_index=hours.indexOf('-',i);if(hyphen_index<0){break;}
var hour_start=hours.substr(i,hyphen_index-i);i+=hyphen_index-i+1;if(i>=hours.length)break;for(;i<hours.length&&/[0-9]/.test(hours[i]);++i);var hour_end=hours.substr(hyphen_index+1,i-(hyphen_index+1));for(var day_index=start_index;day_index<=end_index;++day_index){if(!schedule[day_index]){schedule[day_index]=[];}
schedule[day_index].push({start:hour_start,end:hour_end});}}else{for(var day_index=start_index;day_index<=end_index;++day_index){schedule[day_index]=[];}}}while(hours[i]==','&&(++i)<hours.length);}
return schedule;},};Fur.CartografurMap=function(id,location,bounds,poly_data,format){this._id=id;this._location=location;this._bounds=bounds;this._bounds.transform(Fur.OpenLayersMap.display_projection,Fur.OpenLayersMap.data_projection);this._poly_id_to_feature={};var features=[];for(var i=0;i<poly_data.length;++i){var poly=poly_data[i];var feature=format.read(poly.points);feature.fur_id=poly.poly_id;feature.info={name:poly.name,phone:poly.phone,hours:poly.hours,schedule:Fur.StoreHours.parse_hours_data(poly.hours)};feature.map_id=id;features.push(feature);this._poly_id_to_feature[poly.poly_id]=feature;}
var context=function(feature){return feature;}
var styleMap=new OpenLayers.StyleMap();var lookup={};lookup[Fur.CartografurMap.CLOSED]={fillColor:"#999"};lookup[Fur.CartografurMap.OPEN]={fillColor:"#60BF60"};styleMap.addUniqueValueRules('default','state',lookup,context);styleMap.addUniqueValueRules('select','state',lookup,context);this._vectorLayer=new OpenLayers.Layer.Vector(this._location,{displayInLayerSwitcher:false,styleMap:styleMap});this._vectorLayer.addFeatures(features);};Fur.CartografurMap.CLOSED=0;Fur.CartografurMap.OPEN=1;Fur.CartografurMap.prototype={getName:function(){return this._location;},getLayer:function(){return this._vectorLayer;},filterByHours:function(day_of_the_week,military){var open=[];var closed=[];var features=this._vectorLayer.features;for(var i=0;i<features.length;++i){var feature=features[i];var schedule=feature.info.schedule;if(!schedule){closed.push(feature);continue;}
var day=schedule[day_of_the_week];if(!day){open.push(feature);continue;}
for(var i2=0;i2<day.length;++i2){var range=day[i2];if(military>=range.start&&military<range.end){open.push(feature);break;}}
if(i2==day.length){closed.push(feature);}}
return{open:open,closed:closed};}};