/*kml js by pix*/
jQuery.fn.kml = function(options)
{
    var settings = jQuery.extend
    (
        {
              kml:"",
              map:null
        }, options
    );
    var lang          = null;
    var map  = settings.map.getMapInstance();
    
    kml_style = {
          "default": 
          {
            strokeWidth: 1,
            pointRadius: 4,
            strokeColor: "blue",
            fillColor: "red",
            cursor: "pointer",
            strokeOpacity: 0.7
          },
          "select": 
          {
            strokeWidth: 10,
            pointRadius: 6,
            strokeColor: "#008bae",
            fillColor: "#008bae",
            cursor: "pointer",
            strokeOpacity: 0.7
          }
    }
    
    jQuery.getJSON(settings.langcontroller+'/'+new Date().getTime(),{},function(data){ lang = data; });

    kmlLayer = new OpenLayers.Layer.GML("KML", settings.kml, 
    {
              projection: new OpenLayers.Projection("EPSG:4326"),
              format: OpenLayers.Format.KML,
              formatOptions: {
              extractStyles: true,
              extractAttributes: true
              },style:kml_style['default']
    });
    
    map.addLayers([kmlLayer]);

    kmlLayer.events.register
    (
            "loadend",
            kmlLayer,
            zoomExtent
    );

    selectControl = new OpenLayers.Control.SelectFeature(kmlLayer,
                {onSelect: onFeatureSelect, onUnselect: onFeatureUnselect,hover: false,selectStyle: kml_style['select']});
    map.addControl(selectControl);
    selectControl.activate();

    function onPopupClose(evt)
    {
        selectControl.unselect(selectedFeature);
    }

    function onFeatureSelect(feature)
    {

        selectedFeature = feature;
        
        popupc= "<div>";
          popupc+="<div class='popupTitle'><div class='popupDetailToggle' id='popupDetailToggle'></div>"+feature.attributes.name+"</div>";
            popupc+="<div class='popupDetail' style='display:none' id='popup_detail'>";
            popupc+="<table>";
              popupc+="<tr>";
                popupc+= "<td><b>"+feature.attributes.description+"</b></td>";
              popupc+="</tr>";
              
              popupc+="<tr>";
                popupc+="<td>";
                  popupc+=lang.srch_popup_routeplan_label+"<br>";
                popupc+="</td>";
              popupc+="</tr>"; 
              popupc+="<tr>";
                popupc+="<td>";
                  popupc+=lang.srch_route_plan_start_label+" : <img id='optims' style='cursor:pointer' src='"+lang.image_start_optim_menu+"' />";
                  popupc+=" | "+lang.srch_route_plan_via_label+" : <img id='optimv' style='cursor:pointer' src='"+lang.image_via_optim_menu+"' /> | ";
                  popupc+=lang.srch_route_plan_end_label+" : <img id='optime' style='cursor:pointer' src='"+lang.image_stop_optim_menu+"' />";
                popupc+="</td>";
             popupc+="</td>";
            popupc+="</tr>";
              
            popupc+="<tr>";
              popupc+="<td id='zoomtoloc' style='cursor:pointer' >";
                popupc+=lang.srch_popup_zoom_label+"&nbsp;<img src='"+lang.image_blue_zoom+"' /><br>";
              popupc+="</td>";
            popupc+="</tr>";
            popupc+="</table>";
            popupc+="</div>";
          popupc+="</div>";
        
        la = feature.geometry.getBounds().getCenterLonLat();
        popup = new OpenLayers.Popup.Anchored("chicken",
                                 la,
                                 new OpenLayers.Size(200,100),
                                 popupc,
                                 null, true, onPopupClose);
                                 
        
        
        feature.popup = popup;
        map.addPopup(popup);
        popup.updateSize();
        jQuery("#popupDetailToggle").toggle(
          function (){jQuery("#popup_detail").show();popup.updateSize();},function (){jQuery("#popup_detail").hide();popup.updateSize();}
        );
        
        lawgs = new OpenLayers.LonLat(la.lon,la.lat).transform(
                   new OpenLayers.Projection("EPSG:900913"),
                   new OpenLayers.Projection("EPSG:4326")
        );

        jQuery('#optime').click(function(){jQuery.addOptimPoint(jQuery.routens.typeend,lawgs.lon,lawgs.lat,feature.attributes.name )}); 
        jQuery('#optimv').click(function(){jQuery.addOptimPoint(jQuery.routens.typevia,lawgs.lon,lawgs.lat,feature.attributes.name )});
        jQuery('#optims').click(function(){jQuery.addOptimPoint(jQuery.routens.typestart,lawgs.lon,lawgs.lat,feature.attributes.name )});    
        jQuery('#zoomtoloc').click(function(){map.setCenter(la,9)});
        
    }
    
    function onFeatureUnselect(feature)
    {
        map.removePopup(feature.popup);
        feature.popup.destroy();
        feature.popup = null;
    }

    function zoomExtent()
    {
        if (kmlLayer.getDataExtent() == null)
        {
            alert("bad kml !?");
        }
        else
        {
          map.zoomToExtent(kmlLayer.getDataExtent());
        }
    }

}
