﻿//  MUCHOVIAJE GEO
//      :: Versión:     0.1 [build 20090609]
//
//  ¡¡Recuerda anotar el cambio de versión!!

//CONFIGURATION
var __loading_html_body = "Cargando...";



//JSON HANDLERS
//***************************************************************
//***************************************************************
//***************************************************************
function __do_json_command(name,parammeters)
{
    $.post(service_path, parammeters, 
        function(data){
            if(data.length >0) {
 	            //alert(data);
 	            data = data.replace(/Thread was being aborted./,"");
 	            eval("var o=" + data + ";");
 	            __route_json_command_response(o,name,parammeters);
            }
        });
}
function __route_json_command_response(rs,name,parammeters)
{
    if (rs.error == 1)
    {
        __route_json_command_response_error(rs);
        return;
    }
    
    switch(rs.cmd)
    {
        case "openwindow":
            var mk = GetMarker(rs.marker_id);
            if (mk != null)
                OpenHtmlInfoWindow(mk,rs.iw_anchorx,rs.iw_anchory,rs.iw_body);
            break;
        case "addmarkers":
            for(var im = 0; im < rs.markers.length; im++)
            {
                CreateMarker(rs.markers[i]);
            }
            break;
        case "dosnap":
            break;
    }
}
function __route_json_command_response_error(rs)
{
}
//***************************************************************
//***************************************************************
//***************************************************************

//MARKER MANAGER
//***************************************************************
//***************************************************************
//***************************************************************
var markers = new Array();

function AddMarker(_marker)
{
    var _marker_box = {marker:_marker,id:markers.length};
    
    markers[markers.length] = _marker_box;
}
function DeleteMarker(id)
{
    if (markers.length >= id)
        markers = markers.splice(id,1);
}
function GetMarker(id)
{
    for(var i=0;i<markers.length;i++)
        if (markers[i].id == id)
            return markers[i];
    
    return null;
}
//***************************************************************
//***************************************************************
//***************************************************************

//MAP JAVASCRIPT COMMANDS
//***************************************************************
//***************************************************************
//***************************************************************
function ClearMarkers()
{
    CloseInfoWindow();
//    for(var i=0; i < markers.length; i++)
//    {
//        map.removeOverlay(markers[i]);
//    }
    markers = new Array();
}
function OpenHtmlInfoWindow(marker,anchorx,anchory,body)
{
}
function CreateMarker(obj)
{
//    var point = new GLatLng(obj.latitude, obj.longitude);
    var marker;

    if (obj.iconpath != "")
    {
//        var new_marker_icon = new GIcon(); 
//        new_marker_icon.image = obj.iconpath;
//        new_marker_icon.iconSize = new GSize(obj.iconheight,obj.iconwidth);
    }

    //ICON
    if (obj.iconpath != "")
        marker = new GMarker(point, {title:obj.windowtitle});
    else
        marker = new GMarker(point, {title:obj.windowtitle, icon:new_marker_icon});

    //ACTION COMMAND
    switch(obj.action)
    {
        case "SHOW_WINDOW":
            GEvent.addListener(marker, "click", 
                function() 
                {
                    marker.openInfoWindowHtml(__loading_html_body);
                }); 
            break;
    }

//    map.addOverlay(marker);
    markers[markers.length] = marker;

    return markers.lenght-1;
}
function AddMarkerToMap(marker)
{
}
//***************************************************************
//***************************************************************
//***************************************************************



function CreateMarkerFromObject(obj)
{
//    var point = new GLatLng(obj.latitude, obj.longitude);
    var marker;
    
    if (obj.iconpath != "")
    {
//        var new_marker_icon = new GIcon(); 
//        new_marker_icon.image = obj.iconpath;
//        new_marker_icon.iconSize = new GSize(obj.iconheight,obj.iconwidth);
    }

//    //ICON
//    if (obj.iconpath != "")
//        marker = new GMarker(point, {title:obj.windowtitle});
//    else
//        marker = new GMarker(point, {title:obj.windowtitle, icon:new_marker_icon});
    
    //ACTION COMMAND
    switch(obj.action)
    {
        case "loadbubble":
            GEvent.addListener(marker, "click", 
                function() 
                {
                    marker.openInfoWindowHtml("cargando...");
                    LoadJsonCountryWindow(obj.code, marker);
                }); 
            break;
    }

//    map.addOverlay(marker);
    markers[markers.length] = marker;
    
    return markers.lenght-1;
}
function CreateMarker(latitude, longitude, poi_title, content) 
{  
//    var point = new GLatLng(latitude,longitude); 
    var marker = new GMarker(point,{ title:poi_title }); 
    
//        GEvent.addListener(marker, "click", 
//            function() 
//            {
//                marker.openInfoWindowHtml("<h2>" + title + "</h2><p>" + content + "</p>");  
//            }); 
        
//    map.addOverlay(marker);  
    
    markers[markers.length] = marker; 
    
    return markers.length-1;
}
var default_icon_width = 20;                var default_icon_height = 20;
var default_shadow_icon_width = 20;         var default_shadow_icon_height = 20;
var default_icon_anchor_x = 5;              var default_icon_anchor_y = 1;
var default_info_window_anchor_x = 5;       var default_info_window_anchor_y = 1;

function CreateMarkerWithIcon(latitude, longitude, poi_title, content, image_source, image_shadow_source,icon_width,icon_height,icon_shadow_width,icon_shadow_height,icon_anchor_x,icon_anchor_y,info_window_anchor_x,info_window_anchor_y) 
{
    //alert(latitude+"::"+ longitude+"::"+ poi_title+"::"+ content+"::image_source="+ image_source+"::image_shadow_source="+ image_shadow_source+"::icon_width="+icon_width+"::icon_height="+icon_height+"::icon_shadow_height="+icon_shadow_height+"::icon_shadow_width="+icon_shadow_width+"::icon_anchor_x="+icon_anchor_x+"icon_anchor_y=::"+icon_anchor_y+"::info_window_anchor_x="+info_window_anchor_x+"::info_window_anchor_y="+info_window_anchor_y);
    //Create MV-POI icon...
//    var new_marker_icon = new GIcon(); 
//    new_marker_icon.image = image_source;
//    if (image_shadow_source != "")
//    {
//        new_marker_icon.shadow = image_shadow_source;
//        if ((icon_shadow_width != 0) && (icon_shadow_height != 0))
//            new_marker_icon.shadowSize = new GSize(icon_shadow_width, icon_shadow_height);
//        else
//            new_marker_icon.shadowSize = new GSize(default_shadow_icon_width, default_shadow_icon_height);
//    }
//    if ((icon_width != 0) && (icon_height != 0))
//        new_marker_icon.iconSize = new GSize(icon_width, icon_height);
//    else
//        new_marker_icon.iconSize = new GSize(default_icon_width, default_icon_height);
    
    //if ((icon_anchor_x != 0) || (icon_anchor_y != 0))
//        new_marker_icon.iconAnchor = new GPoint(icon_anchor_x, icon_anchor_y);
    //else
        //new_marker_icon.iconAnchor = new GPoint(default_icon_anchor_x, default_icon_anchor_y);
    
//    if ((info_window_anchor_x != 0) && (info_window_anchor_y != 0))
//        new_marker_icon.infoWindowAnchor = new GPoint(info_window_anchor_x, info_window_anchor_y);
//    else
//        new_marker_icon.infoWindowAnchor = new GPoint(default_info_window_anchor_x, default_info_window_anchor_y);
    
    //Create MV-POI object...
//    var point = new GLatLng(latitude,longitude); 
//    var marker = new GMarker(point,{ icon:new_marker_icon, title:poi_title }); 
    
//        GEvent.addListener(marker, "click", 
//            function() 
//            {
//                marker.openInfoWindowHtml("<h2>" + title + "</h2><p>" + content + "</p>");
//            });
    
//    map.addOverlay(marker);  
//    markers[markers.length] = marker; 
    
    return markers.length-1;
}


var polys = new Array();
function AddPolylineToMap(polynodes,colorcode,linewidth,opacity)
{
    var poly = new GPolyline(polynodes,colorcode,linewidth,opacity);
//    map.addOverlay(poly);
    
    polys[polys.length] = poly;
    
//    map.addOverlay(poly);
}
function ZoomToFit()
{
//    var bounds = new GLatLngBounds();
//    for(var i=0;i<markers.length-1;i++)
//    {
//        bounds.extend(markers[i].getLatLng());
//    }
//    map.setZoom(map.getBoundsZoomLevel(bounds));
//    map.setCenter(bounds.getCenter());
}

//estos solo descomentarlo para local
//        function LoadSnapFromServer(id)
//    {
//        var path = "http://entradas.muchoviaje.com/parques/aspx/buscador.aspx?___gm_async=1&___cmd=getphase&___code=" + id;
//        //alert(path)
//        $.post(path, "", 
//            function(data)
//            {
//                var obj= data;
//                LoadSnapToInterface(obj);
//            });
//    }
//    
function LoadSnapToInterface(snap_stream)
{
    snap_stream = snap_stream.replace(/Thread was being aborted./,"");
    eval("var snap = " + snap_stream + ";");
    
    //alert(snap_stream )
    //Clear previous levels?
    if (snap.clear == 1)
    {
        ClearMarkers();
    }
    //alert('LoadSnapToInterface1')
    if (snap.bounds != null)
        if ((snap.bounds.nelatitude != null) && (snap.bounds.nelongitude != null) && (snap.bounds.swlatitude != null) && (snap.bounds.swlongitude != null))
        {   //Load bounds to map
//            var set_bounds = 
//                    new GLatLngBounds(
//                        new GLatLng(snap.bounds.swlatitude,snap.bounds.swlongitude),
//                        new GLatLng(snap.bounds.nelatitude,snap.bounds.nelongitude));
//                map.setCenter(set_bounds.getCenter(), map.getBoundsZoomLevel(set_bounds));
        }

    //Add pois to map...
    for(var i=0;i<snap.pois.length;i++)
    {
        AddPoiFromJson(snap.pois[i]);
    }
    
    //Add polys to map...
    for(var i=0;i<snap.polys.length;i++)
    {
        AddPolyFromJson(snap.polys[i]);
    }
    
    //On load script...
    if (snap.onload != null)
        if (snap.opload != "")
        {
            eval(snap.onload);
        }
}
var _iw = null;

function CloseInfoWindow()
{
//    map.closeExtInfoWindow();
}

function AddPoiFromJson(poi)
{
    var index = -1;
        
    if ((poi.iconpath != null) && (poi.iconpath != ""))
        index = CreateMarkerWithIcon(poi.latitude, poi.longitude, poi.title, poi.body, poi.iconpath, "",poi.iconwidth,poi.iconheight,0,0,poi.iconoffsetx,poi.iconoffsety,0,0);
    else
        index = CreateMarker(poi.latitude, poi.longitude, poi.title, "");
    
    //If error, abort method...
    if (index == -1)
        return;
        
    //Do Javascript
    if (poi.doclick != null)
        if (poi.doclick != "")
        {
            GEvent.addListener(markers[index], "click",
                function()
                {
                    eval(poi.doclick);
                });
        }
        
    //Info window...
    if (poi.infowindow != null)
    {
        
        if (poi.infowindow.style == null)
        {
            GEvent.addListener(markers[index], "click", 
                function() 
                {
                    markers[index].openInfoWindowHtml(poi.infowindow.body);
                });
        }
        else
        {
            GEvent.addListener(markers[index], "click", 
                function() 
                {   
//                    markers[index].openExtInfoWindow(map,poi.infowindow.style,poi.infowindow.body);
                });
        }
    }
}
function AddPolyFromJson(poly)
{
    var pnodes = new Array();
    var snodes = "";
    
    for (var i=0;i<poly.nodes.length;i++)
    {
        var node = poly.nodes[i];
        
//        var p = new GLatLng(node.latitude,node.longitude);
//        pnodes[pnodes.length-1] = p;
        
        if (snodes != "")
            snodes += ",";
      
//        snodes += "new GLatLng(" + node.latitude + "," + node.longitude + ")";
    }
    eval("pnodes = [" + snodes + "];");
    
    AddPolylineToMap(pnodes,poly.color,poly.linewidth,poly.opacity);
}
