var map;
var localSearch = new GlocalSearch();
var localSearch2 = new GlocalSearch();

var selCountryForMap = "England";

var icon = new GIcon();
//icon.image = "http://www.google.com/mapfiles/marker.png";
icon.image = "http://www.goodhotelguide.com/Images/google-pointer-hotel-shadow.gif";
//icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(34, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);

var iconCesar = new GIcon();
//icon.image = "http://www.google.com/mapfiles/marker.png";
iconCesar.image = "http://www.goodhotelguide.com/Images/google-pointer-cesar-shadow.gif";
//icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
iconCesar.iconSize = new GSize(34, 34);
iconCesar.shadowSize = new GSize(37, 34);
iconCesar.iconAnchor = new GPoint(10, 34);


function fillCountries( id ) 
{
    document.getElementById(id).options[0] = new Option('Select country','0');
    for( var j=1; j<=Country.length; j++ )
    {
        document.getElementById(id).options[j] = new Option(Country[j-1][1],Country[j-1][0]);
    }
}

function setTown( t, tb ) 
{
    document.getElementById(tb).value = document.getElementById( t ).value;
    centerMapByTown(document.getElementById( t ).value, selCountryForMap);
}

function fillRegion( c, r, lblC ) {

    var selCountry = document.getElementById(c).value;
    document.getElementById( lblC ).value = selCountry;
    //alert( selCountry );
    
    document.getElementById(r).disabled = false;
    /*for (ihj = 0; ihj < 1000; ihj++) {
        if( document.getElementById(r).options[ihj]!=null ) {
            document.getElementById(r).options[ihj] = null;
        }
        //document.getElementById(r).remove(ihj);
    }*/
    document.getElementById(r).options.length = 0;

        var ty = 1;
        document.getElementById(r).options[0] = new Option('Select region','0');
        for( var j2=0; j2<Region.length; j2++ )
        {
            if( selCountry == Region[j2][0] )
            {
                document.getElementById(r).options[ty] = new Option(Region[j2][1],Region[j2][1]);
                ty++;
            }
        }
    
    selCountryForMap = document.getElementById(c).options[document.getElementById(c).selectedIndex].text;
}

function fillTown( c, r, t, lblR ) {

    var selCountry = document.getElementById(c).value;
    var selRegion = document.getElementById(r).value;
    
    document.getElementById(t).disabled = false;
    
    var centerCountry = '';
    for( var h=0; h<Country.length; h++ )
    {
        if( selCountry == Country[h][0] ) {
            centerCountry = Country[h][1];
        }
    }
    
    selCountryForMap = document.getElementById(r).options[document.getElementById(r).selectedIndex].text + ", " +document.getElementById(c).options[document.getElementById(c).selectedIndex].text;
    
    //centerMap(selRegion,centerCountry);
    
    document.getElementById( lblR ).value = selRegion;
    
    /*for (ibk = 0; ibk < document.getElementById(t).options.length; i++) {
        //document.getElementById(t).options[ibk] = null;
        document.getElementById(t).remove(ibk);
    }*/
    document.getElementById(t).options.length = 0;

    var ty2 = 1;
    document.getElementById(t).options[0] = new Option('Select town','0');
    
    for( var j3=0; j3<Town.length; j3++ )
    {
        var tmp = Town[j3][0];
        if( selCountry == Town[j3][1] && selRegion.toUpperCase() == Town[j3][2].toUpperCase())
        {
            document.getElementById(t).options[ty2] = new Option(tmp,tmp);
            ty2++;
        }
    }
}

function gup( url, name )
{  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( url );  
    if( results == null )    return "";  
    else    return results[1];
}


function SetPointFromPostcode_CMS(postcode, latId, lngId) {


    localSearch.setSearchCompleteCallback(null,
		function() {

		    if (localSearch.results[0]) {
		        var resultLat = localSearch.results[0].lat;
		        var resultLng = localSearch.results[0].lng;
		        //alert( gup(localSearch.results[0].url,'q') );
		        var point = new GLatLng(resultLat, resultLng);

		        //alert('ustawia: ' + postcode);
		        //callbackFunction(point, gmHotels[gup(localSearch.results[0].url,'q')][0],gmHotels[gup(localSearch.results[0].url,'q')][1]);
		        document.getElementById (latId ).value = resultLat;
                document.getElementById (lngId ).value = resultLng; 
		    } else {
		        //alert("Postcode not found!");
		    }
		});

    //alert('szuka: ' + postcode);	
    localSearch.execute(postcode);
}

function GetPointFromPostcode(postcode) {


           
    localSearch.setSearchCompleteCallback(null,
		function() {

		    if (localSearch.results[0]) {
		        var resultLat = localSearch.results[0].lat;
		        var resultLng = localSearch.results[0].lng;
		        //alert( gup(localSearch.results[0].url,'q') );
		        var point = new GLatLng(resultLat, resultLng);

		        //alert('ustawia: ' + postcode);
		        //callbackFunction(point, gmHotels[gup(localSearch.results[0].url,'q')][0],gmHotels[gup(localSearch.results[0].url,'q')][1]);

		        document.getElementById('gmRes').innerHTML = document.getElementById('gmRes').innerHTML + "UPDATE Hotel SET hotCoord1='" + resultLat + "',hotCoord2='" + resultLng + "' WHERE hotPostcode='" + gup(localSearch.results[0].url, 'q').replace("+"," ") + "'<br />";

		    } else {
		        //alert("Postcode not found!");
		    }
		});

    //alert('szuka: ' + postcode);	
    localSearch.execute(postcode);
}

function usePointFromPostcode(postcode,callbackFunction) 
{
    //alert('wchodzi: ' + postcode);
    
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				//alert( gup(localSearch.results[0].url,'q') );
				var point = new GLatLng(resultLat,resultLng);
				
				//alert('ustawia: ' + postcode);
				//callbackFunction(point, gmHotels[gup(localSearch.results[0].url,'q')][0],gmHotels[gup(localSearch.results[0].url,'q')][1]);
				
				document.getElementById('gmRes').innerHTML = document.getElementById('gmRes').innerHTML + "UPDATE Hotel SET hotCoord1='"+resultLat+"',hotCoord2='"+resultLng+"' WHERE hotPostcode='"+gup(localSearch.results[0].url,'q')+"'<br />";
				
			}else{
			    //alert("Postcode not found!");
			}
		});	
	
	//alert('szuka: ' + postcode);	
	localSearch.execute(postcode);
}

function centerMap2( point )
{
    map.setCenter(point, 8, G_NORMAL_MAP);
}

function centerMapByTown( town , country )
{
    if( town != 'Select town' && town != '' )
    {
        //alert( town + ', ' + country );
        if( map == null ) {
           mapLoad();
           
        }
        localSearch2.setSearchCompleteCallback(null, 
		    function() {
    			
			    if (localSearch2.results[0])
			    {		
				    var resultLat = localSearch2.results[0].lat;
				    var resultLng = localSearch2.results[0].lng;
				    var point = new GLatLng(resultLat,resultLng);
    				
				    map.setCenter(new GLatLng(resultLat,resultLng), 12, G_NORMAL_MAP);
				    for( var b=0; b<all_markers.length; b++ )
                    {
                        all_markers[b].show();
                    }	
			    }else{
			        //alert("Postcode not found!");
			    }
		    });	
        localSearch2.execute(town + ', ' + country );
    }
}

function centerMap( region , country )
{
    if( region != 'Select region' && region != '' )
    {
        //alert( region + ' ' + country );
        if( map == null ) {
           mapLoad();
        }
        localSearch2.setSearchCompleteCallback(null, 
		    function() {
    			
			    if (localSearch2.results[0])
			    {		
				    var resultLat = localSearch2.results[0].lat;
				    var resultLng = localSearch2.results[0].lng;
				    var point = new GLatLng(resultLat,resultLng);
    				
				    map.setCenter(new GLatLng(resultLat,resultLng), 8, G_NORMAL_MAP);
				    for( var b=0; b<all_markers.length; b++ )
                    {
                        all_markers[b].show();
                    }	
			    }else{
			        //alert("Postcode not found!");
			    }
		    });	
        localSearch2.execute(region + ', ' + country );
    }
}

function createMarker(latlng, name, id, cesar) 
{
      if( cesar!= '' ) 
      {
        var marker = new GMarker(latlng, iconCesar, false);
      }
      else 
      {
        var marker = new GMarker(latlng, icon, false);
      }
      //marker.value = 1;
      GEvent.addListener(marker,"mouseover", function() {
        var myHtml = "<a href=\"HotelDetails.aspx?id="+id+"\">"+name+"</a>";
        map.openInfoWindowHtml(latlng, "<a href=\"HotelDetails.aspx?id="+id+"\">"+name+"</a>");
      });
      return marker;
}


var all_markers = new Array(); 

function placeMarkerAtPoint(point, name, id, cesar)
{
    if( map == null ) {
       mapLoad();
    }

    var mark = createMarker(point, name, id, cesar);
    map.addOverlay(mark);
    if( map.getZoom() < 8 ) {
        mark.hide();
    }

    all_markers.push( mark );
	//var marker = new GMarker(point, icon);
	//map.addOverlay(marker);
	
}

function setCenterToPoint(point)
{
	map.setCenter(point, 17);
}

function showPointLatLng(point)
{
	//alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() 
{
		map = new GMap2(document.getElementById("map"));
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(54.622978,-2.592773), 6, G_NORMAL_MAP);	
		
		GEvent.addListener(map, "zoomend", function(oldzoom,zoom) {
            if( zoom < 8 ) {
                for( var b=0; b<all_markers.length; b++ )
                {
                    all_markers[b].hide();
                }
            }
            else {
                for( var b=0; b<all_markers.length; b++ )
                {
                    all_markers[b].show();
                }
            }
        }); 
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

//addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
