//Object that manages the search control state on most pages
//Object that manages the search control state on most pages
State = {
	IsModernBrowser: false,
	SearchMode: "Destination",
	
	CheckModernBrowser: function(){
		State.IsModernBrowser = SupportsAjax();
		if (typeof SaveState == "function") SaveState();
	},
	
	SwitchMode: function (){
		State.SearchMode = (State.SearchMode == "Destination") ? "PropertyName" : "Destination";
		if (typeof SaveState == "function") SaveState();
		
		return State.IsModernBrowser;
	},
	
	Set: function(elementId, value){
		var element = GetElementById(elementId);
		if (element) element.value = value;
	}
}



var DataBinding = false;
function CheckSearchAllowed()
{
    //If we're still in the middle of databinding delay the click until we have finished.
    if(DataBinding) {
        RegisterOnDataBoundEvent(function() { if (CheckSearchAllowed() && searchbutton) searchbutton.click(); });       
        return false;
    }
    
    //Don't allow empty Destination Guide Searches through, make it an accommodation one instead.    
    if(searchitem.options.length == 0 &&  searchtype.value == 1) {
        searchtype.selectedIndex++;    
    }
    
    return true;
}

function GetSearchTypes()
{
	if (State && State.IsModernBrowser && searchitem && searchtype && searchterm && loadmessage)
	{
	    // if these are set then assume that the items are already populated on load	
		if (searchItemBound)
		{
			loadmessage.style.display = "none";
	        //searchtype.disabled = false;
	        //searchtype.style.display = (searchtype.options.length > 0) ? "" : "none";
	        searchItemBound = null;
	    }
		else
		{
		    DataBinding = true;
		    //searchtype.style.display = "none";
		    //searchtype.disabled = true;
		    loadmessage.style.display = "";
		 //   searchitem.options.length = 0;
		    Enlighten.Jasons.Site.Services.Search.GetSearchTypes(searchterm.value, countryId, GetSearchTypes_Success, Failed);
		}
	}	
}

// This is the callback function that
// processes the Web Service return value.
function GetSearchTypes_Success(result)
{
	if (!searchitem) return;
	
	if(searchtype.selectedIndex > -1)
		searchTypeId = searchtype.options[searchtype.selectedIndex].value;	
		
	searchtype.options.length = 0;
	for (i = 0; i < result.length; i++)
	{
		searchtype.options[searchtype.options.length] = new Option(result[i].Text, result[i].Value);
		if(searchTypeId && result[i].Value == searchTypeId)
		    searchtype.selectedIndex = i;
	}
		
	loadmessage.style.display = "none";
	//searchtype.disabled = false;
    //searchtype.style.display = (searchtype.options.length > 0) ? "" : "none";
		
	DataBinding = false;	
	
	if(searchtype.options.length > 0)
	{
		GetSearchItems();		
	}
	else {
		searchitem.options.length = 0;
		searchitem.style.display = "none";
	}
}



function GetSearchItems()
{
	if (State && State.IsModernBrowser && searchitem && searchtype && searchterm && loadmessage)
	{
	    // if these are set then assume that the items are already populated on load	
		if (searchItemBound)
		{
			loadmessage.style.display = "none";
	        searchtype.disabled = false;
	        searchitem.style.display = (searchitem.options.length > 0) ? "" : "none";
	        searchItemBound = null;
	    }
		else
		{
		    DataBinding = true;
		    searchitem.style.display = "none";
	//	    searchtype.disabled = true;
		    loadmessage.style.display = "";
		//    searchitem.options.length = 0;
		    Enlighten.Jasons.Site.Services.Search.GetSearchItems(searchterm.value, countryId, searchtype.value, Success, Failed);
		}
	}		
}

// This is the callback function that
// processes the Web Service return value.
function Success(result)
{
	if (!searchitem) return;


	if (searchitem.selectedIndex > -1)
		searchItemId = searchitem.options[searchitem.selectedIndex].value;	
	
	searchitem.options.length = 0;
	for (i = 0; i < result.length; i++)
	{
		searchitem.options[searchitem.options.length] = new Option(result[i].Text, result[i].Value);
		if(searchItemId && result[i].Value == searchItemId)
		    searchitem.selectedIndex = i;
	}
	loadmessage.style.display = "none";
	//searchtype.disabled = false;
	searchitem.style.display = (searchitem.options.length > 0) ? "" : "none";
		
	DataBinding = false;
	
	//call anyone interested in hearing databinding finished
	for (i=0; i < OnDataBoundEvents.length; i++)	
		OnDataBoundEvents[i]();	
}

function Failed(error){
	loadmessage.style.display = "none";
	searchtype.disabled = false;
	//alert(error.get_message());
	DataBinding = false;	
}

function SearchTerm_OnEnter(element, event, stId, fbId){
	
  var key = event.keyCode? event.keyCode : event.which;
  if (key == 13)
  {  
  
	if (State && State.IsModernBrowser) {							
		OnEnter(element, event, stId , "focus")			
	} else {	
		OnEnter(element, event, fbId , "focus")
	}		    
    return false;
  }		
}

function SearchType_OnEnter(element, event, siId, goId){
	//check if searchTypeElementId is visible
	var si = GetElementById(siId);
	if (si){
		if (State && State.IsModernBrowser)
			OnEnter(element, event, siId , "focus")
		else
			OnEnter(element, event, goId , "click")
	}
}

//MULTIMAP STUFF

function AddMultiMapMarker(map, latitude, longitude, name, content, group){
    content = content.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");

	var icon = new MMIcon( '/images/map/icons/pinpoint.png' );
	icon.iconSize = new MMDimensions( 19, 28 );
	icon.iconAnchor = new MMPoint( 10, 28 );
	icon.infoBoxAnchor = new MMPoint( 7, 0 );
	icon.groupName = group;
	var marker = map.createMarker ( new MMLatLon( latitude, longitude ), {'label': name, 'icon' : icon} );
	marker.setInfoBoxContent ( content, { } );
	return marker;
}

var icon = null;
function AddGoogleMarker(map, latitude, longitude, name, content, popUp){
    content = content.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");

	if(!icon) {
	    icon = new GIcon();
	    icon.image = "/images/map/icons/pinpoint.png";
	    icon.iconSize = new GSize(19, 28);	    
	    icon.iconAnchor = new GPoint(10, 28);
	    icon.shadow = "/images/map/icons/shadow.png";
	    icon.shadowSize = new GSize(36, 28);
	    icon.infoWindowAnchor = new GPoint(7, 0);
	}
	
	var marker = new GMarker(new GLatLng(latitude, longitude),icon);
	marker.content = content;
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(content, {maxWidth: 217});
	});
	//marker.openInfoWindowHtml("BLAM");
	//map.addOverlay(marker);
	clusterer.AddMarker( marker, name )
	
	return marker;
}

function SetDestination(term){
	if (State.SearchMode != "Destination") State.SwitchMode();
	if (searchterm){
		searchterm.value = term;
		UpdateBreadcrumbs(term);	
		GetSearchItems();
	}
}

function SearchDestination(term){
	SetDestination(term);
	RegisterOnDataBoundEvent(function() { if (searchbutton) searchbutton.click(); });
}

var OnDataBoundEvents = new Array();
function RegisterOnDataBoundEvent(func){
	OnDataBoundEvents[OnDataBoundEvents.length] = func;	
}



function UpdateBreadcrumbs(searchterm) {          
   var url = "/services/createbreadcrumbs?searchterm=" + escape(searchterm) + "&countryid="+countryId;
   
   var wRequest =  new Sys.Net.WebRequest();

    // Set the request Url.  
    wRequest.set_url(url);

    // Set the Completed event handler, for processing return data
    wRequest.add_completed(ReceiveResponse);

    // To use executeRequest you must instantiate the
    // executor, assign it to the Web request instance,
    // then call the executeRequest function.
    // Note: Normally to make a Web request you use
    // the invoke method of the WebRequest instance.
    var executor = new Sys.Net.XMLHttpExecutor();
    wRequest.set_executor(executor); 
    executor.executeRequest();      
}

function ReceiveResponse(executor, eventArgs)
{
    if (executor.get_responseAvailable())
    {        
        var response = executor.get_responseData();
        response = response.replace("<div id=\"location\">","").replace("</div>","")
        GetElementById('location').innerHTML = response;
    }
}