/**
 * Provide a custom marker icon by category names.
 *
 * @param properties the properties of the GeoMashup object being customized
 * @param categories the array of category names assigned to post being marked
 * @return the custom GIcon
 */
function customGeoMashupCategoryIcon ( properties, categories ) {
  
  var icon = null;
  
  //properties is the geo-mashup built object, properties.category_opts has all category names from the site we're on.
  //so that can be scanned for tokens
  
  var icon_path = properties.template_url_path+categories2icon_dir(properties, categories);
  icon = new GIcon();
  icon.image = icon_path + '/image.png';
  icon.shadow = icon_path + '/shadow.png';
  icon.printImage = icon_path + '/printImage.gif';
  icon.mozPrintImage = icon_path + '/mozPrintImage.gif';
  icon.printShadow = icon_path + '/printShadow.gif';
  icon.transparent = icon_path + '/transparent.png';
  
  //original icons were all the same size, so these values are just static
  //if icons become different, some thinking will need to happen
  icon.iconSize = new GSize(27,18);
  icon.shadowSize = new GSize(36,18);
  icon.iconAnchor = new GPoint(13,17);
  icon.infoWindowAnchor = new GPoint(13,0);
  //~ icon.imageMap = [33,0,51,1,51,2,47,3,51,4,34,5,51,6,52,7,45,8,52,9,34,10,52,11,34,12,51,13,52,14,51,15,52,16,52,17,52,18,52,19,52,20,52,21,51,22,52,23,49,24,52,25,46,26,51,27,52,28,51,29,52,30,40,31,52,32,36,33,51,34,2,34,7,33,1,32,5,31,1,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0];
  icon.imageMap = [0,0,35,0,35,35,0,35];
  
  return icon;
}

//for the small map on the accident details page
function customGeoMashupSinglePostIcon(properties) {
  var cats;
  
  //to work the theme must provide an array at document.post_categories of either the map iframe or it's parent
  if(document.post_categories) { cats = document.post_categories; }
  else if(parent.document.post_categories) { cats = parent.document.post_categories; }
  if(!cats) { return; }
  
  
  
  var icon = customGeoMashupCategoryIcon( properties, cats );
 
  return icon;
}//end customGeoMashupSinglePostIcon

function category_name2token(name) {
  name = name.toLowerCase();
  name = name.replace(/ /g, '');
  name = name.replace(/\//g, '');
  tokens = new Array(
    'bites'
    , 'car'
    , 'construction'
    , 'motorcycle'
    , 'pedestrian'
    , 'semi'
	, 'aviation'
	, 'boat'
	, 'atv'
	, 'railway'
    , 'bicycle'
    , 'amusement'
	, 'bus'
	, 'farmtractor'
	, 'fireexplosion'
    , 'pickuptruck'
    , 'van'
    , 'suv'
    );

  var index = tokens.length;
  while(--index >= 0) {
    token = tokens[index];
    
    if(name.indexOf(token) >= 0) { 
        return token; 
    }
  }//rof token
  
  return 'uncategorized';
}//end category_name2token

// Prioritizes category icons so multi-categoried posts, show up correctly:
// if we're on a category page, that one shows first, otherwise there's an arbitrary heirarchy.
// This is all poorly organized because I don't have a plan for keeping categories on all blogs the same.
// For now just wing it.
function categories2icon_dir(properties, categories) {
  var priority_cat_token = 'uncategorized'; //default: 1 => Uncategorized
  
  if(!document.token2priority) {
    //if these appear to be 'in numerical order' this is only a co-incidence and not to be relied upon.
    document.token2priority = {
      car: 1
      ,semi: 2
      ,motorcycle: 3
      ,bites: 4
      ,pedestrian: 5
      ,construction: 6
	  ,aviation: 7
	  ,boat: 8
	  ,atv: 9
	  ,railway: 10
      ,bicycle:11
      ,amusement:11
      ,uncategorized: 100
	  ,bus: 1
	  ,farmtractor: 6 
	  ,fireexplosion: 6
      ,pickuptruck: 6
      ,van: 7	
      ,suv: 7  
    };
  }//fi
    
  var token = '';
  var i = categories.length;
  while(--i >= 0) {
    token = category_name2token(properties.category_opts[categories[i]]['name']);
    if(document.token2priority[token] < document.token2priority[priority_cat_token]) {
      priority_cat_token = token;
    }//fi
  }//rof
  return '/images/markers/'+priority_cat_token;
}//end categories2icon

//yoinked from stackoverflow, then tweeked
function listProperties(obj, show) {
   var propList = "";
   for(var propName in obj) {
      if(typeof(obj[propName]) != "undefined") {
         propList += ("\n"+propName);
         if(show) { propList += ": "+obj[propName]; }
      }
   }
   alert(propList);
}




