//patch for IE6,IE7
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}
//end of patch

var clickHandler;
var minimap;
var lat;
var lng;
var locations;
var bounds;
var minimapsAvailable=false;
var geocoder;
var g_minimapid;

function fcs_gmaps_init(smapid) {
    g_minimapid=smapid;
    try {
    if (GBrowserIsCompatible()) {
        minimap = new GMap2(document.getElementById(smapid));
        //minimap.setMapType();
        minimap.addControl(new GLargeMapControl());
        minimap.addControl(new GMapTypeControl());
        //minimap.setCenter(new GLatLng(0,0), 13, G_SATELLITE_MAP); //ROMA!
        minimap.setCenter(new GLatLng(41.8941,12.480469), 6, G_SATELLITE_MAP); //ROMA!
        minimap.enableDoubleClickZoom();
        minimap.enableScrollWheelZoom();
        minimap.setZoom(6);
        minimapsAvailable=true;

	geocoder = new GClientGeocoder();

        $.get('gmaps_srv.asp', fcs_maps_getLocations_complete );
    }
        }
        catch(e) {
           //alert(e + " : Maps services not available.");		// Return error message.
        }
    if (!minimapsAvailable) {
           $('div#fcs_info').html( '<A HREF=# onClick="$(\'div#fcs_info\').hide(\'slow\');" >' +
            e + ': maps services are not available, showyourmusic apologizes for any inconvenience' +
            '&nbsp;[x]</A>' );
           $('div#fcs_info').show('fast');
           setTimeout("$('div#fcs_info').hide('slow');",2000);

           $('div#fcs_info').show('slow');
           $.post('gmaps_srv.asp', fcs_maps_getLocations_complete );
    }
    
    $('a#save').click(function() {
        $('div#formular').hide('slow');
        $.post('locations.asp',
               { type: 'update',
                 sid: $('input#sid').get(0).value,
                 latitude: lat,
                 longitude: lng
               }, 
               fcs_maps_getLocations_complete );        
    });

    $('#SUPLAT').change(function () {fcs_update_marker()});
    $('#SUPLON').change(fcs_update_marker);
}


//-END

//-EXTENTIONS


var gFlagUnblockUI=false;

function fcs_mapmain(unblockui) {
  gFlagUnblockUI=unblockui;
  //LISTEN CLICKS ON THE MAP
  try {
    if (GBrowserIsCompatible()) {
	minimap.setMapType(G_HYBRID_MAP);
        clickHandler = GEvent.addListener(minimap, "click", function(marker, point) {
            fcs_maps_clicked(point);
        });
	//alert(clickHandler);
	//fcs_update_marker();
    }
  }
  catch(e) {
    $.blockUI({ message: e + ': Maps services not available.' });
    setTimeout($.unblockUI, 2000);
  }
  //LISTEN TO 'SAVE POSITION' BUTTON
  $('#editgeo_save').click(function(){
    $('#editgeo_save').val('saving new location...');
    $.post('all_srv.asp', {type:'record_editgeo',
lat: $('#SUPLAT').get(0).value,
lon: $('#SUPLON').get(0).value,
sid: $('input#sid').get(0).value,tid:'SUPPLIER',kid:'SUPCODE',id: $('#SUPCODE').get(0).value},
	function(res){
		$('#editgeo_save').val(res);
		setTimeout("$('#editgeo_save').val('save location');", 1000); 
		//if (gFlagUnblockUI) {setTimeout($.unblockUI,1);}
		if (gFlagUnblockUI) {record_updated($('#SUPCODE').get(0).value); $.unblockUI({fadeOut:50});}
        }
    );       
  });

}

function fcs_update_marker() {
	alert("fcs_update_marker() = DISABLED !");	
}

function fcs_update_marker_bylatlon(llat,llon) {
//alert(llat+ ' x-x '+llon)
llat=parseFloat(llat);
llon=parseFloat(llon);
//alert(llat+ ' x-x '+llon + 'PARSED')
//
 if ( isNaN(llat) || isNaN(llon) ) {
   $('#'+g_minimapid).block({ message:"No location set<br />(" + llat+ ','+llon + ")<br />Click on the map to set."});
	setTimeout("$('#'+g_minimapid).unblock()", 2000);
 } else {
    minimap.clearOverlays();
        var marker = new GMarker(new GLatLng( llat,llon ), {title: 'current location'});
        minimap.addOverlay(marker);
        GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml('<B>'+fcs_marker_name+'</B>');       
        });
    //minimap.panTo( new GLatLng( llat,llon ) );
    setTimeout("minimap.panTo( new GLatLng( "+llat+","+llon+" ) )", 1000);
 }
}

function fcs_maps_clicked(point) {
    if (typeof point == "undefined") {
        alert("no point clicked");
        return false;
    }
    minimap.clearOverlays();
    //fcs_marker.setLatLng(poit);
        var marker = new GMarker(new GLatLng(point.lat(), point.lng() ), {title: 'new location'});
        minimap.addOverlay(marker);
        GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml('<B>'+fcs_marker_name+'</B>');       
        });
    //alert(point.lat()+' @ '+point.lng());
    //UPDATES THE POS
        //ko $('#ARTLAT').get(0).value=point.lat();
        //ko $('#ARTLON').get(0).value=point.lng();
        $('#ARTLON').val(point.lng());
        $('#ARTLAT').val(point.lat());
        $('#SUPLON').val(point.lng());
        $('#SUPLAT').val(point.lat());

// AUTO-ZOOM + AUTO-PAN BY CLICKING !!! :-)
	var z=minimap.getZoom();
	if (z<=15) {setTimeout("minimap.setZoom(minimap.getZoom()+2); minimap.panTo( new GLatLng( "+point.lat()+","+point.lng()+" ) ) ",1000)};

  if (false){
        $.post('all_srv.asp',
               { type: 'corr_articlegroup',
                 latitude:  point.lat(),
                 longitude: point.lng(),
                 sid: $('input#sid').get(0).value
               }, 
               fcs_maps_clicked_complete );        
  }
}

function fcs_maps_clicked_complete(result) {
//$.unblockUI();
    $.blockUI({ message:result});
    setTimeout($.unblockUI, 2000);
}

var geoaddress;

function editgeo_geocode_address() {
  geoaddress=$('#usercity').get(0).value+','+$('#usercountry').get(0).value;
  $('#'+g_minimapid).block({ message:"going to<br />" + geoaddress + " ...<br /><br />"});
  geocoder.getLatLng(
    $('#usercity').get(0).value+','+$('#usercountry').get(0).value,
    function(point) {
      if (!point) {
        $('#'+g_minimapid).block({ message:"location<br />" + geoaddress + "<br />not found !<br /><br />"});
	setTimeout("$('#'+g_minimapid).unblock()", 2000);
      } else {
	$('#'+g_minimapid).unblock();

        $('#ARTLON').val(point.lng());
        $('#ARTLAT').val(point.lat());
        $('#SUPLON').val(point.lng());
        $('#SUPLAT').val(point.lat());

	var z=minimap.getZoom();
	if (z<7) { minimap.setZoom(7); };
	//fcs_update_marker();
	fcs_update_marker_bylatlon(point.lat(),point.lng());
      }
    }
  );
}







