GenericMarker.prototype = new google.maps.Marker(new google.maps.Marker(0, 0));
function GenericMarker(defaultErrorText, markerInfo, opts, addPopup) {
    if(defaultErrorText) {
        this.defaultErrorText = defaultErrorText;
        this.markerInfo = markerInfo;

        var latlng = new google.maps.LatLng(this.markerInfo.lat, this.markerInfo.lng);
        CallParentConstructor(this, google.maps.Marker, latlng, opts);

        this.clickListener = null;

        if(addPopup) {
            var me = this;
            this.clickListener = google.maps.Event.addListener(this, "click", function() {me.showPopup();});
        }
    }
}

GenericMarker.prototype.showPopup = function() {

}

GenericMarker.prototype.getMarkerInfo = function() {
    return this.markerInfo;
}

/**
 * Marker per mappa dei viaggi
 */
TravelMarker.prototype = new GenericMarker();
function TravelMarker(waitingText, defaultErrorText, markerInfo, opts) {
    CallParentConstructor(this, GenericMarker, defaultErrorText, markerInfo, opts, true);

    this.waitingText = waitingText;
    this.popupText = null;
}

TravelMarker.prototype.showPopup = function() {
    if(this.popupText) {
        this.openInfoWindowHtml(this.popupText);
    }
    else {
        this.loadPopupText();
    }
}

TravelMarker.prototype.loadPopupText = function() {
    this.openInfoWindowHtml(this.waitingText + '<span class="loading"></span>');

    var me = this;

    var url = '?page=AjaxHtmlDispatcher';
    var params = 'id=' + this.markerInfo.link + "&class=travels&method=load";
    google.maps.DownloadUrl(url , function(data, responseCode){
        me.doLoadedInfos(data, responseCode);
    },
    params);
}

TravelMarker.prototype.doLoadedInfos = function(data, responseCode) {
    if(responseCode == 200) {
        this.popupText = data;
        this.showPopup();
    }
    else {
        this.showDefaultErrorMessage();
    }
}

TravelMarker.prototype.showDefaultErrorMessage = function() {
    this.openInfoWindowHtml('<span class="error">' + this.defaultErrorText + '</span>');
}

/**
 * Marker per mappa della gallery
 */
GalleryMarker.prototype = new GenericMarker();
function GalleryMarker(defaultErrorText, detailText, pictureByText, markerInfo, opts) {
    if(!opts) {
        opts = this.buildDefaultOpts(markerInfo);
    }
    CallParentConstructor(this, GenericMarker, defaultErrorText, markerInfo, opts, true);

    this.detailText = detailText;
    this.pictureByText = pictureByText;
    this.html = '';
    this.createPopupHtml();
}

GalleryMarker.prototype.buildDefaultOpts = function(markerInfo) {
    var customIcon = new google.maps.Icon();
//    customIcon.shadow = markerInfo.icon;
    customIcon.image = markerInfo.icon;
    customIcon.iconSize = new google.maps.Size(
        markerInfo.iconsize.width,
        markerInfo.iconsize.height);
//    customIcon.shadowSize = new google.maps.Size(
//        markerInfo.iconsize.width,
//        markerInfo.iconsize.height);
    customIcon.iconAnchor = new google.maps.Point(
        markerInfo.iconsize.width / 2,
        markerInfo.iconsize.height / 2);
    customIcon.infoWindowAnchor = new google.maps.Point(
        markerInfo.iconsize.width,
        markerInfo.iconsize.height / 2);
    var opts = {title: markerInfo.title + " - " + markerInfo.author, icon: customIcon};
    return opts;
}

GalleryMarker.prototype.createPopupHtml = function() {
    this.html =
        '<div class="clearfix">' +
            '<table>' +
                '<tr>' +
                    '<td colspan="2">' +
                        '<a href="' + this.markerInfo.link + '">' +
                            '<img src="' + this.markerInfo.src + '" title="' +
                            this.markerInfo.title + '" alt="' + this.markerInfo.title + '" class="mapImage" />' +
                        '</a>' +
                    '</td>' +
                '</tr>' +
                '<tr>' +
                    '<td colspan="2">' +
                        '<span class="mapImageTitle">' + this.markerInfo.title + '</span>' +
                    '</td>' +
                '</tr>' +
                '<tr>' +
                    '<td style="width: 1.5em">' +
                        '<span>' + this.pictureByText + ':</span>' +
                    '</td>' +
                    '<td>' +
                        '<span>' + this.markerInfo.author + '</span>' +
                    '</td>' +
                '</tr>' +
                '<tr>' +
                    '<td colspan="2" style="text-align: center">' +
                        '<a href="'+ this.markerInfo.link + '">' + this.detailText + '</a>' +
                    '</td>' +
                '</tr>' +
            '</table>' +
        '</div>';
}

GalleryMarker.prototype.showPopup = function() {
    this.openInfoWindowHtml(this.html);
}

/**
 * Marker generico selezionabile
 */
SelectableMarker.prototype = new GenericMarker();
function SelectableMarker(defaultErrorText, markerInfo, opts, addPopup,
        iconsPath, selectedIcon, mapHandler) {

    CallParentConstructor(this, GenericMarker, defaultErrorText, markerInfo, opts, addPopup);

    this.iconsPath = iconsPath;
    this.selectedIcon = selectedIcon;
    this.mapHandler = mapHandler;
    this.selected = false;
    this.defaultImage = this.getIcon().image;
}

SelectableMarker.prototype.selectMarker = function(openPopup) {
    if(this.mapHandler && this.mapHandler.resetIcons)
        this.mapHandler.resetIcons();
    this.selected = true;
    this.setImage(this.iconsPath + this.selectedIcon);
    if(openPopup)
        this.openInfoWindowHtml(this.html);
}

SelectableMarker.prototype.isSelected = function() {
    return this.selected;
}

SelectableMarker.prototype.resetIcon = function() {
    this.selected = false;
    this.setImage(this.defaultImage);
}

/**
 * Marker per mappa della topten utente
 */
UserTopTenMarker.prototype = new SelectableMarker();
function UserTopTenMarker(preferredIconPath, mapHandler, canvasId,
        defaultErrorText, labels, markerInfo, selectedIcon, opts, listHandler) {

    CallParentConstructor(this, SelectableMarker, defaultErrorText, markerInfo, opts, true,
        preferredIconPath, selectedIcon, mapHandler);
    this.html = '';
    this.canvasId = canvasId;
    this.labels = labels;
    this.editable = markerInfo.editable;
    this.listHandler = listHandler;
    this.createPopupHtml();
}

UserTopTenMarker.prototype.getMarkerInfo = function() {
    return this.markerInfo;
}

UserTopTenMarker.prototype.createPopupHtml = function() {
    this.html =
        '<div class="ballonWrapper">' +
            '<div class="baloonTitle">' +
                '<span>' + this.labels.locationLabel + '</span>' +
            '</div>' +
            '<div class="baloonValue">' +
                '<span>' + this.markerInfo.location + '</span>' +
            '</div>';
    if(this.markerInfo.descrizione.length != 0) {
        this.html +=
            '<div class="baloonTitle2">' +
                '<span>' + this.labels.descriptionLabel + '</span>' +
            '</div>' +
            '<div class="baloonValue">' +
                '<span>' + this.markerInfo.descrizione + '</span>' +
            '</div>';
    }
    if(this.markerInfo.categories.length != 0) {
        this.html +=
            '<div class="baloonTitle2">' +
                '<span>' + this.labels.categoriesLabel + '</span>' +
            '</div>' +
            '<div class="baloonValue">' +
                '<ul>';
        for(var i = 0, l = this.markerInfo.categories.length; i < l; i++) {
            this.html +=
                    '<li style="display: block;">' + this.markerInfo.categories[i].value + '</li>';
        }
            
        this.html +=
                '</ul>' +
            '</div>';
    }
    this.html +=
        '</div>';
    if(this.editable) {
        this.html +=
        '<div class="baloonEditor">' +
            '<a class="resetForm noUnderline" href="javascript://" onclick="' +
            '$(\'' + this.canvasId + '\').mapHandler.deleteNode(' +
            this.markerInfo.nid + ')">' + this.labels.removeLabel + '</a>' +

            '<a class="resetForm noUnderline" href="javascript://" onclick="' +
            '$(\'' + this.canvasId + '\').mapHandler.changeNodeType(' +
            this.markerInfo.nid + ', ' + (!this.markerInfo.preferred) + ')">' +
            (this.markerInfo.preferred ? this.labels.toVisitedLabel : this.labels.toFavouriteLabel) + '</a>' +
        '</div>';
    }
}

UserTopTenMarker.prototype.showPopup = function() {
    if(this.listHandler) {
        this.listHandler.updateDetails(this.markerInfo);
        this.selectMarker(true);
    }
    else {
        this.openInfoWindowHtml(this.html);
    }
}

/**
 * Marker per mappa della topten della community
 */
PlacesMapMarker.prototype = new SelectableMarker();
function PlacesMapMarker(themePath, defaultErrorText, locationLabel, mapHandler, markerInfo, selectedIcon, opts) {
    CallParentConstructor(this, SelectableMarker, defaultErrorText, markerInfo, opts, true,
        themePath, selectedIcon, mapHandler);
    this.locationLabel = locationLabel;

    this.mapHandler = mapHandler;
    this.html = '';
    this.createPopupHtml();
}

PlacesMapMarker.prototype.createPopupHtml = function() {
    this.html =
        '<div style="width: 210px; height: 50px; overflow: auto;">' +
            '<div style="color: #505050">' +
                '<span>' + this.locationLabel + '</span>' +
            '</div>' +
            '<div>' +
                '<span>' + this.markerInfo.location + '</span>' +
            '</div>' +
        '</div>';
}

PlacesMapMarker.prototype.showPopup = function() {
    this.selectMarker(true);
    if(this.mapHandler && this.mapHandler.markerSelected) {
        this.mapHandler.markerSelected(this.markerInfo);
    }
}

/**
 * Marker per mappa della foto
 */
PhotoMapMarker.prototype = new GenericMarker();
function PhotoMapMarker(defaultErrorText, locationLabel, markerInfo, opts, avoidPopup) {
    CallParentConstructor(this, GenericMarker, defaultErrorText, markerInfo, opts, !avoidPopup);
    this.html = '';
    this.locationLabel = locationLabel;
    this.createPopupHtml();
}

PhotoMapMarker.prototype.createPopupHtml = function() {
    this.html =
        '<div style="width: 210px; height: 50px; overflow: auto;">' +
            '<div style="color: #505050">' +
                '<span>' + this.locationLabel + '</span>' +
            '</div>' +
            '<div>' +
                '<span>' + this.markerInfo.location + '</span>' +
            '</div>' +
        '</div>';
}

PhotoMapMarker.prototype.showPopup = function() {
    this.openInfoWindowHtml(this.html);
}

/**
 * Marker per mappa degli itinerari
 */
TravelBlogMarker.prototype = new GenericMarker();
function TravelBlogMarker(preferredIconPath, mapHandler, canvasId,
        defaultErrorText, labels, markerInfo, selectedIcon, opts) {

    CallParentConstructor(this, GenericMarker, defaultErrorText, markerInfo, opts, true,
        preferredIconPath, selectedIcon, mapHandler);
    this.html = '';
    this.canvasId = canvasId;
    this.labels = labels;
    this.editable = markerInfo.editable;
    this.createPopupHtml();

    if(!this.markerInfo.stayInPage) {
        var me = this;
        if(this.markerInfo.hidePopup) {
            if(this.clickListener) {
                google.maps.Event.removeListener(this.clickListener);
                this.clickListener = null;
            }
        }
        this.clickListener = google.maps.Event.addListener(this, "click",
            function() {window.location = me.markerInfo.link});
    }
}

TravelBlogMarker.prototype.getMarkerInfo = function() {
    return this.markerInfo;
}

TravelBlogMarker.prototype.createPopupHtml = function() {
    this.html =
        '<div style="width: 210px; height: 80px;">' +
            '<div style="color: #505050">' +
                '<span>' + this.labels.location + '</span>' +
            '</div>' +
            '<div>' +
                '<span>' + this.markerInfo.location + '</span>' +
            '</div>';
    if(this.markerInfo.nextLink){
        this.html +=
            '<div style="margin-left: 5px">' +
                '<a  style="margin-left: 5px!important" class="resetForm" href= ' + this.markerInfo.nextLink + '>' + this.markerInfo.nextLinkdescr + '</a>' +
            '</div>';
    }
    if(this.markerInfo.link && this.markerInfo.showMarkerLink){
        this.html +=
                '<div style="margin-left: 5px">' +
                   '<a  style="margin-left: 5px!important" class="resetForm" href= ' + this.markerInfo.link + '>' + this.markerInfo.linkdescr + '</a>' +
                '</div>';
    }
    if(this.markerInfo.prevLink){
        this.html +=
            '<div>' +
                '<a  style="margin-left: 5px!important" class="resetForm" href= ' + this.markerInfo.prevLink + '>' + this.markerInfo.prevLinkdescr + '</a>' +
            '</div>';
    }
    this.html +=
        '</div>';
}

TravelBlogMarker.prototype.showPopup = function() {
    this.openInfoWindowHtml(this.html);
}

/**
 * Marker per mappa dei consigli
 */
AdviceMarker.prototype = new GenericMarker();
function AdviceMarker(defaultErrorText, markerInfo, opts, addPopup, labels, avoidShowLink) {
    CallParentConstructor(this, GenericMarker, defaultErrorText, markerInfo, opts, addPopup);

    this.labels = labels;
    this.avoidShowLink = avoidShowLink;
    this.html = '';
    this.createPopupHtml();
}

AdviceMarker.prototype.createPopupHtml = function() {
    this.html =
        '<div class="ballonWrapper">' +
            (this.markerInfo.address ?
            '<div class="baloonTitle">' +
                '<span>' + this.labels.locationLabel + '</span>' +
            '</div>' +
            '<div class="baloonValue">' +
                '<span>' + this.markerInfo.address + '</span>' +
            '</div>' :
            '') +
            '<div class="baloonTitle">' +
                '<span>' + this.labels.titleLabel + '</span>' +
            '</div>' +
            '<div class="baloonValue">' +
                '<span><b>' + this.markerInfo.title + '</b></span>' +
            '</div>' +
            '<div class="baloonTitle">' +
                '<span>' + this.labels.descriptionLabel + '</span>' +
            '</div>' +
            '<div class="baloonValue">' +
                '<span>' + this.markerInfo.body + '</span>' +
            '</div>' +
        '</div>' +
        (this.avoidShowLink ?
        '' :
        '<div class="baloonFooter">' +
            '<div class="right">' +
                '<a href="' + this.markerInfo.itemLink + '">Visualizza</a>' +
            '</div>' +
        '</div>');
}

AdviceMarker.selectMarker = function(itemID, scrollTo) {
    var items = $$('div.advicesWrapper ul li');
    for(var i = 0; i < items.length; i++) {
        if(items[i].id != itemID) {
            Element.removeClassName(items[i], 'on');
        }
        else {
            Element.addClassName(items[i], 'on');
        }
    }

    if(scrollTo) {
        Effect.ScrollTo(itemID);
    }
}

AdviceMarker.prototype.showPopup = function() {
    AdviceMarker.selectMarker(this.markerInfo.itemID, false);
    this.openInfoWindowHtml(this.html);
}
