/*
 * <copyright>
 *  Copyright (c) 2009 by IICM, Graz University of Technology, Austria
 * </copyright>
 *
 *  This software is the confidential information of IICM,
 *  Graz University of Technology, Austria. You shall not disclose such
 *  confidential information and shall use it only in accordance with
 *  the IICM.
 *
 *  IICM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 *  SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 *  PURPOSE, OR NON-INFRINGEMENT. IICM SHALL NOT BE LIABLE FOR ANY DAMAGES
 *  SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 *  THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */
 
function initStatisticsDetails( mapBaseQueries, searchFieldFirst, searchFieldSecond, languageKey, mapDbInfo ) {
	var selectedDbs = "";

	var currentSearchFieldFirst = trim(searchFieldFirst);
	var currentSearchFieldSecond = trim(searchFieldSecond);
	
	if ( !document.getElementById || !document.createElement ) { return; }

	// Create database-list depending on 'hits>0' and 'database checked'
	for (db in mapDbInfo) {
		if ( mapDbInfo[db] == 'yes') { 
			// Check if hits of selected database greater 0, otherwise do not append to checked list for remote search 
			var hitsCurrentDb= dwr.util.getValue('idSpanDBConnected'+db);
			if ( isNaN(Number(hitsCurrentDb)) ) { hitsCurrentDb = 0; }
			if ( Number(hitsCurrentDb) > 0 ) {
				selectedDbs = selectedDbs+","+trim(db);
			}			
		}
	}	
	if ( selectedDbs.length > 0 ) { selectedDbs = selectedDbs.substring(1,selectedDbs.length); } else { return; }

	if ( trim(languageKey) == "") { languageKey="en"; }

	// Create Table for Statistics Summary and call DWR-Method to get Images and statistical parameters
	dwr.engine.setOrdered(true); 
	if (currentSearchFieldFirst != "" ) {
		createTableStatisticsDetails( );
		getStatisticsDetails( mapBaseQueries, currentSearchFieldFirst, currentSearchFieldSecond, selectedDbs, languageKey );
	}
}


function errh(msg) {   // TEST REMOVE
  	alert("ERROR:"+msg);
}

function getStatisticsDetails( mapBaseQueries, currentSearchFieldFirst, currentSearchFieldSecond, selectedDbs, languageKey ) {
	dwr.engine.setOrdered(true); // TEST	
	useLoadingImage("resources/appl_images/loadingImage24x24.gif","idDivStatisticsDetails"); // CHECK THIS
	StatisticsDetails.getStatisticsDetails( mapBaseQueries, currentSearchFieldFirst, currentSearchFieldSecond,selectedDbs, languageKey,
		{ 	callback:function(StatisticsSummaryTO){ createStatisticsDetails(StatisticsSummaryTO, currentSearchFieldFirst, currentSearchFieldSecond, mapBaseQueries, selectedDbs);},
			errorHandler:function(message) { errorHandler('statistics',message); },
			timeout:0
		}		
	 );	

}

// Creates Table (including Anchors) for Statistics-Summary	Images and statistical parameters
function createTableStatisticsDetails() {
	var tableElement,tbodyElement,trElement,tdElement,imgElement,divElement,inputElement;

	if ( !document.getElementById || !document.createElement ) { return; }
	// Clear Table if exists
	if ( document.getElementById('idStatisticsSummaryPosition') != null && document.getElementById('idTableStatisticsSummary') != null) {
		document.getElementById('idStatisticsSummaryPosition').removeChild(document.getElementById('idTableStatisticsSummary'));
	}
	if ( document.getElementById('idStatisticsSummaryPosition') != null && document.getElementById('idTableStatisticsDetails') != null) {
		document.getElementById('idStatisticsSummaryPosition').removeChild(document.getElementById('idTableStatisticsDetails'));
	}
	
	// Clear Message 'simpleSearchInfoBelow' if exist
	trElement = document.getElementById('idSimpleSearchInfoBelow_txt')
	if ( trElement != null ) {
		trElement.parentNode.removeChild(trElement);
	}	
	trElement = document.getElementById('idSimpleSearchInfoBelow_hr')
	if ( trElement != null ) {
		trElement.parentNode.removeChild(trElement);
	}	
	
	tableElement = document.createElement("table");
	tableElement.id = "idTableStatisticsDetails";
	tbodyElement = document.createElement("tbody");
	//Maximize/Minimize Image - Buttons	
	trElement = document.createElement("tr");
	tdElement = document.createElement("td");
	inputElement = document.createElement("input");
	inputElement.setAttribute("id","idExpandImageButton");	
	inputElement.className="statistics";	
	inputElement.setAttribute("type","button");		
	inputElement.setAttribute("value","Original size"); // TODO LgKey
	setStyle(inputElement,"display:none;");
	inputElement.onclick = new Function("expandImage('idImgStatisticsDetails','idImg2StatisticsDetails','idExpandImageButton','idCollapseImageButton' )");
	tdElement.appendChild(inputElement);
	trElement.appendChild(tdElement);	
	inputElement = document.createElement("input");
	inputElement.setAttribute("id","idCollapseImageButton");
	setStyle(inputElement,"display:none;");		
	inputElement.className="statistics";	
	inputElement.setAttribute("type","button");		
	inputElement.setAttribute("value","Fit into area"); // TODO LgKey
	inputElement.onclick = new Function("fitInImage('idImgStatisticsDetails','idImg2StatisticsDetails','idExpandImageButton','idCollapseImageButton')");	
	tdElement.appendChild(inputElement);
	trElement.appendChild(tdElement);		
	tbodyElement.appendChild(trElement);		
	// Image
	trElement = document.createElement("tr");
	tdElement = document.createElement("td");
	tdElement.id = "idTdStatisticsDetails";
	setStyle(tdElement,"vertical-align:top;");
	divElement = document.createElement("div");
	divElement.id = "idDivStatisticsDetails"
	setStyle(divElement,"width:820px; height:620px; overflow:auto; vertical-align:top; text-align:center;");
	tdElement.appendChild(divElement);
	trElement.appendChild(tdElement);
	tbodyElement.appendChild(trElement);
	tableElement.appendChild(tbodyElement);		
	document.getElementById('idStatisticsSummaryPosition').appendChild(tableElement);	
}
	
	
// Fills Table StatisticsDetails	
function createStatisticsDetails(StatisticsSummaryTO, currentSearchFieldFirst, currentSearchFieldSecond, mapBaseQueries, selectedDbs, languageKey) {	
	var tableElement,tbodyElement,trElement,tdElement,imgElement,img2Element, textElement;	
	var statisticSummary = StatisticsSummaryTO;
	var imageData1 = statisticSummary.img1;	
	var imageWidth1 = statisticSummary.img1Width;
	var imageHeight1 = statisticSummary.img1Height;
	var imageData2 = statisticSummary.img2;		
	var imageWidth2 = statisticSummary.img2Width;
	var imageHeight2 = statisticSummary.img2Height;	
	var statisticalParams = statisticSummary.statisticalParams;
	var messages = statisticSummary.lgKeys;
	var errorMsg = statisticSummary.errorMsg;
	
	
	// Enable Button for Detailed List
	if ( document.getElementById('idButtonShowStatisticsList') != null ) {
		document.getElementById('idButtonShowStatisticsList').disabled= false;
	}
	if ( document.getElementById('idButtonStatisticsExport') != null ) {
		document.getElementById('idButtonStatisticsExport').disabled= false;
	}
	
	// Create Image	
	if (imageData1 != null) {
		imgElement = document.createElement("img");
		imgElement.id = "idImgStatisticsDetails";
		document.getElementById("idDivStatisticsDetails").appendChild(imgElement);		
		dwr.util.setValue('idImgStatisticsDetails',imageData1);
	}
	else {
		alert(messages['statSummary.errorNoValue']);
	}  
	
	if (imageData2 != null) {
		img2Element = document.createElement("img");
		img2Element.id = "idImg2StatisticsDetails";	
		document.getElementById("idDivStatisticsDetails").appendChild(img2Element);	
		dwr.util.setValue('idImg2StatisticsDetails',imageData2);  
	}
	//donīt display fit buttons if image size is smaller than div size
	if ((imageHeight1 + imageHeight2) > 600 || (imageWidth1) > 800){
		document.getElementById('idCollapseImageButton').style.display = "inline";
		document.getElementById('idExpandImageButton').style.display = "none";
	}
		
	
	// Resize Div if height shorter than image-height
	divElement = document.getElementById('idDivStatisticsDetails');
	totalHeightImages = imageHeight1+imageHeight2;
	if (totalHeightImages<600) {			
		setStyle(divElement,"width:820px; height:"+(totalHeightImages+20)+"px; overflow:auto; vertical-align:top; text-align:center;");
	}
	// Create Table for Statistical Parameters
	tableElement = document.createElement("table");
	tbodyElement = document.createElement("tbody");
	for ( i in statisticalParams ) {
		trElement = document.createElement("tr");
		tdElement = document.createElement("td");
		textElement = document.createTextNode(messages['statSummary.'+i]+" "+statisticalParams[i]);
		tdElement.appendChild(textElement);	
		trElement.appendChild(tdElement);
		tbodyElement.appendChild(trElement);
	}
	tableElement.appendChild(tbodyElement);			
	document.getElementById("idTdStatisticsDetails").appendChild(tableElement);	
}


// Expands Image
function expandImage( idImage1, idImage2, idExpand, idCollapse ) {
	var imgElement, divElement;	
	if ( !document.getElementById ) { return; }	
	imgElement = document.getElementById(idImage1); 
	setStyle(imgElement,"");
	var imgWidth = imgElement.width;	
	var imgHeight = imgElement.height;
	var imgHeight2 = 0;
	//alert("Expand Image: "+factorWidth+" - "+factorHeight);	
	if ( document.getElementById(idImage2)!= null ){
		imgElement2 = document.getElementById(idImage2);
		setStyle(imgElement2,"");
		imgHeight2 = imgElement2.height;
	}

	document.getElementById('idExpandImageButton').style.display="none";
	document.getElementById('idCollapseImageButton').style.display="inline";
	divElement = document.getElementById('idDivStatisticsDetails');
	if ((imgHeight+imgHeight2) <= 620) {
		setStyle(divElement,"width:820px; overflow:auto; vertical-align:top; text-align:center;");
	}
	else {
		setStyle(divElement,"width:820px; height:620px; overflow:auto; vertical-align:top; text-align:center;");	
	}
}

// Collapse Image
function fitInImage( idImage1, idImage2, idExpand, idCollapse ) {
	var imgElement1, imgElement2, divElement;	
	if ( !document.getElementById ) { return; }	
	//get values of first image
	imgElement1 = document.getElementById(idImage1); 
	var imgWidth1 = imgElement1.width;	
	var imgHeight1 = imgElement1.height;	
	//get values of second image
	var imgWidth2 = 0;	
	var imgHeight2 = 0;
	if (document.getElementById(idImage2) != null){
		imgElement2 = document.getElementById(idImage2); 
		imgWidth2 = imgElement2.width;	
		imgHeight2 = imgElement2.height;		
	}
	
	if ( imgWidth1 > 800 ){imgWidth1 = 800;}
	if ( imgWidth2 > 800 ){imgWidth2 = 800;}
	
	if ((imgHeight1 + imgHeight2) > 600 ) {
		var imgHeight = imgHeight1;
		imgHeight1 = (600.0/(imgHeight1+imgHeight2))*imgHeight1;
		imgHeight2 = ((600.0/(imgHeight+imgHeight2))*imgHeight2);
		//alert('INSIDEIF-IMG1-'+imgHeight1+'--IMG2--'+imgHeight2);	
	}
	
	setStyle(imgElement1,"width:"+imgWidth1+"px;height:"+imgHeight1+"px");
	if(document.getElementById(idImage2) != null){setStyle(imgElement2,"width:"+imgWidth2+"px;height:"+imgHeight2+"px");}
	

	document.getElementById(idExpand).style.display="inline";
	document.getElementById(idCollapse).style.display="none";			
	
	divElement = document.getElementById('idDivStatisticsDetails');
	setStyle(divElement,"width:820px; height:620px; overflow:auto; vertical-align:top; text-align:center;");	
}



// Collapse Image Save
function fitInImage_save( idImage, idExpand, idCollapse ) {
	var imgElement, divElement;	
	if ( !document.getElementById ) { return; }	
	imgElement = document.getElementById(idImage); 
	var imgWidth = imgElement.width;	
	var imgHeight = imgElement.height;	
	var factorWidth= 800/imgWidth;
	var factorHeight = 600/imgHeight;	
	if (factorWidth<1 || factorHeight<1) { // Image width or height  greate than div-size
		if (factorWidth<factorHeight) { setStyle(imgElement,"width:800px;"); } else { setStyle(imgElement,"height:600px;"); } 
	}
	else { // Image fits in DIV -> hide Buttons
		setStyle(imgElement,""); 	
	}
	document.getElementById(idExpand).style.display="inline";
	document.getElementById(idCollapse).style.display="none";			
	
	divElement = document.getElementById('idDivStatisticsDetails');
	setStyle(divElement,"width:820px; height:620px; overflow:auto; vertical-align:top; text-align:center;");	
}