//*** Set Up strings for drop downs and map hotspots
var Watsdropdown, Accadropdown, Accbdropdown, Acccdropdown, Accddropdown, Fodadropdown, Fodbdropdown, Fodcdropdown, Miscdropdown;
var mapicons,icongif,fullIcondescription,matchedIcon,altIcons,oLocation;
var dropAccStartLength,dropFodStartLength,activeIcon,subChoiceId,activeChoice;
var markArray = new Array();
for (i=1; i<10; i++){markArray[i] = "";}
icongif=mapicons=icidpartial=highlightedItemarray=highlightedItemId=highlightedItemdescription=activeIcon=subChoiceId=matchedIcon=altIcons=oLocation="";

//add map icons and write dropdowns
function writePageData(arrayName)
{
dropdowntext="";
for (i = 0; i<eval(arrayName).length; i++)
	{
	temparray=eval(arrayName)[i].split("##");
	if (oLocation != ""){
		if (oLocation==temparray[0]){oLocation=arrayName + i;}}
	if (temparray[4]=="y"){
		icongif=arrayName.substr(1,3).toLowerCase();
		if (arrayName=="oMisc" && (temparray[0].substr(0,4) == "Bank" || temparray[0].substr(0,4) == "Inte" || temparray[0].substr(0,4) == "Post" || temparray[0].substr(0,4) == "Tele" || temparray[0].substr(0,4) == "Nigh")){icongif = temparray[0].substr(0,4).toLowerCase();}
		if (arrayName=="oMisc" && temparray[0].substr(3,4) == "tran"){icongif = "fod"};
		if (arrayName=="oMisc" && temparray[0].substr(0,4) == "*Tat"){icongif = "tat"}
		if (arrayName.substr(0,4)=="oFod" && temparray[0].substr(3,4) == "tran"){altIcons+="ic"  + arrayName + i + "##";}
		if (temparray[5]!="y"){icongif+="area";}
		mapicons+="<span id=\"icspan"  + arrayName + i + "\" style=\"position: absolute; cursor: hand; left: " + (parseInt(temparray[2])+21)+ "\"><img  src=\"images/" + icongif + ".gif\" id=\"ic"  + arrayName + i + "\" style=\"display: none; position: relative; cursor: hand; top: "+ (parseInt(temparray[3])-386) +"\" onclick=\"iconClick(\'" + arrayName + i + "\')\" title=\"" + temparray[0] + "\"></span>";}
	if (temparray[5]=="y"){
		//add drop down option
		dropdowntext+="<option value=\"" + arrayName + i + "\" id=\"dr" + arrayName + i + "\">" + temparray[0] + "</option>";}
	}
}

//Page open script
//define icons and dropdowns for each map element
if(document.location.search.substr(0,5)=="?show"){oLocation=unescape(document.location.search.substr(5));}

writePageData("oWats");
Watsdropdown=dropdowntext;
writePageData("oAcca");
Accadropdown="<option style=\"background-color: #7DB0F6; color: #000066\" value=\"xoAcca\">All less than $10 per night</option>" + dropdowntext;
writePageData("oAccb");
Accbdropdown="<option style=\"background-color: #7DB0F6; color: #000066\" value=\"xoAccb\">All at $10 - $20 per night</option>"+dropdowntext;
writePageData("oAccc");
Acccdropdown="<option style=\"background-color: #7DB0F6; color: #000066\" value=\"xoAccc\">All at $20 - $40 per night</option>"+dropdowntext;
writePageData("oAccd");
Accddropdown="<option style=\"background-color: #7DB0F6; color: #000066\" value=\"xoAccd\">All at over $40 per night</option>"+dropdowntext;
writePageData("oFoda");
Fodadropdown="<option style=\"background-color: #7DB0F6; color: #000066\" value=\"xoFoda\">Show all budget grub</option>"+dropdowntext;
writePageData("oFodb");
Fodbdropdown="<option style=\"background-color: #7DB0F6; color: #000066\" value=\"xoFodb\">Show all posh nosh</option>"+dropdowntext;
writePageData("oFodc");
Fodcdropdown="<option style=\"background-color: #7DB0F6; color: #000066\" value=\"xoFodc\">Show all pubs and clubs</option>"+dropdowntext;
writePageData("oMisc");
Miscdropdown=dropdowntext;
for (i=1; i<10; i++){
	mapicons+="<span style=\"position: absolute; left: 0; display: none\" id=\"markspan" + i + "\"><img  src=\"images/mark" + i + ".gif\" id=\"mark"  + i + "\" style=\"position: relative; cursor: hand; top: "+ (parseInt(temparray[3])-386) +"\"  onclick=\"markSelect(" + i + ")\" title=\"" + temparray[0] + "\"></span>";}
mapicons+="<span style=\"position: absolute; left: 0; display: none\" id=\"highlightspan\"><img  class=\"noPrint\" src=\"images/highlight.gif\" id=\"highlight\" style=\"position: relative; cursor: hand; top: "+ (parseInt(temparray[3])-386) +"\"  onclick=\"markSelect(0)\"></span>";

//*** show or hide map icons
function mapdisplay(show,arrayName)
{
switchToMap(true);
if (show){
	for(i = 0; i<eval(arrayName).length; i++)
		{
		if(document.getElementById("ic" + arrayName + i)!=null){
			document.getElementById("ic" + arrayName + i).style.display="";
			if (activeIcon!="" && activeIcon.substr(0,4)!="mark"){ 
				if (eval(arrayName)[i]==eval(activeIcon.substr(2,5))[parseInt(activeIcon.substr(7))]){matchedIcon="ic"+arrayName+i;}}
		}}
	}
else
	{
	for(i = 0; i<eval(arrayName).length; i++)
		{
		if(document.getElementById("ic" + arrayName + i)!=null){document.getElementById("ic" + arrayName + i).style.display="none";}
		}
	//check to see if highlight etc. should be hidden
	checkHighlight();
	}
}

function checkHighlight()
{
//try to replace bookshop icon if possible
if(activeIcon.substr(0,6)=="icoMis" && document.getElementById(activeIcon).style.display=="none" && document.getElementById(activeIcon).src.substr(document.getElementById(activeIcon).src.lastIndexOf("/")+1)=="fodarea.gif")
{
do
	{
	if (document.getElementById(altIcons.substr(0,altIcons.indexOf("##"))).style.display==""){
		markArray[0]=altIcons.substr(0,altIcons.indexOf("##"));
		activeIcon=altIcons.substr(0,altIcons.indexOf("##"));
		break;}
	altIcons = altIcons.substr(altIcons.indexOf("##")+2);
	}
while (altIcons.length>2);
}
if (document.getElementById(activeIcon)!=null && activeIcon.substr(0,4)!="mark"){
	//we have an active element
	if (document.getElementById(activeIcon).style.display=="none"){
		//element not displayed so reset highlight info and hide table
		markArray[0]="";
		document.getElementById("highlightspan").style.display="none";
		activeIcon="";
		document.getElementById("descriptable").style.display="none";
		//reset drop down to non-option by sending phoney Id
		resetDrops(activeIcon.substr(2,5)+"zz");
		}
	}
}

function rebuildLists(arrayName,rebuild)
{
if (rebuild){
	if (arrayName.substr(0,4)=="oAcc"){
		if (document.getElementById("dropAcc").length<dropAccStartLength){
			document.getElementById("dropAcccell").innerHTML = "<select size=\"1\" name=\"dropAcc\" id=\"dropAcc\" style=\"font-size: 9pt\" onChange=\"itemSelect(this.value)\"><option value=\"xx\" selected>Show Me Accommodation ----&gt;</option>" + Accadropdown + Accbdropdown + Acccdropdown + Accddropdown + "</select>";
			document.getElementById("dropAcc").selectedIndex = 0;}}
	if (arrayName.substr(0,4)=="oFod"){
		if (document.getElementById("dropFod").length<dropFodStartLength){		
			document.getElementById("dropFodcell").innerHTML = "<select size=\"1\" name=\"dropFod\" id=\"dropFod\" style=\"font-size: 9pt\" onChange=\"itemSelect(this.value)\"><option value=\"xx\">Show Me Food &amp; Drink ----&gt;</option>" + Fodadropdown + Fodbdropdown + Fodcdropdown + "</select>";
			document.getElementById("dropFod").selectedIndex = 0;}}
	}
//whenever lists are modified we need to check the advice not
addRangeNote();
}

//*** define initial display states
function setInitial()
{
dropAccStartLength = document.getElementById("dropAcc").length;
dropFodStartLength = document.getElementById("dropFod").length;
document.getElementById("chFod").checked = false;
document.getElementById("chAcc").checked = false;
document.getElementById("chWat").checked = true;
document.getElementById("chMis").checked = true;
document.getElementById("chAcc").click();
document.getElementById("chFod").click();
document.getElementById("chMis").click();
document.getElementById("chWat").click();
document.getElementById("dropFod").options[0].selected = true;
document.getElementById("dropAcc").options[0].selected = true;
document.getElementById("dropMis").options[0].selected = true;
document.getElementById("dropWat").options[0].selected = true;

//-place in sub routine?
if(document.location.search != "" && document.location.search.substr(0,5)!="?show"){
	oLocation = document.location.search.substr(1);}
if (document.getElementById("dr"+oLocation)!=null){
	itemSelect(oLocation);
	window.scrollTo(0, document.getElementById("maintable").scrollHeight + document.getElementById("bannertable").scrollHeight + 20);
	}
}

function iconClick(arrayId)
{
resetDrops(arrayId);
writeInfo(arrayId);
}

function itemSelect(arrayId)
{
switchToMap(true);
//Check for non-item selection
if (arrayId.substr(0,1)=="x"){
var Accdr1 = "<select size=\"1\" name=\"dropAcc\" id=\"dropAcc\" style=\"font-size: 9pt\" onChange=\"itemSelect(this.value)\"><option style=\"background-color: #FF0000; color: #FFFFFF\" value=\"xa\">Show All Accommodation ----&gt;</option><option style=\"background-color: #FFCCCC; color: #000066\" value=\"xb\">List All Accommodation ----&gt;</option>";
var Foddr1 = "<select size=\"1\" name=\"dropFod\" id=\"dropFod\" style=\"font-size: 9pt\" onChange=\"itemSelect(this.value)\"><option style=\"background-color: #FF0000; color: #FFFFFF\" value=\"xf\">Show All Food &amp; Drink ----&gt;</option><option style=\"background-color: #FFCCCC; color: #000066\" value=\"xg\">List All Food &amp; Drink ----&gt;</option>"
if (arrayId.substr(1,1)=="o"){
	//don't bother if this is already the sub selected version!
	if (document.getElementById("drop"+arrayId.substr(2,3)).options[2].value != arrayId){
	//spot the current active icon and highlight in case we remove it
	startIcon=activeIcon;
	hiddenIcon=false;
	document.getElementById("ch"+arrayId.substr(2,3)).checked=true;
	document.getElementById("ch"+arrayId.substr(2,3)).click();
	document.getElementById("drop"+arrayId.substr(2,3)+"cell").innerHTML = eval(arrayId.substr(2,3)+"dr1")+eval(arrayId.substr(2,4)+"dropdown")+"</select>";
	document.getElementById("drop"+arrayId.substr(2,3)).selectedIndex = 2;
	//reshow our options and replace highlight if possible;
	matchedIcon="";
	if(startIcon!=activeIcon){
		hiddenIcon=true;
		activeIcon=startIcon;}
	mapdisplay(true,arrayId.substr(1,6));
	//check that its missing and we have a match
	if (hiddenIcon){
		if (matchedIcon==""){
			//no match so reset
			activeIcon="";}
		else{
			markArray[0]=matchedIcon;
			document.getElementById("highlightspan").style.display="";
			document.getElementById("descriptable").style.display="";
			resetDrops(activeChoice);
			for (i=1;i<10;i++){
				if (markArray[i]==activeIcon){markArray[i]==matchedIcon;}
			}
			activeIcon=matchedIcon;}
		}
	//trigger range Note check, rebuildLists function will do same for options below
	addRangeNote();
	}}
else{
	switch (arrayId.substr(1,1)){
	case "a":
		document.getElementById("chAcc").checked = false;
		document.getElementById("chAcc").click();
		rebuildLists("oAcc",true);
		break;

	case "b":
		rebuildLists("oAcc",true);
		break;


	case "f":
		document.getElementById("chFod").checked = false;
		document.getElementById("chFod").click();
		rebuildLists("oFod",true);;
		break;

	case "g":
		rebuildLists("oFod",true);
		break;
	}
	}
}

else{
	//Procede with item selection
	resetDrops(arrayId);
	writeInfo(arrayId);
	}
}

function resetDrops(arrayId)
{
dropAccReset = (document.getElementById("dropAcc").length<dropAccStartLength ? 2 : 0);
dropFodReset = (document.getElementById("dropFod").length<dropFodStartLength ? 2 : 0);
	switch (arrayId.substr(0,4)){
		case "oWat":
			document.getElementById("dropFod").options[dropFodReset].selected = true;
			document.getElementById("dropAcc").options[dropAccReset].selected = true;
			document.getElementById("dropMis").options[0].selected = true;
			if (document.getElementById("dr"+arrayId)==null){document.getElementById("dropWat").options[0].selected = true;}
			else{document.getElementById("dr"+arrayId).selected = true;}
			break;	
		case "oMis":
			document.getElementById("dropFod").options[dropFodReset].selected = true;
			document.getElementById("dropAcc").options[dropAccReset].selected = true;
			document.getElementById("dropWat").options[0].selected = true;
			if (document.getElementById("dr"+arrayId)==null){document.getElementById("dropMis").options[0].selected = true;}
			else{document.getElementById("dr"+arrayId).selected = true;}
			break;
		case "oFod":
			document.getElementById("dropAcc").options[dropAccReset].selected = true;
			document.getElementById("dropWat").options[0].selected = true;
			document.getElementById("dropMis").options[0].selected = true;
			if (document.getElementById("dr"+arrayId)!=null){
				document.getElementById("dr"+arrayId).selected = true;}
			else {
				document.getElementById("dropFod").options[dropFodReset].selected = true;}
			break;
		case "oAcc":
			document.getElementById("dropFod").options[dropFodReset].selected = true;
			document.getElementById("dropWat").options[0].selected = true;
			document.getElementById("dropMis").options[0].selected = true;
			if (document.getElementById("dr"+arrayId)!=null){
				document.getElementById("dr"+arrayId).selected = true;}
			else {
				document.getElementById("dropAcc").options[dropAccReset].selected = true;}
			break;
	}
}

function writeInfo(arrayId)
{
//only do if not already the displayed option
if (activeChoice!=arrayId || document.getElementById("descriptable").style.display =="none"){
	activeChoice=arrayId;
	prevActiveIcon=activeIcon;
	temparray = eval(arrayId.substr(0,5))[parseInt(arrayId.substr(5))].split("##");

	//Fill info table
	document.getElementById("TitleCell").innerHTML = "<img border=\"0\" src=\"images/highlight.gif\">:" + temparray[0];
	document.getElementById("DescripCell").innerHTML = temparray[1];
	
	//Set attributes for Highlight
	document.getElementById("highlightspan").style.left = parseInt(temparray[2])+18;
	document.getElementById("highlight").style.top = (parseInt(temparray[3])-389);
	document.getElementById("highlightspan").style.display = "";
	//check for icon display
	if (temparray[4]=="y"){
		document.getElementById("ic"+arrayId).style.display="";
		activeIcon="ic"+arrayId;
		document.getElementById("highlight").title = temparray[0];
		fullIcondescription = temparray[1];
		document.getElementById("viewallcell").style.display="none";}
	else{
		//An area subchoice!
		activeIcon="";
		for(i = 0; i<eval(arrayId.substr(0,5)).length; i++){
			if (document.getElementById("ic" + arrayId.substr(0,5) + i)!=null && document.getElementById("dr" + arrayId.substr(0,5) + i)==null){
				checkarray = eval(arrayId.substr(0,5))[i].split("##");
				if(checkarray[4]=="y" && checkarray[5]!="y" && checkarray[2]==temparray[2] && temparray[3]==checkarray[3]){
					document.getElementById("ic" + arrayId.substr(0,5) + i).style.display="";
					document.getElementById("highlight").title = checkarray[0];
					activeIcon="ic" + arrayId.substr(0,5) + i;
					fullIcondescription = checkarray[1];
					break;
				}
			}
		}

		document.getElementById("viewallcell").innerHTML="<a href=\"javascript:switchToMap(true);writeInfo('"+activeIcon.substr(2)+"')\" class=\"addlink2\">All places in area</a>";
		document.getElementById("viewallcell").style.display="";
	}	
	
	//no matching icon found?
	if (activeIcon==""){
		alert("no icon found!!");}
	else{
		markArray[0]=activeIcon;}

	//Reset active icon if it was a mark and we haven't gone anywhere...
	if (prevActiveIcon.substr(0,4)=="mark"){
		if (markArray[parseInt(prevActiveIcon.substr(4))]==activeIcon){
			activeIcon=prevActiveIcon}
	}

	//Populate hidden tables etc.
	if (temparray[6]!=""){
		document.getElementById("FullDescriptionCell").innerHTML="<b>"+temparray[0]+"</b><br><br>"+temparray[6];
		if (temparray[7]!=""){document.getElementById("PhotoCell").innerHTML="<a target=\"_blank\" href=\"images/" + temparray[7].substr(3) + "\"><img src=\"images/" + temparray[7] + "\" border=0></a>";}
		else{document.getElementById("PhotoCell").innerHTML="";}
		document.getElementById("showFullCell").style.display="";}
	else{
		document.getElementById("showFullCell").style.display="none";}
	//Show Info Table
	document.getElementById("descriptable").style.display = "";
	addRangeNote();
}
}

function switchToMap(switchVal)
{
if (switchVal){
document.getElementById("FullTable").style.display="none";
document.getElementById("MapTable").style.display="";
document.getElementById("showFullCell").innerHTML="<a class=\"addlink2\" href=\"javascript:switchToMap(false)\">full details</a>";
}
else{
document.getElementById("MapTable").style.display="none";
document.getElementById("FullTable").style.display="";
document.getElementById("showFullCell").innerHTML="<a class=\"addlink2\" href=\"javascript:switchToMap(true)\">Map View</a>";
}
}

function markSelect(markId)
{
switchToMap(true);
//subChoiceId="";
if (markId!=0){
	activeIcon="mark"+markId;
	initialIconState=document.getElementById(markArray[markId]).style.display;}
resetDrops(markArray[markId].substr(2));
writeInfo(markArray[markId].substr(2));
//reset the icon display state
if (markId!=0){
	document.getElementById(markArray[markId]).style.display=initialIconState;}
}

//*** add a user flag
function placeMark()
{
switchToMap(true);
var markfull = true;
var alreadymarked = false;
var firstfree = 0;
if (activeIcon.substr(0,4)=="mark"){
	alert("This location already marked and highlighted.");}
else{
	markText = eval(activeIcon.substr(2,5))[parseInt(activeIcon.substr(7))];
	for (i=1; i<10; i++){
		if (markArray[i]==""){
			if (firstfree<1){
				markfull = false;
				firstfree = i;
			}
		}
		else {
			if (document.getElementById("markcell"+i).innerHTML.indexOf(">" + markText.substr(0,markText.indexOf("##")) + "<")!=-1){//markText.indexOf(document.getElementById("markcell"+markId).innerHTML.substr(document.getElementById("markcell"+markId).innerHTML.indexOf(">")).replace("</a>","")!=-1){
				alreadymarked = true;
				break;
			}
		}
	}

if (alreadymarked || markfull){
	if (alreadymarked){
		alert("This item already marked\nwith marker " + i);}
	else {
		alert("All marks assigned.\nPlease remove an existing mark before placing a new one.");}}
else{
	document.getElementById("markspan"+firstfree).style.left=(parseInt(document.getElementById("highlightspan").style.left));
	document.getElementById("mark"+firstfree).style.top=(parseInt(document.getElementById("highlight").style.top));
	document.getElementById("mark"+firstfree).title=document.getElementById("highlight").title;
	document.getElementById("markspan"+firstfree).style.display="";
	markArray[firstfree] = activeIcon;
	titlestr = fullIcondescription;
	//strip tags
	do
		{
		titlestr = titlestr.substr(0,titlestr.indexOf("<")) + titlestr.substr(titlestr.indexOf(">")+1);
		}
	while (titlestr.indexOf("<")!= -1);
	document.getElementById("markcell"+firstfree).innerHTML="<a id=\"marklink" + i + "\" href=\"javascript:markSelect(" + firstfree + ")\" class=\"addlink2\" title=\"" + titlestr + "\">" + document.getElementById("highlight").title + "</a>";
	document.getElementById("markcellb"+firstfree).innerHTML=document.getElementById("highlight").title;
	document.getElementById("markcellc"+firstfree).innerHTML=titlestr;
	activeIcon = "mark"+firstfree}
}
}

//*** does what it says on the tin
function removeMark(markId)
{
	document.getElementById("markspan"+markId).style.display="none";
	document.getElementById("markcell"+markId).innerHTML="";
	document.getElementById("markcellb"+markId).innerHTML="&nbsp;";
	document.getElementById("markcellc"+markId).innerHTML="&nbsp;";
	if (activeIcon=="mark"+markId){
		if (document.getElementById(markArray[parseInt(markId)]).style.display=="none"){
			document.getElementById("highlightspan").style.display = "none";
			document.getElementById("descriptable").style.display = "none";
			activeIcon="";
			markArray[0]="";}
		else{
			activeIcon=markArray[markId];}
			//markArray[0]=markArray[markId];}
		}
	markArray[markId]="";
	//else {
	//	checkHighlight(activeIcon.substr(2,5))}
//check for highlight matching a "subchoice" from the marked item (activeIcon == mark0 and active dislpay=none)
//JUDE Also clear print tables
}

function addRangeNote()
{
warning="";
//only bother checking if there may be a point!
if (activeIcon!="" && ((activeChoice.substr(0,4)=="oFod"&&document.getElementById("dropFod").length<dropFodStartLength) || (activeChoice.substr(0,4)=="oAcc"&&document.getElementById("dropAcc").length<dropAccStartLength))){
//find out if there is an icon for the active choice
if (document.getElementById("ic"+activeChoice) == null){
	//it is a sub choice so check the drop downs
	if (document.getElementById("dr"+ activeChoice)==null){
		warning=(activeChoice.substr(0,4)=="oAcc" ? "accommodation is" : "venue is");
	}
}
else{
	//it is an area choice or direct entry - check the src for area
	if (document.getElementById("ic"+activeChoice).src.indexOf("area.gif")>0){
		//direct warning, we do not expect an icon
		warning=(activeChoice.substr(0,4)=="oAcc" ? "area may include accommodation that is" : "area may include venues that are");}
	else {
		if (document.getElementById("dr"+ activeChoice)==null){
			warning=(activeChoice.substr(0,4)=="oAcc" ? "accommodation is" : "venue is");}
	}
}
}
if (warning==""){
document.getElementById("NoteCell").style.display="none";
}
else{
document.getElementById("NoteCell").innerHTML="<b>Note:</b> This " + warning + " outside your chosen range.";
document.getElementById("NoteCell").style.display="";
}
}

//*** following functions allow selections from links within multiple location descriptions

function defineValues(arrayName,selectionText)
{
for(i = 0; i<eval(arrayName).length; i++)
		{
		if(eval(arrayName)[i].indexOf(selectionText + "##")==0){
			subChoiceId = arrayName+i;
			break;}
		}
}

function subSelect()
{
switchToMap(true);
if(subChoiceId!=""){
resetDrops(subChoiceId);
writeInfo(subChoiceId);}
subChoiceId="";
}