//<![CDATA[
//Class MyMap

//Create icon
var icon = new GIcon();
icon.image = "gmarker_icon.png";
icon.shadow = "gmarker_icon_shadow.png";
icon.iconSize = new GSize(24, 42);
icon.shadowSize = new GSize(56, 40);
icon.iconAnchor = new GPoint(12, 40);
icon.infoWindowAnchor = new GPoint(5, 10);

function MyMap(){
    this.onClick = onClick;
    this.search = search;
    this.updateResultList = updateResultList;
    this.updateMap = updateMap;
    this.addMarker = addMarker;
    this.studios_ids = XMLRPC.call(portal_url, "getStudiosIds");
            
    if (GBrowserIsCompatible()){
        this.gmap = new GMap2(document.getElementById("map_slot"));
        this.gmap.addControl(new GMapTypeControl());
        this.gmap.setCenter(new GLatLng(36.526502, -6.289372), 13, G_HYBRID_MAP);
        this.gmap.zoomIn();
        GEvent.bind(this.gmap, "click", this, this.onClick);
            centre = this.gmap.getCenter();
            this.gmap.addControl(new GLargeMapControl());
            }
    }

        function onClick(overlay, point){
            this.gmarker.setPoint(point);
            this.gmap.closeInfoWindow();
        }

        
        function search (){
                text = document.getElementById("search_term").value
                nametext = document.getElementById("name").value
                directiontext = document.getElementById("direction").value
                categorytext = document.getElementById("category").value
                my_spinner = document.getElementById("spinner");
                my_spinner.style.visibility="visible";
                this.info = XMLRPC.call(portal_url, "getStudiosInfo", text,nametext,directiontext,categorytext);
                my_spinner.style.visibility="hidden";
            this.updateResultList();
            this.updateMap();
        }

        function updateResultList(){
            // Hides all studios divs
            ids = this.studios_ids;
            for (var i = 0; i < ids.length; i++){
                            ele = document.getElementById(ids[i]);
                    ele.style.visibility = "hidden";
                    ele.style.display="none";
            }
                    
            // Shows selected studios
            info = this.info;  	    	
            for (var i = 0; i < info.length; i++){
                    ele = document.getElementById(info[i]['id']);
                    ele.style.visibility = "visible";
                    ele.style.display="";
            }
            if (info.length==0){
                ele = document.getElementById("not_results")
                ele.style.visibility = "visible";
                ele.style.display="";


            }else{
                ele = document.getElementById("not_results")
                    ele.style.visibility = "hidden";
                    ele.style.display="none";}
        }

        function updateMap (){
            this.gmap.clearOverlays();
            info = this.info;
            
            for (var i = 0; i < info.length; i++){
                    var ele = info[i];
                    this.addMarker(ele, info.length);
                    }
            }
    
            function addMarker(ele, info_length){
                    var point = new GLatLng(ele["lat"], ele["lng"]);    		
                    var gmarker = new GMarker(point, icon);
                            var f = function (){
                                    // View map
                                    mymap_slot = document.getElementById("map_slot");
                                    if (mymap_slot.style.visibility =='hidden')
                                        MapView();
                                    document.map.gmap.setCenter(point);
                                    onediv = document.getElementById(ele['id']);
                                    gmarker.openInfoWindowHtml(onediv.innerHTML,{maxWidth : 10000});
                                    }
                    GEvent.addListener(gmarker, "click", f);
                    this.gmap.addOverlay(gmarker);
                    var mark_slot = document.getElementById("marker_" + ele['id']);
                    mark_slot.onmousedown = f;
                    file:///home/mmarin/svn/artesania/PloneWeb/skins/web_ecmascript/map_utils.js
                    if (info_length == 1){
                        f();
                    }
            
        }

    //Functions
function setCenter(lat, lng){
    document.map.gmap.setCenter(lat, lng);
}

function MapView(){
    ele = document.getElementById("map_slot");
    my_mapbutton = document.getElementById("mapbutton");
    if (ele.style.visibility == "visible"){
            ele.style.visibility = "hidden";
            my_mapbutton.value="Mostrar";
            ele.style.display = "none";
    } else {
            ele.style.visibility = "visible";
            my_mapbutton.value="Ocultar";
            ele.style.display = "";
    }
}



function load(){
    document.map = new MyMap();
    document.map.search()
}   
