var oldImage, oldLocation;

var zoomValues = new Array( 0.3, 0.5, 1, 2, 4,  10 ,  40 , 80,  400, 3600 );

var currentZV = 1;

var browser; var browserVersion; var realBrowser;

var treevisible = false;

var mapOn = false;

//Parte para identificar navegadores
var myUA = navigator.userAgent;
var mozVersion = 0;
var firefox = false;

if( navigator.appName.indexOf("Microsoft") > -1 ) {
 	browser = 'ie';


	if( myUA.indexOf( 'MSIE 5.0' ) > -1 )
		browserVersion = 5;	
	else if( myUA.indexOf( 'MSIE 5.5' ) > -1 ) 
		browserVersion = 5.5;
	else if( myUA.indexOf( 'MSIE 6' ) > -1 ) 
		browserVersion = 6;
}
else if( navigator.appName.indexOf("Netscape") > -1 ) {
        browser = 'moz';

	if( myUA.indexOf( 'Firefox' ) > -1 ) {
		firefox = true;
	}

}
if( myUA.indexOf( 'Opera' ) > -1 )
	browser = 'ope';
else if( myUA.indexOf( 'Konqueror' ) > -1 )
	browser = 'kon';
else if( myUA.indexOf( 'Safari' ) > -1 )
	browser = 'saf';




function enableControls() {
	mapOn = true;
	window.status='';
}

function disableControls() {
	mapOn = false;
	window.status = 'Actualizando Mapa...';
}


function verifyAndSend() {

	document.forms.streetsearch.submit();


}





function crossGetFrame( n ) {

	if( browser == 'ie' ) 
             return   document.frames[ n ].document;
	else if( browser == 'moz' )
	     return  document.getElementById( n ).contentDocument;
	else if( browser == 'ope' )
             return   document.frames[ n ].document;
	else if( browser == 'kon' || browser == 'saf' )
             return   document.frames[ n ].document;
}


function openPopUp( page, width, height ){
    ventana = window.open( page ,'legends','resizable=yes,scrollbars=yes,toolbars=no,statusbar=no,height=' + height + ',width=' + width);
    ventana.focus();
}



function setDefaultClickAction( action ) {

	crossGetFrame('map').images['myimage'].style.cursor = 'hand';
	crossGetFrame('map').getElementById('rulerLayer').style.visibility = 'hidden';
	crossGetFrame('map').getElementById('line').style.visibility = 'hidden';
	
	crossGetFrame('map').getElementById('horizontal').style.visibility = 'hidden';
	crossGetFrame('map').getElementById('vertical').style.visibility = 'hidden';
	crossGetFrame('map').getElementById('distancia').style.visibility = 'hidden';




	crossGetFrame('map').status.daction.value = action;

	switch ( action ) {

		case 4:
			crossGetFrame('map').images['myimage'].style.cursor = 'crosshair';
		break;

		
		case 5:
			crossGetFrame('map').images['myimage'].style.cursor = 'help';
		break;

		case 1:
			changeCursor( 1 , crossGetFrame('map').images['myimage']  );
		break;

		case 2:
			changeCursor( 2 , crossGetFrame('map').images['myimage']  );
		break;

		case 3:
			changeCursor( 3 , crossGetFrame('map').images['myimage']  );
		break;



		default:
			crossGetFrame('map').images['myimage'].style.cursor = 'default';
		break;


	}
	


}


function updateOverViewMap( x, y, mw, mh, wz ) {

	if ( browser == 'moz'  )
	       	document.getElementById("ov").src = 'overview.asp?x=' + x + '&y=' + y + '&rz=' + wz + '&rmw=' + mw + '&rmh=' + mh ;
	else if( browser == 'ie' )
		window.document.frames['ov'].location.replace( 'overview.asp?x=' + x + '&y=' + y + '&rz=' + wz + '&rmw=' + mw + '&rmh=' + mh  );	
	else 
		window.document.frames['ov'].location.replace( 'overview.asp?x=' + x + '&y=' + y + '&rz=' + wz + '&rmw=' + mw + '&rmh=' + mh  );	


	currentZV = crossGetFrame("map").forms['status'].zstep.value;		

}




function changeSize( size ) {

	document.getElementById("map").style.width=size;
	document.getElementById("map").style.height= parseInt(size);

        crossGetFrame('map').forms['status'].mw.value=size;
        crossGetFrame('map').forms['status'].mh.value=size;

	selectOpAndSubmit( 7 );
		
}

function changeZoom( zoom ) {
	var x, y;
	currentZV = zoom;		
	x = crossGetFrame('map').forms['status'].x.value;
	y = crossGetFrame('map').forms['status'].y.value;
	crossGetFrame('map').forms['status'].zstep.value =  zoom;
	crossGetFrame('map').forms['status'].z.value = zoomValues[ zoom ];


	selectOpAndSubmit( 77 );

}

function zoomIn( ) {
   	currentZV = crossGetFrame('map').forms['status'].zstep.value;

//	alert( currentZV + '----' +   ( currentZV - 1 )   );

	if( currentZV > 0 ) 
		changeZoom( --currentZV );
}

function zoomOut( ) {
   	currentZV = crossGetFrame('map').forms['status'].zstep.value;

//	alert( currentZV + '----' +   ( currentZV + 1 )   );

	if( currentZV < 9 ) 
		changeZoom( ++currentZV );
}



function rollOver( i, s ) {

	if( s ) {
		oldImage = document.images[i].src;
		document.images[i].src = 'mapimages/' + i + '_on.gif'; 
	}
	else {
		document.images[i].src = oldImage;
	}
}


function selectOpAndSubmit( op ) {
	localShowStatus();

	crossGetFrame('map').forms['status'].op.value=op;
	crossGetFrame('map').forms['status'].submit();


}


function moveMapCenter( dirValue  ) {
	if( mapOn ) {
		crossGetFrame('map').forms['status'].dir.value = dirValue;	

		selectOpAndSubmit( 1 );
	}

}

function zoomTo( value ) {
	crossGetFrame('map').forms['status'].z.value =  value ;
	selectOpAndSubmit( 2 );
}


function showAyuda() {

}


function viewOtherCorner( f ) {
	var url;
	var newX,newY;
	var coords= new Array();

	coords = f.mapselector.value.split(',');

	newX = coords[0];
	newY = coords[1];

	url = 'mapa.asp?s='
		+ '&x=' + newX
		+ '&y=' + newY
		+ '&zstep=' + crossGetFrame("map").status.zstep.value
		+ '&daction=' + crossGetFrame("map").status.daction.value 
		+ '&z=' + crossGetFrame("map").status.z.value
		+ '&mw=' + crossGetFrame("map").status.mw.value
		+ '&mh=' + crossGetFrame("map").status.mh.value
		+ '&sp=' + crossGetFrame("map").status.sp.value
		+ '&poi=' + crossGetFrame("map").status.poi.value;

	if ( browser == 'moz'  )
	       	document.getElementById("map").src = url ;
	else if( browser == 'ie' )
		window.document.frames['map'].location = url ;	
	else
		window.document.frames['map'].location = url ;	
	
}

function cleanForm( forma ) {
	forma.colonia.value = "";
	forma.calle.value = "";
	forma.cp.value = "";

}


function requestMap() {

	var x,y,z,op,dir,mw,mh,sx,sy,zstep,daction,ix,iy,iw,ih,dx,dy;
	
	var query;
	
	var f = document.forms.status;

	x = f.x.value;
	y = f.y.value;
	z = f.z.value;
	op = f.op.value;
	dir = f.dir.value;
	mw = f.mw.value;
	mh = f.mh.value;
	sx = f.sx.value;
	sy = f.sy.value;
	zstep = f.zstep.value;
	daction = f.daction.value;
	ix = f.ix.value;
	iy = f.iy.value;
	iw = f.iw.value;
	ih = f.ih.value;
	dx = f.dx.value;
	dy = f.dy.value;

       query = "x=" + x + "&y=" + y + "&z=" + z + "&op=" + op + "&dir=" + dir +
		 "&mw=" + mw + "&mh=" + mh + "&sx=" + sx +  "&sy=" + sy +  "&zstep=" + zstep +
		 "&daction=" + daction + "&ix=" + ix + "&iy=" + iy + "&iw=" + iw + "&ih=" + ih +
		 "&dx=" + dx + "&dy=" + dy;

	document.images['map'].src = "mapimg.asp?" + query;

}


function showTree() {
	var mylayer = document.getElementById('poitree');

	if (  mylayer.style.visibility == "hidden" ) {
		mylayer.style.visibility = "visible";
		mylayer.style.height = 210;	
		//showPOIS( document.forms['selpois'] );
	}
	else if(  mylayer.style.visibility == "visible" ) {
		mylayer.style.visibility = "hidden";
		mylayer.style.height = 1;	
		//hidePOIS( document.forms['selpois'] );
	}	
	else {
		mylayer.style.visibility = "visible";
		mylayer.style.height = 210;	
		//showPOIS( document.forms['selpois'] );
	}
}

function showPOIS( forma ) {
}

function showMapLayers( forma ) {

   var stpois = "";

   for (k=0; k < forma.pois.length; k++  ) {
      if ( forma.pois[k].checked == true) {
	
		stpois += ( ( k + 1 ) + "," );
      }
   }

   crossGetFrame('map').forms['status'].extra.value = stpois;
   selectOpAndSubmit( 26 );

}

function showPOIS2( forma ) {

   var stpois = "";

   for (k=0; k < forma.pois.length; k++  ) {
      if ( forma.pois[k].checked == true) {
	
		stpois += ( ( k + 1 ) + "," );
      }
   }

   crossGetFrame('map').forms['status'].sp.value = 1;
   crossGetFrame('map').forms['status'].poi.value = stpois;
   selectOpAndSubmit( 50 );

}

function showAllPOIS( forma ) {

   var stpois = "";

   for (k=0; k < forma.pois.length; k++  ) {
      forma.pois[k].checked = true;
      stpois += ( ( k + 1 ) + "," );
   }


   crossGetFrame('map').forms['status'].sp.value = 1;
   crossGetFrame('map').forms['status'].poi.value = stpois;

   //selectOpAndSubmit(26 );



}


function showAllMapLayers( forma ) {

   var stpois = "";

   for (k=0; k < forma.pois.length; k++  ) {
      forma.pois[k].checked = true;
      stpois += ( ( k + 1 ) + "," );
   }


   crossGetFrame('map').forms['status'].extra.value = stpois;

//   selectOpAndSubmit(26 );



}


function hidePOIS( forma ) {
	for (k=0; k < forma.pois.length; k++  ) {
		forma.pois[k].checked = false;
	}
	crossGetFrame('map').forms['status'].sp.value=1;
	crossGetFrame('map').forms['status'].poi.value='';
	//selectOpAndSubmit( 26 );
}


function hideMapLayers( forma ) {
	for (k=0; k < forma.pois.length; k++  ) {
		forma.pois[k].checked = false;
	}

	crossGetFrame('map').forms['status'].extra.value='';
//	selectOpAndSubmit( 26 );
}





function toolTipCorner ( d ) {
	window.status = d;

}

function localShowStatus( ) {
	
	var n = 'map';

	if( browser == 'ie' ) 
	     return   document.frames[ n ].showStatus();
	else if( browser == 'moz' )
	     return  window.frames[ n ].showStatus();
	else
	     return   document.frames[ n ].showStatus();

}


function changeCursor( tipo , objeto ) {
	
	var myCursor;

	objeto.style.cursor = 'hand';

	switch( tipo ) {

		case 1:
			if( browser == 'ie' && browserVersion >= 6 ) {
				myCursor = 'mapimages/hand5.cur';
			}	
			else if( browser == 'ie' ){
				myCursor = 'move';
			}
			else if( browser == 'moz' ) {
				if( ! firefox ) 
					myCursor = '-moz-grab';
				else 
					myCursor = 'move';
			}
			else {
				myCursor = 'move';
			}
			
		break;

	
		case 11:
			if( browser == 'ie' && browserVersion >= 6 ) {
				myCursor = 'mapimages/grabbing.cur';
			}	
			else if( browser == 'ie' ){
				myCursor = 'move';
			}
			else if( browser == 'moz' ) {
				myCursor = '-moz-grabbing';
			}
			else {
				myCursor = 'move';
			}
		break;






	
		case 2:
			if( browser == 'ie' && browserVersion >= 6 ) {
				myCursor = 'mapimages/zoomin2.cur';
			}	
			else if( browser == 'ie' ){
				myCursor = 'crosshair';
			}
			else if( browser == 'moz' ) {
				myCursor = '-moz-zoom-in';
			}
			else {
				myCursor = 'crosshair';
			}
		break;


		case 3:
			if( browser == 'ie' && browserVersion >= 6 ) {
				myCursor = 'mapimages/zoomout5.cur';
			}	
			else if( browser == 'ie' ){
				myCursor = 'hand';
			}
			else if( browser == 'moz' ) {
				myCursor = '-moz-zoom-out';
			}
			else {
				myCursor = 'hand';
			}
		break;

		case 5:
			if( browser == 'ie' && browserVersion >= 6 ) {
				myCursor = 'help';
			}	
			else if( browser == 'ie' ){
				myCursor = 'help';
			}
			else if( browser == 'moz' ) {
				myCursor = 'help';
			}
			else {
				myCursor = 'help';
			}
		break;



		default:
			myCursor = 'auto';
		break;
	}

	objeto.style.cursor = myCursor;

}

