var isActive;
var timeout;

function add2Fav(userid, title)
{
	if (!userid)
	{
		alert("Please reload the page and try adding your bookmark again.");
		return
	}
	if (document.getElementById) {
    	var xmlHttp = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  	}
	//xmlHttp = GetXmlHttpObject();

	var docURL = document.location;
	
	
	var bmElement = document.getElementById("bookmark");
	var bmList = document.getElementById("my-bookmarks");

	var url = "http://www.elementtechnica.com/_ajax.add2Fav.php";
	url = url + "?userid=" + userid;
	url = url + "&url=" + escape(docURL);
	url = url + "&title=" + escape(title);
	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")
			{
				jscss('swap',bmElement,'add-bm', 'is-bm');
				updateLatestBookmarks(userid);
				//o.className=!domtab.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
				//alert("You have bookmarked this product - " + docTitle);
			}
			else if (xmlHttp.responseText == "2")
			{
				
				alert("You have already bookmarked this product!");
			}
			else
			{
				alert(xmlHttp.responseText);
			}
		}
	}
}

function updateLatestBookmarks(userid)
{
	var bmDiv = document.getElementById("latest-bookmarks");
	
	var updateUrl = "http://www.elementtechnica.com/_ajax.updateBookmarks.php";
	updateUrl = updateUrl + "?userid=" + userid;
	
	updateUrl = updateUrl + "&rand=" + Math.random();
	
	if (document.getElementById) {
    	var xmlHttp = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  	}
	
	xmlHttp.onreadystatechange=doUpdateOutput;
	xmlHttp.open("GET", updateUrl, true);
	xmlHttp.send(null);
	
	function doUpdateOutput()
	{
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			isActive = null;
			
			bmDiv.innerHTML = xmlHttp.responseText
		}
	}
}

function delBookmark(bmid, userid)
{
	var bmDiv = document.getElementById("latest-bookmarks");
	var bmElement = document.getElementById("bookmark");
	
	var delUrl = "http://www.elementtechnica.com/_ajax.delBookmark.php";
	delUrl = delUrl + "?bmid=" + bmid;
	delUrl = delUrl + "?userid=" + userid;
	
	delUrl = delUrl + "&rand=" + Math.random();
	
	if (document.getElementById) {
    	var xmlHttp = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  	}
	
	xmlHttp.onreadystatechange=doDeleteOutput;
	xmlHttp.open("GET", delUrl, true);
	xmlHttp.send(null);
	
	function doDeleteOutput()
	{
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			isActive = null;
			
			bmDiv.innerHTML = xmlHttp.responseText
			jscss('swap',bmElement,'add-bm', 'is-bm');
			updateLatestBookmarks(userid);
		}
	}
}


function deleteBookmark(obj, userid, id)
{
	var row = obj.parentNode; // one step up
	var bmDiv = document.getElementById("latest-bookmarks");
	var bmElement = document.getElementById("bookmark");
	
		
		//ajaxStatus('Deleting bookmark...');
			
		if (document.getElementById) {
			var xmlHttp = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		}
		xmlHttp.onreadystatechange=doDelete;
		xmlHttp.open("GET","http://www.elementtechnica.com/_ajax.delBookmark.php?userid="+userid+"&id="+id,true);
		xmlHttp.send(null);
	

	function doDelete()
	{
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			if (xmlHttp.responseText == "1")
			{
				if (row.nodeName.toUpperCase() == 'LI')
				{
					row.parentNode.removeChild(row);
				}
				jscss('swap',bmElement,'add-bm', 'is-bm');
				updateLatestBookmarks(userid);
				// done... don't need to do anything
				
			} else alert(xmlHttp.responseText);

			//ajaxStatus();
		}
	}
}

function showBookmarks(e, userid)
{
	var divid = 'favmenu';
	var heightPX = 27; // define the elements height in PX
	var widthPX = -301;

	if (!isActive) // make sure they've only clicked the link once
	{
		isActive = 1; // the menu is active

		e = e ? e : window.event; // ff : ie
		var obj = e.target ? e.target : e.srcElement; // ff : ie

		xmlHttp = GetXmlHttpObject();
		xmlHttp.onreadystatechange = doBookmarksAJAX;
		xmlHttp.open("GET","_ajax.getBookmarks.php?userid="+userid+"&rand="+Math.random(),true);
		xmlHttp.send(null);
	}

	function doBookmarksAJAX()
	{
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			var parent = document.getElementById('topRightDiv');
			var absOffset = findPos(parent);
			var menu = document.createElement('div');
			menu.setAttribute('id', divid);
			//menu.className = 'favMenu';

			var leftPX = parseInt(absOffset[0]) + widthPX;
			menu.style.left =  leftPX + 'px';

			var topPX = parseInt(absOffset[1]) + heightPX; // top of object + object size
			menu.style.top = topPX + 'px'

			parent.appendChild(menu)
			document.getElementById(divid).innerHTML = xmlHttp.responseText;

			fadeIn(0, divid);

			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');
		var parent = document.getElementById('topRightDiv');
		parent.removeChild(menu);
	}
	else
	{
		fadeOut(100, 'favmenu');
		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)
{
	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);
		}
	}
}

function jscss(a,o,c1,c2)
		{
		  switch (a){
			case 'swap':
			  o.className =! jscss('check',o,c1) ? o.className.replace(c2,c1) : o.className.replace(c1,c2);
			break;
			case 'add':
			  if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
			  var rep=o.className.match(' '+c1)?' '+c1:c1;
			  o.className=o.className.replace(rep,'');
			break;
			case 'check':
			  return new RegExp('\\b'+c1+'\\b').test(o.className)
			break;
		  }
		}



function initFaveLinks(userid)
{
	//if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "fave")){
			anchor.onclick = function (userid) {add2Fav(userid); return false;}
		}
	}
}