// xLoadScript r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
function xLoadScript(url)
{
  if (document.createElement && document.getElementsByTagName) {
    var s = document.createElement('script');
    var h = document.getElementsByTagName('head');
    if (s && h.length) {
      s.src = url;
      h[0].appendChild(s);
    }
  }
}



var END_OF_INPUT = -1;

var base64Chars = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
);

var reverseBase64Chars = new Array();
for (var i=0; i < base64Chars.length; i++){
    reverseBase64Chars[base64Chars[i]] = i;
}

var base64Str;
var base64Count;
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}
function readBase64(){    
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}
function encodeBase64(str){
		if(str=='') return '';
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != END_OF_INPUT){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != END_OF_INPUT){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}


function str_replace(a, b, str) {
	return str_replace2(str, a, b);
}
function str_replace2(SRs, SRt, SRu) {
  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
  SRRi = SRs.indexOf(SRt);
  SRRr = '';
  if (SRRi == -1) return SRs;
  SRRr += SRs.substring(0,SRRi) + SRu;
  if ( SRRi + SRt.length < SRs.length)
    SRRr += str_replace2(SRs.substring(SRRi + SRt.length, SRs.length), SRt, SRu);
  return SRRr;
}


function round(number,X) {
	X = (!X ? 0 : X);
	return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}


function PrmToBase64(str) {
	res = encodeBase64(str);
	res = str_replace('+', '_p_', res);
	res = str_replace('/', '_s_', res);
	return res;
}


function readReverseBase64(){   
    if (!base64Str) return END_OF_INPUT;
    while (true){      
        if (base64Count >= base64Str.length) return END_OF_INPUT;
        var nextCharacter = base64Str.charAt(base64Count);
        base64Count++;
        if (reverseBase64Chars[nextCharacter]){
            return reverseBase64Chars[nextCharacter];
        }
        if (nextCharacter == 'A') return 0;
    }
    return END_OF_INPUT;
}

function ntos(n){
    n=n.toString(16);
    if (n.length == 1) n="0"+n;
    n="%"+n;
    return unescape(n);
}

function decodeBase64(str){
		if(str=='') return '';
    setBase64Str(str);
    var result = "";
    var inBuffer = new Array(4);
    var done = false;
    while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
        && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){
        inBuffer[2] = readReverseBase64();
        inBuffer[3] = readReverseBase64();
        result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
        if (inBuffer[2] != END_OF_INPUT){
            result +=  ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
            if (inBuffer[3] != END_OF_INPUT){
                result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
            } else {
                done = true;
            }
        } else {
            done = true;
        }
    }
    return result;
}



var commpost_sendingDiv = false;
function showCommPostForm(div) {
	if(!AUTHED) {
		if(typeof YouMustBeMember != 'undefined') YouMustBeMember();
		else alert("Vous devez être inscrit et identifié pour pouvoir poster un commentaire !");
		location = '#top';
  	return;
	}
	if(typeof div.isFormShown != 'undefined') {
  	if(div.isFormShown == true) { hideCommPostForm(); return; }
	}
	div.isFormShown = true;
	try { if(commpost_sendingDiv && commpost_sendingDiv!=div) commpost_sendingDiv.getElementsByTagName('DIV')[0].style.display = 'none'; } catch(e){}
	inp = div.getElementsByTagName('INPUT');
	inp = inp[inp.length-1];
	inp.parentNode.style.display = 'block';
	inp.disabled = false;
	commpost_sendingDiv = inp.parentNode.parentNode;
	inp.onclick = function() {
		ta = commpost_sendingDiv.getElementsByTagName('TEXTAREA');
		ta = ta[0];
		txt = ta.value;
		xLoadScript('/user/world_postcomment.php?fromjs=1&token='+TOKEN+'&message='+PrmToBase64(txt));
	};
}

function hideCommPostForm() {
	if(!commpost_sendingDiv) return;
	ta = commpost_sendingDiv.getElementsByTagName('TEXTAREA');
	ta = ta[0];
	ta.value = '';
	subdiv = commpost_sendingDiv.getElementsByTagName('DIV');
	subdiv = subdiv[0];
	subdiv.style.display = 'none';
	commpost_sendingDiv.isFormShown = false;
	commpost_sendingDiv = false;
}


function showPartnerPostForm() {
	div = document.getElementById('PostPartnerDiv');
	if(div.style.display == 'none') {
		div.style.display = 'block';
		document.getElementById('PostPartner_submit').disabled = false;
  }
	else {
		div.style.display = 'none';
		document.getElementById('PostPartner_submit').disabled = true;
  }
}

function PostPartnerGo() {
	document.getElementById('PostPartner_submit').disabled = true;
	xLoadScript('/edition/world_partner-add.php?fromjs=1&token='+TOKEN+'&name='+PrmToBase64(document.getElementById('PostPartner_name').value)+'&url='+PrmToBase64(document.getElementById('PostPartner_url').value)+'&description='+PrmToBase64(document.getElementById('PostPartner_description').value));
}

function hidePartnerPostForm() {
	showPartnerPostForm();
}

function PartnerMove(pos, action) {
	xLoadScript('/edition/world_partner-move.php?token='+TOKEN+'&pos='+pos+'&action='+action);
}

function ChangeSiteImage() {
	div = document.getElementById('ChangeSiteImageDiv');
	if(div.style.display == 'none') {
		div.style.display = 'block';
  }
	else {
		div.style.display = 'none';
  }
}


function FriendAdd(id) {
	xLoadScript('/user/friend-add.php?token='+TOKEN+'&friend='+id);
}

function FriendRemove(id) {
	var tr = document.getElementById('Friend_'+id);
	if(!tr) return;
	tr.style.display = 'none';
	xLoadScript('/user/friend-delete.php?token='+TOKEN+'&friend='+id);
}

function FavoriteRemove(id, tr) {
	while(tr.className!='CustomWorldTR' && tr!=tr.parentNode) {
  	tr = tr.parentNode;
	}
	if(tr.className!='CustomWorldTR') return;
	tr.style.display = 'none';
	xLoadScript('/user/favorite-delete.php?token='+TOKEN+'&world='+id);
}


function sendPrivateMsg(uid) {
	window.open('/user/write-message.php?dest='+uid+'&token='+TOKEN, 'writemsg_'+uid, 'toolbar=no,menubar=no,width=500,height=400,scrollbars=yes,');
}









 function swapNode(A, node) {
    var nextSibling = A.nextSibling;
    var parentNode = A.parentNode;
    node.parentNode.replaceChild(A, node);
    parentNode.insertBefore(node, nextSibling);
 }

		function Hex2DecF(HexVal){
			hexChars = '0123456789ABCDEF';
			HexVal = HexVal.toUpperCase();
			DecVal = 0;
			temp = HexVal.substring(0,1);
			DecVal = (hexChars.indexOf(temp) * 16);
			temp = HexVal.substring(1);
			DecVal += hexChars.indexOf(temp);
			return DecVal;
		}

		function hexdec(str) {
			r = 0;
			n = str.length/2;
			for(i=0; i<n; i++) {
				x = n-1-i;
				c = Hex2DecF(str.substring(i*2,i*2+2))*Math.pow(256,x);
				r += c;
			}
			return r;
		}

		function dechex(i) {
    		a2 = '';
    		ihex = hexQuot(i);
    		idiff = i-(ihex*16);
    		a2 = itohex(idiff) + a2;
    		while( ihex >= 16) {
        		itmp = hexQuot(ihex);
        		idiff = ihex-(itmp*16);
        		a2 = itohex(idiff) + a2;
        		ihex = itmp;
    		} 
    		a1 = itohex(ihex);
    		res = a1 + a2+'';
    		while(res.length%2!=0) res = '0'+res;
    		return res ;
		}
		function hexQuot(i) {
    		return Math.floor(i/16);
		}
function itohex(m) {hexChars = '0123456789ABCDEF';return hexChars.charAt(m);}


var lastOverNote = -1;
function NoteIt_over(n) {
	if(isNoteGiven) return;
	for(i=0; i<4; i++) {
		el = document.getElementById('NoteItSpan').childNodes[i];
		var isOn = (i<=n);
		el.src = PICSBASE+'/star'+(isOn?'':'-void')+'_14x13.gif';
	}
	lastOverNote = parseInt(n);
}
function NoteIt_outSpan() {
	NoteIt_over(-1);
}
var isNoteGiven = false;
function NoteIt_click(n) {
	if(ITSMYWORLD) {alert("Vous ne pouvez pas voter, car ce site vous appartient ...");return;}
	if(isNoteGiven) return;
	isNoteGiven = true;
	xLoadScript('/user/world_note.php?token='+TOKEN+'&note='+(lastOverNote+1)+'&rnd='+Math.random());
}

function NoteIt_over_0() { NoteIt_over(0); }
function NoteIt_over_1() { NoteIt_over(1); }
function NoteIt_over_2() { NoteIt_over(2); }
function NoteIt_over_3() { NoteIt_over(3); }
//function NoteIt_over_4() { NoteIt_over(4); }

function NoteIt_click_0() { NoteIt_click(0); }
function NoteIt_click_1() { NoteIt_click(1); }
function NoteIt_click_2() { NoteIt_click(2); }
function NoteIt_click_3() { NoteIt_click(3); }
//function NoteIt_click_4() { NoteIt_click(4); }


