
var checkmyMapToolscount = 0;
var toolsexists = document.getElementById('linkContainer');
var tooltip, defaulttip, aboutpage, about_info, mapcfg = "";

var OLX, OLY, OLZ, bw, bh, bho;
var BreadcrumbsTable = "Spatial breadcrumbs";
var BreadcrumbsField = ["County_name","Area_name","Local_name","Detail_name"];
var mylayerlist = [];
var mylayerstate = [];


checkmyMapTools();
loadjscssfile("/connect/js/SouthwarkExtra.js", "js")

defaulttip = "General_tips";
aboutpage = "Information";


function checkmyMapTools(){
	if(toolsexists != null){

		var myMapTools = document.getElementById('linkContainer');

		var TipDiv = document.createElement("div");
 	        TipDiv.id = "mapTip";
		var HelpDiv = document.createElement("div");
 	        HelpDiv.id = "mapHelp";
		var LinkDiv = document.createElement("div");
 	        LinkDiv.id = "mapLink";

		var breadcrumbsDiv = document.createElement("div");
 	        breadcrumbsDiv.id = "breadcrumbs";
		breadcrumbsDiv.className = "breadcrumbs";

		myMapTools.appendChild(LinkDiv);
		myMapTools.appendChild(HelpDiv);
	        myMapTools.appendChild(TipDiv);

	        myMapTools.appendChild(breadcrumbsDiv);

		var myMapLink = document.getElementById('mapLink');
		myMapLink.innerHTML = "<button onClick='MapLink()' style='background-color: transparent; border: 0 none; cursor: pointer; float: right; margin: 2px 5px 1px 0; padding: 0 5px 0 0'><span class='myToolText'><img src='/stratus-theme/images/connect/Link_icon.gif' /> Link</span></button>";

		var myMapHelp = document.getElementById('mapHelp');
		myMapHelp.innerHTML = "<button onClick='Helpfile()' style='background-color: transparent; border: 0 none; cursor: pointer; float: right; margin: 2px 5px 1px 0; padding: 0 5px 0 0'><span class='myToolText'><img src='/stratus-theme/images/connect/Help_icon.gif' /> Help</span></button>";

		var myMapTip = document.getElementById('mapTip');
		myMapTip.innerHTML = "<button onClick='ToolTip();' id='ToolTip' style='background-color: transparent; border: 0 none; cursor: pointer; float: right; margin: 2px 5px 1px 0; padding: 0 5px 0 0'><span class='myToolText'><img src='/stratus-theme/images/connect/Tip_icon.gif' /> Tips</span></button>";

		var mybreadcrumbs = document.getElementById('breadcrumbs');
		mybreadcrumbs.style.paddingLeft = "300px";
		mybreadcrumbs.innerHTML = "loading breadcrumbs ...";


		var myInfoDiv = document.getElementById('content');
		var InfoDiv = document.createElement("div");
 	        InfoDiv.id = "info_popup";
 	        InfoDiv.style.position = "absolute";
 	        InfoDiv.style.top = "0px";
		InfoDiv.style.left = "0px";
		InfoDiv.style.zIndex = "9999";
		InfoDiv.style.width = "100%";
		InfoDiv.style.height = "0px";
		myInfoDiv.appendChild(InfoDiv);


var pagecontent = document.getElementById('content');
var OVMapDiv = document.createElement("div");
OVMapDiv.id = "OVMap";
pagecontent.appendChild(OVMapDiv);

var myOVMap = document.getElementById('OVMap');
var OVMap1Div = document.createElement("div");
OVMap1Div.id = "OVMap1";
OVMap1Div.className = "OVMap1";
var OVMap2Div = document.createElement("div");
OVMap2Div.id = "OVMap2";
OVMap2Div.className = "OVMap2";
var OVMap3Div = document.createElement("div");
OVMap3Div.id = "OVMap3";
OVMap3Div.className = "OVMap3";

myOVMap.appendChild(OVMap3Div);
myOVMap.appendChild(OVMap2Div);
myOVMap.appendChild(OVMap1Div);



var OLXY = myMap.getOLMap().getExtent().getCenterLonLat();
OLX = parseInt(OLXY.lon);
OLY = parseInt(OLXY.lat);
var bounds = myMap.getOLMap().getExtent();
OLZ = 2 * (bounds.right - bounds.left);

    if (OLZ > 320000){OLZ = 320000} 

bw = 80;
bh = 160 * (bounds.top - bounds.bottom)/OLZ;
bho = 80 - bh/2;

    if (OLZ == 320000){
 	bw = 0;
	 bh = 0;
 	bho = 0;
	}

			 var portURL = "";
				 if (window.location.port) {portURL=":"+window.location.port}
var riazoom = parseInt(myMap.getOLMap().getZoom() - 2);
if (riazoom < 0) {riazoom=0}

document.getElementById('OVMap1').innerHTML = "<table cellpadding=0 cellspacing=0><td width=35 height=20></td><td width=130 align='right'><a href='javascript:OVM()'>Overview map </a>&nbsp;&nbsp;</td></table>";
document.getElementById('OVMap2').innerHTML = "<img src='http://" + window.location.hostname  + portURL + "/connect/southwark/TranslateURL.jsp?srs=epsg:27700&output=image/png&mapname=B_OV_Map&width=160&height=160&x=" + OLX + "&y=" + OLY + "&zoom=" + OLZ + "&zoomLevel="+riazoom+"'  alt='Overview map'>";
document.getElementById('OVMap3').innerHTML = "<div style='position: absolute; top: " + bho + "px; left: 40px;'><img src='/stratus-theme/images/connect/OVbox.gif' height=" + bh + " width=" + bw + "  alt=''></div><div id='OV2' style='position: absolute;'><a href='javascript:OVM()'><img src='/stratus-theme/images/connect/OVclose.gif' alt='Close overview map'></a></div>";








if (getQueryVariable("mapcfg")) {
	mapcfg = getQueryVariable("mapcfg")
} else {
	mapcfg = "defaultmap";
}

    if (getQueryVariable("info")){
	mapcfg_Info = getQueryVariable("info");
        if (mapcfg_Info !==null){
		about_info = "?INFO=" + mapcfg_Info;
		Helpfile()
    }}else{
	about_info="";
    }

if (getQueryVariable("tooltip")) {
	tooltip = getQueryVariable("tooltip")
        	if (tooltip.toUpperCase() =='YES'){
			tooltip = defaulttip;
		}
	ToolTip();
} else {
	tooltip = defaulttip;
}



// change style sheet for HistoricalSelection
if (getQueryVariable("mapcfg")){
    if (getQueryVariable("mapcfg") !==null){
		if (getQueryVariable("mapcfg") == "HistoricalSelection" || getQueryVariable("mapcfg") == "Historical_Selection") {
			aboutpage = "Historical";
			tooltip = "Hist_tips";
			loadjscssfile( "/stratus-theme/css/historical.css" , "css")
			document.getElementById("bannerIFrame").src = "/stratus-theme/html/banner/historical.html";

}}}




    myMap.getOLMap().events.register('moveend', myMap.getOLMap(), moved);
    dojo.connect(myMap, "onMapClick", "mapListener");
    searchBreadcrumbs();


	}else{
		checkmyMapToolsdelay();
		toolsexists = document.getElementById('linkContainer');
		checkmyMapToolscount = checkmyMapToolscount +1;
	}			
}





function checkmyMapToolsdelay(){
	if (checkmyMapToolscount < 5){
		setTimeout("checkmyMapTools()",1000);
	}
}




function searchBreadcrumbs(){
// This search is using a very generalised layer so the results may be slightly out at the maximum zoom.
                    var params = 
                    {
                        tables: [BreadcrumbsTable],
                        point: 
                        {
                            x: OLX, 
                            y: OLY
                        },
                        srs: myMap.configuration.getSrs(),
                        width: 0.1
                    }
                    
                    var featureSearch = new stratus.search.FeatureSearch();
                    var deferred = featureSearch.searchAtPoint(params);
                    deferred.addSuccessCallback(displayBreadcrumbs);
                    deferred.addErrorCallback(handleQueryException);
}

function displayBreadcrumbs(deferred) {
var results = deferred.getResult();
var resultString = "";
var resultCount = 1;


var OLZ = 1 + myMap.getOLMap().getZoom();
var maxZoomLevels = myMap.getConfiguration().zoomLevels.getLevelCount();
var BreadcrumbsLength = parseInt(0.65 + ((OLZ/maxZoomLevels) * BreadcrumbsField.length));

if (BreadcrumbsLength > BreadcrumbsField.length) {BreadcrumbsLength = BreadcrumbsField.length};
if (BreadcrumbsLength < 1) {BreadcrumbsLength = 1};


for(var item in results[BreadcrumbsTable]) {
if (resultCount == 1){
	for(var i = 0; i < BreadcrumbsLength; i++) {
	var myField = BreadcrumbsField[i]
		if (results[BreadcrumbsTable][item].properties[myField] && i>0) {
		resultString += " > ";
		}
                resultString += results[BreadcrumbsTable][item].properties[myField];
	}
resultCount++
}
}

var mybreadcrumbs = document.getElementById('breadcrumbs');
mybreadcrumbs.innerHTML = resultString;  
}




function moved(e){

var OLXY = myMap.getOLMap().getExtent().getCenterLonLat();
OLX = parseInt(OLXY.lon);
OLY = parseInt(OLXY.lat);
OLZ = myMap.getOLMap().getZoom();
	//OLZ = 320000/(OLZ*OLZ)
var bounds = myMap.getOLMap().getExtent();
	OLZ = 2 * (bounds.right - bounds.left);

    if (OLZ > 320000){OLZ = 320000} 

bw = 80;
bh = 160 * (bounds.top - bounds.bottom)/OLZ;
bho = 80 - bh/2;

    if (OLZ == 320000){
 	bw = 0;
	 bh = 0;
 	bho = 0;
	}


var ob = document.getElementById('OVMap2').style;
	if (ob.display == 'block'){

		var riazoom = parseInt(myMap.getOLMap().getZoom() - 2);
		if (riazoom < 0) {riazoom=0}
		var portURL = "";
		if (window.location.port) {portURL=":"+window.location.port}
		document.getElementById('OVMap1').innerHTML = "<table cellpadding=0 cellspacing=0><td width=35 height=20></td><td width=130 align='right'><a href='javascript:OVM()'>Overview map </a>&nbsp;&nbsp;</td></table>";
		document.getElementById('OVMap2').innerHTML = "<img src='http://" + window.location.hostname  + portURL + "/connect/southwark/TranslateURL.jsp?srs=epsg:27700&output=image/png&mapname=B_OV_Map&width=160&height=160&x=" + OLX + "&y=" + OLY + "&zoom=" + OLZ + "&zoomLevel="+riazoom+"'  alt='Overview map'>";
		document.getElementById('OVMap3').innerHTML = "<div style='position: absolute; top: " + bho + "px; left: 40px;'><img src='/stratus-theme/images/connect/OVbox.gif' height=" + bh + " width=" + bw + "  alt=''></div><div id='OV2' style='position: absolute;'><a href='javascript:OVM()'><img src='/stratus-theme/images/connect/OVclose.gif' alt='Close overview map'></a></div>";
	}


if (document.getElementById("mapContainer").style.paddingLeft) {
document.getElementById('breadcrumbs').style.paddingLeft = document.getElementById("mapContainer").style.paddingLeft;
}
searchBreadcrumbs();


}

function ToolTip() {
var TipURL = "http://maps.southwark.gov.uk/connect/Includes/" + tooltip + ".html"
	if (document.getElementById('info_popup')) {
		if (document.getElementById('ToolTipDIV')) {
			var myInfoDiv = document.getElementById('info_popup').innerHTML = "";
	} else {
		var myInfoDiv = document.getElementById('info_popup');
		myInfoDiv.innerHTML = "<div id='ToolTipDIV' class='myToolPopup'><table border=0 style='width:99%; height:99%'><td align='center' valign='top' style='height:100%'><iframe name='Starting tooltip' title='Starting tooltip' id=Date().getTime() height='100%' frameborder='0' width='100%' marginheight='0' marginwidth='0' src='"+TipURL+"' scrolling='auto'><a href='"+TipURL+"'>Tooltip</a></iframe></td><td align='right' valign='top' style='width:25px'><a href='javascript:ToolTip()'><img src='/stratus-theme/images/connect/close.gif' alt='Close tooltip'></a></td></table></div>";
	}}
window.setTimeout("CloseToolTip()",60000);
}

function CloseToolTip() {
	if (document.getElementById('info_popup')) {
		if (document.getElementById('ToolTipDIV')) {
			var myInfoDiv = document.getElementById('info_popup').innerHTML = "";
	}}
}

function Helpfile() {
	var theDiv = document.getElementById("content");
	var DivW = theDiv.offsetWidth - 100;
	var DivH = theDiv.offsetHeight - 80;
	var helpURL = "http://maps.southwark.gov.uk/connect/Includes/" + aboutpage + ".html" + about_info;
	if (document.getElementById('info_popup')) {
		if (document.getElementById('HelpDIV')) {
			var myInfoDiv = document.getElementById('info_popup').innerHTML = "";
	} else {
		var myInfoDiv = document.getElementById('info_popup');
		myInfoDiv.innerHTML = "<div id='HelpDIV' style='width:"+DivW+"px; height:"+DivH+"px; margin: 30px 0px 0px 40px; background-color: #ffffff'><table border=0 style='width:99%; height:99%'><td align='center' valign='top' style='height:100%'><iframe name='About pages' title='About pages' id=Date().getTime() height='100%' frameborder='0' width='100%' marginheight='0' marginwidth='0' src='" + helpURL + "' scrolling='auto'><a href='"+helpURL+"'>About southwark interactive mapping</a></iframe></td><td align='right' valign='top' style='width:25px'><a href='javascript:Helpfile()'><img src='/stratus-theme/images/connect/close.gif' alt='Close about page'></a></td></table></div>";
	}}
}

function MapLink() {
	if (document.getElementById('info_popup')) {
		if (document.getElementById('MapLinkDIV')) {
			var myInfoDiv = document.getElementById('info_popup').innerHTML = "";
	} else {

			 var portURL = "";
				 if (window.location.port) {portURL=":"+window.location.port}
var partURL = genURL();

			
			var URL = "http://" + window.location.hostname  + portURL + window.location.pathname + "?" + partURL;
			var emURL = "<iframe name=&quot;Embedded southwark interactive mapping&quot; title=&quot;Embedded southwark interactive mapping&quot; width=&quot;480&quot; height=&quot;300&quot; frameborder=&quot;0&quot; scrolling=&quot;no&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; src=&quot;http://" + window.location.hostname  + portURL + "/connect/embedded_s.jsp?" + partURL + "&quot;><a href=&quot;" + URL + "&quot;>Southwark interactive mapping</a></iframe>";
		var myInfoDiv = document.getElementById('info_popup');
		myInfoDiv.innerHTML = "<div id='MapLinkDIV' class='myToolPopup'><table border=0 style='width:99%; height:99%'><td align='center' valign='top'  style='width:360px;height:25px'>Southwark maps: Map links</td><td align='right' valign='top'><a href='javascript:MapLink()'><img src='/stratus-theme/images/connect/close.gif' alt='Close Map Link'></a></td><tr><td colspan=2 valign=top><b>Copy link  for email or sharing</b><br><input type=text style='width:378px; background-color:#b3c4dc; ont-weight:bold' name='URL' onFocus='this.select()' value='" + URL + "'  readonly='readonly'><p><b>Copy the HTML into your website</b><br><input type=text style='width:378px; background-color:#b3c4dc; ont-weight:bold' name='URL' onFocus='this.select()' value='" + emURL + "'  readonly='readonly'></td></table></div>";
	
	}}


}

function FULLSCREEN() {
var partURL = genURL();
			 var portURL = "";
				 if (window.location.port) {portURL=":"+window.location.port}
var URL = "http://" + window.location.hostname  + portURL + "/connect/southwark.jsp?" + partURL;
window.open(URL);


}

function genURL() {

			 var OLXY = myMap.getOLMap().getExtent().getCenterLonLat();
			 var OLZ = 1 + myMap.getOLMap().getZoom();
			 var OLX = parseInt(OLXY.lon);
			 var OLY = parseInt(OLXY.lat);
			 var portURL = "";
				 if (window.location.port) {portURL=":"+window.location.port}
			 var partURL ="";
				if (mapcfg){
				    if (mapcfg !==null){
					partURL += "mapcfg=" + mapcfg;
				}}  else {
					partURL += "mapcfg=defaultmap";
				}



for (i=0;i<myBase.length;i++){
    if (myBase[i]){
        if (myBase[i] !==null){
	partURL += "&mybase" + i + "=" + myBase[i];
}}}

for (i=0;i<=myLayer.length;i++){
   if (myLayer[i]){
        if (myLayer[i] !==null){
	partURL += "&mylayer" + i + "=" + myLayer[i];
}}}

var mylayercount = myLayer.length;
var showlayer = "YES";
for (i=0;i<mylayerlist.length;i++){
	if (mylayerstate[i] == true){
	showlayer = "YES";
		for (j=0;j<=myLayer.length;j++){
			if (mylayerlist[i] == myLayer[j]){
			showlayer = "NO";
		}}
		if (showlayer == "YES"){
		mylayercount += 1;
		partURL += "&mylayer" + mylayercount + "=" + mylayerlist[i];
}}}

partURL += "&x=" + OLX + "&y=" + OLY + "&z=" + OLZ;

return partURL;

}



function OVM() {
var riazoom = parseInt(myMap.getOLMap().getZoom() - 2);
if (riazoom < 0) {riazoom=0}
			 var portURL = "";
				 if (window.location.port) {portURL=":"+window.location.port}

 var ob = document.getElementById('OVMap2').style;
	ob.display = (ob.display == 'block')?'none': 'block';
	document.getElementById('OVMap2').innerHTML = "<img src='http://" + window.location.hostname  + portURL + "/connect/southwark/TranslateURL.jsp?srs=epsg:27700&output=image/png&mapname=B_OV_Map&width=160&height=160&x=" + OLX + "&y=" + OLY + "&zoom=" + OLZ + "&zoomLevel="+riazoom+"'  alt='Overview map'>";

 ob = document.getElementById('OVMap3').style;
	ob.display = (ob.display == 'block')?'none': 'block';
	document.getElementById('OVMap3').innerHTML = "<div style='position: absolute; top: " + bho + "px; left: 40px;'><img src='/stratus-theme/images/connect/OVbox.gif' height=" + bh + " width=" + bw + " alt=''></div><div id='OV2' style='position: absolute;'><a href='javascript:OVM()'><img src='/stratus-theme/images/connect/OVclose.gif' alt='Close overview map'></a></div>";

 ob = document.getElementById('OVMap1').style;
	ob.display = (ob.display == 'none')?'block': 'none';
	document.getElementById('OVMap1').innerHTML = "<table cellpadding=0 cellspacing=0><td width=35 height=20></td><td width=130 align='right'><a href='javascript:OVM()'>Overview map </a>&nbsp;&nbsp;</td></table>";

}



