/*
 * dom
 * Shorthand for the YAHOO.util.Dom class.
 */
var dom=YAHOO.util.Dom;

/*
 * fade-related variables
 * Variables that support the fading feature.
 */
var timer=null;	// The timer that controls when the fading feature can be started.
var isFadeEnabled=false;	// Specifies whether the fading feature is enabled.
var fadeStartTime=2000;	// The time after which the fading feature is enabled in milliseconds.

/*
 * startFade()
 * Starts the fading feature.
 */
function startFade() {
	clearTimeout(timer);
	isFadeEnabled=true;
	highlightListItem(activeListItem);
}

/*
 * ListItem(id,item,list)
 * Represents an item in a list.
 * PROPERTIES
 * id: A unique identifier for the list item.
 * item: The DOM element the list item represents.
 * list: The DOM element that represents the list.
 */
function ListItem(id,item,list) {
	this.id = id;
	this.item = item;
	this.list = list;
}
var activeListItem = null;

/*
 * isChild(parent,child)
 * Specifies whether $child is a child of $parent.
 * PARAMS
 * parent: The parent element.
 * child: An element.
 * RETURNS
 * true if $child is a child of $parent, otherwise false.
 * REMARKS
 * Taken from "onMouseOut fix on nested elements":
 * http://webmaster-forums.code-head.com/showthread.php?t=854
 */
function isChild(parent,child){
	if(child != null){
		while(child.parentNode){
			if((child = child.parentNode)==parent){
				return true;
			}
		}
	}
	return false;
}

/*
 * resetList(event,list)
 * Resets a list by removing the fade style from all items in a list.
 * PARAMS
 * event: The event that caused this function to be called.
 * list: The list to be reset.
 */
function resetList(event,list){
	var target = null;
	if(event.toElement){
		target=event.toElement;
	} else if(event.relatedTarget){
		target=event.relatedTarget;
	}
	if(!isChild(list, target) && list != target){
		var items = dom.getElementsByClassName('item','div',list);
		for(var i=0;i<items.length;i++){
			if(dom.hasClass(items[i],'faded')){
				dom.removeClass(items[i],'faded');
				var fade = new YAHOO.util.Anim(items[i], { opacity: {from:0.3,to:1}},1)
				fade.animate();
			}
		}
		activeListItem = null;
		isFadeEnabled = false;
	}
}

/*
 * highlightItem(event,item)
 * Removes the fade from an item so that it appears highlighted in a list.
 * PARAMS
 * event: The event that caused this function to be called.
 * item: The item whose fade is to be removed.
 */
function highlightItem(event,item){
	if (isFadeEnabled==true) {
		if ((activeListItem==null) || item.id != activeListItem.id) {
			activeListItem = item;
			highlightListItem(item);
		}
	} else {
		activeListItem = item;
		if (timer!=null) {
			clearTimeout(timer);
		}
		timer = setTimeout("startFade()", fadeStartTime);
	}
}

/*
 * highlightListItem(item)
 * Removes the fade from an item so that it appears highlighted in a list.
 * PARAMS
 * listItem: The item whose fade is to be removed.
 */
function highlightListItem(listItem) {
	if (listItem!=null) {
		var items = dom.getElementsByClassName('item','div',listItem.list);
		for(var i=0;i<items.length;i++){
			if (items[i]!=listItem.item){
				if(!dom.hasClass(items[i],'faded')){
					dom.addClass(items[i],'faded');
					var fade = new YAHOO.util.Anim(items[i], { opacity: {from:1,to:0.3}},1)
					fade.animate();
				}
			} else {
				if (dom.hasClass(listItem.item, 'faded')){
					dom.removeClass(listItem.item,'faded');
					var fade = new YAHOO.util.Anim(listItem.item, { opacity: {from:0.3,to:1}},1)
					fade.animate();
				}
			}
		}
	}
}

/*
 * initLists()
 * Initializes all lists on a page so that they support the fade technique.
 */
var initLists=function(){
	var lists=dom.getElementsByClassName('list','div');
	for(var i=0;i<lists.length;i++){
		initList(lists[i]);
	}
	lists=dom.getElementsByClassName('grid','div');
	for(var i=0;i<lists.length;i++){
		initList(lists[i]);
	}
};

function initList(list){
	if(list!=null){
		YAHOO.util.Event.addListener(list,"mouseout",resetList,list);
		var items = dom.getElementsByClassName('item','div',list);
		for(var i=0;i<items.length;i++){
			var listItem = new ListItem(i, items[i], list);
			YAHOO.util.Event.addListener(items[i], "mouseover", highlightItem, listItem);
		}
	}
}

/*
 * Initialize the lists when the DOM is ready.
 */
YAHOO.util.Event.onDOMReady(initLists);