function initializeGoogleMap() {
  if ( GBrowserIsCompatible() ) {
        map = new GMap2(document.getElementById( "map_canvas" ));
        map.setCenter(new GLatLng( 59.332977, 8.647369 ), 7);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GOverviewMapControl( new GSize( 100, 70 ) ) );

        geocoder = new GClientGeocoder();

        map.addControl( new GLargeMapControl() );
        map.addControl( new GMapTypeControl() );

        TellusGmarkerList = new Array();
        TellusGmarkerTextList = new Array();
   }
 }

function showAddressByCoordinates( latitude, longditude, addOverlay, localAddress )
{
    var latLng = new GLatLng( latitude, longditude );
    var latLngMarker = new GMarker( latLng );
    var newAddress = localAddress;
    GEvent.addListener( latLngMarker, "click",
                        function() {
        latLngMarker.openInfoWindowHtml( newAddress );
    });

    if ( addOverlay == true ) {
        map.addOverlay( latLngMarker );
    }

    return latLngMarker;
}

function removeGmarkerAddress( gmarker )
{
    if ( typeof( gmarker ) == 'object' )
        gmarker.remove();
}

function updateGmarkerArray()
{
    var tellusCoordinateList = document.getElementsByName( "TellusCoordinates[]" );
    for( key in tellusCoordinateList ) {
        if ( !isNaN( key ) ) {
            var coordinateInfo = tellusCoordinateList[key].value;
            var newLatitude = coordinateInfo.replace( /^(.+?)x.+$/, "$1" );
            var newLongitude = coordinateInfo.replace( /^.+?x(.+?)\_.+$/, "$1" );
            var newId = coordinateInfo.replace( /^.+?x.+?_(.+?)_.+$/, "$1" );
            var found = false;

            var newAddress = '';
            for ( i in TellusGmarkerTextList ) {
                if ( i == newId ) {
                    newAddress = TellusGmarkerTextList[i];
                    found = true;
                    break;
                }
            }

            if ( found == false ) {
                var newUrl = coordinateInfo.replace( /^.+?x.+?_.+?_(.+?)_.+$/, "$1" );
                var newReadmore = coordinateInfo.replace( /^.+?x.+?_.+?_.+?_(.+?)_.+$/, "$1" );
                var newName = coordinateInfo.replace( /^.+?x.+?_.+?_.+?_.+?_(.+)$/, "$1" );
                newAddress = newName + '<br /><a href="' + newUrl + '">' + newReadmore + '</a>';
                TellusGmarkerTextList[newId] = newAddress;
            }
            var newGmarker = showAddressByCoordinates( newLatitude, newLongitude, false, newAddress );
            TellusGmarkerList[newId] = new Array( newLatitude, newLongitude, newGmarker );
        }
    }
}

function toggleTellusMapProduct( element, latitude, longditude ) {
    if ( TellusGmarkerList.length == 0 ) {
        updateGmarkerArray();
    }

    var elementValue = element.value;
    var found = false;
    for ( key in TellusGmarkerList ) {
        if ( elementValue == key ) {
            found = true;
            break;
        }
    }

    if ( found == false ) {
        TellusGmarkerList[element.value] = new Array( latitude, longditude );
    }
    else {
        if ( latitude > 0 && longditude > 0 ) {
            TellusGmarkerList[element.value][0] = latitude;
            TellusGmarkerList[element.value][1] = longditude;
        }

        latitude = TellusGmarkerList[element.value][0];
        longditude = TellusGmarkerList[element.value][1];
    }

    if ( element.checked == true ) {
        var newAddress = '';
        for ( i in TellusGmarkerTextList ) {
            if ( i == elementValue ) {
                newAddress = TellusGmarkerTextList[i];
                break;
            }
        }

        var newGmarker = showAddressByCoordinates( latitude, longditude, true, newAddress );
        if ( typeof( TellusGmarkerList[element.value][2] ) == 'object' ) {
            removeGmarkerAddress( TellusGmarkerList[element.value][2] );
        }
        TellusGmarkerList[element.value][2] = newGmarker;
    }
    else {
        removeGmarkerAddress( TellusGmarkerList[element.value][2] );
    }
}


function TellusUpdateAll() {
  var checkList = document.getElementById( 'TellusProductContainer' ).getElementsByTagName( "input" );
  var checkbox = document.getElementById( 'tellus-select-all' );
  var isChecked = checkbox.checked;
  for ( key in checkList )
  {
     if ( !isNaN( key ) ) {
        var item = checkList[key];
        if ( item.type == 'checkbox' )
        {
            item.checked = isChecked;
            toggleTellusMapProduct( item, 0, 0 );
        }
     }
  }
}

