/*
 * Query
 * Steuert die Kartenabfragen
 *
 * (c) 2006 3Kon Informationstechnologien GmbH
 *
 * @Author	Tilmann Fruntke <tfruntke@buschjena.de>
 * @date		25.09.2006
 */

var queryWindowList = new Array(20); /* todo: dynamisches array */
var currentSearchWindow = 0;
var curZIndex = 150;
var queryCanvas = null
var hexArr = Array("00","11","22","33","44","55","66","77","88","99","aa","bb","cc","dd","ee","ff");



/* js save */
var isOpen = new Array(20); /* Layer opened? */

function refreshQueryPositions()
{
 addDebugMsg ( "refreshQueryPositions" );
 
 for ( var i = 0; i < queryWindowList.length; i++ )
 		 if ( queryWindowList[ i ] != null )
 		 		queryWindowList[ i ].redrawWindow();
}

function queryWindow( viewport, window, id, myIndex )
{
 	 addDebugMsg("init new queryWindow: " + id + " on viewport: " + viewport + "<br>myIndex: " + myIndex);

	 /*
	  * @TODO: REFACTORING and CODE CLEANING
		*/
	 
 	/* use kaMap implemtation of getRawObject	 */
  this.parentObj = getRawObject( viewport );
	this.windowObj = myViewportWin.domObj;
	this.insideObj = getRawObject( "theInsideLayer" );
	this.contentObj = null;
	this.rectObj = null;
	this.id = myIndex;
	
	
	/*
	 * Canvas creation
	 */
	if ( queryCanvas == null )
		 queryCanvas = myKaMap.createDrawingCanvas( 200 ); // zahl ist der zIndex
	
	
	this.isRectQuery = true;
	
	this.coordArray = null;
	this.pX = 0;
	this.pY = 0;
 
 	if( !this.domObj ) {
		this.domObj = document.createElement('div');
		this.domObj.parent=this.parentObj;
		this.domObj.style.position = "absolute";
    //this.domObj.style.border = "1px red dotted";
		this.domObj.style.zIndex = curZIndex;
		this.domObj.style.cursor = "default";
		this.domObj.style.opacity = ".92";
		this.domObj.id = myIndex;	
		this.parentObj.appendChild( this.domObj );
	}
	
	curZIndex++;
}

/* creates a new query window */
queryWindow.prototype.createWindow = function ( width, height, coords, selectMethod )
{
 this.coordArray = coords;
 this.isRectQuery = (selectMethod == "1" ? true : false);
 
 addDebugMsg ("isSinglePoint: " + this.isRectQuery );
 
 /* window size */
 var inLX = parseInt( this.insideObj.style.left );
 var inLY = parseInt( this.insideObj.style.top );
 
 var vpX = parseInt( this.windowObj.style.left );
 var vpY = parseInt( this.windowObj.style.top );
 
 //addDebugMsg("vpX: " + vpX + " / vpY: " + vpY);
 //addDebugMsg("coords: " + coords + " / coords[2]: " + coords[2]);
 
 if ( this.isRectQuery )
 {
   this.pX = coords[2];
   this.pY = coords[3];
	 
	 var luCorner = myKaMap.geoToPix( coords[0], coords[1] );
	 var rlCorner = myKaMap.geoToPix( coords[2], coords[3] );
	 var rectLeft = luCorner[0] - myKaMap.xOrigin;
	 var rectTop = luCorner[1] - myKaMap.yOrigin;
	 var rectRight = rlCorner[0] - myKaMap.xOrigin;
	 var rectBottom = rlCorner[1] - myKaMap.yOrigin;
	 var rectWidth = rectRight - rectLeft;
	 var rectHeight = rectBottom - rectTop;
	 
	 /* draw rect query */
	 this.rectObj =	document.createElement('div');
   this.rectObj.style.position = "absolute";
   this.rectObj.style.left= rectLeft + "px";
	 this.rectObj.style.top= rectTop + "px";
   this.rectObj.style.width=rectWidth + "px";
   this.rectObj.style.height= rectHeight +  "px";
	 this.rectObj.style.backgroundColor = "#" + hexArr[ Math.round(Math.random() * 15) ] + hexArr[ Math.round(Math.random() * 15) ] + hexArr[ Math.round(Math.random() * 15) ];
	 this.rectObj.style.filter = "alpha(opacity=30)";
	 this.rectObj.style.opacity = ".3";
	 
	 alert( luCorner[0] + " - " + luCorner[1] );
	 //this.insideObj.appendChild ( this.rectObj );
	 myKaMap.addObjectPix( queryCanvas, luCorner[0], luCorner[1], this.rectObj );
  }
	else
	{
	 this.pX = coords[0];
   this.pY = coords[1];
	}
 
 pixCoords = myKaMap.geoToPix( this.pX, this.pY );
 
 //addDebugMsg("this.pX: " +  this.pX + " /  this.pY: " +  this.pY);
 //addDebugMsg("pixCoords[0]: " +  pixCoords[0] + " /  pixCoords[1]: " +  pixCoords[1] );
 
 var wWidth = 400;
 var wHeight = 300;
 var wPointerX = pixCoords[0] - myKaMap.xOrigin + inLX + vpX;
 var wPointerY = pixCoords[1] - myKaMap.yOrigin + headerSize + inLY + vpY;
 var wPointerDir = "1"; 

 /* query box displacement */	
 var leftAddition = 0;
 
 if ( !this.isRectQuery )
 {
 		leftAddition = 89;
		
		/* pointer displacement */
		wPointerX = wPointerX - 2;
		wPointerY = wPointerY - 35;
 }
 else
 {
 	 /* pointer displacement */
		wPointerX = wPointerX - 5;
		wPointerY = wPointerY - 25;
 }
 
 
 //addDebugMsg ("Pointer Coords: " + wPointerX + "/" + wPointerY );
 //addDebugMsg ("InsideLayer Coords: " + this.insideObj.style.left + "/" + this.insideObj.style.top );
 //addDebugMsg ("Abzgl. Origin Coords: " + neueBerechnungX + "/" + neueBerechnungY );
 
 
 
 /* set size on domObj */
 this.domObj.style.width = (wWidth + leftAddition) + "px";
 this.domObj.style.height = wHeight + "px";
 this.domObj.style.left = wPointerX + "px;";
 this.domObj.style.top = (wPointerY - 50) + "px;";
 this.domObj.onclick = this.setActive;
 
 /* create query window  */
 var domWindow = document.createElement('div');
 domWindow.style.position = "absolute";
 domWindow.style.width = (wWidth + leftAddition) + "px";
 domWindow.style.height = wHeight + "px";
 //domWindow.style.border = "3px blue dotted";

 var UpperMiddleWidth = ( wWidth - 17 - 49 );
 var BodyMiddleWidth = ( wWidth - 17 - 23 );
 var LowerMiddleWidth = ( wWidth - 36 - 49 );
 
 var BodyHeight = ( wHeight - 33 - 47 );
 var LowerTop = BodyHeight + 33;

 
 /* create the header line of our query window */
 // upper left
   var childUpperLeft = document.createElement('div');
   childUpperLeft.style.position = "absolute";
   childUpperLeft.style.left= leftAddition + "px";
   childUpperLeft.style.width="17px";
   childUpperLeft.style.height="33px";
	 
	 if ( this.isRectQuery )
   		childUpperLeft.style.backgroundImage="url('layout/images/queryBox_02.png')";
	 else
	 		childUpperLeft.style.backgroundImage="url('layout/images/queryBox_02_corner.png')";
	 
 // upper middle
  var childUpperMiddle = document.createElement('div');
   childUpperMiddle.style.position = "absolute";
   childUpperMiddle.style.left= (leftAddition + 17) + "px";
   childUpperMiddle.style.width= UpperMiddleWidth + "px";
   childUpperMiddle.style.height="33px";
   childUpperMiddle.style.backgroundImage="url('layout/images/queryBox_03.png')";
	 
	 
	// upper right
	var childUpperRight = document.createElement('div');
   childUpperRight.style.position = "absolute";
   childUpperRight.style.left= ( UpperMiddleWidth + leftAddition + 17 ) + "px";
   childUpperRight.style.width="49px";
   childUpperRight.style.height="33px";
   childUpperRight.style.backgroundImage="url('layout/images/queryBox_04.png')";
	
	/* create the body elements */
	// Body left
   var childBodyLeft = document.createElement('div');
   childBodyLeft.style.position = "absolute";
   childBodyLeft.style.left= leftAddition + "px";
   childBodyLeft.style.width="17px";
   childBodyLeft.style.height= BodyHeight + "px";
	 childBodyLeft.style.top="33px";
   childBodyLeft.style.backgroundImage="url('layout/images/queryBox_05.png')";
	 
 // Body middle
  var childBodyMiddle = document.createElement('div');
   childBodyMiddle.style.position = "absolute";
   childBodyMiddle.style.left= (leftAddition + 17) +  "px";
   childBodyMiddle.style.width= BodyMiddleWidth + "px";
   childBodyMiddle.style.height= BodyHeight + "px";
	 childBodyMiddle.style.top="33px";
   childBodyMiddle.style.backgroundImage="url('layout/images/queryBox_06.png')";
	 
	 
	 /* the Content Window */
	var childContent = this.contentObj = document.createElement('div');
   childContent.style.position = "relative";
   childContent.style.width= "100%";
   childContent.style.height= "100%";
	 childContent.style.overflow = "auto";
	 
	/* loading bar... */
	childContent.innerHTML = "<div style=\"width:100%; position:absolute; top:"+(BodyHeight/2-15)+"px; left:"+(BodyMiddleWidth/2-130)+"px;\"><div class=\"qw_spinner\"><img src=\"layout/images/spinner.gif\"></div><div class=\"qw_caption\">Einen Moment Geduld bitte...</div>";
	 
	// Body right
	var childBodyRight = document.createElement('div');
   childBodyRight.style.position = "absolute";
   childBodyRight.style.left= ( BodyMiddleWidth + leftAddition + 17 ) + "px";
   childBodyRight.style.width="23px";
   childBodyRight.style.height= BodyHeight + "px";
	 childBodyRight.style.top="33px";
   childBodyRight.style.backgroundImage="url('layout/images/queryBox_07.png')";
	 
	// Footer Elements
	// Lower left
   var childLowerLeft = document.createElement('div');
   childLowerLeft.style.position = "absolute";
   childLowerLeft.style.left= leftAddition + "px";
   childLowerLeft.style.width="36px";
   childLowerLeft.style.height="47px";
	 childLowerLeft.style.top= LowerTop + "px";
   childLowerLeft.style.backgroundImage="url('layout/images/queryBox_10.png')";
	 
 // Lower middle
  var childLowerMiddle = document.createElement('div');
   childLowerMiddle.style.position = "absolute";
   childLowerMiddle.style.left=( leftAddition + 36 ) + "px";
   childLowerMiddle.style.width= LowerMiddleWidth + "px";
   childLowerMiddle.style.height="47px";
	 childLowerMiddle.style.top= LowerTop + "px";
   childLowerMiddle.style.backgroundImage="url('layout/images/queryBox_11.png')";
	 
	// Lower right
	var childLowerRight = document.createElement('div');
   childLowerRight.style.position = "absolute";
   childLowerRight.style.left= ( LowerMiddleWidth + leftAddition + 36 ) + "px";
   childLowerRight.style.width="49px";
   childLowerRight.style.height="47px";
	 childLowerRight.style.top= LowerTop + "px";
   childLowerRight.style.backgroundImage="url('layout/images/queryBox_12.png')";
	
	
	/* our nice close button :) */
	var childCloseButton = document.createElement('div');
   childCloseButton.style.position = "absolute";
   childCloseButton.style.left="4px";
   childCloseButton.style.width="14px";
   childCloseButton.style.height="14px";
	 childCloseButton.style.top="8px";
   childCloseButton.style.backgroundImage="url('layout/images/closeQuery.gif')";
	 childCloseButton.onclick = this.closeWindow;
	 
	/* yeah, show us our query rect */
/* var childQueryButton = document.createElement('div');
   childQueryButton.style.position = "absolute";
   childQueryButton.style.left="-15px";
   childQueryButton.style.width="14px";
   childQueryButton.style.height="14px";
	 childQueryButton.style.top="8px";
   childQueryButton.style.backgroundImage="url('layout/images/queryCanvas.gif')";
	 childQueryButton.onmousedown = this.showQueryRect;*/
 
 /* the pointer */
 if ( !this.isRectQuery )
 {
 var childPointer = document.createElement('div');
   childPointer.style.position = "absolute";
   childPointer.style.left="0px";
   childPointer.style.width="91px";
   childPointer.style.height="118px";
	 childPointer.style.top="8px";
   childPointer.style.backgroundImage="url('layout/images/queryBox_08.png')";
 }
 	/* from nothing to our dom... */
	//childUpperRight.appendChild ( childQueryButton );
	childUpperRight.appendChild ( childCloseButton );

 	domWindow.appendChild ( childUpperLeft );
	domWindow.appendChild ( childUpperMiddle );
 	domWindow.appendChild ( childUpperRight );

 	domWindow.appendChild ( childBodyLeft );
	
	childBodyMiddle.appendChild ( childContent );
 	domWindow.appendChild ( childBodyMiddle );
 	domWindow.appendChild ( childBodyRight );
	
	domWindow.appendChild ( childLowerLeft );
 	domWindow.appendChild ( childLowerMiddle );
 	domWindow.appendChild ( childLowerRight );
	
	if ( !this.isRectQuery )
		 domWindow.appendChild ( childPointer );
 
 this.domObj.appendChild( domWindow );
 
}
queryWindow.prototype.setContent = function ( text )
{
 this.contentObj.innerHTML = text;
}

/* creates a new query window */
queryWindow.prototype.setActive = function ()
{
 if ( this.style.zIndex != curZIndex )
 {
   this.style.zIndex = curZIndex;
   curZIndex++;
	 return true;
 }
 return false;
}

/* creates a new query window */
queryWindow.prototype.redrawWindow = function ()
{
 
 /* window size */
 var inLX = parseInt( this.insideObj.style.left );
 var inLY = parseInt( this.insideObj.style.top );
 var inLW = parseInt( this.insideObj.parentNode.style.width );
 var inLH = parseInt( this.insideObj.parentNode.style.height );
 
 var vpX = parseInt( this.windowObj.style.left );
 var vpY = parseInt( this.windowObj.style.top );
 
 //addDebugMsg("REDRAW:  inLX: " +  inLX + " /  inLY: " +  inLY );
 //addDebugMsg("REDRAW:  inLW: " +  inLW + " /  inLH: " +  inLH );
// addDebugMsg("REDRAW:  vpX: " +  vpX + " /  vpY: " + vpY );
// addDebugMsg("REDRAW:  this.pX: " +  this.pX + " /  this.pY: " +  this.pY );
 
 pixCoords = myKaMap.geoToPix( this.pX, this.pY );
 
 addDebugMsg("pixCoords[0]: " +  pixCoords[0] + " /  pixCoords[1]: " +  pixCoords[1] );
 

 var wPointerX = pixCoords[0] - myKaMap.xOrigin + inLX + vpX;
 var wPointerY = pixCoords[1] - myKaMap.yOrigin + headerSize + inLY + vpY - 30;

 addDebugMsg ("Pointer Coords: " + wPointerX + "/" + wPointerY );

 this.domObj.style.left = wPointerX + "px;";
 this.domObj.style.top = (wPointerY - 50) + "px;";
 
 // FORMEL ZUM PRÜFEN, OB QUERY WINDOW IM SICHTBAREN EXTENT IST
 // inneDiv.LEFT + POSX - window Body Width > 0
 // inneDiv.TOP + POSY - window Body HEIGHT > 0
 
 if ( inLX + ( pixCoords[0] - myKaMap.xOrigin ) - inLW )
 		addDebugMsg("query window out of range...");
 
}

/* close current query window */
queryWindow.prototype.closeWindow = function ()
{
 /* dirty... */
 var windowObj = this.parentNode.parentNode.parentNode;
 var viewportObj = windowObj.parentNode;
 var winId = windowObj.id;
 var windowInstance = queryWindowList[ winId ];
 addDebugMsg( winId );
 

 //this.parentNode.parentNode.parentNode.style.border="5px black solid";
 //alert ( this );
 viewportObj.removeChild( windowObj );
 //this.rectObj = null;
 if ( windowInstance.rectObj != null )
 		myKaMap.removeObject( windowInstance.rectObj );
		
 windowInstance = null;
 
}


function initInfoQuery( eventID, queryType, coords ) {
    var szLayers = '';
    var layers = myKaMap.getCurrentMap().getAllLayers();
		
		/*if(layers.length==0) {
     alert("No queryable layers at this scale and extent");
     return;
    }*/
    for (var i=0;i<layers.length;i++) {
        szLayers = szLayers + "," + layers[i].name;
    }
		
		/* erstmal für alle layer:
		 */
		 var postQuery = "";
		 var layernum = myKaMap.aMaps[myKaMap.currentMap].CountLayers();
		 for (var i=0;i<layernum;i++) {
        postQuery = postQuery + "&layerlist["+i+"]=" + i;
    }
		addDebugMsg("postQuery" + postQuery);

    var extent = myKaMap.getGeoExtents();
    var scale = myKaMap.getCurrentScale();
    var cMap = myKaMap.getCurrentMap().name;
	var params='map='+cMap+'&q_type='+queryType+'&scale='+scale+postQuery+'&coords='+coords+'&extent='+extent[0]+'|'+extent[1]+'|'+extent[2]+'|'+extent[3];

	var winIndex = currentSearchWindow;
	currentSearchWindow++;
		if ( currentSearchWindow > 20)
			 currentSearchWindow = 0;

		queryWindowList[ winIndex ] = new queryWindow( "viewport", "map", "query_" + winIndex, winIndex );
		queryWindowList[ winIndex ].createWindow( 0,0,coords,queryType );

		
		call('../ajax_query.php?'+params,queryWindowList[ winIndex ], queryWindowList[ winIndex ].setContent);
}

/* login on debug manager */
registerStartedComponent( 'jquery' );

