

var GStreetMapHighLightPoint = function (id, categoryId, latlng, icon,  opts){
    this.id = id;
    this.categoryId = categoryId;
    this.latlng = latlng;
    this.icon = icon ? icon : null;
    if (!this.id) throw "El punto destacado debe tener un identificador";
    if (!this.categoryId) throw "El punto destacado debe pertenecer a una categoria";
    if (!this.latlng) throw "El punto destacado debe tener unas coordenadas";

    if (opts){
        this.name = opts.name ? opts.name : null;
        this.description  = opts.description ? opts.description : null;
        this.photo = opts.photo ? opts.photo : null;
        this.photoCaption = opts.photoCaption ? opts.photoCaption : null;
        this.panoramic = opts.panoramic ? opts.panoramic : null;   
        this.visible = (opts.visible!=undefined) ? opts.visible : true;
        if (this.visible){
            this.visible = (opts.visibleCategory!=undefined) ? opts.visibleCategory : true;
        }
        this.ajax = opts.ajax ? opts.ajax : false;
    }else{
        this.name = null;
        this.description  = null;
        this.photo = null;
        this.photoCaption = null;
        this.panoramic = null;
        this.visible = true;
        this.ajax = false;
    }
    //Se crea el icono y el marcador
    if (this.icon){
        var icon_i = new GIcon();
        icon_i.image = this.icon;
        icon_i.iconSize = new GSize(35,35);
        icon_i.iconAnchor = new GPoint(0,0);
        icon_i.infoWindowAnchor = new GPoint(15,0);
        this.marker = new GMarker(this.latlng,icon_i);
    }else{
        this.marker = new GMarker(this.latlng);
    }
    //Se conecta el evento para abrir la capa con la información
    GEvent.bind(this.marker,'click',this,this.openInfo);
}

GStreetMapHighLightPoint.prototype = {
    refreshViewState : function (){
        if (this.visible) this.marker.show();
        else this.marker.hide();
    },
    show : function (){
        this.visible = true;
        this.marker.show();
    },
    hide : function (){
        this.visible = false;
        this.marker.hide();
    },
    openInfo : function (){
        var w_opts = {maxWidth : 300};
        if (this.ajax){
            //Se hace una peticion en busca de los datos y se abre
            //la ventana de información
            this.marker.openInfoWindowHtml("<div style='width:150px;height:50px;background:transparent url(images/loading.gif) no-repeat center center;'></div>",w_opts);
            this.updateInfoAjax();
            this.marker.openInfoWindow((new GStreetMapHighLightsPointWindowMaker(this)).getDOMNode(),w_opts);
        }else{
            //Se abre la ventana de información con los datos que
            //hay actualmente
            this.marker.openInfoWindow((new GStreetMapHighLightsPointWindowMaker(this)).getDOMNode(),w_opts);
        }
    },
    updateInfoAjax : function (){
        var ajaxRequest = new Ajax.Request(
                        this.url,
                        {
                                method: 'get',
                                parameters: "hlpid="+this.id,
                                asynchronous: false,
                                onSuccess: (function (transport){
                                    var info = transport.responseJSON;
                                    if (info){
                                        this.name = info.name ? info.name : '';
                                        this.description = info.description ? info.description : '';
                                        this.photo = info.photo ? info.photo : null;
                                        this.photoCaption = info.photoCaption ? info.photoCaption : '';
                                        this.panoramic = info.panoramic ? info.panoramic : null;
                                    }
                                }).bind(this)
                        });
    }
}

/*GStreetMapHighLightsPointWindowMaker*/
var GStreetMapHighLightsPointWindowMaker = function (gsmhlp){
    //Se crea la capa contenedora
    var div = null;
    var node = document.createElement('div');
    node.setAttribute('class','streetview-window-container');
    node.setAttribute('className','streetview-window-container');
    //
    //Se crea la capa para el titulo
    div = document.createElement('div');
    div.setAttribute('class','streetview-window-container-title');
    div.setAttribute('className','streetview-window-container-title');
    div.innerHTML = gsmhlp.name;
    node.appendChild(div);
    //
    //Se crea la capa para la descripción
    div = document.createElement('div');
    div.setAttribute('class','streetview-window-container-description');
    div.setAttribute('className','streetview-window-container-description');
    div.innerHTML = gsmhlp.description;
    node.appendChild(div);
    //
    //Se crea la capa para la panoramica y la foto
    div = document.createElement('div');
    div.setAttribute('class','streetview-window-container-button');
    div.setAttribute('className','streetview-window-container-button');
        var a = null;
        if (gsmhlp.panoramic){
            a = document.createElement('a');
            a.setAttribute('class','pan-command');
            a.setAttribute('className','pan-command');
            a.setAttribute('href','#');
            //a.setAttribute('href',"");
            a.onclick = function (){
                var window = new GStreetMapMultimediaWindow();
                window.openwindow({item : a, title:this.name,panoramic:this.panoramic});
                return false;
            }.bind(gsmhlp);
            a.innerHTML = "Panorámica";
            div.appendChild(a);
        }
        if (gsmhlp.photo){
            a = document.createElement('a');
            a.setAttribute('class','img-command');
            a.setAttribute('className','img-command');
            a.setAttribute('href','#');
            a.onclick = function (){
                var window = new GStreetMapMultimediaWindow();
                window.openwindow({item : a, title:this.name, photo : this.photo, photoCaption : this.photoCaption});
                return false;
            }.bind(gsmhlp);
            a.innerHTML = "Foto";
            div.appendChild(a);
        }
    node.appendChild(div);
    //
    this.node = node;
}

GStreetMapHighLightsPointWindowMaker.prototype = {
    getDOMNode : function (){
        return this.node;
    }
}

/*GStreetMapHighLightsPointList*/
var GStreetMapHighLightsPointsManager = function (){
    this.list = new Array();
}

GStreetMapHighLightsPointsManager.prototype = {
    add : function (hlp){
        this.list.push(hlp);
    },
    refreshHLPointsViewState : function (){
        for(var i=0;i<(this.list.length);i++){
            this.list[i].refreshViewState();
        }
    },
    addHLPointsToMap : function (map){
        //TODO esto hay que iterarlo de otra manera
        for(var i=0;i<(this.list.length);i++){
            map.addOverlay(this.list[i].marker);
            this.list[i].refreshViewState();
        }
    },
    iterateHLPoints : function (closure){
        for(var i=0;i<(this.list.length);i++){
            closure(this.list[i]);
        }
    },
    getHLPoint : function (id){
        for(var i=0;i<(this.list.length);i++){
            if (this.list[i].id==id){
                return this.list[i];
            }
        }
        return null;
    }
}

