// JavaScript Document

var iimagepath="interface/";
var serviceurl = 'http://apps.formfinder.at/pfsrv/';
var serviceurl2 = 'http://data.formfinder.at/pfsrv/'; 
var clientinfo='<clientinfo><app>mobileplayer</app><id>xyz</id><key>xyz</key></clientinfo>';

var aktlistarray={};
var itemsatonce=4;
var detailsthumbsatonce=10;
//var aktproject={};
var aktproject=new Object();
var areaarray= new Array('searchcontainer','resultcontainer','imagecontainer','detailscontainer');
var personfunctarr = new Array(["architect", "Architect"], ["engineer", "Engineer"], ["contractor", "Contractor"], ["client", "Client"]);
//////////////
//htmlinit
function htmlinit(){
	window.scrollTo(0,1);
	getconfig();
};
////


///////////////
//getconfig
function getconfig(){
	configloadingstart();
	//appstartcontent loading image
	var requeststring='<projectfinder><action>get_config</action>'+clientinfo+'<requestdata mode="or"></requestdata><requestoption/></projectfinder>';
	$.post(serviceurl, requeststring, function(data){ configreplyaufbereiten(data); }, "xml");	
}

/////////
//configreplyaufbereiten
function configreplyaufbereiten(data){
	configloadingend();

	var xml=data.documentElement;
	var selectiongroupsnode = xml.getElementsByTagName("selectiongroups")[0];
	
	fillsearchfromconfig(selectiongroupsnode, 'country', 'customsearch_country_opt');
	fillsearchfromconfig(selectiongroupsnode, 'application', 'customsearch_application_opt');
	fillsearchfromconfig(selectiongroupsnode, 'buildingshape', 'customsearch_buildingshape_opt');
	fillsearchfromconfig(selectiongroupsnode, 'claddingmaterial', 'customsearch_claddingmaterial_opt');
	fillsearchfromconfig(selectiongroupsnode, 'structurematerial', 'customsearch_structurematerial_opt');

	configready();
}



//////
//fillsearchfromconfig
function fillsearchfromconfig(selectiongroupsnode, sortgroup, target){
	var opt_group = selectiongroupsnode.getElementsByTagName(sortgroup)[0];
	var optitems = opt_group.getElementsByTagName("item");
	var optstr='';
	optstr+='<option value="">- any -</option>';
	for(var i=0; i<optitems.length; i++){
		optstr+='<option value="'+optitems[i].attributes.getNamedItem("id").nodeValue+'">'+optitems[i].firstChild.nodeValue+'</option>';
	}
	$('#'+target).html(optstr); 
	document.getElementById(target).selectedIndex = 0;
}


////////
//configloadingstart
function configloadingstart(){
	var configloadingstr='';
	configloadingstr+='Loading configuration, please wait...';
	configloadingstr+='<br /><img src="'+iimagepath+'loading_d.gif" alt="" />';	
	$("#appstartcontent").html(configloadingstr);
}

/////////
//configloadingstart
function configloadingend(){
	$("#appstartcontent").html("");
}





/////////
//configready
function configready(){
	//fillconfig ...
	$("#b_search_latest").click(function(){
		getprojectlist('<requestdata mode="and"><project_id condition="gt">1</project_id></requestdata><requestoption><sort order="desc">project_id</sort><limit>'+resultlimit()+'</limit></requestoption>');
	});
	$("#b_search_year").click(function(){
		getprojectlist('<requestdata mode=\"and\"><year condition=\"gt\">1</year></requestdata><requestoption><sort order=\"desc\">year</sort><limit>'+resultlimit()+'</limit></requestoption>');
	});
	$("#b_search_modified").click(function(){
		getprojectlist('<requestdata mode="and"><project_id condition="gt">1</project_id></requestdata><requestoption><sort order="desc">entrymodified</sort><limit>'+resultlimit()+'</limit></requestoption>');
	});	
	
	fillcustomsearch();
	//handleresultlimit();
	for(var i=0; i<areaarray.length; i++){ 
		$("#"+areaarray[i]).css("display",(areaarray[i]=="searchcontainer" ? "block" : "none")); 
	};
	
	//fillsearch from config...// done before
	
	
	$("#appstartcontainer").css("display","none"); 
	showarea("searchcontainer");	
	
}

/////////////////////////
//customsearch
function customsearch(){
	theForm = document.forms['f_customsearch']; 
	var qs_a='<requestdata mode="and">';
	var qs='';
	if(theForm.customsearch_country_opt.value!=''){
		qs+='<country_id condition="is">'+escape(theForm.customsearch_country_opt.value)+'</country_id>';
	};
	if(theForm.customsearch_application_opt.value!=''){
		qs+='<application_id condition="is">'+escape(theForm.customsearch_application_opt.value)+'</application_id>';
	};	

	if(theForm.customsearch_buildingshape_opt.value!=''){
		qs+='<buildingshape_id condition="is">'+escape(theForm.customsearch_buildingshape_opt.value)+'</buildingshape_id>';
	};	
	if(theForm.customsearch_claddingmaterial_opt.value!=''){
		qs+='<claddingmaterial_id condition="is">'+escape(theForm.customsearch_claddingmaterial_opt.value)+'</claddingmaterial_id>';
	};	
	if(theForm.customsearch_structurematerial_opt.value!=''){
		qs+='<structurematerial_id condition="is">'+escape(theForm.customsearch_structurematerial_opt.value)+'</structurematerial_id>';
	};		



	if(theForm.extrasearch1_opt.value!=''){
		qs+='<'+theForm.extrasearch1_opt.value+' condition="'+theForm.extrasearch1_cond.value+'">'+escape(theForm.extrasearch1_val.value)+'</'+theForm.extrasearch1_opt.value+'>';
	};
	if(theForm.extrasearch2_opt.value!=''){
		qs+='<'+theForm.extrasearch2_opt.value+' condition="'+theForm.extrasearch2_cond.value+'">'+escape(theForm.extrasearch2_val.value)+'</'+theForm.extrasearch2_opt.value+'>';
	};	
	
	//anfrage an function req
	qs_e='</requestdata><requestoption><sort order="desc">year</sort><limit>'+resultlimit()+'</limit></requestoption>';
	if(qs!=''){
		getprojectlist(qs_a+qs+qs_e);
	}
}

/////////////
//fillcustomsearch
function fillcustomsearch(){
	var extraoptions='\
	<option value="" selected="selected">- any -</option>\
	<option value="textsearch_title">Search title</option>\
	<option value="textsearch_person">Search persons</option>\
	<option value="textsearch_all">Search all</option>\
	<option value="project_id">Project id</option>\
	<option value="year">Year</option>\
	<option value="rating">Rating</option>\
	<option value="entrymodified">Modified</option>\
	<option value="country">Country</option>\
	<option value="town">Town</option>\
	<option value="zipcode">Zipcode</option>\
	<option value="geoposlat">Geoposition latitude</option>\
	<option value="geoposlon">Geoposition longitude</option>\
	<option value="application">Application</option>\
	<option value="buildingtype">Building type</option>\
	<option value="buildingshape">Building shape</option>\
	<option value="structuretype">Structure type</option>\
	<option value="structureshape">Structure shape</option>\
	<option value="structurematerial">Structure material</option>\
	<option value="claddingtype">Cladding type</option>\
	<option value="claddingmaterial">Cladding material</option>\
	<option value="length">Length</option>\
	<option value="width">Width</option>\
	<option value="height">Height</option>\
	<option value="architect">Architect</option>\
	<option value="engineer">Engineer</option>\
	<option value="contractor">Contractor</option>\
	<option value="client">Client/Owner</option>\
	';
	var extraconditions='\
	<option value="lt">&lt;</option>\
	<option value="gt">&gt;</option>\
	<option value="is">=</option>\
	<option value="le">&le;</option>\
	<option value="ge">&ge;</option>\
	';	
	//
	$("#extrasearch1_opt").html(extraoptions);
	$("#extrasearch2_opt").html(extraoptions);
	$("#extrasearch1_cond").html(extraconditions);	
	$("#extrasearch2_cond").html(extraconditions);	
	document.getElementById('resultlimit').selectedIndex = 0;
	document.getElementById('extrasearch1_opt').selectedIndex = 0;
	document.getElementById('extrasearch2_opt').selectedIndex = 0;
	document.getElementById('extrasearch1_cond').selectedIndex = 2;
	document.getElementById('extrasearch2_cond').selectedIndex = 2;
	
}

////////
//resultlimit
function resultlimit(){
	//return $("#resultlimit").val();	
	return escape($("#resultlimit").val()); 
}
////////
//handleresoultlimit
function handleresultlimit(){
	//var limitstr='<option value="100">100</option><option value="400">400</option><option value="">no</option>';
	//$("#resultlimit").html(limitstr);
	//$("#resultlimit").change(function(){	alert("c");	}
	
	/*
	$(document).ready(function(){ 
		$("#resultlimit").change(function(){	alert("c");	}				   
	}
	
	$("input[type='text']").change( function() {
		alert($(this).val());
	});
	*/
	//alert($("#resultlimit").val());

}


///////////////////////////////////////////////////////////////////////////
//////////
//showarea
function showarea(zieldiv){
	//var y = document.getElementById(zieldiv).style.top;
	//var y=document.getElementById(zieldiv).offsetTop;
	//window.scrollTo(0,y);
	
	
	for(var i=0; i<areaarray.length; i++){
		//$("#"+areaarray[i]).css-visible=(areaarray[i]!=zieldiv)?false:true;
		//var state=(areaarray[i]!=zieldiv)?"hidden":"visible";
		//$("#"+areaarray[i]).css("visibility", state); 
		//$('#content').css("visibility",(visibleState ? "visible" : "hidden"));  
		$("#"+areaarray[i]).css("display",(zieldiv==areaarray[i] ? "block" : "none")); 
	};


	//$("#searchcontainer").css('display','none');
	var el = document.getElementById(zieldiv);
    el.scrollIntoView(true);
}




//////////
//getprojectlist
function getprojectlist(requestdata){
	var requeststring='<projectfinder><action>get_projectlist</action>'+clientinfo;
	requeststring+=requestdata;
	requeststring+='</projectfinder>';
	listloadingstart();
	showarea('resultcontainer');
	$.post(serviceurl, requeststring, function(data){ listreplyaufbereiten(data); }, "xml");	
}
/////////
//listloadingstart
function listloadingstart(){
	$("#title_result").html(' ...');	
	aktlistarray={};
	$("#resultcontent").html('<img src="'+iimagepath+'loading_d.gif" alt="" />');
	$("#resultnav").html(''); 
}
/////////
//listloadingend
function listloadingend(){
	$("#resultcontent").html("");
}
//////////
//listreplyaufbereiten
function listreplyaufbereiten(data){
	listloadingend();
	var xml=data;
	var items = xml.getElementsByTagName("item");//noch check wenn kein item
	var resultcount=items.length;
	for(var i=0; i<resultcount; i++){
		var tmpobj={};
		tmpobj['project_id']=nodetextnotnull(items[i].getElementsByTagName('project_id')[0]);
		tmpobj['title']=nodetextnotnull(items[i].getElementsByTagName('title')[0]);
		tmpobj['image']=nodetextnotnull(items[i].getElementsByTagName('image')[0]);
		aktlistarray[i]=tmpobj;		
	}
	displaylist(0);
	$("#title_result").html(': Found '+resultcount+' projects');
};




//////////
//displaylist
function displaylist(pos){
	var liststring='';
	for(var i=0; i<itemsatonce; i++){
		if(aktlistarray[pos+i]){
			liststring+=buildlistitem(pos+i);
		}
	};
	//liststring+=getlistnav(pos);
	$("#resultcontent").html(liststring);
	
	$("#resultnav").html(getlistnav(pos)); 
}

/////////
//buildlistitem
function buildlistitem(pos){
	var itemstring='';
	var aktitem=aktlistarray[pos];
	var itemclick="javascript:getprojectdetails('<requestdata><project_id>"+aktitem.project_id+"</project_id></requestdata>')";
	itemstring+='<div class="listitem" id="listitem_'+aktitem.project_id+'" onClick="'+itemclick+'" ><img class="itemimage" src="'+aktitem.image+'" alt="" /><div class="itemtitle">'+aktitem.title+'</div></div>';
	return itemstring;	
}

/////////
//xxgetlistnav
function xxgetlistnav(pos){
	listnavstring='';
	var back=(aktlistarray[pos-itemsatonce])?'<input type="button" value="back" onClick="javascript:displaylist('+(pos-itemsatonce)+')" />':'<input type="button" value="back" disabled="disabled" />';
	var next=(aktlistarray[pos+itemsatonce])?'<input type="button" value="next" onClick="javascript:displaylist('+(pos+itemsatonce)+')" />':'<input type="button" value="next" disabled="disabled" />';
	listnavstring='<div>'+back+' '+next+'</div>';	
	return listnavstring;
}

/////////
//getlistnav
function getlistnav(pos){
	listnavstring='';
	var back=(aktlistarray[pos-itemsatonce])?'<img class="resultnavimage" src="'+iimagepath+'nav-left-akt.png" onClick="javascript:displaylist('+(pos-itemsatonce)+')" />':'<img class="resultnavimage" src="'+iimagepath+'nav-left-inakt.png" />';
	var next=(aktlistarray[pos+itemsatonce])?'<img class="resultnavimage" src="'+iimagepath+'nav-right-akt.png" onClick="javascript:displaylist('+(pos+itemsatonce)+')" />':'<img class="resultnavimage" src="'+iimagepath+'nav-right-inakt.png" />';
	listnavstring='<div>'+back+next+'</div>';	
	return listnavstring;
}






/////////////////////////////////////////////////////////////
////////////
//getprojectdetails
function getprojectdetails(requestdata){
	var requeststring='<projectfinder><action>get_projectdetails</action>'+clientinfo;
	requeststring+=requestdata;
	requeststring+='</projectfinder>';
	detailsloadingstart();
	//showarea('detailscontainer');
	showarea('imagecontainer');	
	$.post(serviceurl, requeststring, function(data){ detailsreplyaufbereiten(data); }, "xml");		
}
/////////
//listloadingstart
function detailsloadingstart(){
	$("#detailscontent").html('<img src="'+iimagepath+'loading_d.gif" alt="" />');
	$("#detailsbigimagearea").html('<img src="'+iimagepath+'loading_d.gif" alt="" />');
	$("#detailsthumbsarea").html('');
	$("#detailsthumbsnav").html('');
	$("#title_image").html(' ...');
	$("#title_details").html(' ...');	
}
/////////
//listloadingend
function detailsloadingend(){
	$("#detailscontent").html("");
	$("#detailsbigimagearea").html("");
}
//////
//detailsreplyaufbereiten
function detailsreplyaufbereiten(data){
	detailsloadingend();
	aktproject={};

	var xml=data.documentElement;
	var projectnode = xml.getElementsByTagName("item")[0];  // = projects/item
	//var projectnode = data.getElementsByTagName("item")[0];  // = projects/item
	
	//var resultcount=projectnode.childNodes.length; // notice, whitespace includet
	
	///////// aktproject-texte //////
	var aktnode=projectnode.firstChild;
	while(aktnode){
		if(aktnode.nodeType==1){
			var tname=(aktnode.nodeName)?aktnode.nodeName:"foo";
			var text=(aktnode.childNodes[0] && aktnode.childNodes[0].nodeType==3)?aktnode.childNodes[0].nodeValue:"";
			var id=(aktnode.attributes.getNamedItem("id"))?aktnode.attributes.getNamedItem("id").nodeValue:false;
			aktproject[tname]={};
			aktproject[tname]['text']=text;
			if(id){
				aktproject[tname]['id']=id;
			}
		};		
		aktnode=aktnode.nextSibling;
	}
	/////////// images ///////////////
	//aktproject['images']={}; //statt imageresources
	aktproject['images']= new Array(); 
	var imageresourcenode =xml.getElementsByTagName("imageresources")[0]; 
	var images= imageresourcenode.getElementsByTagName("item");
	for(var i=0; i<images.length ; i++){
		var tmpobj = {};
		var pimagenode=images[i];		
		tmpobj['title']=nodetextnotnull(pimagenode.getElementsByTagName("title")[0]);
		tmpobj['description']=nodetextnotnull(pimagenode.getElementsByTagName("description")[0]);
		tmpobj['format']=nodetextnotnull(pimagenode.getElementsByTagName("format")[0]);
		tmpobj['size1']=nodetextnotnull(pimagenode.getElementsByTagName("size1")[0]);
		tmpobj['size2']=nodetextnotnull(pimagenode.getElementsByTagName("size2")[0]);
		tmpobj['fileurl']=nodetextnotnull(pimagenode.getElementsByTagName("fileurl")[0]);
		//aktproject.images[i]=tmpobj;
		aktproject.images.push(tmpobj);
	}
	//////////////  people  ///////////////////
	aktproject['people']= new Array(); 
	var peoplenode =xml.getElementsByTagName("people")[0]; 
	for(var i=0; i<personfunctarr.length; i++){
		aktproject.people[personfunctarr[i][0]] = new Array();
		var aktfunctionnode=peoplenode.getElementsByTagName(personfunctarr[i][0])[0];
		var person= aktfunctionnode.getElementsByTagName("person");
			for(var p=0; p<person.length; p++){
				var tmpObj=new Object();
				tmpObj['text']=person[p].firstChild.nodeValue;
				tmpObj['id']=person[p].attributes.getNamedItem("id").nodeValue;
				tmpObj['function']=personfunctarr[i][1];
				aktproject.people[personfunctarr[i][0]].push(tmpObj);					
			}
	}
	
	//////////////////////////////////////
	//$("#detailscontent").html("...daten...");
	filldetails();
	writeimagecontent();
}







//////////
//nodetextnotnull
function nodetextnotnull(cont){
	contcheck=(cont.firstChild!=null)?cont.firstChild.nodeValue:"";
	return contcheck;
}





///////////////
// filldetails
function filldetails(){
	var detstr="";
	detstr+='<b>'+aktproject.title.text+'</b><br />';
	
	////// head ////
	var pa= new Array(["-sec-" ,"Head data"], ["year" ,"Year"], ["-sec-" ,"Location"], ["country", "Country"], ["town" ,"Town"], ["zipcode" ,"Zipcode"], ["geoposlat" ,"Geopos.lat"], ["geoposlon" ,"Geopos.lon"], ["-sec-" ,"Dimension"], ["length" ,"Length"], ["width" ,"Width"], ["height" ,"Height"], ["-sec-" ,"Additional data"], ["application" ,"Application"], ["buildingtype" ,"Building type"], ["buildingshape" ,"Building shape"], ["structuretype" ,"Structure shape"], ["structurematerialcat" ,"Struct. mat. cat."], ["structurematerial" ,"Structure mat."], ["claddingtype" ,"Cladding type"], ["claddingmaterialcat" ,"Cladding mat.cat."], ["claddingmaterial" ,"Cladding mat."]);
	detstr+='<table class="projectdetailstable">';
	for(var i=0; i<pa.length; i++){
		if(pa[i][0]=="-sec-"){
			detstr+='<tr><td colspan="2" class="projectdetailssec">'+pa[i][1]+'</td></tr>';
		}else{
			detstr+='<tr><td class="projectdetailsatt">'+pa[i][1]+':</td><td class="projectdetailsval">'+aktproject[pa[i][0]].text+'</td></tr>';
		}
	}
	//// people //////
	detstr+='<tr><td colspan="2" class="projectdetailssec">People</td></tr>';
	for(var i=0; i<personfunctarr.length; i++){
		var aktpf=personfunctarr[i][0];
		if(aktproject.people[aktpf].length>0){
			var persstr='<ul class="detailspersonlist" >';
			for(var p=0; p<aktproject.people[aktpf].length; p++){
				persstr+='<li>'+aktproject.people[aktpf][p].text+'; </li>';
			};
			persstr+='</ul>';
			detstr+='<tr><td class="projectdetailsatt">'+personfunctarr[i][1]+':</td><td class="projectdetailsval">'+persstr+'</td></tr>';
		}
	};
	///////////////
	detstr+='</table>';

	if(aktproject.databaseentry.text!=""){
		detstr+='<br />'+aktproject.databaseentry.text+'<br />';
	}
	detstr+=(aktproject.shortdescription.text!="")?'<br />'+aktproject.shortdescription.text+'<br />':'';
	//////////////////
	detstr+='<form name="form_getpdf" id="form_getpdf" method="post" action="'+serviceurl2+'" enctype="application/x-www-form-urlencoded" target="_blank"><input type="hidden" name="notrawxml" id="notrawxml" style="display: none;" /></form>';
	/////////////////////
	detstr+='<br/><div style="text-align: center;"><a href="javascript:getprojectpdf(\'<requestdata><project_id>'+aktproject.project_id.text+'</project_id></requestdata>\')" />get project PDF</a></div><br />';
	//
	$("#detailscontent").html(detstr);
	writetitleextra();
}











///////////////
//writeimagecontent
function writeimagecontent(){
	//build the thumblist, include clickevents
	//start with first image
	//showdetailsbigimage(aktproject.images[0].size2);
	detailsthumblist(0);
}



///////////
//showdetailsbigimage
function showdetailsbigimage(imageurl){
	var str='';
	str+='<img class="imgsize2" src="'+imageurl+'" alt="" />'; 
	$("#detailsbigimagearea").html(str);	
}

//////////////
//detailsthumblist
function detailsthumblist(pos){
	//write the list at pos with x images
	var liststring='';
	for(var i=0; i<detailsthumbsatonce; i++){
		if(aktproject.images[pos+i]){
			liststring+=builddetailsthumbitem(pos+i);
		}
	};
	$("#detailsthumbsarea").html(liststring);
	$("#detailsthumbsnav").html(getthumbsnav(pos)); 	
	showdetailsbigimage(aktproject.images[pos].size2);//start with fisrt in list
}

///////////
//builddetailsthumbitem
function builddetailsthumbitem(pos){
	var itemstring='';
	var aktitem=aktproject.images[pos];
	var itemclick="javascript:showdetailsbigimage('"+aktitem.size2+"')";
	itemstring+='<img class="detailsthumbimage" src="'+aktitem.size1+'" alt="" onClick="'+itemclick+'" />';	
	return itemstring;
}


/////////
//getthumbsnav
function getthumbsnav(pos){
	navstring='';
	var detailslink='<a href="javascript:showarea(\'detailscontainer\')" />show details</a>'; 
	var back=(aktproject.images[pos-detailsthumbsatonce])?'<input type="button" value="back" onClick="javascript:detailsthumblist('+(pos-detailsthumbsatonce)+')" />':'<input type="button" value="back" disabled="disabled" />';
	var next=(aktproject.images[pos+detailsthumbsatonce])?'<input type="button" value="next" onClick="javascript:detailsthumblist('+(pos+detailsthumbsatonce)+')" />':'<input type="button" value="next" disabled="disabled" />';
	navstring='<div>'+back+' '+next+' '+detailslink+'</div>';	
	return navstring;
}


///////////
//writetitleextra
function writetitleextra(){
	var str='';
	var maxlen=30;
	str+=aktproject.title.text;
	if (str.length > maxlen) {
		str = str.substr(0, maxlen) + "...";  
	} 	
	$("#title_image").html(str);
	$("#title_details").html(str);
}

/////////////////////////

//getprojectdetails
function getprojectpdf(requestdata){
	var requeststring='<projectfinder><action>get_project_pdf</action>'+clientinfo;
	requeststring+=requestdata;
	requeststring+='</projectfinder>';
	$('#notrawxml').val(requeststring);
	$('#form_getpdf').submit();		
}


