$(document).ready(function() {
      
        jQuery.fn.exists = function(){return jQuery(this).length>0;}

        $("#prod_images").yoxview({});
        
        $(".ic_container").capslide({
              caption_color	: 'black',
              caption_bgcolor	: 'white',
              overlay_bgcolor : '#000',
              border			: '0px solid #ccc',
              showcaption	    : false
        });
        
        //$(".image-overlay").ImageOverlay();
      
        // HOUSE ICON
        var icn_house_red = new GIcon(G_DEFAULT_ICON);
        icn_house_red.image = '/images/icn_casa_red.png';
        icn_house_red.iconSize = new GSize(17, 29);
        icn_house_red.iconAnchor = new GPoint(8, 29);
        icn_house_red.shadow = "/images/icn_casa_shadow.png";
        icn_house_red.shadowSize = new GSize(38, 29);
        // --
        
        var icn_house_blue = new GIcon(G_DEFAULT_ICON);
        icn_house_blue.image = '/images/icn_casa_blue.png';
        icn_house_blue.iconSize = new GSize(17, 29);
        icn_house_blue.iconAnchor = new GPoint(8, 29);
        icn_house_blue.shadow = "/images/icn_casa_shadow.png";
        icn_house_blue.shadowSize = new GSize(38, 29);
        // --        
          
                
       if (GBrowserIsCompatible()) {
          map = new GMap2(document.getElementById("map_canvas"));
          map.setMapType(G_NORMAL_MAP);
          map.setUIToDefault();
          
          geocoder = new GClientGeocoder();

          var Torino = new GLatLng(45.0705621, 7.6866186);

          map.setCenter(Torino, 13);
        }      
        
        // url per la ricerca live
        var search_opt;
        
        var vertex = new Array();
        
        //GMAP display
        var bounds = new GLatLngBounds();        
      
      
      
        //default
        $("#opt1").val('Torino');
        $("#opt2").val('');
        
        $("#zona_torino_citta").click(function(){
          $("#opt1").val('Torino');
          $("#opt2").val('');
        });
        
        $("#zona_torino_provincia").click(function(){
          $("#opt1").val('!Torino');
          $("#opt2").val('Torino');
        });
        
        $("#zona_altro").click(function(){
          $("#opt1").val('');
          $("#opt2").val('!Torino');
        }); 
        
        $("#search_opt input").click(function(){
          updateMap();
        });  
        
        
        // set values to detail search form  
        if ($.url.param("opt22") == 'Acquisto'){ $("#opt22_acq").attr('checked','checked');}
        if ($.url.param("opt22") == 'Locazione'){ $("#opt22_loc").attr('checked','checked');}
        
        if ($.url.param("opt21") == 'Residenziale'){ $("#opt21_res").attr('checked','checked');}
        if ($.url.param("opt21") == 'Commerciale'){ $("#opt21_com").attr('checked','checked');}
        
        
        if ($.url.param("opt1") == 'Torino' && $.url.param("opt2") == ''){
          $("#zona_torino_citta").attr('checked','checked');
          $("#opt1").val('Torino');
          $("#opt2").val('');
        }
        
        if ($.url.param("opt1") == '!Torino' && $.url.param("opt2") == 'Torino'){
          $("#zona_torino_provincia").attr('checked','checked');
          $("#opt1").val('!Torino');
          $("#opt2").val('Torino');
        }        

        if ($.url.param("opt1") == '' && $.url.param("opt2") == '!Torino'){
          $("#zona_altro").attr('checked','checked');
          $("#opt1").val('');
          $("#opt2").val('!Torino');
        } 
        
        
        $("#search_opt_detail input").click(function(){
          $("#search_form_detail").submit();
        });       
                
               
        
        

        function updateMap() {
          search_opt = '';

          $("#search_opt input:checked").each(function(i) {      
            search_opt += $(this).attr('name') + '=' + $(this).val()+'&';
          });
          
          $("#search_opt_detail input:checked").each(function(i) {      
            search_opt += $(this).attr('name') + '=' + $(this).val()+'&';
          });
          
          
          if ($("#opt1").exists()) {
            search_opt += 'opt1' + '=' + $('#opt1').val()+'&';
            search_opt += 'opt2' + '=' + $('#opt2').val();
          } else {
            search_opt = location.search;
          }          
          
          //alert(search_opt);
          
          map.clearOverlays(); 
          bounds = new GLatLngBounds();   
        
          // get prod_id if setted in url
          var prod_id = null; 
          var str = /\/(\d+)\?/.exec(location.href);          
          if (str != null) {prod_id = str[1];}
          // --
          
          $.getJSON("/catalogue/viewProducts/browseProductsJSON?" + search_opt, function(json) {
          
                if (json.Locations.length > 0) {
                    for (i=0; i<json.Locations.length; i++) {
                        var location = json.Locations[i];
                        if (prod_id == null || prod_id == location.prod_id) {                        
                          if (location.type == 'Acquisto'){
                            addLocation(location, icn_house_red);
                          } else {
                            addLocation(location, icn_house_blue);
                          }
                        }
                    }
                    
                    level = 0;
                    // zoom out more if only one point
                    if (prod_id != null){level = 3;}
                    
                      
                    zoomToBounds(level);
                }
               // drawOptions();
          });
          
        }
        
        
        function zoomToBounds(level) {
            if (level == null){
              level = 1;
            }
            
            map.setCenter(bounds.getCenter());
            map.setZoom(map.getBoundsZoomLevel(bounds)-level);        
        }        
        
        function addLocation(location,cue) {
            var point = new GLatLng(location.lat, location.lng);       
            var marker = new GMarker(point, {icon: cue});
            map.addOverlay(marker);
            bounds.extend(marker.getPoint());  
            
            GEvent.addListener(marker, "click", function() {
              marker.openInfoWindowHtml(
                '<div style="width: 300px">' +
                '<b>' + location.title + '</b>' + 
                '<br />' +
                location.descr + 
                '<br /><br />' + 
                '<div style="text-align:center">' + location.images_html + '</div>' + 
                '</div>' +
                
                '<div style="text-align: right; font-size: 0.8em">' +
                '<a href="/catalogue/viewProducts/browseProducts/' + location.prod_id + '?' + search_opt + '">Leggi la scheda...</a>' +
                '</div><br />'
              );    
            });         
        }      
        
        function addLocationPoi(lat, lng, cue, name, text) {
            var point = new GLatLng(lat, lng); 
            var marker = new GMarker(point, {icon: cue});
            map.addOverlay(marker);            
            GEvent.addListener(marker, "click", function() {
              marker.openInfoWindowHtml(
                '<div style="width: 300px">' +
                '<b>' + name + '</b><br /><br />' + 
                text +
                '</div><br />'
              );    
            });         
        }           
        
        function drawOptions(){
          // draw turin's center              
          vertex.push(new GLatLng(45.07918769113124, 7.676181793212891));
          vertex.push(new GLatLng(45.07796037560735, 7.6709675788879395));
          vertex.push(new GLatLng(45.06812574756636, 7.663886547088623));
          vertex.push(new GLatLng(45.05866836141897, 7.69068717956543));
          vertex.push(new GLatLng(45.06233631548527, 7.694635391235352));
          vertex.push(new GLatLng(45.06551906013705, 7.698497772216797));
          vertex.push(new GLatLng(45.07455102749882, 7.689828872680664));
          vertex.push(new GLatLng(45.07918769113124, 7.676181793212891)); // first point
          var center = new GPolygon(vertex,"#800",4,.5,'#FAA',0);
  	      map.addOverlay(center);
  	      
  	      // PUT POI ON MAP
          if (poilist.length > 0) {
              for (i = 0; i < poilist.length; i++) {
                  addLocationPoi(poilist[i]['lat'], poilist[i]['lng'], poilist[i]['icon'], poilist[i]['name'], poilist[i]['text']);
              }
          }  	      
        } 
        
        updateMap();
        
      });
