// JavaScript Document



var perline = 8;
var allowHide = true;
var statusDivSet = false;

var curStatusElem;
var curStatus = '';



var nocolour = '';

var customMode = false;

var pickerElementId = 'dynamicStatusChanger';
var iframePickerId = 'dynamicStatusChangerIFrame';
var iframeDivWidth = '220px';
//if (document.all) { nocolour = ''; }


var detect = navigator.userAgent.toLowerCase();// alert(detect);
var OS,browser,version,total,thestring;
if (_checkIt('konqueror')) { 
	browser = "Konqueror"; OS = "Linux"; 
} else if (_checkIt('safari')) 
	browser = "Safari" 
		else if (_checkIt('opera')) 
		browser = "Opera" 
			else if (_checkIt('msie')) 
			browser = "IE" 
				else if (!_checkIt('compatible')) { 
					browser = "Netscape Navigator" 
					version = detect.charAt(8); 
				} else browser = "Unknown"; 

if (!version) version = detect.charAt(place + thestring.length); 
if (!OS) { 
	if (_checkIt('linux')) OS = "Linux"; 
	else if (_checkIt('x11')) OS = "Unix"; 
		else if (_checkIt('mac')) OS = "Mac" 
			else if (_checkIt('win')) OS = "Windows" 
				else OS = "Unknown"; 
} 
var doiframe = (browser == "IE");
function _checkIt(string) { 
	place = detect.indexOf(string) + 1;
	thestring = string; 
	return place; 
}// EOF _checkIt()

//------------------------



function getObject(id) { 
	return $(id);
}// EOF getObject()


function pickStatus(elem) {
if(status.length<=0)return false;
    if (!statusDivSet) { setStatusDiv(); }
	allowHide = false;
	//objListen('mousemove', getElem(pageId), showTarget);
	listen('mousemove', pageId, hideTarget);
	var picker = getObject(pickerElementId);

    if (doiframe) var iframe = getObject(iframePickerId);

    if (elem == curStatusElem && picker.style.display == 'block') {
		if(picker!=null)
        picker.style.display = 'none';

		
        if (doiframe && iframe!=null) iframe.style.display = 'none';

        return;

    }//EOF if-elem

    curStatusElem = elem;

    picker.style.top = getAbsoluteOffsetTop(elem) + 14 + "px";
    picker.style.left = getAbsoluteOffsetLeft(elem) + 2 + "px";

//    picker.style.left = getAbsoluteOffsetLeft(getObject('helloList')) + 2 + "px";
    customMode = false;
    if (doiframe) {
        iframe.style.top = picker.style.top;
        iframe.style.left = picker.style.left;
        iframe.style.width = iframeDivWidth;
        iframe.style.height = "auto";
        iframe.style.display = 'block';
		iframe.style.border="0";
		iframe.setAttribute('frameborder' ,'0');
		//statusDivSetVisible(1);
    }//EOF if-doiframe

    picker.style.display = 'block';
	

}
function setStatusDiv() {
    if (!document.createElement) { return; }
    var elemDiv = document.createElement('div');
    if (typeof(elemDiv.innerHTML) != 'string') { return; }
    elemDiv.id = pickerElementId;
    elemDiv.style.position = 'absolute';
    elemDiv.style.display = 'none';
    elemDiv.style.border = '1px solid #D6CFB1';
	elemDiv.style.width = '220px';
    elemDiv.style.zIndex = 99;

str = '<ul id="userStatusPicker">';
for(var i in status){
	str += '<li><a href="#" onMouseOver="allowHide=false" onMouseOut="allowHide=true" onclick="return assign(\''+ i +'\', \''+ status[i] +'\')">' + unescape(status[i]) + '</a></li>';
}
str += '<li><a href="#" onclick="return false()" onMouseOver="allowHide=false" onMouseOut="allowHide=true"/>--</a></li>';
//str += '<li><a href="javascript:void(0);askNewStatus()" onMouseOver="allowHide=false" onMouseOut="allowHide=true"><i>~New Status~</i></a></li>';
str += '<li><a href="'+ statusSettingUrl +'" onMouseOver="allowHide=false" onMouseOut="allowHide=true">Settings</a></li>';
str += '<li id="statusPickerLogout"><a href="'+ logoutUrl +'" onMouseOver="allowHide=false" onMouseOut="allowHide=true">Log Out</a></li>';
str += '</ul>';

elemDiv.innerHTML = str;

    document.body.appendChild(elemDiv);
    if (doiframe) {
        var iframeDiv = document.createElement('iframe');
        iframeDiv.id = iframePickerId;
		iframeDiv.style.position = 'absolute';
        iframeDiv.style.display = 'none';
        iframeDiv.style.border = '0';
        iframeDiv.style.zIndex = 4;
		iframeDiv.style.width = iframeDivWidth;
		iframeDiv.style.height = 'auto';
        document.body.appendChild(iframeDiv);
		
    }// EOF if-doiframe
	if(getObject('userStatusPicker').offsetWidth!=null){
		//elemDiv.style.width = getObject('userStatusPicker').offsetWidth + "px";
	}
   statusDivSet = true;
}// EOF setStatusDiv()


function getAbsoluteOffsetTop(obj) {
    var top = obj.offsetTop;
    var parent = obj.offsetParent;
    while (parent != document.body) {
        top += parent.offsetTop;
        parent = parent.offsetParent;
    }//EOF while-parent
    return top;
}// EOF getAbsoluteOffsetTop()

function askNewStatus(){
		var sample = 'New Status';
		var newStatus = prompt('Enter Your New Status', sample );
		if (newStatus!=null && (!stricmp(newStatus, sample)) && newStatus.length>0){
			newStatus = newStatus.substring(0, 20);
		}else{
		}
}
function stricmp(str1, str2){
	return (str1.toLowerCase() === str2.toLowerCase());
}
function addNewStatus(){
		
}
function assign(v, txt){
	obj = getObject('onlineStatusSpan');
	if(obj!=null){
		obj.innerHTML = txt;
		updateUserStatus(v);
		curStatus = v;
	}
	hideStatusPicker();
	return false;
}

function getAbsoluteOffsetLeft(obj) {
    var left = obj.offsetLeft;
    var parent = obj.offsetParent;
    while (parent != document.body) {
        left += parent.offsetLeft;
        parent = parent.offsetParent;
    }//EOF while-parent
    return left;
}// EOF getAbsoluteOffsetLeft()


function hideTarget(e){
	if(statusDivSet && allowHide)
		hideStatusPicker();
}
function hideStatusPicker(){
   var picker = getObject(pickerElementId);
    if (statusDivSet) {
	if(picker!=null)
        picker.style.display = 'none';
        if (doiframe) {
            var iframe = getObject(iframePickerId);
			if(iframe!=null)
            iframe.style.display = 'none';
        }//EOF if-doiframe
    }//EOF if-noClose
	statusDivSet = false
}
function hideAsEsc(){

}

function objListen(event, obj, func) {
	if(obj!=null){
    if (obj.addEventListener)  // W3C DOM
        obj.addEventListener(event,func,false);
    else if (elem.attachEvent)  // IE DOM
        obj.attachEvent('on'+event, function(){ func(new W3CDOM_Event(obj)) } );
	}
        // for IE we use a wrapper function that passes in a simplified faux Event object.
}

function updateUserStatus(st){
	var pars = 'u='+siteUserId+'&status=' + st;
	var url = siteUrl + 'members/online.php';
	new Ajax.Request(url, {method:'post',parameters:pars,onComplete:newStatusUpdated});
}
function newStatusUpdated(resp){
	txt = resp.responseText;
	if(txt.indexOf('rror') > 0){
		clearInterval(onlineTimer);
	}
}	

var _curStatus = '';
function saveStatus(st){
st = st.substring(0,20);
_curStatus = st;
pars = 'u='+siteUserId+'&astatus=' + st;
url = siteUrl + 'members/online.php';
new Ajax.Request(url, {method:'post',parameters:pars,onComplete:newStatusAdded});
}
function newStatusAdded(rep){
	xmlDoc = html2xml(rep.responseText);
	r = xmlDoc.documentElement.getElementsByTagName("id");					
	s = r[0].childNodes[0].nodeValue
	st = _curStatus;
	status[s] = st;
	rep = rep.responseText;
	if(rep.indexOf('rror')>0){
		clearInterval(onlineTimer);
	}
	
	sp = $('onlineStatusSpan');
	sp.update(st)
	curStatus = st;
	hideStatusPicker();
}
			function vj(){
			sp = getElem('onlineStatusSpan');
			if(sp!=null){
			s = sp.innerHTML;
			v = getObject('s');
			
			if(v==null){
				 s = '<input type="text" name="s" onkeyUp="store(this.value, event)" maxsize="20" size="' + s.length + '" id="s" value="'+ s +'">';
				 sp.innerHTML = s
				 getObject('s').select();
				 getObject('s').focus();
				}
			}
			}
			function store(v, evt){
				sp = getElem('onlineStatusSpan');
				evt = (evt) ? evt : window.event
				curEvent = evt;
				var charCode = (evt.which) ? evt.which : evt.keyCode;
				if(charCode==27){
					sp.innerHTML = curStatus;
				}
				if(charCode==13){
				existing = 0
				for(var i in status){
					if(stricmp(status[i], v)){
					existing ++;
					}
				}
				if(existing==0){
					saveStatus(v);
				}else{
					
					sp.innerHTML = v;
				}
				};
				
			}
function print_r(ob){
cont = '';
for(var i in ob){
	cont += i + '=' + ob[i];
	cont += "\n";
}
alert(cont);
}

html2xml = function(xmlText){
	var doc = null;
		//code for IE
		if (window.ActiveXObject)  {
			  doc=new ActiveXObject("Microsoft.XMLDOM");
			  doc.async="false";
			  doc.loadXML(xmlText);
		  }
		// code for Mozilla, Firefox, Opera, etc.
		else{
				var parser=new DOMParser();
				doc=parser.parseFromString(xmlText,"text/xml");
		  }
	return doc;
}


