// A function to create the marker and set up the event window
function createMarker(point, aD, point_x, point_y, mapBounds)
{
    // HTML-Code fuer InfoWindow generieren
    var authorHtml = '';
    authorHtml += generateInfoWindowBegin();
    authorHtml += generateAuthorHtml(aD);
    authorHtml += generateInfoWindowEnd();                    

    // AnE: benutzerdefiniertes Overlay
    var markerLabel = '1';
    var iconOffset = getMarkerLabelOffset(1);            
    var marker = new authorMarker(point, { icon: Icon, authorText: markerLabel, authorClass: iconOffset.css, authorOffsetX: iconOffset.x, authorOffsetY: iconOffset.y, last_i: i, clickable: true});
    GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(authorHtml);
  });
  
  var myLatLng = new GLatLng(point_y, point_x, true);         
  // save the info we need to use later for the side_bar
  gmarkers[i] = marker;
  htmls[i] = authorHtml;

  i++;
  
  return marker;
} // createMarker()

// This function picks up the click and opens the corresponding info window
function myclick(i) {
  gmarkers[i].openInfoWindowHtml(htmls[i]);
}

// Funktion, die HTML-Code fuers InfoWindow generiert (fuer einen Autor)
function generateAuthorHtml(aD)        
{
    var authorHtml = '';
    
    authorHtml += '<div class="infoWindowAuthorContainer">';

    var line1 = '';

    if (aD.title != '')
        line1 += aD.ti + ' ';
    if (aD.firstName != '')
        line1 += aD.fN + ' ';
    if (aD.lastName != '')
        line1 += aD.lN + ' ';
        
    if (line1 != '')
    {
        authorHtml += '<div class="infoWindowauthorDataLine1 infoWindowSpacer">'+line1+'</div>';
    }
        
    var institute = '';
    if (aD.institute1 && aD.i1 != '')
        institute += aD.institute1;
    if (aD.institute2 && aD.i2 != '')
    {
        if (institute != '')
            institute += ', ';
        institute += aD.i2;
    }
    if (aD.i3 && aD.i3 != '')
    {
        if (institute != '')
            institute += ', ';
        institute += aD.i3;
    }
    if (institute != '')
    {
//        if (authorHtml != '')
//            authorHtml += '<br>';
        authorHtml += '<div class="infoWindowSpacer">'+institute+'</div>';
    }

    // Adress-Teil
    // street, city, postal_code, country
    var address = '';
    if (aD.st && aD.st != '')
        address += aD.st;
    var city_complete = '';
    if (aD.pc && aD.pc != '')
        city_complete = aD.pc;
    if (aD.ci && aD.ci != '')
    {
        if (city_complete != '')
            city_complete += ' ';
        city_complete += aD.ci;
    }
    if (city_complete != '')
    {
        if (address != '')
            address += ', ';
        address += city_complete;
    }
    if (aD.co && aD.co != '')
    {
        if (address != '')
            address += ', ';
        address += aD.co;
    }
    if (address != '')
    {
//        if (authorHtml != '')
//            authorHtml += '<br>';
        authorHtml += '<div class="infoWindowSpacer">'+address+'</div>';
    }
    
    var volume_text = 'Volume ';
    var volume_link = 'http://www.science-of-synthesis.com/prod/user/index.html?ID=';
    
    // Volumes vorhanden
    if (aD.vo != '')
    {
        authorHtml += '<div class="infoWindowSpacer">';
        // in Array splitten
        var volumes = aD.vo.split(',');
        for (iv = 0; iv < volumes.length; iv++)
        {
            var volume_no = volumes[iv];
            var volume_no_stripped = volume_no;
            var article_no = volumes[iv+1];
            // mit fuehrenden Nullen auffuellen: volume 3-stellig, Artikelnr.
            // 5-stellig
            var vlength = volume_no.length;
            var alength = article_no.length;
            for (var jv = vlength; jv < 3; jv++)
            {
                volume_no = '0'+volume_no;
            }
            for (var kv = alength; kv < 5; kv++)
            {
                article_no = '0'+article_no;
            }            
            authorHtml += '<a href="'+volume_link+'SD-'+volume_no+'-'+article_no+'" target="_blank" class="infoWindowVolumeLink">'+volume_text+volume_no_stripped+'</a><br>';
            iv++;
        }
        authorHtml += '</div>';
    } // Volumes vorhanden

    authorHtml += '</div>';
    
    return authorHtml;
} // generateAuthorHtml()

function generateInfoWindowBegin()
{
    var codeBegin = '<div class="infoWindow">';
    
    return codeBegin;
} // generateInfoWindowBegin()

function generateInfoWindowEnd()
{
    var codeEnd = '';
    codeEnd += '</div>';
    
    return codeEnd;
} // generateInfoWindowEnd()

// Funktion, die Versatz fuer Marker-Beschriftung ermittelt
// abhaengig von Marker-Icon-Groesse
function getMarkerLabelOffset(iconSize)
{
    var offsetX = 0;
    var offsetY = 0;
    var css = 'markerNumberOfAuthors1';

    switch(iconSize)
    {
        case 1:
            offsetX = -6;
            offsetY = -37;
            css = 'markerNumberOfAuthors1';
            break;
            
        case 2:
            offsetX = -10;
            offsetY = -45;
            css = 'markerNumberOfAuthors2';                    
            break;
            
        case 3:
            offsetX = -13;
            offsetY = -53;
            css = 'markerNumberOfAuthors3';                    
            break;
            
        
    } // switch

    var offsetObject =
    {
        x: offsetX,
        y: offsetY,
        css: css
    };
    
    return offsetObject;
} // getMarkerLabelOffset


//
// *** benutzerdefiniertes Overlay
//

function authorMarker(point, opt_opts)
{
    this.latlng_=point;
    this.opts_=opt_opts;

    this.authorClass_=opt_opts.authorClass||"markerNumberOfAuthors1";
    this.authorOffsetX_=opt_opts.authorOffsetX||0;
    this.authorOffsetY_=opt_opts.authorOffsetY||0;
    this.authorText_=opt_opts.authorText||"";            
    this.clickable_=opt_opts.clickable||true;
    GMarker.apply(this,arguments)        
} // authorMarker()

authorMarker.prototype=new GMarker(new GLatLng(0,0));

authorMarker.prototype.initialize=function(map)
{
    GMarker.prototype.initialize.apply(this,arguments);
    this.map_=map;
    this.div_=document.createElement("div");
    this.div_.className=this.authorClass_;

    if (this.opts_.last_i)
    {
        var markerLabelId = 'markerLabel'+this.opts_.last_i;
        // ID (fuers Verstecken benoetigt)
        this.div_.id = markerLabelId;
    }

    this.div_.innerHTML=this.authorText_;
    this.div_.style.position="absolute";
    this.div_.style.cursor="pointer";
    map.getPane(G_MAP_MARKER_PANE).appendChild(this.div_);
    
    if(this.clickable_)
    {
        function addEvent(obj,event)
        {
            return function()
            {
                GEvent.trigger(obj,event)
            }
        }
        var additionalEvents = ['click','dblclick','mousedown','mouseup'];
        for(var i=0; i< additionalEvents.length; i++)
        {
                var eventName = additionalEvents[i];
                GEvent.addDomListener(this.div_, eventName, addEvent(this, eventName))
        }
    }            
}; // initialize()

authorMarker.prototype.redraw=function(force)
{
    GMarker.prototype.redraw.apply(this,arguments);
    this.redrawLabel_();
}; // redraw()

authorMarker.prototype.redrawLabel_=function()
{
    var p=this.map_.fromLatLngToDivPixel(this.latlng_);
    var z=GOverlay.getZIndex(this.latlng_.lat());
    this.div_.style.left=(p.x+this.authorOffsetX_)+"px";
    this.div_.style.top=(p.y+this.authorOffsetY_)+"px";
    this.div_.style.zIndex=z;
}; // redrawLabel_()

authorMarker.prototype.copy=function()
{
    return new authorMarker(this.latlng_,this.opts_)
}; // copy()

authorMarker.prototype.remove=function()
{
    GEvent.clearInstanceListeners(this.div_);
            
    if(this.div_.parentNode)
    {
        this.div_.parentNode.removeChild(this.div_);
    }
    this.div_=null;
    GMarker.prototype.remove.apply(this,arguments);
}; // remove()

//
// *** ENDE benutzerdefiniertes Overlay
//

