document.writeln('<div id="profilediv" class="popprofileBack" style="z-index:4; visibility: hidden; position: absolute;" onMouseOut="if (mouseLeaves(this, event)) { hideprofilediv();}"></div>');
document.writeln('<div id="ajaxdiv" class="popprofileBack" style="z-index:4; visibility: hidden; position: absolute;"></div>');
document.writeln('<div id="vinkdiv" style="z-index:4; background-image: url(\'http://faceup.dk/vinkback.png\');width:277px;height:167px; visibility: hidden; position: absolute;"></div>');

var profile_div = document.getElementById("profilediv");

var posx = 0;
var posy = 0;

var pop_left 	= null;
var pop_top		= null;

var obj;
var coors 		= new Array(2);
var element 	= null;
var popup_type 	= null;

//vink vars
var cnt = 97;
var start = 0;
var img = new Array(cnt); 


function showInline(id, sex, username, age, picture, crc, seen, myobj)
{ 
	obj 		= myobj;
	findCor(obj); // Find coordinates
	setLayer(id, sex, picture, username, seen, age, crc); // make visible
	popup_type = "profile_popup";
}

function showInlineOld(id, sex, username, age, picture, seen, myobj)
{
showInline(id, sex, username, age, picture, "???", seen, myobj);
}

function findCor(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop 	= obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop 	+= obj.offsetTop;
			if (obj.id == "news")
			  curtop -= obj.scrollTop;
		}
	}
	pop_left 	= curleft;
	pop_top		= curtop;
}

function setLayer(id, sex, picture, username, seen, age, crc){
if (navigator.appName == "Microsoft Internet Explorer"){
		if((posx+(218+18))<document.body.offsetWidth){ 	// pop right
			pop_left = pop_left+18;
		}else{							// pop left
			pop_left = pop_left-(218);
		}
	}
	if (navigator.appName == "Netscape"){
		if((posx+(218+18))<window.innerWidth){ 	// popright
			pop_left = pop_left+18;
		}else{							// pop left
			pop_left = pop_left-(218);
		}
	}
	if (picture.length <=1)
	picture = "nopicture" + sex + ".png";
	else if (picture.length <=25)
	picture += ".jpg";
	
	linkText = "<a href='http://faceup.dk/visprofil.php?id="+ id +"' title='Åbn "+username+"&rsquo;s profil'>";
	if (picture.length >25)
	imageText = linkText + "<img border='0' src='" + picture + "' alt='Åbn " + username + "&rsquo;s profil'></a>";
	else
	imageText = linkText + "<img border='0' src='http://faceup.dk/images/profiles/mili/" + picture + "' alt='Åbn " + username + "&rsquo;s profil'></a>";
	s = "";
	if ( sex==0 )
	s = "s";
	
	infoText = "<img src='http://faceup.dk/gfx/action_stop.gif' border='0' onclick='javascript:hideprofilediv();' style='float:right;cursor:hand;'><font size='-1'><img src='/" + s + "he.gif" + "'> "+ linkText + username + "</a>, " + age + " år." + "<br><img src='http://faceup.dk/gfx/email_edit.png'><a href='http://faceup.dk/skrivbrev.php?id="+id+"&c="+crc+"'> Skriv brev</a><br><img src='http://faceup.dk/gfx/book_edit.png'><a href='http://faceup.dk/gaestebog.php?id=" + id + "'> Åbn gæstebog</a>";
	if (seen != "-")
		infoText +="<br>Sidst online:<br>" + seen;
	infoText += "</font>";
	
	profile_div.style.top 		= pop_top + 'px'; // use mouse coordinates
	profile_div.style.left 		= pop_left + 'px';
	profile_div.innerHTML 		= "<table width='218' height='114' border='0' cellpadding='0' cellspacing='0'><tr><td valing='middle' align='center' width='81'>" + imageText + "</td><td valing='top' width='137' height='114'>" + infoText + "</td></tr></table>";
	profile_div.style.visibility 	= 'visible';
}


// henter mus koordinater
function getMouseXY(e){
	
	if (!e) { 
		var e = window.event;
	}
		
	if (e.pageX || e.pageY){
		posx = e.pageX;
		posy = e.pageY;
		
	} else if (document.documentElement && document.documentElement.scrollTop) {
		// Explorer 6 Strict
		posx = e.clientX + document.documentElement.scrollLeft;
		posy = e.clientY + document.documentElement.scrollTop;
	} else if (document.body) { 
		// all other Explorers 
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
	
	pop_left = posx;
	pop_top = posy;
	
	
	if (!element && popup_type!=null) {
		element = document.getElementById("profilediv");
	}
	
	if (popup_type=="profile_popup") {
		if (element.style.visibility == "visible") {
		check_coordinates();
		}
	}
	
}


function mouseLeaves (element, evt) {
	if (typeof evt.toElement != 'undefined' && typeof element.contains != 'undefined') {
		return !element.contains(evt.toElement);
	} else if (typeof evt.relatedTarget != 'undefined' && evt.relatedTarget) {
		return !contains(element, evt.relatedTarget);
	}
}

function contains (container, containee) {
  while (containee) {
    if (container == containee) {
      return true;
    }
    containee = containee.parentNode;
  }
  return false;
}

function check_coordinates(){
	
	if (popup_type == "profile_popup") {
		
		if ((posx + 27) < pop_left) {
			hideprofilediv();
		}
		if ((posy + 12 ) < pop_top) {
			hideprofilediv();
		}
		if (posx > (pop_left + 224)) {
			hideprofilediv();
		}
		if (posy > (pop_top + 120)) {
			hideprofilediv()
		}
	}	
}

// skjul div
function hideprofilediv() {
	popup_type = null;
	var element = document.getElementById("profilediv");
	if (element.style) {
		element.style.visibility = 'hidden';
	}
}
function hideajaxdiv() {

	var element = document.getElementById("ajaxdiv");
	if (element.style) {
		element.style.visibility = 'hidden';
	}
}

//Ajax gøgl
function makeHttpRequest(url, callback_function, return_xml) {
   var http_request = false; 

   if (window.XMLHttpRequest) { // Mozilla, Safari,... 
       http_request = new XMLHttpRequest(); 
       if (http_request.overrideMimeType) { 
           http_request.overrideMimeType('text/xml'); 
       } 
   } else if (window.ActiveXObject) { // IE 
       try { 
           http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
       } catch (e) { 
           try { 
               http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
           } catch (e) {} 
       } 
   } 

   if (!http_request) { 
       alert('Din browser understøtter ikke Ajax. Nogle features virker derfor ikke.'); 
       return false; 
   } 
   http_request.onreadystatechange = function() { 
       if (http_request.readyState == 4) { 
           if (http_request.status == 200) { 
               if (return_xml) { 
                   eval(callback_function + '(http_request.responseXML)'); 
               } else { 
                   eval(callback_function + '(http_request.responseText)'); 
               } 
           } else { 
               alert('Fejl i svar fra server.(Kode: ' + http_request.status + ')'); 
           } 
       } 
   } 
   http_request.open('GET', url, true);
   http_request.send(null); 
}	

function setAjax(id)
{
var time=new Date(); 
var ajax_div = document.getElementById("ajaxdiv");
var url ="http://faceup.dk/anmodven.php?id=" + id + "&check=" + time.getMinutes();
ajax_div.style.visibility= 'visible';
ajax_div.style.top 		= posy + 'px'; // use mouse coordinates
ajax_div.style.left 		= posx + 'px';
makeHttpRequest(url, "popAjaxDiv");
}

function searchNick()
{
name = document.forms["name_search_form"].nick.value;
if (name.length > 0)
{
var url ="http://faceup.dk/checksearchname.php?name=" + name;
makeHttpRequest(url, "popNickCount");
}
else
	popNickCount("0");
}

function popNickCount(str)
{
	document.getElementById("nicksFundet").innerHTML = str + " fundet.";
}

function addFriend(id)
{
url = "http://faceup.dk/putven.php?add=" + id + "&check=" + Math.floor(Math.random()*10000);
makeHttpRequest(url, "friendAdded");
}

function friendAdded()
{
document.location.reload();
}

function removeFriend(id)
{
url = "http://faceup.dk/putven.php?remove=" + id + "&check=" + Math.floor(Math.random()*10000);
makeHttpRequest(url, "friendRemoved");
}

function friendRemoved()
{
document.location.reload();
}

function openSendVink(id)
{
var time=new Date(); 
var vink_div = document.getElementById("vinkdiv");
var url ="http://faceup.dk/sendvink.php?id=" + id + "&check=" + time.getMinutes();
vink_div.style.visibility= 'visible';
vink_div.style.top = posy + 'px'; // use mouse coordinates
vink_div.style.left = (posx-140) + 'px';
makeHttpRequest(url, "popVinkDiv");
}

function sendVink()
{
vinx = document.forms["vinkForm"].selected.value;
msgx = document.forms["vinkForm"].message.value;
usx = document.forms["vinkForm"].userid.value;
if (document.forms["vinkForm"].selected.value >= 0 && document.forms["vinkForm"].selected.value != 'undefined')
{
url = "http://faceup.dk/addvink.php?send=" + vinx + "&msg=" + msgx + "&to=" + usx + "&check=" + Math.floor(Math.random()*10000);
//alert(url);
makeHttpRequest(url, "vinkSent");
}
}

function vinkSent()
{
document.getElementById("vinkdiv").innerHTML = "<table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0'><tr><td height='20' align='right'><img border='0' style='cursor:hand' src='http://faceup.dk/gfx/action_stop.gif' width='16' height='16' onClick='javascript:exitVink()' title='luk'></td></tr><tr><td align='center'>Dit vink er er blevet afsendt.<br><br><a href='#' onclick='javascript:exitVink()'>Luk vindue.</a></td></tr></table>";
}

function popVinkDiv(str){
	document.getElementById("vinkdiv").innerHTML = str;
	initVinks();
    next();
}

function popAjaxDiv(str){
	document.getElementById("ajaxdiv").innerHTML = str;
}

function showProfile(id){
window.open("http://faceup.dk/visprofil.php?id="+id,"","toolbar=no,location=no,status=yes,scrollbars=yes,left=0,top=0,width=868,height=700");
}

function removeVisit(id)
{
url = "http://faceup.dk/hidevisit.php?v="+id+"&check=" + Math.floor(Math.random()*10000);
makeHttpRequest(url, "visitRemoved");
var elt = document.getElementById("visit"+id);
if (elt.style) {elt.style.visibility = 'hidden';elt.style.display = 'none';}
}

function visitRemoved(str)
{}

document.onmousemove = getMouseXY;

