//is IE6
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
//is IE7
var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/;
//is greater than IE7
var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;
//is IE
var isMSIE = /*@cc_on!@*/false;

var map;
    var geocoder;

function removeUserPhoto(id, originalImage, smallImage, largeImage){
	if (confirm("Are you sure you want to remove this image?")){
		xajax_removeUserPhoto(id, originalImage, smallImage, largeImage);
	}
}

//Google map functions
function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(45.523875, -122.670399), 13);
    (45.523875, -122.670399)
    geocoder = new GClientGeocoder();
  }
}

function load() {
  if (GBrowserIsCompatible()) {
    geocoder = new GClientGeocoder();
    map = new GMap2(document.getElementById('map_canvas'));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(45.523875, -122.670399), 13);
  }
}

function searchLocations() {
     var address = document.getElementById('addressInput').value;
     geocoder.getLatLng(address, function(latlng) {
       if (!latlng) {
         alert(address + ' not found');
       } else {
//         searchLocationsNear(latlng);
         document.searchForm.lat.value = latlng.lat();
         document.searchForm.lng.value = latlng.lng();
         document.searchForm.submit();
       }
     });
   }

   function searchLocationsNear() {
//     var radius = document.getElementById('radiusSelect').value;
//     var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
	var searchUrl = 'phpsqlsearch_genxml.php';
     GDownloadUrl(searchUrl, function(data) {
       var xml = GXml.parse(data);
       var markers = xml.documentElement.getElementsByTagName('marker');
       map.clearOverlays();
//       var sidebar = document.getElementById('sidebar');
//       sidebar.innerHTML = '';
       if (markers.length == 0) {
//         sidebar.innerHTML = 'No results found.';
         map.setCenter(new GLatLng(45.523875, -122.670399), 13);
         return;
       }

       var bounds = new GLatLngBounds();
       for (var i = 0; i < markers.length; i++) {
         var sname = markers[i].getAttribute('name');
         var address = markers[i].getAttribute('address');
         var distance = parseFloat(markers[i].getAttribute('distance'));
         var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                 parseFloat(markers[i].getAttribute('lng')));
         
         var marker = createSearchMarker(point, sname, address);
         map.addOverlay(marker);
//         var sidebarEntry = createSidebarEntry(marker, sname, address, distance);
//         sidebar.appendChild(sidebarEntry);
         bounds.extend(point);
       }
       map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
     });
   }

    function createSearchMarker(point, sname, address) {
      var marker = new GMarker(point);
      var html = '<b>' + sname + '</b> <br/>' + address;
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }

    function createSidebarEntry(marker, sname, address, distance) {
      var div = document.createElement('div');
      var html = '<b>' + sname + '</b> (' + distance.toFixed(1) + ')<br/>' + address;
      div.innerHTML = html;
      div.style.cursor = 'pointer';
      div.style.marginBottom = '5px'; 
      GEvent.addDomListener(div, 'click', function() {
        GEvent.trigger(marker, 'click');
      });
      GEvent.addDomListener(div, 'mouseover', function() {
        div.style.backgroundColor = '#eee';
      });
      GEvent.addDomListener(div, 'mouseout', function() {
        div.style.backgroundColor = '#fff';
      });
      return div;
    }

function showAddress(address) {
  initialize();
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          //alert(address + " not found");
        } else {
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml("<a target='_blank' href='http://maps.google.com/maps?daddr=" + address + "' title='Get Direction' />" + address + "</a>");
        }
      }
    );
}
}


function showMarkers(addresses) {  
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(45.523875, -122.670399), 13);
    map.setUIToDefault();

    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    
	
	function createMarker(point) {
          var marker = new GMarker(point);

          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("Marker <b>" + addresses + "</b>");
          });
          return marker;
        }

    
    
    var addressArr = addresses.split(";");    
    for (var i=0; i < addressArr.length; i++){    	
    	var address = addressArr[i];	
    	var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address, function(point, address) {
												        if (point) {
												         	alert(address);
												         	map.setCenter(point,10);
													        map.addOverlay(createMarker(point));
		        										}
		    										});
    }
  }
}
function getBLAddress(blId){
	xajax_getAddress(blId);
}

function showBLDirectory(letter, index){
	for (i=1; i<=26; i++){
		e = document.getElementById("blu"+i);
		f = document.getElementById("bld"+i);
		e.style.textDecoration = 'none';
		f.style.textDecoration = 'none';
	}
	document.getElementById("blu"+index).style.textDecoration = 'underline';
	document.getElementById("bld"+index).style.textDecoration = 'underline';
	document.blForm.letter.value= letter;
	xajax_showBLDirectory(1, 20, xajax.getFormValues('blForm'), 'all_businesslisting.tpl', 'default_buttons.tpl', '', 'grid_paging_type2.tpl', 'blContent', 'blForm');	
}

function showEventDirectory(letter, index){
	for (i=1; i<=26; i++){
		e = document.getElementById("eventu"+i);
		f = document.getElementById("eventd"+i);
		e.style.textDecoration = 'none';
		f.style.textDecoration = 'none';
	}
	document.getElementById("eventu"+index).style.textDecoration = 'underline';
	document.getElementById("eventd"+index).style.textDecoration = 'underline';
	document.eventForm.letter.value= letter;
	xajax_showEventDirectory(1, 20, xajax.getFormValues('eventForm'), 'all_events.tpl', 'default_buttons.tpl', '', 'grid_paging_type2.tpl', 'eventContent', 'eventForm');	
}

function showDealDirectory(letter, index){
	for (i=1; i<=26; i++){
		e = document.getElementById("dealu"+i);
		f = document.getElementById("deald"+i);
		e.style.textDecoration = 'none';
		f.style.textDecoration = 'none';
	}
	document.getElementById("dealu"+index).style.textDecoration = 'underline';
	document.getElementById("deald"+index).style.textDecoration = 'underline';
	document.dealForm.letter.value= letter;
	xajax_showDealDirectory(1, 20, xajax.getFormValues('dealForm'), 'all_deals.tpl', 'default_buttons.tpl', '', 'grid_paging_type2.tpl', 'dealContent', 'dealForm');	
}

function getAmenities(atId){
	xajax_getAmenities(atId);
}

function flagIt(type, id){
	switch (type){
		case "bl":{
			if (confirm ("Are you sure you want to flag this listing?")){
				xajax_flagIt(type, id);
			}
		}break;
		case "deal":{
			if (confirm ("Are you sure you want to flag this deal?")){
				xajax_flagIt(type, id);
			}
		}break;
		case "event":{
			if (confirm ("Are you sure you want to flag this event?")){
				xajax_flagIt(type, id);
			}
		}break;
		
		case "blReview":{
			if (confirm ("Are you sure you want to flag this review?")){
				xajax_flagIt(type, id);
			}
		}break;
		case "dealReview":{
			if (confirm ("Are you sure you want to flag this review?")){
				xajax_flagIt(type, id);
			}
		}break;
		case "eventReview":{
			if (confirm ("Are you sure you want to flag this review?")){
				xajax_flagIt(type, id);
			}
		}break;
		case "blogComment":{
			if (confirm ("Are you sure you want to flag this comment?")){
				xajax_flagIt(type, id);
			}
		}break;
	}	
}

function searchCity(city){
	document.searchForm.txtKeyword.value = "all";
	document.searchForm.txtNear.value = city;
	document.searchForm.radius.selectedIndex = 3;
	searchLocations();
}