// Copyright (c) 2008 Anna Putrino and Stanislav Chachkov (http://www.hikr.org), All rights reserved 
function  content(item){
		  if(item.textContent){
				return item.textContent;
			}else{
				if(item && item.childNodes && item.childNodes.length){
					var ret=""
					for(var i=0;i<item.childNodes.length;i++){
						if(item.childNodes[i] && item.childNodes[i].nodeValue)ret+=item.childNodes[i].nodeValue
					}
					return ret
				}
			}
		}
		
		function  setOpacity(obj,opc,op1){
			//opc=parseInt(opc);
			//if(typeof obj.style.opacity == "string") obj.style.opacity = opc/100;
			//else if(typeof obj.style.MozOpacity == "string") obj.style.MozOpacity = opc/100;
			//else obj.style.filter = "alpha(opacity="+opc+")";
			obj.style.filter = "alpha(opacity="+opc+")";
			obj.style.opacity = opc/100;
		}
		
		function  getOpacity(obj){
			if(typeof obj.style.opacity == "string") return obj.style.opacity ;
			else if(typeof obj.style.MozOpacity == "string") return obj.style.MozOpacity ;
			else var op=obj.style.filter.substr(14);  return op.replace(")","");
		}
		
	
function loadXMLDoc(url,callback,sync) {if (window.XMLHttpRequest) {var req = new XMLHttpRequest();
if(callback){req.onreadystatechange = function(){callback(req)};}
try{req.open("GET", url, !sync);req.send(null);}catch(error){}}else if (window.ActiveXObject){var req = new ActiveXObject("Microsoft.XMLHTTP");
if (req){if(callback){req.onreadystatechange = function(){callback(req)};}req.open("GET", url, !sync);req.send();}}}	
		
function loadXMLDocPost(url,data,callback) {
if (window.XMLHttpRequest){var req = new XMLHttpRequest();
if(callback){req.onreadystatechange = function(){callback(req)};}req.open("POST", url, true);req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	req.send(data);
} else if (window.ActiveXObject) {var req = new ActiveXObject("Microsoft.XMLHTTP");if (req) {if(callback){req.onreadystatechange = function(){callback(req)};}req.open("POST", url, true);req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');req.send(data);}}}	
	
function xml2array(item){var r = new Array();for(var i=0;i<item.childNodes.length;i++){r[item.childNodes[i].tagName]=content(item.childNodes[i]);}return r;}

function popup1(path,win_name,w,h){popup0(path,w,h)}

function popup0(path,w,h,fullscreen,win_name)
{if(!win_name)win_name="yyy"
var left=(screen.availWidth-w)/2;var top=(screen.availHeight-h)/2;
var p='left='+left+',top='+top+',width='+w+',height='+h+',resizable=yes,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no';
if(fullscreen)p+=",fullscreen=yes";
var wi=open(path,win_name,p);
if((navigator.userAgent.indexOf('MSIE'))==-1)wi.focus();
return wi}


function clearTable(t){if(t.rows && t.rows.l>0){var l=t.rows.l;for(var i=l;i-->0;){t.deleteRow(i);}}}		
function r4showHTML(id,t){var div=document.getElementById(id);if(div)div.innerHTML=t}
function r4setInputValue(id,t){var input=document.getElementById(id);if(input)input.value=t}
			
		function r4switch(id){
			var div=document.getElementById(id)
			if(div){
				if(div.style.display=='none'){
					div.style.display='block'
				}else{
					div.style.display='none'
				}

				var tog_open=document.getElementById(id+"_open")
				var tog_close=document.getElementById(id+"_close")
				if(tog_open && tog_close){
					var swap=tog_open.style.display
					tog_open.style.display=tog_close.style.display
					tog_close.style.display=swap
				}
			}			
		}
		
function r4displayStyle(id,s){var d=document.getElementById(id);if(d)div.style.display=s}
function r4displayNone(id){var d=document.getElementById(id);if(d)d.style.display='none'}
function r4displayBlock(id){var d=document.getElementById(id);if(d)d.style.display='block'}
function r4displayInline(id){var d=document.getElementById(id);if(d)d.style.display='inline'}
						
		function r4selectOption(option_id){
			var option=document.getElementById(option_id)
			if(option)option.selected=true
		}	
	
		function r4setBorder(name,border_color,border_width,border_style){ 
			var div=document.getElementById(name)
			if(div){ 
				div.style.borderColor=border_color	
				div.style.borderWidth=border_width
				div.style.borderStyle=border_style
			}
		}
		function r4setBackgroundColor(name,color){
			var div=document.getElementById(name)
			if(div)div.style.backgroundColor=color
		}			
		
		
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
        
		var um_open=false;
		function r4hideUserMenu(){
				//r4displayNone("pnav")
		   r4displayNone("u_menu")
		   um_open=false;
		}
		
         
    function r4showUserButton(root,id,user_login,user_name,type){
		   if(um_open) return
		   var anchor=document.getElementById("anchor_"+type+"_"+id)
		   if(anchor){
			  anchor.src=root+"r4icons/um_arrow_over.gif"
			  anchor.onmouseout=function(){
			    if(um_open) return
					anchor.src=root+"r4icons/um_arrow_down.gif"
			  }
			  anchor.onclick=function(){
		 		 var xx=findPosX(anchor)
		 		 var yy=findPosY(anchor)+11
		 		 var u_menu=document.getElementById("u_menu")
		 		 var um_home=document.getElementById("um_home")
		 		 var um_journal=document.getElementById("um_journal")
		 		 var um_piz=document.getElementById("um_piz")
		 		 var um_photo=document.getElementById("um_photo")   
		 		 var um_favs=document.getElementById("um_favs")
		 		 var um_friends=document.getElementById("um_friends")
         		 //var um_addfriend=document.getElementById("um_addfriend")       
				  var um_sendpm=document.getElementById("um_sendpm")
		 		 um_home.href=root+"user/"+user_login+"/" 
		 		 um_journal.href=root+"user/"+user_login+"/tour/"
		 		 um_piz.href=root+"user/"+user_login+"/piz/"
		 		 um_photo.href=root+"user/"+user_login+"/gallery/"
		 		 um_favs.href=root+"user/"+user_login+"/gallery/favs/"
		 		 um_friends.href=root+"user/"+user_login+"/friends/"
		 		 //um_addfriend.href=root+"add_friend.php?user_login="+user_login
				 um_sendpm.href=root+"pm.php?act=new&to="+user_login
		 		 if(u_menu){			   
					   u_menu.style.position="absolute"
					   u_menu.style.left=xx+"px"
					   u_menu.style.top=yy+"px"
					   u_menu.style.display='block'
         		 }
				 var f=function (e){ 
				   if(!e) e=window.event
				   var targ;
				   if (e.target) targ = e.target;
	               else if (e.srcElement) targ = e.srcElement;
	               if (targ.nodeType == 3) // defeat Safari bug
		           targ = targ.parentNode;
				   if(targ.parentNode.id=="u_menu") return
			       r4hideUserMenu()
				   anchor.src=root+"r4icons/um_arrow_down.gif"
			       anchor.onmouseover=function(){
				     anchor.src=root+"r4icons/um_arrow_over.gif"
			       }
	               document.body.onmousedown=null
			     }
			     document.body.onmousedown=f
			     um_open=true;

				  
			  }
			  
		   }
		   
		}
		
		function r4showUserMenuWindow(root,id,user_login,user_name,type){
		   
		   var cnav=document.getElementById("cnav")
		   cnav.onmouseout=null
           cnav.style.backgroundColor="#aa0000"
		   var pnav=document.getElementById("pnav")
		   pnav.onmouseout=null

		   var anchor=document.getElementById("anchor_"+type+"_"+id)
		   
		   var xx=findPosX(anchor)
		   var yy=findPosY(anchor)+15
		   var u_menu=document.getElementById("u_menu")
		   var um_home=document.getElementById("um_home")
		   var um_journal=document.getElementById("um_journal")
		   var um_piz=document.getElementById("um_piz")
		   var um_photo=document.getElementById("um_photo")   
		   var um_favs=document.getElementById("um_favs")
		   var um_friends=document.getElementById("um_friends")
           //var um_addfriend=document.getElementById("um_addfriend")
		   var um_sendpm=document.getElementById("um_sendpm")
		   um_home.href=root+"user/"+user_login+"/" 
		   um_journal.href=root+"user/"+user_login+"/tour/"
		   um_piz.href=root+"user/"+user_login+"/piz/"
		   um_photo.href=root+"user/"+user_login+"/gallery/"
		   um_favs.href=root+"user/"+user_login+"/gallery/favs/"
		   um_friends.href=root+"user/"+user_login+"/friends/"
		  // um_addfriend.href=root+"add_friend.php?user_login="+user_login
		   um_sendpm.href=root+"pm.php?act=new&to="+user_login

		   if(u_menu){			   
			   u_menu.style.position="absolute"
			   u_menu.style.left=xx+"px"
			   u_menu.style.top=yy+"px"
			   u_menu.style.display='block'
           }
			   var f=function (){ 
			   r4hideUserMenu()
			   document.body.onmousedown=null
			   }
			   document.body.onmousedown=f
			   um_open=true;
		   
		}
		
		function r4checkAll(checkall_id,checkbox_name){
			var checkall=document.getElementById(checkall_id)	
	        var checkme=document.getElementsByName(checkbox_name)	
            if ( checkall.checked == true ) {
                for (var i=0; i < checkme.length; i++ ) {
                      checkme[i].checked = true;
                }
            }else {
              for ( var i=0; i < checkme.length; i++ ) {
                    checkme[i].checked = false;
               }
            }		
		}
function r4scrollSize() {
var myLeft = 0, myTop = 0;
if( typeof( window.pageXOffset ) == 'number' ) {
myLeft = window.pageXOffset;
myTop = window.pageYOffset;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
myLeft = document.documentElement.scrollLeft;
myTop = document.documentElement.scrollTop;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
myLeft = document.body.scrollLeft;
myTop = document.body.scrollTop;
}
return {left:myLeft,top:myTop};
}

function r4windowSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return {width:myWidth,height:myHeight};
}

	
function r4setCookie(name,value,days){
var today = new Date()
var milli=Date.parse(today)
today.setTime(milli+days*24*60*60*1000)
var UTCstring = today.toUTCString()
cookiestring=name+"="+escape(value)+";EXPIRES="+UTCstring
document.cookie=cookiestring}
function r4showOVForm(gare){r4js(["cff"],function(){r4showOV(gare);});}
function r4getInDiv(id,url){loadXMLDoc(url, function(request){if (request.readyState == 4)r4showHTML(id,request.responseText)})}
var r4infowindow_under=''
function r4getInInfoWindow(src,url,opt,pdata){
r4displayBlock('map_info_loading')
var f=function(req) {
if (req.readyState == 4) {
var iw=req.responseXML.getElementsByTagName("infowindow")[0]
var tabs=req.responseXML.getElementsByTagName("tab")
var w=parseInt(iw.getAttribute('width'))
var h=parseInt(iw.getAttribute('height'))
var title=iw.getAttribute('title')
var text=""
if(tabs && tabs.length>0){				
var tabs_ul=""
for(var i=0;i<tabs.length;i++){
var tab_title=tabs[i].getAttribute("title")
tabs_ul+="<li "+(i==0?"class='active' ":"")+"><a href='#tab_"+i+"' onclick='r4showTab(this);return false'>"+tab_title+"</a></li>"
text+="<div id='tab_"+i+"' "+(i>0?" style='display:none' ":"")+">"+content(tabs[i])+"</div>"
}
r4showHTML("map_info_tabs","<ul class='tabnav'>"+tabs_ul+"</ul>")
r4displayBlock("map_info_tabs")
}else{			
r4displayNone("map_info_tabs")
text=content(iw)
}
r4openInfoWindow(src,w,h,title,text)
if(opt && opt.onopen)opt.onopen()
}
}
if(pdata){
loadXMLDocPost(url, pdata, f)
}else{	
loadXMLDoc(url, f)	
}
}

function r4openInfoWindow(src,w,h,title,text,under) { 
r4displayNone('map_info_loading')
var info_div=document.getElementById('map_info_div')
var info_cont=document.getElementById('map_info')		
var info_title=document.getElementById('map_info_title')		
if(info_div.style.display=='block')info_div.style.display='none'
if(title){info_title.innerHTML=title}else{info_title.innerHTML="Info"}		
var page=null
if(under){var page=document.getElementById(under)
}else{
page=document.getElementById('contentmain_l');under='contentmain_l';
if(!page){page=document.getElementById('contentmain_swiss');under='contentmain_swiss';}
if(!page)return
}
var srcel=null
info_div.style.position='absolute';
if(src){
	srcel=document.getElementById(src)
	if(srcel.style.position == 'fixed') info_div.style.position='fixed'
}
if(info_div.style.position!='fixed'){
	if(srcel){
	info_div.style.top=findPosY(srcel)+'px'
	}else{
	var top=page.offsetTop+50
	info_div.style.top=top+'px'
	}
	if(w==0)w=450
	info_div.style.width=(10+w)+'px'
	var left=((page.offsetWidth-w)/2)+page.offsetLeft
	info_div.style.left=left+'px'
	setOpacity(page,'30','0.30')
	info_div.style.zIndex=200;
}else{
	info_div.style.top='50px'
	if(w==0)w=450
	info_div.style.width=(10+w)+'px'
	var wsize=r4windowSize() 
	var left=((wsize.width-w)/2)
	info_div.style.left=left+'px'
	info_div.style.zIndex=10000;
}
	
	
	info_cont.style.width=w+'px'
	if(h>0)info_cont.style.height=h+'px'
	info_cont.innerHTML=text		
	r4infowindow_under=under
	info_div.style.display='block'
}

function r4closeInfoWindow() {
	if(r4infowindow_under){
		var info_div=document.getElementById('map_info_div')
		info_div.style.display='none'
		var page=document.getElementById(r4infowindow_under)
		if(page)setOpacity(page,'100','1')
		r4infowindowunder=''
	}
	r4displayNone("map_info_tabs")
}

function r4showTab(a){
	a.blur()

	var ul=a.parentNode.parentNode
	for(var i=0; i<ul.childNodes.length;i++){
		var li=ul.childNodes[i]
		if(li.nodeName=='LI'){
			if(li.className=='active'){
				li.className=null
				document.getElementById(li.childNodes[0].hash.substring(1)).style.display='none'
			}
			if(li==a.parentNode){
				li.className='active'
				document.getElementById(a.hash.substring(1)).style.display='block'
			}
		}
	}
	
}


function r4iCSS(src){
	var s = document.createElement('style')
	s.type="text/css"
	s.appendChild( document.createTextNode("@import url("+src+");"))
  var head = document.getElementsByTagName('head')[0]; 
	head.appendChild(s)
}
function r4iJS(src){
var s = document.createElement('script')
s.type = 'text/javascript'
s.src = r4_sroot+"js/"+src
var head = document.getElementsByTagName('head')[0]; 
head.appendChild(s)
}
function r4loadJS(scr, o,i) {
	if(!i)i=0;
	if(scr[i].t && ! scr[i].t()){	
		r4iJS(scr[i].src)
	}else if(scr[i].f && !self[scr[i].f]){	
		r4iJS(scr[i].src)
	}r4waitLoadJS(scr,o,i);	
}
function r4waitLoadJS(scr,o,i){
if ((scr[i].t && ! scr[i].t()) || (scr[i].f && !self[scr[i].f])) {
	setTimeout(function(){r4waitLoadJS(scr,o,i)},100);
}else if((i+1)<scr.length){r4loadJS(scr, o, i+1);
}else{if(o)o();}}

function r4loadGMap(k,o){
	window.r4loadGMapOK=function(){	window.onunload=GUnload;if(o)o()}
	var s = document.createElement("script");
	s.type = "text/javascript";
	s.src = "http://maps.google.com/maps?hl="+r4_lang+"&file=api&v=2&key="+k+"&async=2&callback=r4loadGMapOK";
	document.body.appendChild(s);
}
function r4js(js,o){var scr=[];for(var i=0;i<js.length;i++)scr.push({src:js[i]+(r4libs[js[i]].v?".js?v="+r4libs[js[i]].v:".js"),f:r4libs[js[i]].f,t:r4libs[js[i]].t});r4loadJS(scr,o)}
function r4navt(e){if(window.r4navdf)return;if(!document.getElementById)return;if(window.event)e=window.event;if(e.ctrlKey){var l=null;switch(e.keyCode?e.keyCode:e.which?e.which:null){case 0x27:l=document.getElementById('NextLink');break;case 0x25:l=document.getElementById('PrevLink');break;case 0x26:l=document.getElementById('UpLink');break;}if(l&&l.href)document.location=l.href;}}
function r4navd(f){window.r4navdf=f}
var r4libs={
	"bounds":{v:5,f:"GridBounds"},//-2
	"tile":{v:7,f:"TileWorld"},//-12
	"quad2":{v:7,f:"QuadWorld"},//-1
	"hikrmap":{v:12,f:"HikrMap"},//-62
	"new_minimap":{v:33,f:"r4miniMap"},//33
	"new_minimap_edit":{v:11,f:"Fed"},//-28
	"new_user_map":{v:6,f:"UserMap"},//-4
	"geotag_box":{v:7,f:"r4openGTB"},//-11
	"notif_box":{v:0,f:"r4displayNotif"},
	"geotag_rando":{v:0,f:"r4openGTB"},
	"view_photo":{v:13,f:"r4zoomPhoto"},//13
	"prototype":{v:3,f:"Ajax"},
	"effects":{v:2,f:"Effect"},
	"controls":{v:3,f:"Autocompleter"},
	"dragdrop":{v:2,f:"Draggable"},
	"cff":{v:0,f:"r4showOV"},
	"comments":{v:1,f:"r4showCommentForm"},//1
	"comm_box":{v:2,f:"r4toComm"},//2
	"fav":{v:1,f:"add_fav"},
	"list_box":{v:0,f:"load_lists"},
	"sitemeter":{v:1,f:"SiteMeter"},
	"ufo":{v:2,f:"UFO"},//-37
	"gex":{v:0,f:"GEarthExtensions"},
	"cal":{v:0,f:"r4loadCal"}
}
var r4_piz_types=['peak','pass','hut','climb','eisfa','viafe','wand','lake','ort','ruin','bridg','cave','point']

function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

function r4s(){
	if(window.r4_aci)return
		r4js(["prototype","effects","controls"],function(){
				new Ajax.Autocompleter("piz_name", "s_su", r4_root+"search.php", {parameters:"act=complete",minChars:2,frequency:0.6,updateElement:function(li){
						if(li.id=='suggestions'){
							$('hikr_search').submit()
						}else{
							var id=li.id.split(":")
							  if(id[0]=='user'){
								 document.location.href=r4_root+"user/"+id[1]+"/"
							  }else{
								  document.location.href=r4_root+"dir/_"+id[1]+"/" 
							  }
						}
				}
				});
				window.r4_aci=true
		})
	
	
}

function r4addOnload(f){
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload=f;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      f();
    }
  }
}

function r4showMenu(menu,ul_id,li_id){
  var ul=document.getElementById(ul_id)
  var li=document.getElementById(li_id)

  loadXMLDoc(r4_root+'menu.php?menu='+menu+'&next='+encodeURIComponent(document.location.href), function(req){
		  if(req.readyState==4){
			  ul.innerHTML=req.responseText
			  li.onmouseover=null;
		  }
  })
}

