
var openchild1= " ";
var cvlat,cvlon;
var lenmin,gr,min,minh,startm,starts,starths,lensec,sec,sech,f1lat,f1lon,inputf;
var lastform=-1;
//***************************
  function copyroute_tr(nr) {
//***************************
 	var track1=document.getElementById('track1');
	var keuze=document.getElementById('keuze');
	var vmiddel=document.getElementById('vmiddel');
	var trkname=document.getElementById('trkname');
	var funcr=document.getElementById('funcr');
	var ctrid=document.getElementById('ctrid');
	var crdid=document.getElementById('crdid');
	ctrid.value=atrid[nr];
	crdid.value=ardid[nr];
	trkname.value=trname[nr];
	keuze.value=trtype[nr];
	vmiddel.value=trvmiddel[nr];
	trcoord[nr]=trcoord[nr].replace(/& /g,"\n"); 
	trcoord[nr]=trcoord[nr].replace(/&/g,"\n"); 
      track1.value= trcoord[nr];
	funcr.value= "yes";
	track1.focus();
	return true;
}
//***************************
  function copylatlon (selection,vpnaam,vpplaats,vplat,vplon){
//***************************
 	var lat=document.getElementById('lat');
	var lon=document.getElementById('lon');
	var haven=document.getElementById('haven');
	var aankplts=document.getElementById('aankplts');

	
	if (selection == "2") {
		lat.value=vplat;
		lon.value=vplon;
		haven.value=vpnaam;
		aankplts.value=vpplaats;

		}

	//alert ("selected: " + document.getElementById('laloform').options[document.getElementById('laloform').selectedIndex].value);
	lastform="1";
	document.getElementById('llf').value="-gg.hhhhhh";
	document.getElementById('laloform').value="1";
	return true;
}
//***************************
function trim(s){
//***************************
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
//* substring: start,stop terwijl substr start, lengte is!
}
//**************************
function jslalomin2(string) {
//**************************
startm=string.indexOf(" ")+1;
lenmin=string.length - startm;
gr=string.substr(0,startm);
gr=parseFloat(gr);
min =string.substr(startm,lenmin);
minh=parseFloat(min)/60 ;
if (string.substr(0,1) == "-")
  cvlat=(parseFloat(string.substr(0,startm)) -  minh).toFixed(6)+"";
else
  cvlat=(parseFloat(string.substr(0,startm)) + minh).toFixed(6)+"";
} 
//**************************
function jslalomin3(string) {
//**************************
startm=string.indexOf(" ")+1;
lenmin=string.length - startm;
min =string.substr(startm,lenmin);
starts=min.indexOf(" ")+1;
sec=min.substr(starts,2);
min=string.substr(startm,2);
minh=parseFloat(min)/60 ;
sech=parseFloat(sec)/3600;
//alert ("ll3 minh: "+ minh + " sech: "+sech+ " min: "+ min + " sec: "+ sec + " input: "+string);

if (string.substr(0,1) == "-")
  cvlat=(parseFloat(string.substr(0,startm)) -  minh - sech).toFixed(6)+"";
else
  cvlat=(parseFloat(string.substr(0,startm)) + minh + sech).toFixed(6)+"";
}

//**************************
function jslalominh(string) {
//**************************
startm=string.indexOf(".");
lenmin=string.length - startm;
minh =string.substr(startm,lenmin);
min=parseFloat(minh)*60 ;
min=min.toFixed(4);
cvlat=parseFloat(string.substr(0,startm)).toFixed(0)+" " + min;
//alert("string binnen?: " + string+ " met punt op: "+startm+ " en  mmhhh: "+minh+" en  min: "+min+ " en lath: " +cvlat);
}
//**************************
function jslalommss(string) {
//**************************
startm=string.indexOf(".");
lenmin=string.length - startm;
minh =string.substr(startm,lenmin);
minh=(parseFloat(minh)*60).toFixed(1);
starts=minh.indexOf(".");
min=(minh+"").substr(0,starts);
lensec=minh.length-starts;
sech=(minh+"").substr(starts,lensec);
sech=parseFloat(sech*60);
sec=sech.toFixed(0);
cvlat=parseFloat(string.substr(0,startm)).toFixed(0)+" " + min+" "+sec;
//alert("string binnen?: " + string+ " met punt op: "+startm+ " en  mmhhh: "+minh+" en  min: "+min+ "en sec: "+sec+" en lath: " +cvlat);
}
//***************************
  function setlatlon (newform,oldform,extensie){
//***************************
 var idlat= 'lat'+extensie;
 var idlon= 'lon'+extensie; 
 if (lastform != -1) oldform=lastform;
 
	
//*1 trim lat en lon?
//*2 not empty?
//*3 test per format tegen regexp en waardebereik gr mm ss
 var regf1lat=/^[-]{0,1}[0-9]{1,2}.[0-9]{2,14}$/;
 var regf1lon=/^[-]{0,1}[0-9]{1,3}.[0-9]{2,14}$/;
 var regf2lat=/^[-]{0,1}[0-9]{1,2} [0-9]{1,2}.[0-9]{1,12}$/;
 var regf2lon=/^[-]{0,1}[0-9]{1,3} [0-9]{1,2}.[0-9]{1,12}$/;

 var regf3lat=/^[-]{0,1}[0-9]{1,2} [0-9]{1,2} [0-9]{1,2}$/;
 var regf3lon=/^[-]{0,1}[0-9]{1,3} [0-9]{1,2} [0-9]{1,2}$/;
 
	cvlat=trim(document.getElementById(idlat).value);
	cvlon=trim(document.getElementById(idlon).value);
//      alert ("newform: "+newform + " oldform: " + oldform + " vorigeform: " +lastform+ "\n"
//		+" in-lat: "+ idlat + " in-lon: "+ idlon);
//      alert("trimmed: "+cvlat);
	var msglat=cvlat;msglon=cvlon;fout= ""; 
	var tcvlat=cvlat;
	var tcvlon=cvlon;
if (cvlat == "" && cvlon == ""){
	lastform=newform;
	if (newform == "1") inputf="-gg.hhhhhh";
	if (newform == "2") inputf="-gg mm.hhhh";
	if (newform == "3") inputf="-gg mm ss";
	document.getElementById('llf').value=inputf;
	}
	
if (cvlat != "" && cvlon != ""){
//##1
	if (oldform == "1") {
		//check f1 lat
 		if (cvlat.length<= 3 || tcvlat.replace(" ","") != cvlat || regf1lat.test(cvlat) != true || Math.abs(cvlat) - 90.00 > 0.00)
		fout+= cvlat+" -->lat format("+oldform+") [-]gg.hhhhh and between -90.0 and 90.0 \n";
		// check f1 lon
		if (cvlon.length<= 3 || tcvlon.replace(" ","") != cvlon || regf1lon.test(cvlon) != true || Math.abs(cvlon) - 180.00 > 0.00)
		fout+= cvlon+" -->lon format("+oldform+") [-]ggg.hhhhh and between -180.0 and 180.0 \n";
		}
	if (oldform == "2") {
		//check f2 lat
		if (cvlat.length<= 4 || regf2lat.test(cvlat) != true  )
		fout +=cvlat+" -->lat format("+oldform+") [-]gg mm.hhh and between -90 59.9 and 90 59.9 \n";
		//check f2 lon
		if (cvlon.length<= 4 || regf2lon.test(cvlon) != true )
		fout +=cvlon+" -->lon format("+oldform+") [-]ggg mm.hhh and between -180 59.9 and 180 59.9 \n";
		}
	if (oldform == "3") {
		//check f3 lat
		if (cvlat.length<= 4 || regf3lat.test(cvlat) != true  )
		fout += cvlat+" -->lat format("+oldform+") [-]gg mm ss and between -90 59 59 and 90 59 59 \n";
		//check f3 lat
		if (cvlon.length<= 4 || regf3lon.test(cvlon) != true )
		fout += cvlon+" -->lon format("+oldform+") [-]ggg mm ss and between -180 59 59 and 180 59 59 \n";
		}
//##1e
	if (fout != "") 
		alert (demo["err22"]+"\n"+fout+"\n \n ---- set format-change before input! -----\n ---- change input! -----\n ---- or refresh page! -----");
      else {
	 if (newform != oldform) {	
		if (oldform == "1") {
			f1lat=cvlat;
			f1lon=cvlon;
			}
		if (oldform == "2") {
			f1lat=msglat;
			jslalomin2(cvlat);
//##2
			if (Math.abs(cvlat) - 90.00 > 0.00)
				fout += msglat+" -->lat format("+oldform+") [-]gg mm.hhh between -90 and 90";
			if(Math.abs(minh) - 60.00 > 0.00)
				fout += msglat+" -->lat format("+oldform+") [-]gg mm.hhh between  -90 59.9 and 90 59.9";
			if (fout == "") f1lat=cvlat;
			f1lon=msglon;
			jslalomin2(cvlon);
			if (Math.abs(cvlat) - 180.00 > 0.00)
				fout += msglon+" -->lon format("+oldform+") [-]gg mm.hhh between -180 and 180";
			if(Math.abs(minh) - 60.00 > 0.00)
				fout += msglon+" -->lon format("+oldform+") [-]gg mm.hhh between  -180 59.9 and 180 59.9";
			  
//##2e
			if (fout == "") f1lon=cvlat;
			}
		if (oldform == "3") {
			f1lat=msglat;
			jslalomin3(cvlat);
//##3
			if (Math.abs(cvlat) - 90.00 > 0.00)
				fout += msglat+" -->lat format("+oldform+") [-]gg mm ss between -90 and 90";
			if (Math.abs(minh) - 59.00 > 0.00)
				fout += msglat+" -->lat format("+oldform+") [-]gg mm ss between  -90 59 59 and 90 59 59";
			if (Math.abs(sech) - 59.00 > 0.00)
				fout += msglat+" -->lat format("+oldform+") [-]gg mm ss between  -90 59 59 and 90 59 59";
//##3e
			if (fout == "") f1lat=cvlat;
			f1lon=msglon;
			jslalomin3(cvlon);
//##4
			if (Math.abs(cvlat) - 180.00 > 0.00)
				fout += msglon+" -->lon format("+oldform+") [-]gg mm ss between -90 and 90";
			if(Math.abs(minh) - 59.00 > 0.00)
				fout += msglon+" -->lon format("+oldform+") [-]gg mm ss between  -90 59 59 and 90 59 59";
			if(Math.abs(sech) - 59.00 > 0.00)
				fout += msglon+" -->lon format("+oldform+") [-]gg mm ss between  -180 59 59 and 180 59 59";
//##4e
			if (fout == "") f1lon=cvlat;
			}
		if (fout != "") 
			alert (demo["err22"]+"\n"+fout+"\n \n ---- change input! -----\n ---- or refresh page! -----");
      	else {
		if (newform == "1") {
			document.getElementById(idlat).value=f1lat;
			document.getElementById(idlon).value=f1lon;
			}
		if (newform == "2") {
			jslalominh(f1lat);
			document.getElementById(idlat).value=cvlat;
			jslalominh(f1lon);
			document.getElementById(idlon).value=cvlat;
			}
		if (newform == "3") {
			jslalommss(f1lat);
			document.getElementById(idlat).value=cvlat;
			jslalommss(f1lon);
			document.getElementById(idlon).value=cvlat;
			}
		}
		}
	lastform=newform;
	if (newform == "1") inputf="-gg.hhhhhh";
	if (newform == "2") inputf="-gg mm.hhhh";
	if (newform == "3") inputf="-gg mm ss";
	document.getElementById('llf').value=inputf;
	}
	}
return true;
} 
//**************************
function getvariable(val) {
//**************************
var dummy = eval(val); 
document.write(dummy); 
}
//**************************
function confirmSubmit(wat,titel){
//**************************
var agree=null;
switch (wat) {
case 'reis': agree=confirm(demo["cfm_reisa"] +  titel + demo["cfm_reisb"]); break;
case 'reiziger': agree=confirm(demo["cfm_alga"] +  titel + demo["cfm_rzgb"]); break;
case 'kennis' :  agree=confirm(demo["cfm_alga"] +  titel + demo["cfm_kenb"]); break;
case 'relatie' :  agree=confirm(demo["cfm_alga"] +  titel + demo["cfm_relb"]); break;
case 'uitn' :  agree=confirm(demo["cfm_alga"] +  titel + demo["cfm_uitnb"]); break;
default: agree=confirm(demo["cfm_dflt"]);
	}
//alert ("komen we in de confirm? "+ wat + " toest: " + agree);
if (agree) 	return true ;
else  	return false ;
}
//**************************
function focuswindow(vname,cmdv){
//**************************
//**later nog focus andere vnames
if (vname == "openen" || vname == "melden"   ) { //1
     if (window.melden && window.melden.open && !window.melden.closed)  { //3
         window.melden.focus( );
	   } //3
    } //1
if (vname == "volgen"  ) { //1
     if (window.volgen && window.volgen.open && !window.volgen.closed)  { //3
         window.volgen.focus( );
	   } //3
    } //1
if (vname == "logon"  ) { //1
     if (window.logon && window.logon.open && !window.logon.closed)  { //3
         window.logon.focus( );
	   } //3
    } //1
if (vname == "test"  ) { //1
     if (window.test && window.test.open && !window.test.closed)  { //3
         window.test.focus( );
	   } //3
    } //1
}
//**************************************
function message(id) {
//**************************************
//alert ("Start message funktie id: "+ id );
var mess_id = id, htmltxt;
if (mess_id == 0) messWrite(mess0,lines[0]);
if (mess_id == 1) messWrite(mess1,lines[1]);
}
//**************************************
function messWrite(t,lns) {
//**************************************
//alert ("Start funktie messWrite: " + t + "," + lns);
var banner;
banner = '<div id="logo"><img  src="./picture_library/jade1_banner2.jpg" /></div>';
//<img class="logo" src="./picture_library/jade1_banner2.jpg" >';
document.write('<html> <head> <title>' +schrmtittel+ '</title> ');
document.write('<style> @import url(./zeil_style2.css); </style>  </head>');
document.write('<body class="doc" onload="javascript:self.focus()">');
document.write(banner);
document.write ('<div id="domMenu_Jade1"></div> <script language="javascript"> domMenu_activate(');
document.write ("'domMenu_Jade1'); </script>");
document.write('<div class="doc" >');

for (nr = 0; nr < lns; nr++) document.write(t[nr] );
document.write(' </div></body></html>');
}
//**************************************
function messWrite2(t,lns) {
//**************************************
//probeersel om waarschuwing in FF weg te krijgen: werkt niet;
var banner, htxt= "";
for (nr = 0; nr < lns; nr++) htxt +=(t[nr] );
banner = '<div id="logo"><img  src="./picture_library/jade1_banner2.jpg" /></div>';
//<img class="logo" src="./picture_library/jade1_banner2.jpg" >';
htmltxt= '<html> <head> <title>' +schrmtittel+ '</title> ' + '<style> @import url(./zeil_style2.css); </style>  </head>' + '<body class="doc" onload="javascript:self.focus()">' + banner + '<div id="domMenu_Jade1"></div> <script language="javascript"> domMenu_activate(' + "'domMenu_Jade1'); </script>" + '<div class="doc" >' + htxt + ' </div></body></html>';
document.write(htmltxt);
}

//**************************
function openwindow(vname,cmdv){
//**************************
var timid;
var host;
var f_tekst="Fietstocht stopplaatsen Wil";
var r_tekst="Reactie op Positie melder";
var s_tekst="Specificatie tekst";
var t_tekst="Uitleg Fietstocht scherm";
var pm_tekst="Positie Melding";
var rte_tekst="Open nieuwe Reis/Route";
var tst_tekst="Waarden Overdrachtsvariabelen"; 
if (vname == "fietstocht"  ) {
     if (!window.ftocht || (window.ftocht && window.ftocht.closed) )  {
          host = 'fietstocht.php';
          ftocht=window.open(host,vname,'width=1000pt,height=700,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
          void(0);
          }
     if (window.ftocht && window.ftocht.open && !window.ftocht.closed)  {
   //           window.ftocht.document.title= t_tekst; //!win98
                 window.ftocht.focus( );
         }
    }
if (vname == 'reactiea') {
    if (!window.vraag || (window.vraag && window.vraag.closed) ) {
         host = 'reactie1_form.php';
         vraag=window.open(host,vname,'width=1000pt,height=700,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
  //      window.vraag.document.title= r_tekst; //!win98
        void(0);
         }
    if (window.vraag && window.vraag.open && !window.vraag.closed) window.vraag.focus( );
   }

if (vname == "fietstocht_doc"  ) { //1
	vname="help";
	if (!window.help || (window.help && window.help.closed) )  { //2
          host = 'help.php?mbr=gmap';
          help=window.open(host,vname,'width=850pt,height=700,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
           void(0);
          } //2
     if (window.help && window.help.open && !window.help.closed)  { //3
     //           window.help.document.title= t_tekst; //!win98 of IE6 probleem
                 window.help.focus( );

          } //3
      } //1
if (vname == 'reactie') { //1
    if (!window.vraag || (window.vraag && window.vraag.closed) ) { //2
         host = 'reactie_form.php';
         vraag=window.open(host,vname,'width=800pt,height=700,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
        void(0);
         } //2
    if (window.vraag && window.vraag.open && !window.vraag.closed){ //3
    //   window.vraag.document.title= r_tekst; //!win98 of IE6 probleem
        window.vraag.focus( );
             } //3
     } //1
 if (vname == "specificatie"  ) { //1
     if (!window.doc || (window.doc && window.doc.closed) )  { //2
          host = 'jade1_' + jstaal + '.doc';
          doc=window.open(host,vname,'width=800pt,height=700,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
          void(0);
          } //2
     if (window.doc && window.doc.open && !window.doc.closed)  { //3
   //           window.doc.document.title= s_tekst; //!win98
                 window.doc.focus( );
         } //3
    } //1
if (vname == "openen"  ) { //1
     if (!window.melden || (window.melden && window.melden.closed) )  { //2
          host = 'reis_openen.php?cmd='+cmdv;

          melden=window.open(host,vname,'width=750pt,height=600,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
          void(0);
          } //2
     if (window.melden && window.melden.open && !window.melden.closed)  { //3
   //           window.melden.document.title= rte_tekst; //!win98
	          window.melden.focus( );
	         } //3
    } //1
if (vname == "melden"  ) { //1
     if (!window.melden || (window.melden && window.melden.closed) )  { //2
          host = 'pos_melding.php?cmd='+cmdv;
          melden=window.open(host,vname,'width=750pt,height=600,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
          void(0);
          } //2
     if (window.melden && window.melden.open && !window.melden.closed)  { //3
   //           window.melden.document.title= pm_tekst; //!win98
                 window.melden.focus( );
         } //3
    } //1
if (vname == "volgen"  ) { //1
	if (!window.volgen || (window.volgen && window.volgen.closed) )  { //2
          host = 'pos_laatste.php';
		 volgen=window.open(host,vname,'width=1000pt,height=700,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
          void(0);
          } //2
     if (window.volgen && window.volgen.open && !window.volgen.closed)  { //3
   //           window.volgen.document.title= pm_tekst; //!win98
                 window.volgen.focus( );
         } //3
    } //1

if (vname == "test"  ) { //1
  	alert ("cmd: " +cmdv);
   if (!window.test || (window.test && window.test.closed) )  { //2
          host = 'testoutput.php?cmd='+cmdv;
          test=window.open(host,vname,'width=350pt,height=400,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes,location=yes,dependent=1');
          void(0);
          } //2
     if (window.test && window.test.open && !window.test.closed)  { //3
   //           window.test.document.title= pm_tekst; //!win98
                 window.test.focus( );
         } //3
    } //1


}
//**************************
function cl_childs(){
//**************************
if (window.ftocht_d_sch && window.ftocht_d_sch.open && !window.ftocht_d_sch.closed) {
	  window.ftocht_d_sch.opener = null;
        window.ftocht_d_sch.close();

     }
if (window.spec_sch && window.spec_sch.open && !window.spec_sch.closed) {
        window.spec_sch.opener = null;
        window.spec_sch.close();
     }

if (window.reactie_sch && window.reactie_sch.open && !window.reactie_sch.closed) {
       window.reactie_sch.opener = null;
        window.reactie_sch.close();
        }
}
//**********************
//********close reactie fietstocht***********
function reactcl() {

   if (window.ftocht_d_sch && window.ftocht_d_sch.open && !window.ftocht_d_sch.closed) {
                 window.ftocht_d_sch.opener = null;
                 window.ftocht_d_sch.close( );
                        }
      if (window.reactie_sch && window.reactie_sch.open && !window.reactie_sch.closed) {
  //        window.reactie_sch.alert('Controleer na de OK of het  Reactie-scherm van het Fietstocht-scherm afgesloten is. Zo niet sluit dat dan met de hand.');
           window.reactie_sch.opener = null;
           window. reactie_sch.close( );
                        }
}

//********jade1l***********
function jade1cl() {
window.opener.focus();
//window.opener.alert('Controleer of Specificatie-scherm en Reactie-scherm van het startscherm nu afgesloten zijn. Zo niet sluit ze dan met de hand en geef pas daarna ok.');
window.opener.close();
reactcl();
 }
//********sendcl***********
function sendcl() {
 alert(demo['scl_alrt'] );
//alert ('Let op! Dit bericht moet vastlopers voorkomen. Veilige werkwijze: Controleer, als u in de Gmap een punt hebt geopend, voordat u de OK geeft of de Reactie-schermen al verzonden zijn. Hierna kunt u dat niet meer doen. Sluit ook het startscherm: Positiemeldingen en de andere open schermen van de applikatie (Specificatie en Uitleg) met uitzondering van het Fietstocht-scherm , dat  scherm wordt na de OK afgesloten. Tot nu toe waren er alleen af en toe vastlopers in Win98. Als u het geforceerd herstarten voor lief neemt kunt u direct de OK proberen. Het lijkt er namelijk op, dat dit lange bericht het vastlopen al voorkomt.');
if (window.opener && window.opener.open && !window.opener.closed)  jade1cl();
  window.close();
 }
//********checkinput***********
function checkinput ( form ) {
//*****************************
for(i=0; i<form.elements.length; i++) {
	if (form.elements[i].value.search(/<[^>]*script*\"?[^>]*>/i) != -1  
      ||  form.elements[i].value.search(/<[^>]*object*\"?[^>]*>/i) != -1  
      ||  form.elements[i].value.search(/<[^>]*iframe*\"?[^>]*>/i) != -1  
	||  form.elements[i].value.search(/<[^>]*applet*\"?[^>]*>/i) != -1  
      ||  form.elements[i].value.search(/<[^>]*meta*\"?[^>]*>/i) != -1  
	||  form.elements[i].value.search(/<[^>]*style*\"?[^>]*>/i) != -1  
	||  form.elements[i].value.search(/<[^>]*form*\"?[^>]*>/i) != -1  
	||  form.elements[i].value.search(/<[^>]*\?php*\"?[^>]*/i) != -1  ) {
		alert( "\t\t There is program-like-coding in one of the Form-field's. That is not allowed! \r\n Change input: \r\n" +form.elements[i].value );
		return false ;
		}
	}
return true ;
}
//********* breedte scherm***************
	function viewPort() {
//***************************************
var h = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
var w = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
return { width : w , height : h }
	
}
var viewport = viewPort();
//alert ("dit is nu de screen dimensie: " + viewport.width + ' , ' + viewport.height);

//***************************************
/**
 * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * iScroll Lite Edition based on iScroll v4.0 Beta 4
 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2010 Matteo Spinelli, http://cubiq.org/
 * Released under MIT license
 * http://cubiq.org/dropbox/mit-license.txt
 * 
 * Last updated: 2011.03.10
 * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 
 */

;(function(){
function iScroll (el, options) {
  var that = this, doc = document, i;

  that.wrapper = typeof el == 'object' ? el : doc.getElementById(el);
  that.wrapper.style.overflow = 'hidden';
  that.scroller = that.wrapper.children[0];
  that.scroller.style.cssText += '-webkit-transition-property:-webkit-transform;-webkit-transform-origin:0 0;-webkit-transform:' + trnOpen + '0,0' + trnClose;
  that.scroller.style.cssText += '-webkit-transition-timing-function:cubic-bezier(0.33,0.66,0.66,1);-webkit-transition-duration:0;';

  // Default options
  that.options = {
    hScroll: true,
    vScroll: true,
    bounce: has3d,
    bounceLock: false,
    momentum: has3d,
    lockDirection: true,
    hScrollbar: true,
    vScrollbar: true,
    fixedScrollbar: isAndroid,
    fadeScrollbar: (isIDevice && has3d) || !hasTouch,
    hideScrollbar: isIDevice || !hasTouch,
    scrollbarClass: '',
    onScrollStart: null,
    onScrollEnd: null,
	etname: 'Sidebar',
	elname: ''
    };

  // User defined options
  switch(el.substr(0,2)) {
	case 'zi':  that.etname = 'Sidebar'; break;	
	case 'iw':  that.etname = 'Infowindow'; break;	
	case 'EPuntx':  that.etname = 'Infowindow'; break;	
	case 'EStart':  that.etname = 'Infowindow'; break;	
	case 'EVerslag':  that.etname = 'Infowindow'; break;
	case 'SPuntx':  that.etname = 'Infowindow'; break;	
	case 'SStart':  that.etname = 'Infowindow'; break;	
	case 'SVerslag':  that.etname = 'Infowindow'; break;		
	};
   that.elname= el.substring(0,el.length);

  for (i in options) {
    that.options[i] = options[i];
    };

  that.options.hScrollbar = that.options.hScroll && that.options.hScrollbar;
  that.options.vScrollbar = that.options.vScroll && that.options.vScrollbar;
  that.refresh();

  that._bind(RESIZE_EV, window);
  that._bind(START_EV);
/*  that._bind(MOVE_EV);
  that._bind(END_EV);
  that._bind(CANCEL_EV);*/
  }

iScroll.prototype = {
  x: 0, y: 0,
  
  handleEvent: function (e) {
    var that = this;
    
    switch(e.type) {
      case START_EV: that._start(e); break;
      case MOVE_EV: that._move(e); break;
      case END_EV:
      case CANCEL_EV: that._end(e); break;
      case 'webkitTransitionEnd': that._transitionEnd(e); break;
      case RESIZE_EV: that._resize(); break;
      }
    },
  
  
  _scrollbar: function (dir) {
      var that = this,
      doc = document,
      bar;

      if (!that[dir + 'Scrollbar']) {
      	if (that[dir + 'ScrollbarWrapper']) {
        		that[dir + 'ScrollbarIndicator'].style.webkitTransform = '';  // Should free some mem
        		that[dir + 'ScrollbarWrapper'].parentNode.removeChild(that[dir + 'ScrollbarWrapper']);
        		that[dir + 'ScrollbarWrapper'] = null;
        		that[dir + 'ScrollbarIndicator'] = null;
        		}
      	return;
      	}

      if (!that[dir + 'ScrollbarWrapper']) {
      	// Create the scrollbar wrapper
      	bar = doc.createElement('div');
     	 if (that.options.scrollbarClass) {
        		bar.className = that.options.scrollbarClass + dir.toUpperCase();
       	 	}
     	else {
        		bar.style.cssText = 'position:absolute;z-index:100;' + (dir == 'h' ? 'height:7px;bottom:1px;left:2px;right:7px' : 'width:7px;bottom:7px;top:2px;right:1px');
        		}
     	bar.style.cssText += 'pointer-events:none;-webkit-transition-property:opacity;-webkit-transition-duration:' + (that.options.fadeScrollbar ? '350ms' : '0') + ';overflow:hidden;opacity:' + (that.options.hideScrollbar ? '0' : '1');
      	that.wrapper.appendChild(bar);
      	that[dir + 'ScrollbarWrapper'] = bar;

      	// Create the scrollbar indicator
      	bar = doc.createElement('div');
      	if (!that.options.scrollbarClass) {
        		bar.style.cssText = 'position:absolute;z-index:100;background:rgba(0,0,0,0.5);border:1px solid rgba(255,255,255,0.9);-webkit-background-clip:padding-box;-webkit-box-sizing:border-box;' + (dir == 'h' ? 'height:100%;-webkit-border-radius:4px 3px;' : 'width:100%;-webkit-border-radius:3px 4px;');
        		}
     	 bar.style.cssText += 'pointer-events:none;-webkit-transition-property:-webkit-transform;-webkit-transition-timing-function:cubic-bezier(0.33,0.66,0.66,1);-webkit-transition-duration:0;-webkit-transform:' + trnOpen + '0,0' + trnClose;

      	that[dir + 'ScrollbarWrapper'].appendChild(bar);
      	that[dir + 'ScrollbarIndicator'] = bar;
      	}

      if (dir == 'h') {
      	that.hScrollbarSize = that.hScrollbarWrapper.clientWidth;
      	that.hScrollbarIndicatorSize = m.max(m.round(that.hScrollbarSize * that.hScrollbarSize / that.scrollerW), 8);
      	that.hScrollbarIndicator.style.width = that.hScrollbarIndicatorSize + 'px';
      	that.hScrollbarMaxScroll = that.hScrollbarSize - that.hScrollbarIndicatorSize;
      	that.hScrollbarProp = that.hScrollbarMaxScroll / that.maxScrollX;
    	}
     else {
      	that.vScrollbarSize = that.vScrollbarWrapper.clientHeight;
      	that.vScrollbarIndicatorSize = m.max(m.round(that.vScrollbarSize * that.vScrollbarSize / that.scrollerH), 8);
      	that.vScrollbarIndicator.style.height = that.vScrollbarIndicatorSize + 'px';
      	that.vScrollbarMaxScroll = that.vScrollbarSize - that.vScrollbarIndicatorSize;
      	that.vScrollbarProp = that.vScrollbarMaxScroll / that.maxScrollY;
    	}

    // Reset position
    that._indicatorPos(dir, true);
    },
  
  _resize: function () {
    var that = this;

    //if (that.options.momentum) that._unbind('webkitTransitionEnd');

     setTimeout(function () {
      	that.refresh();
    	}, 0);
     },
  
  _pos: function (x, y) {
    var that = this;

    that.x = that.hScroll ? x : 0;
    that.y = that.vScroll ? y : 0;

    that.scroller.style.webkitTransform = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose;

    that._indicatorPos('h');
    that._indicatorPos('v');
  },
  
  _indicatorPos: function (dir, hidden) {
    var that = this,
      pos = dir == 'h' ? that.x : that.y;
    
    if (!that[dir + 'Scrollbar']) return;
    
    pos = that[dir + 'ScrollbarProp'] * pos;
  
    if (pos < 0) {
      	pos = that.options.fixedScrollbar ? 0 : pos + pos*3;
      	if (that[dir + 'ScrollbarIndicatorSize'] + pos < 9) pos = -that[dir + 'ScrollbarIndicatorSize'] + 8;
   	 }
   else if (pos > that[dir + 'ScrollbarMaxScroll']) {
      	pos = that.options.fixedScrollbar ? that[dir + 'ScrollbarMaxScroll'] : pos + (pos - that[dir + 'ScrollbarMaxScroll'])*3;
      	if (that[dir + 'ScrollbarIndicatorSize'] + that[dir + 'ScrollbarMaxScroll'] - pos < 9) pos = that[dir + 'ScrollbarIndicatorSize'] + that[dir + 'ScrollbarMaxScroll'] - 8;
    	}
    that[dir + 'ScrollbarWrapper'].style.webkitTransitionDelay = '0';
    that[dir + 'ScrollbarWrapper'].style.opacity = hidden && that.options.hideScrollbar ? '0' : '1';
    that[dir + 'ScrollbarIndicator'].style.webkitTransform = trnOpen + (dir == 'h' ? pos + 'px,0' : '0,' + pos + 'px') + trnClose;
  },
  
  _transitionTime: function (time) {
    var that = this;
    
    time += 'ms';
    that.scroller.style.webkitTransitionDuration = time;

    if (that.hScrollbar) that.hScrollbarIndicator.style.webkitTransitionDuration = time;
    if (that.vScrollbar) that.vScrollbarIndicator.style.webkitTransitionDuration = time;
  },
  

  _start: function (e) {
    var that = this,
      point = hasTouch ? e.changedTouches[0] : e,
      matrix;

    that.moved = false;

    var nodeType = e.explicitOriginalTarget ? e.explicitOriginalTarget.nodeName.toLowerCase():(e.target ? e.target.nodeName.toLowerCase():'');
     if((nodeType !='select' && nodeType !='option' && nodeType !='input' && nodeType!='textarea') || that.elname=='sscroll' ) e.preventDefault();
	

    that.moved = false;
    that.distX = 0;
    that.distY = 0;
    that.absDistX = 0;
    that.absDistY = 0;
    that.dirX = 0;
    that.dirY = 0;
    that.returnTime = 0;
    
    that._transitionTime(0);
    
    if (that.options.momentum) {
      	matrix = new WebKitCSSMatrix(window.getComputedStyle(that.scroller, null).webkitTransform);
     	 if (matrix.m41 != that.x || matrix.m42 != that.y) {
        		that._unbind('webkitTransitionEnd');
        		that._pos(matrix.m41, matrix.m42);
      		}
    	}

    that.scroller.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.66,0.66,1)';
    if (that.hScrollbar) that.hScrollbarIndicator.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.66,0.66,1)';
    if (that.vScrollbar) that.vScrollbarIndicator.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.66,0.66,1)';
    that.startX = that.x;
    that.startY = that.y;
    that.pointX = point.pageX;
    that.pointY = point.pageY;
    
    that.startTime = e.timeStamp;

    if (that.options.onScrollStart) that.options.onScrollStart.call(that);

    // Registering/unregistering of events is done to preserve resources on Android
//    setTimeout(function () {
//      that._unbind(START_EV);
      that._bind(MOVE_EV);
      that._bind(END_EV);
      that._bind(CANCEL_EV);
//    }, 0);
  },
  
  _move: function (e) {
    if (hasTouch && e.touches.length > 1) return;

    var that = this,
      point = hasTouch ? e.changedTouches[0] : e,
      deltaX = point.pageX - that.pointX,
      deltaY = point.pageY - that.pointY,
      newX = that.x + deltaX,
      newY = that.y + deltaY;

    e.preventDefault();

    that.pointX = point.pageX;
    that.pointY = point.pageY;

    // Slow down if outside of the boundaries
    if (newX > 0 || newX < that.maxScrollX) {
      	newX = that.options.bounce ? that.x + (deltaX / 2.4) : newX >= 0 || that.maxScrollX >= 0 ? 0 : that.maxScrollX;
    	}
    if (newY > 0 || newY < that.maxScrollY) { 
      	newY = that.options.bounce ? that.y + (deltaY / 2.4) : newY >= 0 || that.maxScrollY >= 0 ? 0 : that.maxScrollY;
    	}

    if (that.absDistX < 4 && that.absDistY < 4) {
      	that.distX += deltaX;
      	that.distY += deltaY;
      	that.absDistX = m.abs(that.distX);
      	that.absDistY = m.abs(that.distY);
      	return;
    	}
    
    // Lock direction
    if (that.options.lockDirection) {
      	if (that.absDistX > that.absDistY+3) {
        		newY = that.y;
        		deltaY = 0;
      		}
	else if (that.absDistY > that.absDistX+3) {
        		newX = that.x;
        		deltaX = 0;
      		}
    	}
    
    that.moved = true;
    that._pos(newX, newY);
    that.dirX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
    that.dirY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;

    if (e.timeStamp - that.startTime > 300) {
      	that.startTime = e.timeStamp;
      	that.startX = that.x;
      	that.startY = that.y;
    	}
  },
  
  _end: function (e) {
    if (hasTouch && e.touches.length != 0) return;

    var that = this,
      point = hasTouch ? e.changedTouches[0] : e,
      target, ev,
      momentumX = { dist:0, time:0 },
      momentumY = { dist:0, time:0 },
      duration = e.timeStamp - that.startTime,
      newPosX = that.x, newPosY = that.y,
      newDuration;

//    that._bind(START_EV);
    that._unbind(MOVE_EV);
    that._unbind(END_EV);
    that._unbind(CANCEL_EV);

    if (!that.moved) {
      	if (hasTouch) {
        		that.doubleTapTimer = null;

        		// Find the last touched element
        		target = point.target;
        		while (target.nodeType != 1) {
          			target = target.parentNode;
        			}

        		ev = document.createEvent('MouseEvents');
        		ev.initMouseEvent('click', true, true, e.view, 1, point.screenX, point.screenY, point.clientX, point.clientY,e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,0, null);
        		ev._fake = true;
       		 target.dispatchEvent(ev);
      		}

      	that._resetPos();
     	 return;
    	}

    if (duration < 300 && that.options.momentum) {
     	 momentumX = newPosX ? that._momentum(newPosX - that.startX, duration, -that.x, that.scrollerW - that.wrapperW + that.x, that.options.bounce ? that.wrapperW : 0) : momentumX;
      	momentumY = newPosY ? that._momentum(newPosY - that.startY, duration, -that.y, (that.maxScrollY < 0 ? that.scrollerH - that.wrapperH + that.y : 0), that.options.bounce ? that.wrapperH : 0) : momentumY;

      	newPosX = that.x + momentumX.dist;
      	newPosY = that.y + momentumY.dist;

      	 if ((that.x > 0 && newPosX > 0) || (that.x < that.maxScrollX && newPosX < that.maxScrollX)) momentumX = { dist:0, time:0 };
       	if ((that.y > 0 && newPosY > 0) || (that.y < that.maxScrollY && newPosY < that.maxScrollY)) momentumY = { dist:0, time:0 };
    	}

    if (momentumX.dist || momentumY.dist) {
      	newDuration = m.max(m.max(momentumX.time, momentumY.time), 10);

          /*      if (newPosX > 0 || newPosX < that.maxScrollX || newPosY > 0 || newPosY < that.maxScrollY) {
        		// Subtle change of scroller motion
      	  	that.scroller.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.66,0.5,1)';
       		 if (that.hScrollbar) that.hScrollbarIndicator.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.66,0.5,1)';
        		if (that.vScrollbar) that.vScrollbarIndicator.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.66,0.5,1)';
      		}*/

     	 that.scrollTo(newPosX, newPosY, newDuration);
     	 return;
    	}

    that._resetPos(200);
  },
  
  _resetPos: function (time) {
    var that = this,
      resetX = that.x,
      resetY = that.y;

    if (that.x >= 0) resetX = 0;
    else if (that.x < that.maxScrollX) resetX = that.maxScrollX;

    if (that.y >= 0 || that.maxScrollY > 0) resetY = 0;
    else if (that.y < that.maxScrollY) resetY = that.maxScrollY;

    if (resetX == that.x && resetY == that.y) {
      	if (that.moved) {
        		if (that.options.onScrollEnd) that.options.onScrollEnd.call(that);    // Execute custom code on scroll end
        		that.moved = false;
     		 }

      	if (that.hScrollbar && that.options.hideScrollbar) {
        		that.hScrollbarWrapper.style.webkitTransitionDelay = '300ms';
        		that.hScrollbarWrapper.style.opacity = '0';
      		}
     	 if (that.vScrollbar && that.options.hideScrollbar) {
        		that.vScrollbarWrapper.style.webkitTransitionDelay = '300ms';
        		that.vScrollbarWrapper.style.opacity = '0';
      		}

      	return;
    	}

    // Invert ease
    if (time) {
      	that.scroller.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.0,0.33,1)';
      	if (that.hScrollbar) that.hScrollbarIndicator.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.0,0.33,1)';
      	if (that.vScrollbar) that.vScrollbarIndicator.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.0,0.33,1)';
    	}

    that.scrollTo(resetX, resetY, time || 0);
  },

  _transitionEnd: function (e) {
    var that = this;
    
    if (e) e.stopPropagation();

    that._unbind('webkitTransitionEnd');

    that._resetPos(that.returnTime);
    that.returnTime = 0;
  },


  /**
   *
   * Utilities
   *
   */
  _momentum: function (dist, time, maxDistUpper, maxDistLower, size) {
    var that = this,
      deceleration = 0.0006,
      speed = m.abs(dist) / time,
      newDist = (speed * speed) / (2 * deceleration),
      newTime = 0, outsideDist = 0;

    // Proportinally reduce speed if we are outside of the boundaries 
    if (dist > 0 && newDist > maxDistUpper) {
      	outsideDist = size / (6 / (newDist / speed * deceleration));
      	maxDistUpper = maxDistUpper + outsideDist;
      	that.returnTime = 800 / size * outsideDist + 100;
      	speed = speed * maxDistUpper / newDist;
      	newDist = maxDistUpper;
    	}
   else if (dist < 0 && newDist > maxDistLower) {
      	outsideDist = size / (6 / (newDist / speed * deceleration));
      	maxDistLower = maxDistLower + outsideDist;
      	that.returnTime = 800 / size * outsideDist + 100;
      	speed = speed * maxDistLower / newDist;
      	newDist = maxDistLower;
    	}

    newDist = newDist * (dist < 0 ? -1 : 1);
    newTime = speed / deceleration;

    return { dist: newDist, time: m.round(newTime) };
  },

  _offset: function (el, tree) {
    var left = -el.offsetLeft,
      top = -el.offsetTop;
      
    if (!tree) return { x: left, y: top };

    while (el = el.offsetParent) {
      	left -= el.offsetLeft;
      	top -= el.offsetTop;
    	} 

    return { x: left, y: top };
  },

  _bind: function (type, el) {
    (el || this.scroller).addEventListener(type, this, false);
  },

  _unbind: function (type, el) {
    (el || this.scroller).removeEventListener(type, this, false);
  },


  /**
   *
   * Public methods
   *
   */
  destroy: function () {
    var that = this;

    // Remove the scrollbars
    that.hScrollbar = false;
    that.vScrollbar = false;
    that._scrollbar('h');
    that._scrollbar('v');

    // Free some mem
    that.scroller.style.webkitTransform = '';

    // Remove the event listeners
    that._unbind('webkitTransitionEnd');
    that._unbind(RESIZE_EV);
    that._unbind(START_EV);
    that._unbind(MOVE_EV);
    that._unbind(END_EV);
    that._unbind(CANCEL_EV);
  },

  refresh: function () {
    var that = this;

    that.wrapperW = that.wrapper.clientWidth;
    that.wrapperH = that.wrapper.clientHeight;
    that.scrollerW = that.scroller.offsetWidth;
    that.scrollerH = that.scroller.offsetHeight;
    that.maxScrollX = that.wrapperW - that.scrollerW;
    that.maxScrollY = that.wrapperH - that.scrollerH;
    that.dirX = 0;
    that.dirY = 0;

    that._transitionTime(0);

    that.hScroll = that.options.hScroll && that.maxScrollX < 0;
    that.vScroll = that.options.vScroll && (!that.options.bounceLock && !that.hScroll || that.scrollerH > that.wrapperH);
    that.hScrollbar = that.hScroll && that.options.hScrollbar;
    that.vScrollbar = that.vScroll && that.options.vScrollbar && that.scrollerH > that.wrapperH;

    // Prepare the scrollbars
    that._scrollbar('h');
    that._scrollbar('v');
  
    that._resetPos();
  },

  scrollTo: function (x, y, time, relative) {
    var that = this;

    if (relative) {
      	x = that.x - x;
      	y = that.y - y;
    	}

    time = !time || (m.round(that.x) == m.round(x) && m.round(that.y) == m.round(y)) ? 0 : time;

    that.moved = true;

    if (time) that._bind('webkitTransitionEnd');
    that._transitionTime(time);
    that._pos(x, y);
    if (!time) setTimeout(function () { that._transitionEnd(); }, 0);
  },

  scrollToElement: function (el, time) {
    var that = this, pos;
    el = el.nodeType ? el : that.scroller.querySelector(el);
    if (!el) return;

    pos = that._offset(el);
    pos.x = pos.x > 0 ? 0 : pos.x < that.maxScrollX ? that.maxScrollX : pos.x;
    pos.y = pos.y > 0 ? 0 : pos.y < that.maxScrollY ? that.maxScrollY : pos.y;
    time = time === undefined ? m.max(m.abs(pos.x)*2, m.abs(pos.y)*2) : time;

    that.scrollTo(pos.x, pos.y, time);
  },
  zien: function() {
	var that = this;

    		alert("Check to detect if you run this application on a Tablet or Mobile.\n Tablet/Mobile?: "+ isTablet + "\n Your Browser has userAgent: " + ua +"\n \nYour Operating system: " + navigator.appVersion + "\n"+that.etname+" has vertical Scrollbar:  "+ that.vScrollbar + " \n"+ that.etname+ " has SCROLL function: " + that.vScroll+ "\n for div-id: " + that.elname);
	}
};
 

var has3d = 'WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix(),
  hasTouch = 'ontouchstart' in window,
  isIDevice = (/iphone|ipad/gi).test(navigator.appVersion),
  isAndroid = (/android/gi).test(navigator.appVersion),
  RESIZE_EV = 'onorientationchange' in window ? 'orientationchange' : 'resize',
  START_EV = hasTouch ? 'touchstart' : 'mousedown',
  MOVE_EV = hasTouch ? 'touchmove' : 'mousemove',
  END_EV = hasTouch ? 'touchend' : 'mouseup',
  CANCEL_EV = hasTouch ? 'touchcancel' : 'mouseup',
  trnOpen = 'translate' + (has3d ? '3d(' : '('),
  trnClose = has3d ? ',0)' : ')',
  m = Math;

if (typeof exports !== 'undefined') exports.iScroll = iScroll;
else window.iScroll = iScroll;


})();
//************************

 
