var map;
var cx;
var cy;
var z;
var url = window.location.host;  

function loadGmap(coordx, coordy, zoom)
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("gmap"));  
        map.setCenter(new GLatLng(coordy, coordx), zoom);  
        prev = new GLatLng(coordy, coordx);
 
        zoomObj = new GLargeMapControl ();
        map.addControl(zoomObj);
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl ());
        
        GEvent.addListener(map,"click", function(overlay,latlng)
        {     
            if (latlng)
            {
                map.setCenter(new GLatLng(latlng.y, latlng.x), map.getZoom());
            }
        });
    }
}

function zoooooom(x, y)
{
    map.setCenter(new GLatLng(y, x));
    map.setZoom(15);
}

function createMarker(x, y, name, link, id, cont_name, country_name, town_name, addr,idd)
{
    var x = x;
    var y = y;
    var icon_ob = new GIcon(G_DEFAULT_ICON);
    icon_ob.image = "http://" + url + "/tpl/seh/en/images/marker.png";
    icon_ob.iconSize = new GSize(16, 16);
    icon_ob.iconAnchor = new GPoint(8, 8);
    icon_ob.infoWindowAnchor = new GPoint(8, 2);
    icon_ob.shadow = "";
    
    // Set up our GMarkerOptions object
    markerOptions = { icon:icon_ob };
    //alert(x + " == " + y + " == " + name);
    var latlng = new GLatLng(y, x);
    marker = new GMarker(latlng, markerOptions);

    map.addOverlay(marker);
        
    GEvent.addListener(marker,"click", function()
    {
        var myHtml = "<a href='" + link + "'>" + name + "</a><br>"+cont_name+", "+country_name+","+town_name;
//        myHtml += '<br /><a href="#" onclick="zoooooom('+x+','+y+'); return false;">ZOOOOOOOOOOM</a>';
        map.openInfoWindowHtml(latlng, myHtml);
    });
    
    GEvent.addListener(marker,"mouseover", function()
    {
        count_tmp = 0;
        count = 0;
//        top = 0;
        $("a[@id^='hotel_']").each(
            function(i)
            {
                if (this.id == id)
                {
                    count = count_tmp;
                }
                count_tmp = count_tmp +1;
            }
        );
        if (document.getElementById("hotel_box"))
        {
            document.getElementById("hotel_box").scrollTop = count*15;
            $("#" + id).css({backgroundColor:"#666677"});
        }
    });
    
    GEvent.addListener(marker,"mouseout", function()
    {
        if ($("#" + id))
            $("#" + id).css({backgroundColor:""});
    });
}

function loadGmapSimple(coordx, coordy, zoom)
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("gmap")); 
         
        map.setCenter(new GLatLng(coordy, coordx), zoom);  
 
        zoomObj = new GLargeMapControl ();
        map.addControl(zoomObj);
        map.addControl(new GMapTypeControl());
        //map.addControl(new GScaleControl ());
        
        GEvent.addListener(map,"click", function(overlay,latlng)
        {     
            if (latlng)
            {   
                map.setCenter(new GLatLng(latlng.y, latlng.x), map.getZoom());
            }
        });
    }
}

function createMarkerSimple(x,y)  
{
    if ((x!='') && (x!=''))
    {
    var icon_ob = new GIcon(G_DEFAULT_ICON);
    icon_ob.image = "http://" + url + "/tpl/seh/en/images/marker.png"; 
    icon_ob.iconSize = new GSize(16, 16);
    icon_ob.iconAnchor = new GPoint(8, 8);
    icon_ob.infoWindowAnchor = new GPoint(8, 2);
    icon_ob.shadow = "";
    
    // Set up our GMarkerOptions object
    markerOptions = { icon:icon_ob };
    var latlng = new GLatLng(y, x);
    marker = new GMarker(latlng, markerOptions);
    map.addOverlay(marker);
    }    
    /*GEvent.addListener(marker,"click", function()
    {
        var myHtml = name;
        map.openInfoWindowHtml(latlng, myHtml);
    });     */
}

function geocodeInit(coordx,coordy,zoom,address,tzoom)
{
    cx = coordx;
    cy = coordy;
    z = zoom;
    
    if ((coordx == 0) || (coordy == 0) || (coordx == '') || (coordy == ''))
    {
        geocoder = new GClientGeocoder();
        geocoder.getLatLng(
            address,
            function(point)
            {    
                if (point)  
                {
                    map.setCenter(point, tzoom);
                    cx = point.x;
                    cy = point.y;
                    z = tzoom;
                }
            }
        );
    }
            
}

function geocodeInitMarkerSimple(coordx,coordy,zoom,address,tzoom)
{
    cx = coordx;
    cy = coordy;
    z = zoom;
    
    if ((coordx == 0) || (coordy == 0) || (coordx == '') || (coordy == ''))
    {
        geocoder = new GClientGeocoder();
        geocoder.getLatLng(
            address,
            function(point)
            {    
                if (point)  
                {
                    map.setCenter(point, tzoom);
                    cx = point.x;
                    cy = point.y;
                    z = tzoom;
                    createMarkerSimple(cx,cy)  
                }
                else
                    return 1;
            }
        );
    }
    createMarkerSimple(cx,cy)  
}


function geocodeInitMarker(x, y, name, link, id, cont_name, country_name, town_name, addr,idd)
{
    if ((x == 0) || (y == 0) || (x == '') || (y == ''))
    {
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(
            addr,
            function(point)
            {    
                if (point)  
                {
                    x = point.x;
                    y = point.y;
                    createMarker(x, y, name, link, id, cont_name, country_name, town_name, addr,idd)
                }
                else
                    return 1;
            }
        );
    
    }
    createMarker(x, y, name, link, id, cont_name, country_name, town_name, addr,idd) 
}

function loadHotels(coords)
{
    $.getJSON('/index.php?s=ajax&ev=ajax_hotels', {x:coords.x, y: coords.y, z: map.getZoom()}, function (data)
    {
        map.clearOverlays();
        for(id in data.hotels)
        {
            hotel = data.hotels[id];

            tmp_addr = hotel['address'];
            tmp_n = hotel['name'];
            tmp_loc = hotel['town_name'] + ', ' + hotel['district_name'];
            createMarker(hotel['coordx'], hotel['coordy'], tmp_n,'/' + hotel['cont_uname']+'/' + hotel['country_uname'] + '/' + hotel['town_uname'] + '/' + hotel['district_uname'] + '/' + hotel['uname'] + '.htm','hotel_{$v.id}', hotel['cont_name'], hotel['country_name'], tmp_loc, tmp_addr);
        }
    });
}
