/**
 * 
 */
var googleMapObj = null;

/**
 * 
 */
var shopSelected = null;

/**
 * 
 */
var shopIds = [];

/**
 * 
 * @return
 */
function initialize()
{    
	if( GBrowserIsCompatible() )
	{
		createMapObject();
		createShops();
    }
}

/**
 * 
 * @return
 */
function createMapObject()
{
	//
	googleMapObj = new GMap2( document.getElementById( "google_div" ) );
	googleMapObj.enableScrollWheelZoom();
	googleMapObj.enableContinuousZoom();
	googleMapObj.enableDoubleClickZoom();
	googleMapObj.addControl( new GLargeMapControl() );
	googleMapObj.addControl( new GMapTypeControl() );
	googleMapObj.setCenter( new GLatLng( 0, 0 ), 1, G_NORMAL_MAP );

	//
	if( 1 )
	GEvent.addDomListener
	( 
		googleMapObj, 
		"drag", 
		function()
		{
			$( '#storeShopInfoPanel' ).css( 'visibility', 'hidden' );
		} 
	);
}

/**
 * Genera una cadena id en base a unas coords.
 * @param lat
 * @param lng
 * @return
 */
function getShopId( lat, lng )
{
	return "" + new Number( lat ).toFixed( 4 ) + "" + new Number( lng ).toFixed( 4 );
}

/**
 * 
 * @return
 */
function createShops()
{
	//
	var iconoStore = new GIcon( G_DEFAULT_ICON, "../css/css_img/store-locator-icon-store.png" );
	iconoStore.iconSize = new GSize( 19, 22 );
	iconoStore.shadow = "";
	
	//
	var iconoOutlet = new GIcon( G_DEFAULT_ICON, "../css/css_img/store-locator-icon-outlet.png" );
	iconoOutlet.iconSize = new GSize( 19, 22 );
	iconoOutlet.shadow = "";
	
	//
	$.get
	( 
		'../php/storeLocator.php?getShops&' + Math.random(),
		function( shops )
		{
			//
			shops = jQuery.parseJSON( shops );

			//
			var n = 0;
			for( var f = 0 ; f < shops.length ; f++ )
			{
				//
				var shop = shops[f];
				var icono = shop.outlet ? iconoOutlet : iconoStore;
				var marker = new GMarker( new GLatLng( shop.coord_lat, shop.coord_lng ), icono );
				var id = getShopId( shop.coord_lat, shop.coord_lng );
				shopIds.push( { 'id': id, 'lat': shop.coord_lat, 'lng': shop.coord_lng, 'html': shop.html } );
				
				//
				if( 1 )
				{
					GEvent.addListener
					( 
						marker, 
						"click", 
						function()
						{
//							shopSelected = { 'lat': shop.coord_lat, 'lng': shop.coord_lng, 'html': shop.html };
//							alert( shop.html );
//							showShopInfo( new GLatLng( shop.coord_lat, shop.coord_lng ), shop.html );
							showShopInfo( this.getLatLng() );
						}
					);
					googleMapObj.addOverlay( marker );
				}
			}
		}
	);
}

/**
 * 
 * @return
 */
function showShopInfo( latLng )
{
	var id = getShopId( latLng.y, latLng.x );
	for( var f = 0 ; f < shopIds.length ; f++ )
	{
		if( shopIds[f].id == id )
		{
			googleMapObj.setCenter( latLng, 15 );
			setShopPosition( latLng );
			var div = $( '#storeShopInfoPanel' );
			div.empty();
			div.append( shopIds[f].html );
			div.css( 'visibility', 'visible' );
			return;
		}
	}
}

/**
 * 
 * @return
 */
function setShopPosition( latLng )
{
	var centerPos = googleMapObj.getCenter();
	var pos1 = googleMapObj.fromLatLngToDivPixel( centerPos );
	var pos2 = googleMapObj.fromLatLngToDivPixel( latLng );
	var xo = $( '#google_div' ).width() / 2;
	var yo = $( '#google_div' ).height() / 2;
	var div = $( '#storeShopInfoPanel' );
	div.css( 'left', xo + ( pos2.x - pos1.x ) - div.width() / 2 - 10 );
	div.css( 'top',  yo + ( pos2.y - pos1.y ) - $( '#google_div' ).height() - div.height() - 53 );
}

/**
 * 
 * @param x
 * @param y
 * @return
 */
function gotoStore( lat, long )
{
	$( '#storeAjaxPanel' ).css( 'visibility', 'hidden' );
	$( '#storeShopInfoPanel' ).css( 'visibility', 'hidden' );
	googleMapObj.setCenter( new GLatLng( lat, long ), 15 );
}

/**
 * 
 * @return
 */
function updateStoreList()
{
	//
	$( '#storeShopInfoPanel' ).css( 'visibility', 'hidden' );

	//
	$.get
	( 
		'../php/storeLocator.php?autocomp&text=' + 
			document.getElementById( 'search-store-text' ).value + 
			'&' + Math.random(),
		function( datos )
		{
			//
			datos = jQuery.parseJSON( datos );
			if( !datos.hayResultados )
			{
				$( '#storeAjaxPanel' ).css( 'visibility', 'hidden' );
				return;
			}
			
			//
			$( '#storeAjaxPanel' ).empty();
			for( var f = 0 ; f < datos.num_items ; f++ )
				$( '#storeAjaxPanel' ).append( datos.items[f] );
			
			//
			var pos = $( '#search-store-text' ).position();
			$( '#storeAjaxPanel' ).css( 'left', pos.left );
			$( '#storeAjaxPanel' ).css( 'visibility', 'visible' );
		}
	);
}

