/**
 * GeoMashup customization examples 
 * 
 * * The filename must be changed to custom.js for customizations to take effect.
 * * You can edit the examples and enable customizations you want.
 * * If you know javascript, you can add your own customizations using the Google Maps API
 *   documented at http://code.google.com/apis/maps/documentation/reference.html#GMap2
 *
 * Properties
 * A properties object is available in all custom functions, and has these useful
 * variables:
 * * properties.url_path - the URL of the geo-mashup plugin directory
 * * properties.template_url_path - the URL of the active theme directory
 * * properties.map_content - 'global', 'single', or 'contextual'
 * * properties.map_cat     - the category ID of a cateogory map
 *
 * The old custom-marker.js from pre-1.0 versions of Geo Mashup is no longer used.
 */

/**
 * Trakkers custom global object
 */
var GEO_MASHUP_TRAKKERS = {
	toggle_post_id: null
};

/**
 * Provide a z-index for a marker.
 */
function customGeoMashupZIndex( marker ){
	var color_order = {
		'green': -10,
		'orange': -20,
		'blue': -30,
		'other': -40
	},
	image = marker.getIcon().image,
	number_match = image.match(/\/[-\D]*(\d+)[-\w]*\.\w*$/);

	if ( number_match && number_match[1] ) {
		return parseInt( number_match[1], 10 ) * -10;
	}

	for( color in color_order ) {
		if ( color_order.hasOwnProperty( color ) && typeof color !== 'function' ) {
			if ( image.indexOf( color ) >= 0 ) {
				return color_order[color];
			}
		}
	}
	return color_order.other;
}

/**
 * Provide a custom marker icon by category ids.
 *
 * @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, obj ) {
  var i, icon = null, green_icon, orange_icon, rank, 
		id_list = '|' + categories.join('|') + '|', 
		author_id_list = obj ? '|' + obj.author_categories.join('|') + '|' : '', 
		hasCategory = function( id ) {
			// A little helper function to test for categories
			// Use the name list to see if a category is present
			return ( id_list.indexOf( '|' + id + '|' ) >= 0 );
		},
		hasAuthorCategory = function( id ) {
			// A little helper function to test for categories
			// Use the name list to see if a category is present
			return ( author_id_list.indexOf( '|' + id + '|' ) >= 0 );
		};

	green_icon = new GIcon();
	green_icon.image = properties.custom_url_path + '/images/green_marker.png';
	green_icon.iconSize = new GSize( 33, 36 );
	green_icon.iconAnchor = new GPoint(6, 20);
	green_icon.infoWindowAnchor = new GPoint(5, 16);
	
	orange_icon = new GIcon( green_icon );
	orange_icon.image = properties.custom_url_path + '/images/orange_marker.png';
		
	// If there is a marker for this category name, use it
	// Comparisons with category names are case-sensitive
	if ( obj && window.name == 'recent_posts' ) {

		// We're on the blog page "recent 3" map
		if ( ! properties.blog_marker_index ) {
			properties.blog_marker_index = 0;
		}
		properties.blog_marker_index++;
		
		icon = new GIcon();
		icon.image = properties.custom_url_path + '/images/green0' + properties.blog_marker_index + '.png';
		//icon.shadow = properties.url_path + '/images/mm_20_shadow.png';
		icon.iconSize = new GSize( 33, 36 );
		//icon.shadowSize = new GSize( 22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 16);


	} else if ( ( properties.parent_id == 1875 || properties.parent_id == 1858 || properties.parent_id == 1856 ) ) {

		// We're in a "top-ranked" page
		rank = '';
		if ( obj && obj.rank ) {
			rank = obj.rank.toString();
			while ( rank.length < 2 ) {
				rank = '0' + rank;
			}
		}
		if ( hasCategory( 8 ) ) {
			// Women
			icon = new GIcon( orange_icon );
			if ( rank ) {
				icon.image = properties.custom_url_path + '/images/orange_rank_' + rank + '.png';
			}
		} else { 
			// Men
			icon = new GIcon( green_icon );
			if ( rank ) {
				icon.image = properties.custom_url_path + '/images/green_rank_' + rank + '.png';
			}
		}

	} else if ( properties.parent_id == 5253 ) {
		
		// Blog post map
		if ( hasAuthorCategory( 36 ) ) {
			icon = orange_icon;
		} else {
			icon = green_icon;
		}

	} else if ( hasCategory( 26 ) || hasCategory( 8 ) || hasCategory( 9 ) || hasCategory( 53 ) || hasCategory( 97 ) || hasCategory(4) || hasCategory(259)) {

		icon = green_icon;
	
	} else if ( hasCategory( 21 ) && hasCategory( 40 ) ) {

		icon = green_icon;
		
	} else if ( hasCategory( 22 ) && hasCategory( 40 ) || hasCategory( 27 ) || hasCategory( 35 )  || hasCategory( 8 )) {

		icon = orange_icon;
	
	} else if ( hasCategory( 30 ) || hasCategory( 36 ) || hasCategory( 54 )) {

		icon = new GIcon();
		icon.image = properties.custom_url_path + '/images/blue_marker.png';
		//icon.shadow = properties.url_path + '/images/mm_20_shadow.png';
		icon.iconSize = new GSize( 33, 36 );
		//icon.shadowSize = new GSize( 22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 16);
	
	} else if ( hasCategory( 155 ) ) {

		icon = new GIcon();
		icon.image = properties.custom_url_path + '/images/purple_marker.png';
		//icon.shadow = properties.url_path + '/images/mm_20_shadow.png';
		icon.iconSize = new GSize( 33, 36 );
		//icon.shadowSize = new GSize( 22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 16);
	
	} else if ( hasCategory( 28 ) ) {

		icon = new GIcon();
		icon.image = properties.custom_url_path + '/images/bike_marker.png';
		//icon.shadow = properties.url_path + '/images/mm_20_shadow.png';
		icon.iconSize = new GSize( 18, 32 );
		//icon.shadowSize = new GSize( 22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 16);
		
	} else if ( hasCategory( 29 ) ) {

		icon = new GIcon();
		icon.image = properties.custom_url_path + '/images/swim_marker.png';
		//icon.shadow = properties.url_path + '/images/mm_20_shadow.png';
		icon.iconSize = new GSize( 18, 32 );
		//icon.shadowSize = new GSize( 22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 16);
	}

  return icon;
}

/**
 * Provide a custom image for locations with multiple posts.
 *
 * @param properties the properties of the GeoMashup object being customized
 * @return the custom GIcon
 */
function customGeoMashupMultiplePostImage ( properties, marker ) {
	var icon = marker.getIcon();
	GeoMashup.map.removeOverlay( marker );

	// Select an icon image based on the current one
	if ( icon.image.indexOf( 'green' ) >= 0 ) {
		icon.image = properties.custom_url_path + '/images/multi_green.png';
	} else if ( icon.image.indexOf( 'orange' ) >= 0 ) {
		icon.image = properties.custom_url_path + '/images/multi_orange.png';
	} else if ( icon.image.indexOf( 'blue' ) >= 0 ) {
		icon.image = properties.custom_url_path + '/images/multi_blue.png';
	}

	// Adjust the icon dimensions to match the image size
	// ( This assumes all multi icons are the same size )
	icon.iconSize = new GSize( 71, 72 );
	icon.iconAnchor = new GPoint( 35, 36 );
	icon.infoWindowAnchor = new GPoint( 33, 31 );
	GeoMashup.map.addOverlay( marker );
}

/**
 * Handle extInfoWindow content
 */
function customizeGeoMashupMap ( properties, map ) {
	var loc;

	if ( window.name == 'recent_posts' ) {
		// Redefine marker clicks
		for( point in GeoMashup.locations ) {
			if ( GeoMashup.locations.hasOwnProperty( point ) && typeof point !== 'function' ) {
				loc = GeoMashup.locations[point];
				GEvent.clearListeners( loc.marker, 'click' );
				GEvent.addListener( loc.marker, 'click', function( post ) {
					return function() {
						var target = parent ? parent : window;
						parent.location.href = post.permalink;
					};
				}( GeoMashup.posts[loc.posts[0]] ) );
			}
		}
	} else {
		GEvent.addListener( map, 'extinfowindowopen', function( e ) {
			var $ = jQuery,
				height,
				width,
				adj_top,
				start_index = 0,
				do_fade = ( ! $.browser.msie ),
				$posts = $( '.info-post' ),
				$list = $( '#toggle-list' ),
				$toggles = null,
				$commas = null,
				$custom_window = $( '#custom_info_window_red' ),
				showpost = function( i ) {
					$posts.hide().eq(i).show();
					if ( $toggles && $commas ) {
						$toggles.show().eq(i).hide();
						$commas.show();
						if ( i == 0 ) {
							$commas.eq(0).hide();
						} else {
							$commas.eq( i - 1 ).hide();
						}
					}
				};
		
			if ( do_fade ) {
				$custom_window.css( 'opacity', '0.3' );
			}
			if ( $posts.length > 1 ) {
				$posts.each( function( i ) {
					var title = $( this ).find( 'h2' ).text().replace(/^\s+|\s+$/g, ''),
						$link = $( '<span class="toggle">' + title + '</span>' );
					$link.eq(0).click( function() { 
						showpost(i); 
						return false; 
					} );
					if ( i > 0 ) { 
						$list.append( $('<span class="info-toggle-comma">, </span>') );
					}
					$list.append( $('<span class="info-toggle"></span>').append( $link ) );
				} );
				$toggles = $( '.info-toggle' );
				$commas = $( '.info-toggle-comma' );
				$( '#also-here-panel' ).show();

			}
			
			if ( $posts.length > 0 ) { 
				if ( GEO_MASHUP_TRAKKERS.toggle_post_id ) {
					$posts.each( function( i ) {
						if ( $posts.eq( i ).hasClass( 'info-post-id-' + GEO_MASHUP_TRAKKERS.toggle_post_id ) ) {
							start_index = i;
						}
					} );
					GEO_MASHUP_TRAKKERS.toggle_post_id = null;
				}
				showpost( start_index );
				if ( $.browser.msie && $.browser.version < '8.0' ) {
					// Fix height
					height = $custom_window.height() + $posts.eq( start_index ).height();
					$custom_window.height( height );
				}
				if ( do_fade ) {
					$( '#custom_info_window_red' ).fadeTo( 1000, 1.0 );
				}

				setTimeout( function() {
					GeoMashup.map.panBy( new GSize( 0, 8 ) );
				}, 500 );
			}

		} );
	}
}



