ns4 = (document.layers) ? true : false;
ie4 = (document.all && !document.getElementById) ? true : false;
ie5 =(document.getElementById && document.all) ? true : false;
ns6 = (!document.all && document.getElementById) ? true : false;

var scalebarCellIDs = new Array('scalebarCell1', 'scalebarCell2','scalebarCell3','scalebarCell4','scalebarCell5','scalebarCell6');
var scalebarID = "scalebarDiv";
var captionCellID = "caption";
var scb_mapWidth = 400;
var scb_toPrint = false;

parent.onResize = repaint;

function ScaleBar(left, top, color1, color2, bgColor, mapwidth, toPrint) {
	var textColor = 'black';
	var ie_cell1Style, ie_cell2Style, ie_cell3Style, ie_textStyle;
	scb_mapWidth = mapwidth;
	scb_toPrint = toPrint;
	
	if (bgColor.toLowerCase() == 'black' || bgColor == '#000000')	textColor = 'white'
	if (color1 == '') color1 = 'spacer';
	if (color2 == '') color2 = 'spacer';
	
	if (toPrint) {
		ie_cell0Style = ' style="border=thin; border-color:Black; border-style:solid; border-top-width:1px; border-left-width:0px; border-bottom-width:1px; border-right-width:0px; width:1px; height=4px;"';
		ie_cell1Style = ' style="border=thin; border-color:Black; border-style:solid; border-top-width:1px; border-left-width:0px; border-bottom-width:1px; border-right-width:0px; width:8px; height=4px;"';
		ie_cell2Style = ' style="border=thin; border-color:Black; border-style:solid; border-top-width:1px; border-left-width:0px; border-bottom-width:1px; border-right-width:0px; width:8px; height=4px;"';
		ie_cell3Style = ' style="border=thin; border-color:Black; border-style:solid; border-top-width:1px; border-left-width:0px; border-bottom-width:1px; border-right-width:0px;  width:40px; height=4px;"';
	} else {
		ie_cell0Style = ' style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:2pt; font-weight: normal;background-color:' + color2 + ';border=thin; border-color:Black; border-style:solid; border-top-width:1px; border-left-width:0px; border-bottom-width:1px; border-right-width:0px; width:1px; height=4px;"';
		ie_cell1Style = ' style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:2pt; font-weight: normal;background-color:' + color1 + ';border=thin; border-color:Black; border-style:solid; border-top-width:1px; border-left-width:0px; border-bottom-width:1px; border-right-width:0px; width:8px; height=4px;"';
		ie_cell2Style = ' style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:2pt; font-weight: normal;background-color:' + color2 + ';border=thin; border-color:Black; border-style:solid; border-top-width:1px; border-left-width:0px; border-bottom-width:1px; border-right-width:0px; width:8px; height=4px;"';
		ie_cell3Style = ' style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:2pt; font-weight: normal;border=thin;background-color:' + color2 + '; border-color:Black; border-style:solid; border-top-width:1px; border-left-width:0px; border-bottom-width:1px; border-right-width:0px;  width:40px; height=4px;"';
	}
	ie_textStyle = 	' style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:7pt; font-weight: normal; color:' + textColor + ';"';			

	if (ns4) {
		fontStart = '<font face="Verdana,Arial,\'MS Sans Serif\',sans-serif" size="7" point-size="7" color="'+textColor+'">';
		var lyr = document.layers[scalebarID] = new Layer(Math.round(mapwidth/2));
		eval("document."+scalebarID+" = lyr");
		lyr.top = top;
		lyr.left = left;
		lyr.clip.height = 26;
		if (bgColor != null && bgColor != '') lyr.bgColor = bgColor;
		lyr.visibility = 'show';
		
		var lyrs = '<layer id="text1" width=10 height=7 top=6 left=4 bgColor="' + bgColor + '">';
		lyrs += fontStart + '0</font></layer>';
		lyrs += '<layer id="' + scalebarCellIDs[0] + '" width=8 height=5 top=10 left=16 bgColor="' + color1 + '"></layer>';
		lyrs += '<layer id="' + scalebarCellIDs[1] + '" width=8 height=5 top=10 left=24 bgColor="' + color2 + '"></layer>';
		lyrs += '<layer id="' + scalebarCellIDs[2] + '" width=8 height=5 top=10 left=32 bgColor="' + color1 + '"></layer>';
		lyrs += '<layer id="' + scalebarCellIDs[3] + '" width=8 height=5 top=10 left=40 bgColor="' + color2 + '"></layer>';
		lyrs += '<layer id="' + scalebarCellIDs[4] + '" width=8 height=5 top=10 left=48 bgColor="' + color1 + '"></layer>';
		lyrs += '<layer id="' + scalebarCellIDs[5] + '" width=40 height=5 top=10 left=56 bgColor="' + color2 + '"></layer>';
		lyrs += '<layer id="' + captionCellID + '" width=60 height=6 top=6 left=100 bgColor="' + bgColor + '">';
		lyrs += fontStart + '0 m</font></layer>';	

		lyr.document.open();
		lyr.document.write(lyrs);
		lyr.document.close();
		this.css = eval(document.layers[scalebarID]);
	} else if (ie4 || ie5) {
		var str = '\n<DIV id='+scalebarID+' style="position:absolute; left:'+left+'; top:'+top+'; width:' + Math.round(mapwidth/2);
		if (bgColor!=null) str += '; background-color:'+bgColor		
		str += ';media=all;">';
		if (toPrint && bgColor!=null) {
			str += '<img id="scb_bgImg" vspace=0 hspace=0 border=0 src="' + toolImgURL + bgColor + 
				'.gif" style="position:absolute;left:0,top;0; width:' + Math.round(mapwidth/2) + '; height:20px;">';
		}
		str += '<table id="scb_table1" align="center" cellpadding="2" cellspacing="2"';
		str += ' style="position:absolute;top:0;left:0';
		if (bgColor!=null) str += '; background-color:'+bgColor		
		str += '; border: none';
		str += '; padding: 0px';
		str += ';">'		
		str += '<tr><td ' + ie_textStyle + '>0</td><td>';
		str += '<table id="table2" align="center" cellpadding="0" cellspacing="0"';
		str += ' style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif';
		str += '; font-size:7pt';
		str += '; font-weight: normal';
		str += '; color: ' + textColor;
		str += ';">';
		str += '<tr>';
		if (toPrint) {
			str += '<td><img src="' + toolImgURL + color2 + '.gif" id="scaleBarStart" ' + ie_cell0Style + '></td>';
			str += '<td><img src="' + toolImgURL + color1 + '.gif" id="' + scalebarCellIDs[0] + '" ' + ie_cell1Style + '></td>';
			str += '<td><img src="' + toolImgURL + color2 + '.gif" id="' + scalebarCellIDs[1] + '" ' + ie_cell2Style + '></td>';
			str += '<td><img src="' + toolImgURL + color1 + '.gif" id="' + scalebarCellIDs[2] + '" ' + ie_cell1Style + '></td>';
			str += '<td><img src="' + toolImgURL + color2 + '.gif" id="' + scalebarCellIDs[3] + '" ' + ie_cell2Style + '></td>';
			str += '<td><img src="' + toolImgURL + color1 + '.gif" id="' + scalebarCellIDs[4] + '" ' + ie_cell1Style + '></td>';
			str += '<td><img src="' + toolImgURL + color2 + '.gif" id="' + scalebarCellIDs[5] + '" ' + ie_cell3Style + '></td>';
		} else {
			str += '<td id="scaleBarStart" ' + ie_cell0Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[0] + '" ' + ie_cell1Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[1] + '" ' + ie_cell2Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[2] + '" ' + ie_cell1Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[3] + '" ' + ie_cell2Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[4] + '" ' + ie_cell1Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[5] + '" ' + ie_cell3Style + '>&nbsp;</td>';		
		}
		str += '</tr></table></td>';
		str += '<td id="' + captionCellID +'"' + ie_textStyle + '>250 m</td></tr></table>';		
		str += '</DIV>';

		document.body.insertAdjacentHTML("BeforeEnd",str);
		this.css = eval(document.all[scalebarID].style);
	} else if (ns6) {
		var str = '<table id="table1" align="center" cellpadding="2" cellspacing="2"';
		str += ' style="position:relative; left:0; top:0';
		if (bgColor!=null) str += '; background-color:'+bgColor		
		str += '; border: none';
		str += '; padding: 0px';		
		str += ';">'		
		str += '<tr><td ' + ie_textStyle + '>0</td><td>';
		str += '<table id="table2" align="center" cellpadding="0" cellspacing="0"';
		str += ' style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif';
		str += '; font-size:7pt';
		str += '; font-weight: normal';
		str += '; color: Black';
		str += ';">';
		str += '<tr>';
		
		if (toPrint) {
			str += '<td><img height=4 src="' + toolImgURL + color1 + '.gif" id="' + scalebarCellIDs[0] + '" ' + ie_cell1Style + '></td>';
			str += '<td><img height=4 src="images/' + color2 + '.gif" id="' + scalebarCellIDs[1] + '" ' + ie_cell2Style + '></td>';
			str += '<td><img height=4 src="images/' + color1 + '.gif" id="' + scalebarCellIDs[2] + '" ' + ie_cell1Style + '></td>';
			str += '<td><img height=4 src="images/' + color2 + '.gif" id="' + scalebarCellIDs[3] + '" ' + ie_cell2Style + '></td>';
			str += '<td><img height=4 src="images/' + color1 + '.gif" id="' + scalebarCellIDs[4] + '" ' + ie_cell1Style + '></td>';
			str += '<td><img height=4 src="images/' + color2 + '.gif" id="' + scalebarCellIDs[5] + '" ' + ie_cell3Style + '></td>';
		} else {
			str += '<td id="' + scalebarCellIDs[0] + '" ' + ie_cell1Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[1] + '" ' + ie_cell2Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[2] + '" ' + ie_cell1Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[3] + '" ' + ie_cell2Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[4] + '" ' + ie_cell1Style + '>&nbsp;</td>';
			str += '<td id="' + scalebarCellIDs[5] + '" ' + ie_cell3Style + '>&nbsp;</td>';		
		}
		
		str += '</tr></table></td>';
		str += '<td id="' + captionCellID +'"' + ie_textStyle + '>250 m</td></tr></table>\n';		

		mydiv = document.createElement("DIV");
		mydiv.setAttribute("id", scalebarID);

		var styl = 'position:absolute; left:'+left+'px; top:'+top+'px; width:'+Math.round(mapwidth/2)+'px';
		if (bgColor!=null) styl += '; background-color:' + bgColor;		
		styl += '; border:0';
		styl += ';'
//		mydiv = document.createElement("DIV");
//		mydiv.setAttribute("id", scalebarID);
		mydiv.setAttribute("style", styl);
		document.body.appendChild(mydiv);
		if (str != null && str.length > 0) {
			mydiv.innerHTML = str;
		}

		this.css = document.getElementById(scalebarID).style;
	}

	this.update = updateScaleBar;
	this.show = showScaleBar;
	this.hidden = hiddenScaleBar;
	this.setMapWidth = scalebarSetMapWidth;
	this.moveTo = scalebarMoveTo;
}

function updateScaleBar(mapZoom) {

	var dMultiplier = 1;
	var magnify;
	var scaleBarWidth = mapZoom / 4;
	
	while (scaleBarWidth < 1) {
		scaleBarWidth *= 10;
		dMultiplier *= 10;
	}
	while (scaleBarWidth >= 10) {
		scaleBarWidth *= 0.1;
		dMultiplier *= 0.1;
	}
	scaleBarWidth = new String(scaleBarWidth);
	
	var c = scaleBarWidth.charAt(0);
	switch (c) {
		case '2':	scaleBarWidth = 2.5;	break;
		case '5':	scaleBarWidth = 5; break;
		default: 	scaleBarWidth = 1;
	}

	scaleBarWidth /= dMultiplier;
	scaleBarWidth = Math.round(scaleBarWidth);
	magnify =  Math.round(scb_mapWidth * scaleBarWidth / mapZoom);
	drawScaleBar(scaleBarWidth, magnify);
}

function drawScaleBar (scaleBarWidth, magnify) {
	var strCaption = ' m';
	var length1, length2;
	
	while (magnify < Math.round(scb_mapWidth/7)) {
		scaleBarWidth = new String(scaleBarWidth);
		var c = scaleBarWidth.charAt(0);
		switch (c) {
			case '1': { scaleBarWidth *= 2.5; magnify *= 2.5;}	break;
			case '2': { scaleBarWidth *= 2; magnify *= 2;}	break;
			case '5': { scaleBarWidth *= 2; magnify *= 2;}	break;
		}		
	}
	if (scaleBarWidth >= 1000) {
		scaleBarWidth = scaleBarWidth / 1000;
		strCaption =  ' km';
	}
	length1 = Math.round(magnify / 10);
	length2 = magnify - (length1 * 5) -1 //Math.round(magnify / 2);
	
	var IdsLength = scalebarCellIDs.length;
	if (ie4||ie5) {
		for (var i=0; i < IdsLength-1; i++) {
			document.all[scalebarCellIDs[i]].style.width = length1;
		}
		document.all[scalebarCellIDs[IdsLength-1]].style.width = length2;
		document.all[captionCellID].innerText = scaleBarWidth + strCaption;
		document.all[scalebarID].style.width = magnify +80;
		if (scb_toPrint) {
			document.all['scb_bgImg'].style.width = document.all[scalebarID].style.width;
		}
	} else if (ns6) {
		for (var i=0; i < IdsLength-1; i++) {
			document.getElementById(scalebarCellIDs[i]).style.width = length1;
		}
		document.getElementById(scalebarCellIDs[IdsLength-1]).style.width = length2;
		document.getElementById(captionCellID).innerHTML = scaleBarWidth + strCaption;
		document.getElementById(scalebarID).style.width = magnify + 80;
		window.resizeBy(1,1);
		window.resizeBy(-1,-1);		
	} else if (ns4) {
		var IdsLength = scalebarCellIDs.length;
		var curleft = 16;
		for (var i=0; i < IdsLength-1; i++) {
			with (document.layers[scalebarID].document.layers[scalebarCellIDs[i]]) {
				left = curleft;
				clip.width = length1;
			}
			curleft += length1;
		}
		with (document.layers[scalebarID].document.layers[scalebarCellIDs[IdsLength-1]]) {
			left = curleft;
			clip.width = length2;
		}
		with (document.layers[scalebarID].document.layers[captionCellID]) {
			left = curleft + length2 + 4;
			document.open();
			document.write(fontStart +  scaleBarWidth + ' ' + strCaption + '</font>');
			document.close();
		}
	}
}

function showScaleBar () {
	this.css.visibility = (ns4)?'show':'visible';
}
function hiddenScaleBar () {
	this.css.visibility = (ns4)?'hide':'hidden'
}
function scalebarSetMapWidth (newMapWidth) {
	scb_mapWidth = newMapWidth;
}
function scalebarMoveTo (left, top) {
	this.css.left = left;
	this.css.top = top;
}

function repaint () {
	this.history.go(0);
}


