// JavaScript Document
var xmlHttp;
var isActive; // works with showBookmarks
var timeout;
var domain = "http://www.vibe.to";

function getRecentViews(userid)
{
	ajaxStatus('Grabbing your recent profile views...');
	var divname = "ajaxProfileViews";
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		document.location.href = 'http://www.vibe.to/friends/'+userid+'/friends/';
		return
	} 
	var url="http://www.vibe.to/_ajax.profileViews.php"
	url=url+"?userid="+userid
	url=url+"&rand="+Math.random()
	xmlHttp.onreadystatechange=doOutput 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
	function doOutput() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			var views = document.createElement('div');
			views.id = divname;
			views.innerHTML = xmlHttp.responseText;
			var node = document.getElementById('titlePromptEtc');
			var viewsPrompt = document.getElementById('viewsPrompt');
			node.insertBefore(views, viewsPrompt.nextSibling);
			ajaxStatus();
			
			// stupid IE doesn't take setAttribute() ...
			
			document.getElementById('viewsPrompt').onclick = function()
			{
				toggleViews(1);
			}
			document.getElementById(divname).className = "recentViews";
			
			toggleViews(-1); // toggle on
			
			return false;
		} 
		else
		{
			document.getElementById('viewsPrompt').onclick = function()
			{
				return false; // set the viewsPrompt button to do nothing	
			}
		}
	} 
} 

function toggleViews(toggle) // -1 on, 1 off
{
	if (toggle > 0)
	{
		document.getElementById('ajaxProfileViews').style.display = "none";
		document.getElementById('viewsPrompt').onclick = function()
		{
			toggleViews(-1);
		}
		document.getElementById('viewsPrompt').className = "prompt_fade";
	}
	else
	{
		document.getElementById('ajaxProfileViews').style.display = "block";
		document.getElementById('viewsPrompt').onclick = function()
		{
			toggleViews(1);
		}
		document.getElementById('viewsPrompt').className = "prompt";
	}
	
	return false;
}

function ajaxQuickFind(firstSearch)
{
	xmlHttp=GetXmlHttpObject();
	ajaxStatus('Searching the database...');
	
	var divname = "quickFindContainer"; // name of results div
	
	if (xmlHttp == null)
	{
		// no ajax support, submit the form
		var quickForm = document.getElementById('quickfind');
		quickForm.submit();
		return
	}
	
	var url = "http://www.vibe.to/_ajax.quickFind.php";
	url = url + "?gender=" + document.getElementById('quick_gender').value;
	url = url + "&location=" + document.getElementById('quick_location').value
	url = url + "&agemin=" + document.getElementById('quick_agemin').value
	url = url + "&agemax=" + document.getElementById('quick_agemax').value
	
	if (firstSearch < 0) // they've already searched once
	{
		var prevSearchResults = document.getElementById(divname);
		document.getElementById('memberQuickFind').removeChild(prevSearchResults);
	}
	
	var container = document.createElement("div");
	container.setAttribute("id", divname);
	document.getElementById('memberQuickFind').appendChild(container);
	
	xmlHttp.onreadystatechange=doOutput;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
		
	function doOutput()
	{		
		if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
		{			
			container.innerHTML = xmlHttp.responseText;
			ajaxStatus();
			
			// we're done, allow them to submit again
			document.getElementById('quickFindSubmit').onclick = function()
			{
				ajaxQuickFind(-1);
			}
		}
		else
		{
			// make sure they don't submit more than once
			document.getElementById('quickFindSubmit').onclick = function()
			{
				return false;	
			}				
		}
	}
}

function hideQuickResults()
{
	var results = document.getElementById('quickFindContainer').innerHTML = "";
}

function fetchQuote(e, postid, username, table)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		return;	
	}
	
	if (window.event) window.event.returnValue = false;
	else if (e && e.preventDefault) e.preventDefault();
	
	var url = domain + "/_ajax.fetchQuote.php?postid=" + postid + "&username=" + username + "&table=" + table + "&rand=" + Math.random();
	
	xmlHttp.onreadystatechange=doOutput;
	xmlHttp.open("GET", url,true);
	xmlHttp.send(null);
	
	function doOutput()
	{
		if(xmlHttp.readyState == 4)
		{
			var replybox = document.getElementById('replybox');
			replybox.value += xmlHttp.responseText;
			
			coords = findPos(replybox);
		 	window.scrollTo(0, coords[1]);
		}
	}
}

function add2Fav(userid)
{
	if (!userid)
	{
		alert("You must login before you can bookmark pages");
		return
	}
	xmlHttp = GetXmlHttpObject();
	
	var docURL = document.location;
	var docTitle = document.title;
	
	var url = "http://www.vibe.to/_ajax.add2Fav.php";
	url = url + "?userid=" + userid;
	url = url + "&url=" + escape(docURL);
	url = url + "&title=" + escape(docTitle);
	url = url + "&rand=" + Math.random();
	
	xmlHttp.onreadystatechange=doOutput;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	function doOutput()
	{
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			isActive = null; // deactivate the bookmark menu
			
			if (xmlHttp.responseText == "1")
			{
				alert("You have successfully bookmarked this page.\n\n" + docTitle);
			}
			else
			{
				alert("We couldn't save the page to your favourites list because of an error");	
			}
		}
	}
}

function showBookmarks(e, userid)
{		
	var divid = 'favmenu';
	
	if (isActive) return;
	
	isActive = 1;
	
	e = ( e ) ? e : window.event;
	var obj = e.target ? e.target : e.srcElement;
	
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = doBookmarksAJAX;
	xmlHttp.open("GET","http://www.vibe.to/_ajax.getBookmarks.php?userid="+userid+"&rand="+Math.random(),true);
	xmlHttp.send(null);	
	
	function doBookmarksAJAX()
	{
		if (xmlHttp.readyState == 4)
		{
			var absOffset = findPos(obj);
			var menu = document.createElement('div');
			menu.id = divid;
			//menu.className = 'favMenu';
			
			var leftPX = parseInt(absOffset[0]) - 274;
			menu.style.left =  leftPX + 'px';
			
			var topPX = parseInt(absOffset[1]) + 27;
			menu.style.top = topPX + 'px'
			
			document.body.appendChild(menu)
			document.getElementById(divid).innerHTML = xmlHttp.responseText;
					
			menu.onmouseout = function()
			{
				timeout = window.setTimeout("hideBookmarks(1)", 3000);
			}
			menu.onmouseover = function()
			{
				if (timeout)
				{
					clearTimeout(timeout);
				} // else they haven't put their mouse over the menu yet
			}	
			
		}
	} // end
}

function hideBookmarks(fade)
{
	var ms = fade ? 350 : 1; // 350 = fade delay, 1 = instant
	if (!fade)
	{
		if (timeout)
			clearTimeout(timeout);
		isActive = 0;
		var menu = document.getElementById('favmenu');
		if (!menu) return;
		document.body.removeChild(menu);
	}
	else
	{
		setTimeout("hideBookmarks(0)", ms);
	}
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function fadeIn(opacity, objname)
{
	obj = document.getElementById(objname);
	if (obj)
	{
		if (opacity <= 100)
		{
			if (obj.style.MozOpacity != null) 
			{
				/* Mozilla's pre-CSS3 proprietary rule */
				obj.style.MozOpacity = (opacity/100)-.001;
				/* the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1 */
			} else if (obj.style.opacity != null) 
			{
				/* CSS3 compatible */
				obj.style.opacity = (opacity/100)-.001;
			} else if (obj.style.filter != null) 
			{
				/* IE's proprietary filter */
				obj.style.filter = "alpha(opacity="+opacity+")";
				/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
			}
			opacity += 10;
			window.setTimeout("fadeIn("+opacity+", '"+objname+"')", 35);
		}
	}
}

function fadeOut(opacity, objname, debug)
{
	var obj = document.getElementById(objname);
	
	if (obj)
	{
		if (opacity >= 0)
		{
			if (obj.style.MozOpacity != null) 
			{
				/* Mozilla's pre-CSS3 proprietary rule */
				obj.style.MozOpacity = (opacity/100)-.001;
				/* the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1 */
			} else if (obj.style.opacity != null) 
			{
				/* CSS3 compatible */
				obj.style.opacity = (opacity/100)-.001;
			} else if (obj.style.filter != null) 
			{
				/* IE's proprietary filter */
				obj.style.filter = "alpha(opacity="+opacity+")";
				/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
			}
			opacity -= 10;
			window.setTimeout("fadeOut("+opacity+", '"+objname+"')", 35);
		}
	} else if (debug) alert("Couldn't get object: " + objname);
}

function rateNow(rating, userid, rateclass, pid)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) return;
	var url = "http://www.vibe.to/_ajax.userVote.php"
	url = url + "?rating=" + rating;
	url = url + "&userid=" + userid;
	url = url + "&rateclass=" + rateclass;
	url = url + "&pid=" + pid;
	url = url + "&rand=" + Math.random();
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4)
		{ 
			if (rateclass == 'profile')
			{
				document.getElementById('rateDrop').style.display = 'none';
				var doneMsg = document.createTextNode('rating ' + rating + ' submitted');
				document.getElementById('rateSpot').appendChild(doneMsg);
			}
			else if (rateclass == 'picture')
			{
				var div = document.getElementById('ratingContainer');
				if (!div) return;
				
				var br = document.createElement('br');
				var text = document.createTextNode('saved');
				
				div.appendChild(br);
				div.appendChild(text);
			}
		} 
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function dirBrowser(hlink, dir, objref)
{
	var divid = objref + "_child";
	
	if (document.getElementById(divid))
	{
		hlink.style.fontWeight = 'normal';
		collapseDir();
		return;
	}
		
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = doBrowseDir;
	xmlHttp.open("GET","http://www.vibe.to/_ajax.dirBrowser.php?dir="+dir+"&rand="+Math.random(),true);
	xmlHttp.send(null);	
	
	function doBrowseDir()
	{
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			hlink.style.fontWeight = 'bold';
			var obj = document.getElementById(objref);
			var div = document.createElement('div');
			div.setAttribute('id', divid);
			div.innerHTML = xmlHttp.responseText;
			
			obj.appendChild(div);
			
			fadeIn(0, divid);
		}
	}
	
	function collapseDir()
	{
		var parent = document.getElementById(objref);
		var child = document.getElementById(divid);
		
		parent.removeChild(child);
	}
}
	
function ajaxStatus(string)
{
	var output = document.getElementById('ajaxStatus');
		
	if (string && !output)
	{
		var status = document.createElement('div');
		status.setAttribute('id', 'ajaxStatus');
		status.className = 'AJAXStatus';
		var innerTXT = document.createTextNode(string);
		status.appendChild(innerTXT);
		document.body.appendChild(status);
	}
	else // no string, kill output
	{
		if (output)
		{
			fadeOut(100, 'ajaxStatus');
			setTimeout("killAjaxStatus()", 350);
		}
	}
	
}

function killAjaxStatus()
{
	var output = document.getElementById('ajaxStatus');
	document.body.removeChild(output);
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	return objXMLHttp
} 
