var map, id, height, width, x, y , index

function load_url( dx , dy ) {

	x = dx
	y = dy
	height = getWindowHeight() - 30 ; width = getWindowWidth()
	load_xml( 1 , 20 )
	
}
function loader() {

	var dx , dy ; id = identify("map") ; dx = id.getAttribute("dx") , dy = id.getAttribute("dy")	
	index = id . getAttribute("map_index")

	map = new construct( -dx + width / 2 , -dy + height / 2 ) ; update()
	id.onmouseup = _zoom
	id.onmousemove = pan
	if( recommended ) { id.onmousedown = coordinate  }
	else { id.ondragstart = coordinate ; id.ondragend = pan }
}
function westbound() {
	var win = identify("win")
	return id.getAttribute("size_x") - win . offsetWidth 
}
function southbound() {
	return id.getAttribute("size_y") - height
}

function construct( dx, dy ) {

	var lmax = westbound() , rmax = southbound()

	if (dx < 0 && dx > -lmax)	this.x = dx ; else if( dx < -lmax ) this.x = -lmax ; else this.x = 0
	if (dy < 0 && dy > -rmax)	this.y = dy ; else if( dy < -rmax ) this.y = -rmax ; else this.y = 0
}
function update() { height = getWindowHeight() - 30 ; width = getWindowWidth() ; css ( map ) }
function identify(id) { return document.getElementById(id) }

function css( map1 ) {

	id.style.marginLeft = map1.x + "px" ; id.style.marginTop = map1.y + "px" 
	id.style.width = id.getAttribute("size_x") + 1 + "px"
	id.style.height = id.getAttribute("size_y") + "px"
	
	var win = identify("win")
	win.style.height = height + "px"

	if( ! recommended ) {
		win.style.height = height - 20 + "px"
		win.style.width = width - 40 + "px"
	}
	
}
function get_xml( mode ) {
	var dx , dy ; if (map) { dx = x - map.x ; dy = y - map.y } else if ( zoom == 0 ) { dx = 0 ; dy = 0 ; height = getWindowHeight() - 30 ; width = getWindowWidth() }
	else { dx = x ; dy = y }
	var s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
	s += "<root>" ; s += zoom ; s += " ";  s += mode ; s += " " ; s += dx ; s += " " ; s += dy ; s += " " ; 
	s += width ; s += " " ; s += height ; s += " " ; s += index ; s += "</root>"
	return s
}
function record(event) { 
	if( recommended ) {
		x = event.clientX ; y = event.clientY  
	}
	else  { x = window.event.clientX ; y = window.event.clientY  }
}

function coordinate(event) { record(event) }
function build( event ) {
	var dx = id.getAttribute("dx") , dy = id.getAttribute("dy") 
	if(recommended)
		map = new construct(dx , dy)
	else
		map = new construct(-dx , dy)
	css( map )
}
function pan(event) {
zoom(event)
//	setTimeout("zoom(event)", 20)
}

function _zoom(event) {
	var d = document.createElement("div")
	var t = document.createTextNode("pan event")
	d . appendChild(t)
	document.body.appendChild(d)
	
	var control = -1 , mode = -1
	if( recommended ) {
		if ( event.altKey ) control = 0 // zoom in
		else if ( event.ctrlKey ) control = 1 // out
		else control = 2
	}
	else {
		if ( window.event.altKey ) control = 0
		else if ( window . event.ctrlKey ) control = 1
		else control = 3
	}
	switch( control ) {
	  case 0:
		if( zoom < 2 ) mode = 1
		else mode = 10
		record(event) ; load_xml( ++zoom , mode ) ; build( event )
		break
	  case 1:
		if ( zoom > 0 ) { record(event) ; load_xml( --zoom , 0 ) ; build( event ) }
		break
	  case 2:
		var dx = event.clientX - x , dy =  event.clientY - y , map_x = map . x , map_y = map . y
		map = new construct(map.x + dx, map.y + dy) ; css( map )
		// alert( dx +" , "+ dy + " , "+ map.x +" , "+ map.y + " index = " + index +" wb = "+ westbound() )
		if ( index == 1 && map . x == 0 ) {
			// alert("move west")
			load_xml( zoom , 2 )
		
		}
		else if ( index == 0 && - map . x == westbound() && zoom > 0 )
			load_xml( zoom , 3 ) 
		break
	  case 3:
		var dx = window.event.clientX - x , dy =  window.event.clientY - y ; map = new construct(map.x + dx, map.y + dy) ; css( map )
		break
	}
}
function getWindowHeight() {
	
	if (window.self && self.innerHeight) return self.innerHeight
	if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight
	return 0
}
function getWindowWidth() {
	if (window.self && self.innerWidth) return self.innerWidth
	if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth
	return 0
}

