// Favorite management
var Favorites = {
	curId : 0,
	add : function(url, title, e) {
		
		// Create new fav success message div
		var newId = 'favMsg_' + (++this.curId);
		var newDiv = document.createElement('div');
		newDiv.id = newId;
		newDiv.style.padding = '.3em';
		newDiv.style.fontSize = '.8em';
		newDiv.style.fontWeight = 'bold';
		newDiv.style.borderStyle = 'solid';
		newDiv.style.borderWidth = '1px';
		newDiv.style.borderColor = '#f3d648';
		newDiv.style.backgroundColor = '#f8f1bb';
		newDiv.style.zIndex = '11000';
		newDiv.style.position = 'absolute';
		newDiv.style.visibility = 'hidden';
		newDiv.style.opacity = '0';
		newDiv.style.filter = 'alpha(opacity=0)';
		newDiv.innerHTML = 'Added to <a href="/acsd/favorites/">favorites</a>...';
		document.body.appendChild(newDiv);

		var xClick = null;
		var yClick = null;
		
		if(e) {
			xClick = (e.pageX) ? e.pageX : (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);
			yClick = (e.pageY) ? e.pageY : (e.clientY + document.body.scrollTop + document.documentElement.scrollTop);
			YAHOO.util.Event.preventDefault(e);
		}
		var xmlhttp = null;
		if(window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		}
		else if(window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}  
		else {
			alert('Unable to save favorite.');
		}
		xmlhttp.open('GET', '/acsd/favorites/add/?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title), true);
		// Set the callback function
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				if(xClick != null && yClick !=null) {
					Favorites.animatedMsg(newId, xClick, yClick);
				}
			}
		};
		xmlhttp.send(null);
	},
	
	animatedMsg : function(favMsgId, xClick, yClick) {

					var favMsgDiv = document.getElementById(favMsgId);
					if(favMsgId) {
						favMsgDiv.style.left = (xClick - 130) + 'px';
						favMsgDiv.style.top = yClick + 'px';
						favMsgDiv.style.visibility = 'visible';
						
		
						var showAttributes = {
							opacity: { from: 0, to: 1 }
						};
						var animOpen = new YAHOO.util.Anim(favMsgDiv, showAttributes, .4);					
						animOpen.onComplete.subscribe(function() {
																var hideAttributes = {
																	opacity: { from: 1, to: 0 }
																};
																var animClose = new YAHOO.util.Anim(favMsgDiv, hideAttributes, .75);
																animClose.onComplete.subscribe(
																								function() {
																									document.body.removeChild(document.getElementById(favMsgId));
																								}
																							  );
																animClose.animate();
															  });
						animOpen.animate();
					}
	}

			
	
}
