var VegUtils = {
	Hover: {
        Init: function() {
            VegUtils.Hover.Preload();

            $j("img.hover").parent("a").hover(
				function() { $j(this).children("img.hover").attr('src', VegUtils.Hover.NewImage($j(this).children("img.hover").attr('src'))); },
				function() { $j(this).children("img.hover").attr('src', VegUtils.Hover.OldImage($j(this).children("img.hover").attr('src'))); }
			);
        },

        Preload: function() {
            $j(window).bind('load', function() {
                $j('img.hover').each(function(key, elm) {
                    var sHoverImage = VegUtils.Hover.NewImage($j(this).attr('src'));
                    var currObj = $j(this);

                    var imageTest = new Image();
                    imageTest.onerror = function() {
                        $j(currObj).removeClass("hover");
                        $j(currObj).unbind("mouseenter").unbind("mouseleave");
                    };
                    imageTest.onload = function() {
                        $j('<img>').attr('src', sHoverImage);
                    };
                    imageTest.src = sHoverImage;
                });
            });
        },

        NewImage: function(src) {
            var iExtensionPoint = src.search(".((jpg)|(gif)|(png))");
			//alert(src.substring(0, iExtensionPoint) + '-hover' + src.substring(iExtensionPoint));
            return src.substring(0, iExtensionPoint) + '-hover' + src.substring(iExtensionPoint);
        },

        OldImage: function(src) {
            return src.replace(/-hover\./, '.');
        }
    }
}

function fitMap( map, points ) 
{
	var bounds = new GLatLngBounds();
	for (var i=0; i< points.length; i++) {
		bounds.extend(points[i]);
	}
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
}

function fitMapDynamic(map, points)
{
	if(!map.getBounds().containsLatLng(points[10]))
	{
		var bounds = new GLatLngBounds();
		for (var i=0; i< 10; i++) {
			bounds.extend(points[i]);
		}
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	}
}

$j(document).ready(function() {
	// If an image has the class hover, make our magic happen
    if ($j('img.hover').length > 0) {
        VegUtils.Hover.Init();
    }
});
