var mall_fav_container = null;
var mall_fav_index = 0;						// index of last displayed merchant
var mall_favs = [];
var mall_favs_current = [];					// currently displayed merchants
var mall_fav_len = 0;
var mall_fav_display = 1;					// number of merchants to display at 1 time
var mall_fav_timer = null;
var mall_fav_delay = 4500;					// milliseconds to wait between animation

function mall_fav_init(e)
{
	var node, i, len, arr, a;
	
	if ( (node = document.getElementById("mall_favorites")) )
	{
		//mall_favs = node.getElementsByTagName("IMG");
		mall_favs = getElementsByClassName('merchant_image', node);
		mall_fav_len = mall_favs.length;
		
		if ( mall_fav_len > mall_fav_display )
		{
			a = document.createElement("A");
			a.setAttribute("href", "javascript:mall_fav_all();");
			a.setAttribute("id", "mall_fav_all");
			a.setAttribute("title", "Click to expand all favorites");
			a.appendChild( document.createTextNode("View All Favorites") );
			node.appendChild( document.createElement("BR") );
			node.appendChild( a );
			
			mall_fav_swap();
			mall_fav_timer = setInterval(mall_fav_swap, mall_fav_delay);
		} else {
			var count = 0;
			while ( count < mall_fav_len )
			{
				mall_favs_current[count] = mall_favs[count];
				mall_favs[count].style.display = "block";
				count += 1;
			}
			mall_fav_index = ( count > 0 ) ? count - 1 : 0;
		}
	}
}

function mall_fav_all()
{
	clearInterval( mall_fav_timer );
	
	for ( var i = 0; i < mall_fav_len; i++ )
		mall_favs[i].style.display = "block";
	
	var a = document.getElementById("mall_fav_all");
	a.setAttribute("title", "Click to hide favorites");
	a.setAttribute("href", "javascript:mall_fav_hide();");
	a.innerHTML = "Hide Favorites";
}

function mall_fav_hide()
{
	var i, len, a;
	
	for (i = 0; i < mall_fav_len; i++)
		mall_favs[i].style.display = "none";
	
	len = mall_favs_current.length;
	for (i = 0; i < len; i++)
		mall_favs_current[i].style.display = "block";
	
	a = document.getElementById("mall_fav_all");
	a.setAttribute("href", "javascript:mall_fav_all();");
	a.setAttribute("title", "Click to expand all favorites");
	a.innerHTML = "View All Favorites";
	
	window.scrollTo(0, 0);
	mall_fav_timer = setInterval(mall_fav_swap, mall_fav_delay);
}

function mall_fav_swap()
{
	var i, count = 0, index = mall_fav_index;
	
	if ( mall_favs_current.length > 0 )
		for (i = 0; i < mall_fav_display; i++)
			mall_favs_current[i].style.display = "none";
	
	mall_favs_current = [];
	
	while ( count < mall_fav_display )
	{
		if ( ++index >= mall_fav_len )
			index = 0;
		
		mall_favs[ index ].style.display = "block";
		mall_favs_current.push( mall_favs[ index ] );
		count += 1;
	}
	
	mall_fav_index = index;
}

if ( window.attachEvent )
	window.attachEvent('onload', mall_fav_init);
else
	window.addEventListener('load', mall_fav_init, false);