// JavaScript Document

var COMMON_SEARCH_WORDS = new Array("the", "building", "ryerson", "a", "an", "of") ;
var SEARCH_MARKER = [] ;
var SEARCH_MARKER_INFO = [] ;
var SEARCH_MARKER_POINT = [] ;
var SEARCH_MARKER_INDEX = 0 ;
var SEARCH_RESULT ;

/**
 * This function will search the xml files for the given input
 * and once it finds a related link, it will show a link in the
 * search result section where the user can click on the link to
 * see the searched building on the map.
 */
function searchMap(strQuery)
{
	displayLoadingInfo("on") ;
	//check to see if the xml files are loaded or not;
	//if they are not loaded yet, load them and continue to search
	
	if(!RYE_XML_DOC_CLASSROOM)
		loadMapXml("db/buildings/classroomBuildings.xml", "classroomLocationList", "classroom") ;
		
	if(!RYE_XML_DOC_OTHER_CENTER)
		loadMapXml("db/buildings/informationBuildings.xml", "infoLocationList", "infoLoc") ;
	
	//alert("searching for: " + strQuery) ;
	SEARCH_RESULT = "<hr /><ul id='searchResult' class='locationMarker'>" ;
	SEARCH_MARKER = removeArrayContent(SEARCH_MARKER) ;
	SEARCH_MARKER_INFO = removeArrayContent(SEARCH_MARKER_INFO) ;
	SEARCH_MARKER_POINT = removeArrayContent(SEARCH_MARKER_POINT) ;
	SEARCH_MARKER_INDEX = 0 ;
	// remove anyother markers from the map
	RYE_MAP.clearOverlays() ;
	
	// Split the query into separated words and search them
	// individually.
	var queryWords = strQuery.split(" ") ;
	//alert("abc") ; 
	// for each words, do a search in the xml files
	for(var i = 0 ; i < queryWords.length ; i++)
	{
		var searchWord = queryWords[i].toLowerCase() ;

		//only search this word, if it is not a common
		//word, such as: "the", "building"
		if(isCommonWord(searchWord) == false)
		{
			//alert("searching " + searchWord) ;
		
			doSearch(RYE_XML_DOC_CLASSROOM, searchWord) ;
			doSearch(RYE_XML_DOC_OTHER_CENTER, searchWord) ;
		}
	}
	
	if(SEARCH_MARKER_INDEX == 0) //NO SEARCH RESULT FOUND
	{
		SEARCH_RESULT = "<hr /><b>Sorry, I don't understand you query</b>" ;
		document.getElementById('searchResultDisplay').innerHTML = SEARCH_RESULT ;
	}
	else
	{
		SEARCH_RESULT += "</ul>" ;
		document.getElementById('searchResultDisplay').innerHTML = SEARCH_RESULT ;
	}
	
	displayLoadingInfo("off") ;

}

function isCommonWord(strCheck)
{
	//alert(strCheck) ;
	
	for(var j = 0 ; j < COMMON_SEARCH_WORDS.length ; j++)
	{
		//alert(COMMON_SEARCH_WORDS[j]) ;
		if(COMMON_SEARCH_WORDS[j] == strCheck)
		{
			//alert("match found") ;
			return true ;
		}
	}
	
	return false ;
}

function doSearch(xmlFile, query)
{
	//alert("searching xml file") ;
	var bldId ;
	var bldName ;
	var matchFound = false ;
	var infoTabs = [] ;
	var infoTabContents = [] ;
	var matchingMarkerIndex ;

	// obtain the array of markers and loop through it
	var markers = xmlFile.documentElement.getElementsByTagName("marker");

	for (var i = 0; i < markers.length; i++)
	{
		bldId = markers[i].getElementsByTagName("label")[0].getAttribute("id") ;
		bldName = GXml.value(markers[i].getElementsByTagName("label")[0]);
		var bldNameHtml = "" ;
		var tempBldName = bldName.split(" ") ;
		
		if(bldId.toLowerCase() == query.toLowerCase())
		{
			bldId = "<b>" + bldId + "</b>" ;
			matchingMarkerIndex = i ;
		}
			
		for(var j = 0 ; j < tempBldName.length ; j++)
		{
			if(query.toLowerCase() == tempBldName[j].toLowerCase())
			{
				matchingMarkerIndex = i ;
				bldNameHtml += "<b>" + tempBldName[j] + "</b> " ;
			}
			else
			{
				bldNameHtml += tempBldName[j] + " ";
			}
		}

		if(matchingMarkerIndex == i)
		{
			matchFound = true ;
			//alert("match found " + bldId + " in " + xmlFile) ;
			// obtain the attribues of each marker
			var lat = parseFloat(markers[i].getAttribute("lat"));
			var lng = parseFloat(markers[i].getAttribute("lng"));
			var point = new GLatLng(lat,lng);
			//obtain the label tag and then retrieve the value and the id of the label
			var label = "<span class='bldCode'>" + bldId + " - </span><span class='bldName'>" + bldNameHtml + "</span>" ;
			
			//obtain the html info for this <marker>
			/**
			 * Since the htmlInfo element also has multiple elements for tabs,
			 * loop over its child elements and save all the tabs in an array
			 */
			var html = markers[i].getElementsByTagName("htmlInfo");
			//first loop over htmlInfo element to get all the tabs
			var tabs = html[0].getElementsByTagName("tab") ;
			var tabLabel ;
			var tabContent ;
			
			for (var j = 0 ; j < tabs.length ; j++)
			{
				//now, for each tab's get it's label tag and content tag
				tabLabel = GXml.value(tabs[j].getElementsByTagName("tabLabel")[0]) ;
				tabContent = GXml.value(tabs[j].getElementsByTagName("tabContent")[0]) ;
				
				infoTabs.push(tabLabel) ;
				infoTabContents.push(tabContent) ;
			}
			var tabbedWindow = [] ;
			for (var j = 0 ; j < infoTabs.length ; j++)
			{
				tabbedWindow.push(new GInfoWindowTab(infoTabs[j], infoTabContents[j])) ;
			}
			createSearchedMarker(point,label, bldName, tabbedWindow);
			infoTabs = removeArrayContent(infoTabs) ;
			infoTabContents = removeArrayContent(infoTabContents) ;
			tabbedWindow = removeArrayContent(tabbedWindow) ;

			//a building code has been found already
			//no need to look further
			//break ;
		}
	}

	//if(matchFound == false)
//		alert("no match found") ;
}

function searchBuildingCode(xmlFile, bldCode)
{
	//alert("searching xml file") ;
	var bldId ;
	var matchFound = false ;
	var infoTabs = [] ;
	var infoTabContents = [] ;

	// obtain the array of markers and loop through it
	var markers = xmlFile.documentElement.getElementsByTagName("marker");

	for (var i = 0; i < markers.length; i++)
	{
		bldId = markers[i].getElementsByTagName("label")[0].getAttribute("id") ;
		if(bldId.toLowerCase() == bldCode.toLowerCase())
		{
			matchFound = true ;
			//alert("match found " + bldId + " in " + xmlFile) ;
			// obtain the attribues of each marker
			var lat = parseFloat(markers[i].getAttribute("lat"));
			var lng = parseFloat(markers[i].getAttribute("lng"));
			var point = new GLatLng(lat,lng);
			//obtain the label tag and then retrieve the value and the id of the label
			var labelValue = GXml.value(markers[i].getElementsByTagName("label")[0]);
			var label = "<span class='bldCode'><b>" + bldId + "</b> - </span><span class='bldName'>" + labelValue + "</span>" ;
			
			//obtain the html info for this <marker>
			/**
			 * Since the htmlInfo element also has multiple elements for tabs,
			 * loop over its child elements and save all the tabs in an array
			 */
			var html = markers[i].getElementsByTagName("htmlInfo");
			//first loop over htmlInfo element to get all the tabs
			var tabs = html[0].getElementsByTagName("tab") ;
			var tabLabel ;
			var tabContent ;
			
			for (var j = 0 ; j < tabs.length ; j++)
			{
				//now, for each tab's get it's label tag and content tag
				tabLabel = GXml.value(tabs[j].getElementsByTagName("tabLabel")[0]) ;
				tabContent = GXml.value(tabs[j].getElementsByTagName("tabContent")[0]) ;
				
				infoTabs.push(tabLabel) ;
				infoTabContents.push(tabContent) ;
			}
			var tabbedWindow = [] ;
			for (var j = 0 ; j < infoTabs.length ; j++)
			{
				tabbedWindow.push(new GInfoWindowTab(infoTabs[j], infoTabContents[j])) ;
			}
			createSearchedMarker(point,label, labelValue, tabbedWindow);
			infoTabs = removeArrayContent(infoTabs) ;
			infoTabContents = removeArrayContent(infoTabContents) ;
			tabbedWindow = removeArrayContent(tabbedWindow) ;

			//a building code has been found already
			//no need to look further
			break ;
		}
	}

	//if(matchFound == false)
//		alert("no match found") ;
}

function createSearchedMarker(point,name, nameTooltip, markerTab)
{
	var marker = new GMarker(point);
	var tabs = [] ;
	
	GEvent.addListener(marker, "click", function(){
		marker.openInfoWindowTabsHtml(markerTab) ;
	});
		
	// save the info we need to use later for the sidebar
	SEARCH_MARKER.push(marker) ;
	SEARCH_MARKER_INFO.push(markerTab) ;
	SEARCH_MARKER_POINT.push(point) ;
	
	//create the search result
	SEARCH_RESULT += '<li><a title = "' + nameTooltip + '" href = "javascript:displaySearchedMarker(' + SEARCH_MARKER_INDEX + ')">' + name + '</a></li>' ;

	SEARCH_MARKER_INDEX++;
}

function displaySearchedMarker(index)
{
	// remove anyother markers from the map
	RYE_MAP.clearOverlays() ;
	//map.setCenter(RYE_MARKER_POINT[index]) ;
	//add the new marker to the map
	RYE_MAP.addOverlay(SEARCH_MARKER[index]);
	SEARCH_MARKER[index].openInfoWindowTabsHtml(SEARCH_MARKER_INFO[index]);

}
