var map;
var geo;
var VA_DEFAULT_ICON_NAME='icon_green';

jQuery.noConflict();
jQuery(document).ready(function() {
    activateSearchAddress('#saddress', '#editmarkerform #id_location');
    //activateConfirmSubmit('#editmarkerform') 
});

function editlink(idlink){
    var ahref = jQuery('#'+idlink).attr('href');
    var atext = jQuery('#'+idlink).text();
    jQuery("#id_url").attr('value',""+ahref);
    jQuery("#id_anchor").attr('value',""+atext);
    showeditlinkform();
    jQuery('#frm_editlink').find("input[name='key']").remove();
    jQuery("#frm_editlink").append('<input type="hidden" name="key" value="'+idlink+'"/>');
    return false;
}
function reseteditlinkform(){
    jQuery('#frm_editlink').find("input[name='key']").remove();
}
function showeditlinkform(){
    jQuery('#hideformbtn').hide();
    jQuery('#showformbtn').show();
    //jQuery('#frm_editlink').show();
    jQuery('#frm_editlink').fadeIn(500); 
}
function hideeditlinkform(){
    jQuery('#hideformbtn').show();
    jQuery('#showformbtn').hide();
    //jQuery('#frm_editlink').hide();
    jQuery('#frm_editlink').fadeOut(500);
}
function hiddenElement(idelement){
    jQuery('#'+idelement).hide();
}
function showElement(idelement){
    jQuery('#'+idelement).show();
}

function markerInfo(){
    
}

function activateConfirmSubmit(idform){
    jQuery(idform).submit(function() {
        if (confirm('Seguro que quieres guardar los datos')) return true;
        else return false;
    });  
}

function activateSearchAddress(idlink, idInputStrAddress){
    jQuery(idlink).click(function(){
        var address= jQuery(idInputStrAddress).val();
        search_address(address, 15);
        return false;
    }); 
}

function loadgmap(lat, lng, zoom) {
    if (GBrowserIsCompatible()) {
        map=new GMap2(document.getElementById('map'));
        map.setCenter(new GLatLng(lat,lng),zoom);
        map.setUIToDefault();
        map.setMapType(G_PHYSICAL_MAP);
        map.clearOverlays();
        geo = new GClientGeocoder();
    }
}

function addMapEventClick(){
    GEvent.addListener(map, 'click', function(marker,point){ 
        if (!marker) {
            map.clearOverlays()
            newEditableMarker(new GLatLng(point.y, point.x),'', VA_DEFAULT_ICON_NAME); //G_DEFAULT_ICON
        }
    });
}

function search_address(address, dfzoom){
    geo.getLocations(address, function (result){
        map.clearOverlays();
        if (result.Status.code == G_GEO_SUCCESS){
            lat=result.Placemark[0].Point.coordinates[1];
            lng=result.Placemark[0].Point.coordinates[0];                
            map.setCenter(new GLatLng(lat, lng), dfzoom);
            drawDragableMarker(new GLatLng(lat, lng), address, VA_DEFAULT_ICON_NAME);
        }else{ 
            alert("No hay ningun resultado");
        } 
    });
    return false;
}

function updateLatLngZoomFields(point, zoom){
    jQuery('#id_lat').val(point.lat());
    jQuery('#id_lng').val(point.lng());
    if( typeof(zoom) != 'undefined'){
        jQuery('#id_zoom').val(zoom);
    }
}
function resetLatLngFields(){
    jQuery('#id_lat').val('');
    jQuery('#id_lng').val('');
}


function getIcon(icon_name) {
    if (icon_name =='' || icon_name == G_DEFAULT_ICON){
        return G_DEFAULT_ICON;
    }
    var icon = new GIcon();
    icon.image =  "/sitemedia/markers/img/mkicons/" + icon_name + ".png";
    icon.shadow = "/sitemedia/markers/img/mkicons/icon_shadow.png";
    icon.iconSize = new GSize(50, 50);
    icon.shadowSize = new GSize(50, 50);
    icon.iconAnchor = new GPoint(18, 50);
    icon.infoWindowAnchor = new GPoint(5, 1);
    return icon;
}

function newEditableMarker(point, html, icon_name) {
    var marker=new GMarker(point, {icon: getIcon(icon_name), draggable: true, title:'prueba'});
    map.addOverlay(marker);
    updateLatLngZoomFields(point, map.getZoom());
    GEvent.addListener(marker, "drag", function() {
        var point = marker.getPoint();
        updateLatLngZoomFields(point);    
    });    
    GEvent.addListener(marker, "dragend", function() {
        var point = marker.getPoint();
        var zoom = map.getZoom();
        updateLatLngZoomFields(point, zoom);    
    });
    GEvent.addListener(marker, "click", function() {
        map.clearOverlays();
        resetLatLngFields();
    });
} 

function drawDragableMarker(point, html, icon_name) {
    var marker=new GMarker(point, {icon: getIcon(icon_name), draggable: true, title:'prueba'});
    map.addOverlay(marker);
    updateLatLngZoomFields(point, map.getZoom());
    GEvent.addListener(marker, "drag", function() {
        var point = marker.getPoint();
        updateLatLngZoomFields(point);    
    });    
    GEvent.addListener(marker, "dragend", function() {
        var point = marker.getPoint();
        var zoom = map.getZoom();
        updateLatLngZoomFields(point, zoom);    
    });
} 

function drawSimpleMarker(point, icon_name, title, html) {
    var marker=new GMarker(point, {icon: getIcon(icon_name), title:title});
    map.addOverlay(marker);
    GEvent.addListener(marker, "click", function() {
        map.openInfoWindowHtml(marker.getPoint(),html,{maxWidth:350, pixelOffset:new GSize(0, -13)});        
    });
} 

function markersFromResults(results){
    var item, html;
    var bounds = new GLatLngBounds;
    
    for (item in results){
        html = jQuery('.mk:eq('+item+') .mk_blockinfo').html();
        html += jQuery('.mk:eq('+item+') .mk_info').html();
        drawSimpleMarker(new GLatLng(results[item].lat, results[item].lng), VA_DEFAULT_ICON_NAME, results[item].name, '<div class="mkwinfo">'+html+'</div>');
        bounds.extend( new GLatLng(results[item].lat,results[item].lng) );
    }
    map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds)-1);
}

