/*
eForum component javascript by Ioannis Sannos
http://www.isopensource.com
Created for Elxis CMS http://www.elxis.org
License: Creative Commons 3.0 Share Alike
*/


function eforumajaxobj() {
	if (window.XMLHttpRequest) { //IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE6, IE5
	}
	return null;
}

var eforumhttp = eforumajaxobj();

/* USER LIVE SEARCH (SEND PM - ADV SEARCH -ADD ANNOUNCEMENT) */
function liveSearch(adv) {
	if (eforumhttp==null) {
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	adv = parseInt(adv);
	var liverecip = document.getElementById('liverecip').value;
	document.getElementById("eforumlive").innerHTML = '';
	if (liverecip.length > 1) { //at least 2 chars
		document.getElementById("eforumlive").style.display = "";
		var rnd = Math.random();
        try {
        	var itemid = parseInt(document.getElementById('liveItemid').value);
        	var mylang = document.getElementById('livelang').value;
            eforumhttp.open('POST', 'index2.php', true);
            eforumhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            eforumhttp.setRequestHeader('charset', 'utf-8');
            eforumhttp.onreadystatechange = showliveres;
            eforumhttp.send('option=com_eforum&task=livesearch&Itemid='+itemid+'&mylang='+mylang+'&q='+liverecip+'&adv='+adv+'&rnd='+rnd);
        }
        catch(e){}
        finally{}
	} else {
		document.getElementById("eforumlive").style.display = "none";
		return;
	}
}

/* SHOW LIVE SEARCH RESULTS */
function showliveres() {
    var livebox = document.getElementById('eforumlive');
	if(eforumhttp.readyState == 4) {
		if(eforumhttp.status == 200) {
			livebox.innerHTML = eforumhttp.responseText;
		}
	}
}

function liveappenduser(uname, adv) {
	if (adv == 2) { //announcement add form
		document.getElementById('anuser').value = uname;
	} else if (adv == 1) { //advanced search
		document.getElementById('searchauthor').value = uname;
		eftoggle('efhelp2', ''); //fixes a help toggler issue
	} else { //quick search
		var recbox = document.getElementById('recipients');
		var currentrec = recbox.value;
		if (currentrec == '') {
			recbox.value = uname;
		} else {
			recbox.value = currentrec+','+uname;
		}
	}
	document.getElementById("eforumlive").innerHTML = '';
	document.getElementById("eforumlive").style.display = "none";
	document.getElementById('liverecip').value = '';
}


/* VALIDATE SEND PERSONAL MESSAGE */
function subsendpm() {
	var form = document.eforumpostform;
	if (form.recipients.value == '') {
		alert('You have to select a recipient!');
	} else if (form.subject.value == '') {
		alert('You have to write a subject!');
	} else if (form.message.value == '') {
		alert('You have to write a message!');
	} else {
		try {
			form.onsubmit();
		}
		catch(e){}
		form.submit();
	}
}

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


function efchangeicon() {
	var selObj = document.getElementById('eforumicon');
	var icon = selObj.options[selObj.selectedIndex].value;
	if (icon != '') {
		var tplurl = document.getElementById('eftplurl').value;
		var imgObj = document.getElementById('efmposticonfile');
		imgObj.src = tplurl+'/images/post_icons/'+icon;
	}
}


function eftogglelayer(whichLayer) {
	eftoggle(whichLayer, 'block');
}
function eftoggle(whichLayer, showstyle) {
	if (document.getElementById) {
		if (document.getElementById(whichLayer).style.display == 'none') {
			document.getElementById(whichLayer).style.display = showstyle;
		} else {
			document.getElementById(whichLayer).style.display = 'none';
		}
	} else if (document.all) {
		if (document.all[whichLayer].style.display == 'none') {
			document.all[whichLayer].style.display = showstyle;
		} else {
			document.all[whichLayer].style.display = 'none';
		}
	} else if (document.layers) {
		if (document.layers[whichLayer].style.display == 'none') {
			document.layers[whichLayer].style.display = showstyle;
		} else {
			document.layers[whichLayer].style.display = 'none';
		}
	}
}


function addattachment() {
	var attfield = document.createElement('INPUT');
	attfield.setAttribute("type", "file");
	attfield.setAttribute("class", "eforumtext");
	attfield.setAttribute("name", "efattachment[]");
	attfield.style.display = 'block';
	var attbox = document.getElementById('attachmentsbox');
	attbox.appendChild(attfield);
}

function efexpandthumb(aid, w, h) {
	var imgObj = document.getElementById('eforumattachment'+aid);
	if (imgObj.width < w) {
		imgObj.width = w;
		imgObj.height = h;
	} else {
		var newh = parseInt((100 * h)/w);
		imgObj.width = 100;
		imgObj.height = newh;
	}
}

/* SCROLL TO POSITION (POST) */
function efscrolltopos(elementid) {
	if (document.getElementById(elementid)) {
		var obj = document.getElementById(elementid);
		var curtop = 0;
		if (obj.offsetParent) {
			do {
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			window.scroll(0,curtop);
		}
	}
}

/* JUMP TO FORUM/BOARD */
function efjump() {
	var selObj = document.getElementById('efjumpto');
	if (!selObj) { return false; }
	var bc = selObj.options[selObj.selectedIndex].value;
	if (bc == '') { return false; }
	var sef = parseInt(document.getElementById('efjump_sef').value);
	if (sef == 1) {
		var url = document.getElementById('efjump_urlbase').value;
		if (bc.indexOf('#') == -1) {
			url += bc+'/';
		} else {
			url += '#category'+parseInt(bc.substr(1));
		}
	} else {
		var Itemid = parseInt(document.getElementById('efjump_itemid').value);
		var url = document.getElementById('efjump_urlbase').value;
		if (bc.indexOf('#') == -1) {
			url += 'index.php?option=com_eforum&task=board&Itemid='+Itemid+'&bid='+parseInt(bc);
		} else {
			url += 'index.php?option=com_eforum&Itemid='+Itemid+'#category'+parseInt(bc.substr(1));
		}
	}
	window.location.href = url;
}

/* pagination for search results */
function eforumgotopage(pg) {
	pg = parseInt(pg);
	if (pg < 1) { pg = 0; }
	var frm = document.getElementById('efsearchpagform');
	document.getElementById('efnavpage').value = pg;
	frm.submit();
}

/* toggle announcements type */
function eforumtoggleann(pub) {
	pub = parseInt(pub);
	if (pub == 0) {
		document.getElementById('angroupbox').style.display = 'none';
		document.getElementById('anuserbox').style.display = 'block';
	} else {
		document.getElementById('angroupbox').style.display = 'block';
		document.getElementById('anuserbox').style.display = 'none';
	}
}


/* REMOVE/HIDE ANNOUCEMENT */
function removeannounce(aid) {
	if (eforumhttp==null) {
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	aid = parseInt(aid);
	if (aid > 0) {
		var aidel = 'annbox'+aid;
		var rnd = Math.random();
        try {
        	var itemid = parseInt(document.getElementById('annitemid').innerHTML);
        	var mylang = document.getElementById('annlang').innerHTML;
            eforumhttp.open('POST', 'index2.php', true);
            eforumhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            eforumhttp.setRequestHeader('charset', 'utf-8');
			eforumhttp.onreadystatechange = function () {
				if (eforumhttp.readyState == 4) {
					document.getElementById(aidel).innerHTML = '';
					document.getElementById(aidel).style.display = 'none';
				}
			};
            eforumhttp.send('option=com_eforum&task=removeann&Itemid='+itemid+'&mylang='+mylang+'&aid='+aid+'&rnd='+rnd);
        }
        catch(e){}
        finally{}
    }
}

/* scroll to top */
var goto_topx_type = -1;
var goto_topx_itv = 0;

function eforum_timer() {
	var eforumy = goto_topx_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;
	var eforummoveby = 15;
	eforumy -= Math.ceil(eforumy * eforummoveby / 100);
	if (eforumy < 0) { eforumy = 0; }
	if (goto_topx_type == 1) {
		document.documentElement.scrollTop = eforumy;
	} else {
		document.body.scrollTop = eforumy;
	}
	if (eforumy == 0) {
		clearInterval(goto_topx_itv);
		goto_topx_itv = 0;
	}
}

function eforum_gotop() {
	if (goto_topx_itv == 0) {
		if (document.documentElement && document.documentElement.scrollTop) {
			goto_topx_type = 1;
		} else if (document.body && document.body.scrollTop) {
			goto_topx_type = 2;
		} else {
			goto_topx_type = 0;
		}
		if (goto_topx_type > 0) { goto_topx_itv = setInterval('eforum_timer()', 25); }
	}
}

function eforum_gotomoved(newurl) {
	window.close();
	if (newurl != '') {
		window.opener.location.href = newurl;
	}
}

function eforumvslide(id) {
	timeToSlide = 35; //in milliseconds
	obj = document.getElementById(id);
	if(obj.style.display == "none"){
		obj.style.visibility = "hidden";
		obj.style.display = "block";
		height = obj.offsetHeight;
		obj.style.height="0px";
		obj.style.visibility = "visible";
		pxPerLoop = height/timeToSlide;
		efslide(obj,0,height,pxPerLoop,'down');
	} else {
		height = obj.offsetHeight;
		pxPerLoop = height/timeToSlide;
		efslide(obj,height,height,pxPerLoop,'up');
	}
}

function efslide(obj,offset,full,px,direction){
	if(direction == 'down'){
		if(offset < full){
			obj.style.height = offset+"px";
			offset=offset+px;
			setTimeout((function(){efslide(obj,offset,full,px,'down');}),1);
		} else {
			obj.style.height = "auto";
		}
	}else if(direction == 'up'){
		if(offset > 5){
			obj.style.height = offset+"px";
			offset=offset-px;
			setTimeout((function(){efslide(obj,offset,full,px,'up');}),1);
		} else {
			obj.style.display = "none";
			obj.style.height = height+"px";
		}
	}
}


window.onload = eforuminitevents;
var efcursorx = 0;
var efcursory = 0;
var efcursorel = 0;
function eforuminitevents() {
	if (!document.all) {
		window.captureEvents(Event.MOUSEMOVE);
		window.onmousemove=efgetCursorXY;
	} else {
		document.onmousemove=efgetCursorXY;
	}
}

/* FIND CURSOR LOCATION */
function efgetCursorXY(e) {
	efcursorx = (!document.all) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	efcursory = (!document.all) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}

/* POP LIVE USER BOX */
function efuserpop(uid) {
	if (eforumhttp==null) { return; }
	efuserunpop();
	uid = parseInt(uid);
	efcursorel = parseInt(Math.random() * 10000);
	var posx = efcursorx;
	var posy = efcursory;
	var popbox = document.createElement('DIV');
	popbox.setAttribute('class', 'efuserpop');
	popbox.setAttribute('id', 'efpopbox'+efcursorel);
	popbox.style.display = 'block';
	popbox.style.position = 'absolute';
	popbox.style.zIndex = 8;
	posx = posx - 52;
	posy = posy - 145;
	popbox.style.top = posy+'px';
	popbox.style.left = posx+'px';
	popbox.innerHTML = 'loading...';
	document.body.appendChild(popbox);
    try {
    	eforumhttp.open("GET", 'index2.php?option=com_eforum&task=liveuserbox&uid='+uid+'&rnd='+efcursorel, true);
        eforumhttp.setRequestHeader('Content-Type', 'text/plain');
        eforumhttp.setRequestHeader('charset', 'utf-8');
		eforumhttp.onreadystatechange = function () {
			if (eforumhttp.readyState == 4) {
				if(eforumhttp.status == 200) {
					popbox.innerHTML = eforumhttp.responseText;
				}
			}
		};
		eforumhttp.send(null);
    }
    catch(e){}
    finally{}
}

/* UN-POP LIVE USER BOX */
function efuserunpop() {
	if (efcursorel > 0) {
		if (document.getElementById('efpopbox'+efcursorel)) {
			var popbox = document.getElementById('efpopbox'+efcursorel);
			popbox.innerHTML = '';
			document.body.removeChild(popbox);
		}
		efcursorel = 0;
	}
}


/* CONFIRM ACTION */
function efconfirm(clink, cmsg) {
	var doconf = confirm(cmsg);
	if (doconf == true) {
		window.location = clink;
	}
}

