var requests = 0;
if(!page) var page = 'nopage';
if(!LocaleJS) var LocaleJS = {};
LocaleJS.notRegistered = 'Entschuldigung, f\u00FCr diese Funktion musst Du angemeldet sein.';
LocaleJS.confirmEndingFriendship = 'Willst du wirklich eure Freundschaft aufheben?';
LocaleJS.confirmDelete = 'Willst du dies wirklich l\u00F6schen?';

// Standard functions
function getEvent(e){return (e ? e : window.event);}
function getEventElement(e){var v=(e ? e : window.event);return(v.srcElement ? v.srcElement :(v.target ? v.target : v.currentTarget)); }
function installDOMEvent(o,h,f){var g;if(g=o[h]){o[h]=function(e){g(e);f(e);}}else{o[h]=f;}}
// setCookie( name , value , exp_days , path )
function setCookie(){var a=arguments;var x='';if(a[2]){var d=new Date();d.setTime(d.getTime()+(a[2]*24*60*60*1000));x+="; expires="+d.toGMTString();}var p=a[3]?'; path='+a[3]:'; path=/';document.cookie=a[0]+"="+a[1]+x+p;}
function getCookie(n){ var c=document.cookie;if(c.length>0){var s=c.indexOf(n+'=');if(s!=-1){s+=n.length+1;var e=c.indexOf(';',s);if(e==-1)e=c.length;return c.substring(s,e);}}return null;}

function mainInit(){
	ActivityButtons.install('content_title');
	ActivityButtons.install('user_title');
	ActivityButtons.install('where');
	CheckMail.init();
}
installDOMEvent( window, 'onload', mainInit );
var MainButtons = {
   names :  ['home','clubbing','konzerte','scout','performers','locations'],
   codes :  ['u','s','sr'],
   load : function(){
      var names = this.names; var codes = this.codes;
      if(Loggedin) names.push( 'meinprofil','leute','freunde','post','einstellungen','abmelden' );
      for(var i=0;i<names.length;i++) for(var j=0;j<codes.length;j++) Preloader.installImage( names[i], codes[j] );
      if(Loggedin){
         var names = ['p','f']; var codes = ['uu','uur','su','sur','us','usr','ss','ssr'];
         for(var i=0;i<names.length;i++) for(var j=0;j<codes.length;j++) Preloader.installImage( 'postfreunde', names[i], codes[j] );
      }
   },
   install : function(){
      for(var i=0;i<this.names.length;i++) if(obj = document.getElementById('navi_'+this.names[i])){
         obj.onmouseover = mouseoverImage;
         obj.onmouseout = mouseoutImage;
         obj.onclick = mouseclickImage;
      }
   }
};
var ActivityButtons = {
   load : function(){
      if(!Loggedin) return;
      var names = [ 'inprofil','inkalender','inkalender1','favorit','online','freund','save' ];
      var codes = [ 'u','ur','s','sr' ];
      for(var i=0;i<names.length;i++) for(var j=0;j<codes.length;j++) Preloader.installImage( names[i], codes[j] );
      Preloader.installImage( 'freund', 'p' );
      Preloader.installImage( 'freund', 'pr' );
   },
   install : function(id){
      var obj;
      if(obj = document.getElementById(id)){
         var ids = obj.getElementsByTagName('img');
         for(var i=0;i<ids.length;i++) if(ids[i].id) {
            if(Loggedin){ ids[i].onmouseover = mouseoverButton; ids[i].onmouseout = mouseoutButton; }
            ids[i].onclick = mouseclickButton;
         }
      }
   }
};
var Preloader = {
   installImage : function(){
      var obj = this; var args = [];
      for(var i=0;i<arguments.length;i++){
         var str = arguments[i]; args.push( str );
         if(!obj[ str ] && i<arguments.length-1) obj[ str ] = {};
         if(i<arguments.length-1) obj = obj[str];
      }
      obj[str] = new Image();
      obj[str].onload = this.onload;
      obj[str].onerror = this.onerror;
      obj[str].src = pathfix+'graphics/button_'+args.join('_')+'.gif';
   },
   onload : function(e){ this.loaded = true; },
   onerror : function(e){ this.loaded = false; }
};

MainButtons.load();
ActivityButtons.load();

function navbarOnload(){
   var pwd, cov;
   if(!( pwd = document.getElementById('navpassword') ) ) return;
   pwd.onfocus = navbarInput;
   pwd.style.display = 'none';
   cov = document.getElementById('pwdcover');
   cov.style.display = 'inline';
   cov.onfocus = navbarUncover;
   cov.pwd = pwd;
   document.getElementById('navusername').onfocus = navbarInput;
}
function navbarUncover(){
   this.style.display = 'none';
   this.pwd.style.display = 'inline';
   this.pwd.focus();
}
function navbarInput(){
   if(arguments[0] && arguments[0].tagName=='INPUT'){ var obj = arguments[0]; }else{ var obj = this; }
   obj.style.color = 'black';
   obj.style.textAlign = 'left';
   obj.value = '';
   obj.onfocus = null;
   return true;
}
function emptyQueue(){ for(var i=0;i<onloadQueue.length;i++) onloadQueue[i](); }
function mouseoverImage(){
   var name = this.id.substr(5);
   var tag = ( name==page ? 'sr' : 's' );
   var obj = Preloader[ name ];
   if(obj[ tag ].loaded) this.firstChild.firstChild.src = obj[ tag ].src;
}
function mouseoutImage(){
   var name = this.id.substr(5);
   var tag = ( name==page ? 's': 'u' );
   var obj = Preloader[ name ];
   if(obj[ tag ].loaded) this.firstChild.firstChild.src = obj[ tag ].src;
}
function mouseclickImage(){
   var name = this.id.substr(5);
   if(Preloader[ name ]['sr'].loaded) this.firstChild.firstChild.src = Preloader[ name ]['sr'].src;
   this.onmouseout = null;
}
function mouseoverButton(){
   var obj = Preloader[ this.src.split('_')[1] ];
   if(obj && obj.s && this.src == obj.s.src && obj.sr.loaded) this.src = obj.sr.src; 
   else if(obj && obj.u && this.src == obj.u.src && obj.ur.loaded ) this.src = obj.ur.src;
}
function mouseoutButton(){
   var obj = Preloader[ this.src.split('_')[1] ];
   if(obj && obj.sr && this.src == obj.sr.src && obj.s.loaded) this.src = obj.s.src; 
   else if(obj && obj.ur && this.src == obj.ur.src && obj.u.loaded ) this.src = obj.u.src;
}
function mouseclickButton(){
   if(!Loggedin){ alert(LocaleJS.notRegistered); return false; };
   var bits = this.src.split('_');
   if(bits[1]=='freund'){ if(bits[2].substr(0,1)=='s' && !confirm(LocaleJS.confirmEndingFriendship)) return false; }
   else if(bits[1]=='delete' && !confirm(LocaleJS.confirmDelete)) return false;
   makeRequest(pathfix+'requests/save_item.php?code='+this.id+'&st='+bits[2].substr(0,1), handleSaveResponse, null, true );
   return false;
}
function handleSaveResponse( http ){
//   alert(http.responseText);
   if(!http || !http.responseText) return;
   var bits = http.responseText.split(':'); 
   if(!(img = document.getElementById(bits[0]))) return;
   var cits = bits[0].split('_');
   var obj = Preloader[ cits[0] ];
   if(bits[1]=='on') img.src = obj.s.src;
   else if(bits[1]=='off') img.src = obj.u.src;
   else if(bits[1]=='pending'){ img.src = obj.p.src; img.onclick = null; img.onmouseover = null; img.onmouseout = null; }
   if(window.furtherHandles){ furtherHandles( bits ); }
}
var CheckMail = {
   count : 0,
   checkInterval : 200,
   checksPerRequest : 25,
   version : 0,
   timeout : null,
   updated : false,
   init : function(e){
      if(!Loggedin) return;
      if(!(this.pf = document.getElementById('mail_switch'))) return;
      this.pf.post = this.pf.src.match(/_p_/);
      this.pf.rot = this.pf.src.split('_')[3].substr(0,2);
      this.pf.onmouseover = this.onMouseover;
      this.pf.onmouseout = this.onMouseout;
      this.pf.onclick = this.onClick;
      this.version = mailVersion;
      setCookie('pf', (CheckMail.pf.post ? 'p' : 'f') );
      this.setTimeout();
   },
   setTimeout : function(){ this.timeout = setTimeout( 'CheckMail.check()',this.checkInterval); },
   check : function(){
      this.count++;
      var cv = getCookie('mv');
      if( ( cv && this.version < cv ) || this.count==this.checksPerRequest ){
         this.count = 0;
         setCookie('pf', (CheckMail.pf.post ? 'p' : 'f') );
         makeRequest(pathfix+'check_mail.php?v='+this.version, CheckMail.handleMail);
      }
      else this.handlePostFreunde( getCookie('npf') );
      this.setTimeout();
   },
   handleMail : function(http){
      var len, ver;
      CheckMail.handlePostFreunde( getCookie('npf') );
      if(!http) return false;
      if(-1==( len = http.responseText.indexOf(';') )) return false;
      if(CheckMail.version==( ver = http.responseText.substr(0,len) )) return false;
      CheckMail.version = ver;
      document.getElementById('postfreunde').innerHTML = http.responseText.substr( len+1 );
   },
   handlePostFreunde : function(status){
      if(!status || ( this.pf.rot && status==this.pf.rot ) ) return false;
      this.pf.rot = status;
      this.pf.src = Preloader.postfreunde[ this.pf.post ? 'p' : 'f' ][ this.pf.rot ].src;
   },
   onMouseover : function(){ this.src =  Preloader.postfreunde[ this.post ? 'p' : 'f' ][ this.rot+'r' ].src; },
   onMouseout : function(){ this.src =  Preloader.postfreunde[ this.post ? 'p' : 'f' ][ this.rot ].src; },
   onClick : function(){
      if(this.rot!='uu'){ this.rot = 'uu'; setCookie('npf','uu'); };
      this.post = !this.post;
      var arr = ( this.post ? ['p','block','none'] : ['f','none','block'] );
      setCookie('pf',arr[0]);
      document.getElementById('mailbox').style.display = arr[1];
      document.getElementById('friendbox').style.display = arr[2];      
      this.onmouseover();
   }
}
function handleMailClick( obj ){
   var win = openMessageWindow( obj.href );
   if(!win){
      setCookie('npf','uu');
      if(typeof(CheckMail)=='object' && CheckMail.pf && CheckMail.pf.rot!='uu'){ CheckMail.pf.rot = 'uu'; CheckMail.pf.onmouseout(); };
      obj.className = 'read';
   }
   return win;
}
function openMessageWindow( url ){
   if(!Loggedin){ alert(LocaleJS.notRegistered); return false; };
   var bits = url.split('=');
   var win = window.open(url, (( bits[1] ? bits[1] : 'messages' )) ,'height=440,width=325,scrollbars=1,resizable=yes');
   if(win && win.focus) win.focus();
   return !win;
}
// Using XMLHttpRequest. adapted from http://developer.mozilla.org/en/docs/AJAX:Getting_Started
function testHttp(http, flag){ try{ if(http.readyState == 4) if(http.status == 200){ if(flag) updateThrobber(false); return true; }; }catch( e ){ if(flag) updateThrobber(false); } return false; }
// our makeRequest takes upto 4 args: url, callback fn, object passed to callback, update throbber
function makeRequest(url, callback) {
   var http;
   if (window.XMLHttpRequest) http = new XMLHttpRequest();
   else if (window.ActiveXObject) try{ http = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ http = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}; };
   if (!http){ alert('Sorry but your browser does not support this feature.'); return false; }
   var ex = arguments[2]; var fl = arguments[3];
   if (fl) updateThrobber(true);
   http.onreadystatechange = function(){ if( window.testHttp && testHttp(http, fl) && callback ) callback(http, ex); };
   http.open('GET', url, true);
   http.send(null);
   return true;
}
function makePostRequest( url, post, callback ){
   var http;
   // prepare the MIME POST data
	var bndstr = 's2M3r119p7P7eRs3M4r210p8P8eRs2M3r119p7P7eR';
	var bnd = '--' + bndstr;
	var body = bnd + '\n';
	for(var x in post) body += 'Content-Disposition: form-data; name="'+x+'"'+'\n'+'\n'+post[x]+'\n'+bnd+'\n';
   if (window.XMLHttpRequest) http = new XMLHttpRequest();
   else if (window.ActiveXObject) try{ http = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ http = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}; };
   if (!http){ alert('Sorry but your browser does not support this feature.'); return false; }
   var ex = arguments[3]; var fl = arguments[4];
   if (fl) updateThrobber(true);
   http.onreadystatechange = function(){ if( window.testHttp && testHttp(http, fl) && callback ) callback(http, ex); };
   http.open('POST', url, true);
   http.setRequestHeader('Content-type', 'multipart/form-data; boundary=' + bndstr );
   http.setRequestHeader('Connection', 'close');
   http.setRequestHeader('Content-length', body.length);
   http.send(body);
   return true;
} 

function updateThrobber( bool ){
   if (bool) requests++; else requests--;
   if (requests==1) document.body.style.cursor = 'progress';
   else if (requests==0) document.body.style.cursor = null;
}
