//Vars
var isIE     = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
var isMoz    = document.implementation && document.implementation.createDocument;
var isSafari = (navigator.userAgent.toLowerCase().indexOf('safari')!=-1) ? true : false;

//Static variable for the last instance of the last search
runSearch.lastInstance = 0;
runSearch.results = new Array();

//Funcs

document.getElementsByClass = function (className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}	
	}
	return returnElements;
}

function toggleLayer(cls, state, tag) {
	var els = document.getElementsByClass(cls,tag);
	var elsLen = els.length;
	//alert(state);
	
	for (i = 0; i < elsLen; i++) {
    if (state) {
      els[i].style.visibility='visible';
    } else {
      els[i].style.visibility='hidden';
    }
	}
}

function changeType(type) {
		
	document.getElementById('search_results').innerHTML = "";
	
	// Handle the type change
	document.getElementById('name-items').style.display  = "none";
	document.getElementById('name-monsters').style.display  = "none";
	document.getElementById('name-quest').style.display  = "none";
  document.getElementById('name-pois').style.display  = "none";
		
	if (type != "" && type != "rings" && type != "amine" && type != "dungeon") {
		document.getElementById('name-' + type).value = "";
		document.getElementById('name-' + type).style.display  = "inline";
	}
	
}

function fillOptions(cls) {

  sel = document.getElementById('select_cls');
  sel.options.length = 1; //Remove all but the "------" (nothing selected) option
  
  if (cls=='') return;
  to_add = records[cls];
  
  for (i=0; i<to_add.length; i++) {
    //Add Text, value (id)
    sel.options[sel.options.length] = new Option(to_add[i][1], to_add[i][0]);
  }

}

function resetMapPosition() {
    
	document.getElementById('search_results').innerHTML = "";
	window.scrollTo(0, 0);
    
	
}

function runSearch(type, next) {

  if (next) { //Find the next instance of the last search
    if (runSearch.results.length <= 1) return false;
    
    document.getElementById('search_result_'+runSearch.lastInstance).className = '';
    
    //Increment instance cursor
    runSearch.lastInstance++;
    if (runSearch.results.length == runSearch.lastInstance) {
      runSearch.lastInstance = 0;
    }

    //Scroll to the next instance, update result listing
    document.getElementById('search_result_'+runSearch.lastInstance).className = 'search_current';
    Scroller.scrollTo(runSearch.results[runSearch.lastInstance].id);
	
  } else { //Perform a new search
	
	var search = "";
  if (type == "rings" || type == "dungeon" || type == "amine") {
    search = type;
  }
	else if (type != "") 
		search = document.getElementById('name-' + type).value;
	
  if (search == '') return false;
    
	// Need to get the id of the monster/quest/item
	var ajax = new AJAX();
	if (ajax.init() == false) {
		return false;
	}
	var url="inc/xmap/get_data_id.php";
	// Get the params from the form (i.e. user details)
	var params = "type=" + type;
	params += "&name=" + search;
	ajax.postData(url, params, function(response) {
		response = response.split("|");
		if (response[0] == "false") 
			return false;
		
		// Add the prefix onto the id, eg m for monster, i for item
		var id = type.charAt(0) + response[1];
		runSearch.results = document.getElementsByClass(id);
    
		if (runSearch.results.length == 0) {
		  document.getElementById('button_next').disabled = true;
		  document.getElementById('search_results').innerHTML = 'No results!';
		} else {
		  //Show searched layer if hidden
		  classes = document.getElementById(runSearch.results[0].id).className.split(' ');
		  //Enable the "Next" button
		  document.getElementById('button_next').disabled = false;
		  
		  for (i=0; i<classes.length; i++) {

				if (classes[i].charAt(0) == 'm' && classes[i].charAt(1) != 'o') {
					toggleLayer(classes[i], true, 'a');
					break;
				} else if (classes[i].charAt(0) == 'q' && classes[i].charAt(1) != 'u') {
					toggleLayer(classes[i], true, 'a');
					break;
				} else if (classes[i].charAt(0) == 'i' && classes[i].charAt(1) != 't') {
					toggleLayer(classes[i], true, 'a');
					break;
				} else if (classes[i].charAt(0) == 'p' && classes[i].charAt(1) != 'o') {
					toggleLayer(classes[i], true, 'a');
					break;
				} else if (classes[i].charAt(0) == 'r') {
					toggleLayer(classes[i], true, 'a');
					break;
				} else if (classes[i].charAt(0) == 'd') {
					toggleLayer(classes[i], true, 'a');
					break;
				} else if (classes[i].charAt(0) == 'a') {
					toggleLayer(classes[i], true, 'a');
					break;
				} 
		  }
		
		  if (runSearch.results.length >= 1) {
			resultsHTML = '';
	
      if (type == "rings" || type == "dungeon" || type == "amine") {
        //First result appears selected
        resultsHTML += '<a id="search_result_0" class="search_current" href="javascript:void(0)" onclick="Scroller.scrollTo(\''+records[type][0][0]+'\'); highlightResult(0); return false;">1. '+records[type][0][1]+'</a><br />\n';
        for (i=1; i<records[type].length; i++) {
          resultsHTML += '<a id="search_result_'+i+'" href="javascript:void(0)" onclick="Scroller.scrollTo(\''+records[type][i][0]+'\'); highlightResult('+i+'); return false;">'+(i+1)+'. '+records[type][i][1]+'</a><br />\n';
        }
      }
      else {
        //First result appears selected
        resultsHTML += '<a id="search_result_0" class="search_current" href="javascript:void(0)'+runSearch.results[0].id+'" onclick="Scroller.scrollTo(\''+runSearch.results[0].id+'\'); highlightResult(0); return false;">1. '+runSearch.results[0].title+'</a><br />\n';
        for (i=1; i<runSearch.results.length; i++) {
          resultsHTML += '<a id="search_result_'+i+'" href="javascript:void(0)'+runSearch.results[i].id+'" onclick="Scroller.scrollTo(\''+runSearch.results[i].id+'\'); highlightResult('+i+'); return false;">'+(i+1)+'. '+runSearch.results[i].title+'</a><br />\n';
        }
      }
			
			document.getElementById('search_results').innerHTML = resultsHTML;
		  }
		
		  Scroller.scrollTo(runSearch.results[0].id);
		  runSearch.lastInstance = 0;
		}
		return false;
	}); // end ajax callback
		
  } // new search
	return false;
}

function highlightResult(n) {
  results = document.getElementById('search_results').getElementsByTagName('a');

  for (i=0; i<results.length; i++) {
    results[i].className = '';
  }
  
  results[n].className = 'search_current';
  runSearch.lastInstance = n;
}

function displayTip(name, link, image, tip, caller) {
    // Use default image if one does not exist
    if (image == "")
        image = "images/tooltip_deafult.png"
    if (link) {
      text = '<img src="' + image + '" /><h4 class="name"><a href="'+link+'#">'+name+'</a></h4>' + 
              tip+'<br />';
    } else {
      text = '<img src="' + image + '" /><h4 class="name">'+name+'</h4>' + 
              tip+'<br />';
    }
    sexyBOX(text,
            245,
            caller.offsetLeft - 122 + (caller.offsetWidth /2 ),
            caller.offsetTop - 100);
    return false;
}

function displayTipQuest(name, link, tip, caller) {
  if (link) {
    text = '<h4 class="name"><a href="'+link+'#">'+name+'</a></h4>'+tip+'<br />';
  } else {
    text = '<h4 class="name">'+name+'</h4>'+tip+'<br />';
  }
  sexyBOX(text,
          245,
          caller.offsetLeft - 122 + (caller.offsetWidth /2 ),
          caller.offsetTop - 100);
  return false;
}


function displayMap(image, name, description, link, caller) {

    if (image == "") {
        image = "images/no_map.png";
    }
    
    if (link) {
        text = '<div class="image"><img src="' + image + '" /></div><h4 class="name"><a href="'+link+'#">'+name+'</a></h4>'+description+'<br />';
    } else {
        text = '<div class="image"><img src="' + image + '" /></div><h4 class="name">'+name+'</h4>'+description+'<br />';
    }
    
    mapBOX(text,
            caller.offsetLeft + (caller.offsetWidth /2 ),
            caller.offsetTop - 25);
    return false;
}
