// on page load call TB_init
window.addEvent('domready', SP_init);

var SP_userid=0;
var myTimer = null;
var SP_lastevent;

function SP_init(){
	$$("a.nick").each(function(el){
		if (SmallProfilTime>0) el.onmouseover=SP_bind; 
		el.onclick=SP_click
	});
}

function SP_bind(event) {
	var event = new Event(event);
	event.preventDefault();
	this.blur();
	if (this.id.substr(0,1)=='u')
		SP_userid = parseInt(this.id.substr(1));
	else
		SP_userid = parseInt(this.id);
	SP_lastevent=event;
	// clear old timer
	if (myTimer!=null) myTimer = $clear(myTimer);
	myTimer = showQP.delay(SmallProfilTime*1000,this,event);

	this.onmouseout=function () {
		myTimer = $clear(myTimer); //Cancels quickpage
	}
	
	this.onmousemove=function (event) {
		var event = new Event(event);
		event.preventDefault();
		this.blur();
		SP_lastevent=event;
	}	


	this.onmouseover=SP_bind;
	this.onclick=SP_click;
	return false;
}

function SP_click(event) {
	var event = new Event(event);
	event.preventDefault();
	this.blur();
	if (this.id.substr(0,1)=='u')
		npage(parseInt(this.id.substr(1)));
	else
		npage(parseInt(this.id));
	this.onmouseover=SP_bind;
	this.onclick=SP_click;
	return false;
}

function showQP(event) {
	var event = SP_lastevent;
	var height=280;	var width=505;
	SP_close();
	
	var screenHeight=window.innerHeight?window.innerHeight:screen.height;
	var screenWidth=window.innerWidth?window.innerWidth:document.body.clientWidth;
	var posX=event.client.x>screenWidth/2?event.page.x-width+10:event.page.x-10;
	var posY=event.client.y>screenHeight/2?event.page.y-height+10:event.page.y-10;

	var layer = new Element('div', {'styles':{'visibility':'visible','width': width+'px','height': height+'px','z-index':'50','position': 'absolute','top': posY + 'px','left':posX+'px'}})
	layer.setAttribute('id','SmallProfil');			
	layer.injectInside(document.body);

	if(!profilePopup){ var profilePopup=0; }

	var jsLoader = new Element('script');
	jsLoader.setAttribute('id','jsLoader');
	jsLoader.src='/ajax/v3_smallprofil.php?id='+SP_userid;
	jsLoader.injectInside(document.body);
}

function SP_close() {
	if ($('bglayer')) $('bglayer').dispose(); 
	if ($('SmallProfil')) $('SmallProfil').dispose();
	if ($("jsLoader")) $("jsLoader").dispose();
	this.onmousemove=null;
}


Distance=new function() {
	return {
		trim:function(sString){
			while(sString.substring(0,1)==' '){
				sString=sString.substring(1,sString.length);
			}
			while(sString.substring(sString.length-1,sString.length)==' ') {
				sString=sString.substring(0,sString.length-1);
			}
			return sString;
		},getCords:function(){
			var cook=Cookie.read( "dist" );
			cook=cook.split(":");
			return cook;
		},compute:function(a,b,c,d){
			function r(a){
				return a*m.PI/180;
			};
			var m=Math;
			var e=m.round((m.acos((m.sin(r(b))*m.sin(r(d)))+(m.cos(r(b))*m.cos(r(d))*m.cos(r(a)-r(c))))*6371)*10)/10;
			if(!e.toString().substring(m.floor(e).toString().length)){
				e=e.toString()+',0';
			} else {
				e=e.toString().replace(/\./,',');
			}
			return e;
			},get:function(longitude,latitude){
				var home=Distance.getCords();
				
				
				if(longitude==0||home==false) { 
					return 'nicht verfügbar'; 
				} else{
					return Distance.compute(longitude,latitude,home[0],home[1])+' km';
				}
			}
		}
	}

function npage(bid) {
	if (nickClick==0)
		npageshow(bid,0,'');
	else
		SmallProfil.showdirect(bid);	
}

function npageshow(bid,id,zusatz) {

	var h=600;
  	if ( (h+150) < screen.availHeight)
    	h=screen.availHeight-150;

	if (profilePopup==1)
		var popup = window.open("/profil/?id="+bid+'&np='+id+'&'+zusatz,'np'+bid,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=no,width=758,height="+h+",top=10,left=10");
	else
		window.location='/profil/?id='+bid+'&np='+id+'&'+zusatz;
	
}

function opengroup(bid) {
	if (profilePopup==1)
		parent.opener.location='/community/groups/detail/?id='+bid;
	else
		window.location='/community/groups/detail/?id='+bid;
}

function openevent(bid) {
	if (profilePopup==1)
		parent.opener.location='/community/calendar/detail/?id='+bid;
	else
		window.location='/community/calendar/detail/?id='+bid;
}


function startgame(bid) {
	if (profilePopup==1)
		var popup = window.open("/profil/game/?id="+bid,'np',"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=no,width=758,height=600,top=10,left=10");
	else
		window.location='/profil/game/?id='+bid;
}
