var blockHeader = 148;
var blockMargin = 8;
var blanketHeight = 0;

var notMoz = document.all?true:false;

var columnheights = new Array();
columnheights[0] = new Array();
columnheights[1] = new Array();
columnheights[2] = new Array();

function acceptMembership(accept, group_id, username, user_id)
{
	if(accept)
	{
		action = 'accept';
	}
	else
	{
		action = 'decline';
	}
	
	if(confirm("Are you sure you want to " + action + " " + username + "'s membership?"))
	{
		document.location = 'admin.php?group_id=' + group_id + '&user_id=' + user_id + '&action=' + action;
	}
}

function leaveGroup(group_id)
{
	if(confirm("Are you sure you want to leave this Group?"))
	{
		document.location = 'leave.php?group_id=' + group_id;
	}
}

function findHeight()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ))
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && ( document.body.clientWidth || document.body.clientHeight ))
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return myHeight + 'px';
}

function createBlanket( onclick_callback )
{
	var blanketHeight = 0;
	
	if(columnheights[0] > columnheights[1] && columnheights[0] > columnheights[2])
	{
		blanketHeight = columnheights[0];
	}
	if(columnheights[1] > columnheights[0] && columnheights[1] > columnheights[2])
	{
		blanketHeight = columnheights[1];
	}
	if(columnheights[2] > columnheights[0] && columnheights[0] > columnheights[1])
	{
		blanketHeight = columnheights[0];
	}
	
	blanketHeight+= blockHeader;
	
	if(blanketHeight == 120)
	{
		blanketHeight = parseInt($('myPanel').offsetHeight) + blockHeader + 20;
	}
		
	var tempBlock = document.createElement('div');
	tempBlock.setAttribute('id','blanket');
	tempBlock.style.position = "absolute";
	tempBlock.style.zIndex = 1;
	tempBlock.style.left = 0 + "px";
	tempBlock.style.top = 0 + "px";
	
	tempBlock.style.backgroundColor = 'black';
	tempBlock.style.display = 'block';
	tempBlock.style.width = '100%';
	
	//hardcoded - I think we need to change the markup of the blanket/adder panels so they're a bit easier to work with in IE
	tempBlock.style.height = '2500px';

	tempBlock.style.filter = "alpha(opacity=40)";
	
	$('panels').appendChild(tempBlock);
	
	setOpacity("blanket",0.4);
	
	var el = $("blanket");
	if (el.addEventListener)
	{
		el.addEventListener('click', onclick_callback, false); 
	}
	else if(el.attachEvent)
	{
		el.attachEvent('onclick', onclick_callback);
	}	
}

function killBlanket()
{
	if($('blanket'))
	{
		$('panels').removeChild($('blanket'));
	}
}

function setOpacity(node, val) {	
	if(node.filters)
	{
		try
		{
			node.filters["alpha"].opacity = val*100;
		} catch (e) { }
	}
	
	if($(node))
	{
		$(node).style.opacity = val;
	}
}

function togglePanel(element, funName) {	
	$('panel-list').style.zIndex = 1000;
	$(element).style.zIndex = 1000;
	if($(element).style.display == 'block' )
	{
		$(element).style.display = 'none';
		$('panels').style.display = 'none';
		killBlanket();
	} else if(($(element).style.display == 'none' || $(element).style.display == '') ) {
		$(element).style.display = 'block';
		$('panels').style.display = 'block';		
		createBlanket(funName);
	}
}

function toggleSkinsPanel() {
	togglePanel("skins-panel", toggleSkinsPanel);
}

function toggleInvitePanel() {
	togglePanel("invite-panel", toggleInvitePanel);
}

function checkInvitations()
{
	for(i = 0; i < 6; i++)
	{
		if($('name-' + i).value > "" && $('email-' + i).value == "")
		{
			alert("It looks like you've forgotten to enter an email address in one of the fields!");
			return false;
		}
	}
	return true;
}

function resetLinks(element)
{
	linklist =  $('community-sub-navigation').getElementsByTagName("a");
	for(i = 0; i < linklist.length; i++)
	{							
		linklist[i].style.fontWeight = 'normal';
	}
	element.style.fontWeight = 'bold';
}

function sidebar(action, element)
{
	hideSidebarBlocks();	
	if(action == "show")
	{		
		$('sidebar-' + element + '-list').innerHTML = '<p style="border: 0;">Updating...</p>';
		$(element + '-hide').style.display = 'inline';
		$(element + '-show').style.display = 'none';
	}
	else
	{
		$('sidebar-' + element + '-list').innerHTML = '';
		$(element + '-hide').style.display = 'none';
		$(element + '-show').style.display = 'inline';	
	}
	
	var params = "&action=" + action + "-" + element;
	var superAjax =	new
		Ajax.Updater(
			'sidebar-' + element + '-list',
			"update.php",
			{
				method: 'post',
				parameters: params,
				evalScripts: true
			}
		);	
}

function checkSearchForm(inputToCheck)
{
	var terms = $(inputToCheck).value;
	if(terms.length < 3)
	{		
		alert("Please enter 3 or more characters to search.");
		return false;
	}
	return true;
	
}

function hideSidebarBlocks()
{
	var blocks = getElementsByClass("community-block");
	for(i = 0; i < blocks.length; i++)
	{
		if(parseInt(blocks[i].style.left) == 610)
		{
			var FadeInterval = 1;
			var FadeIterations = 1;
			setTimeout("doSidebarFadeOut(10,'" + blocks[i].id + "')", FadeInterval);
		}
	}
}

function showSidebarBlocks()
{
	var blocks = getElementsByClass("community-block");
	for(i = 0; i < blocks.length; i++)
	{
		if(parseInt(blocks[i].style.left) == 610)
		{
			var FadeInterval = 1;
			var FadeIterations = 1;
			setTimeout("doSidebarFadeIn(0,'" + blocks[i].id + "')", FadeInterval);			
		}
	}

}

function doSidebarFadeOut(colorId, targetId) {	
	if(IterationsCompleted < FadeIterations)
	{
		if(colorId >= 0)
		{
			setOpacity(targetId, FadeSteps[colorId]);
			colorId--;		
			// Wait a little bit and fade another shade
			setTimeout("doSidebarFadeOut("+colorId+",'"+targetId+"')", FadeInterval);
		}
	}
}

function doSidebarFadeIn(colorId, targetId) {	
	if(colorId <= 10)
	{
		setOpacity(targetId, FadeSteps[colorId]);
        	colorId++;		
        	// Wait a little bit and fade another shade
        	setTimeout("doSidebarFadeIn("+colorId+",'"+targetId+"')", FadeInterval);
        }
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function repositionFooter()
{
	$('footer').style.display = 'none';
	var blocks = getElementsByClass("community-block");	
	if($("primary-content"))
	{
		if(getSidebarHeight() > $("primary-content").offsetHeight)
		{
			setFooter(getSidebarHeight() + 8);
		}
		else
		{
			setFooter($("primary-content").offsetHeight + 8);
		}
	}
	else
	{	
		var leftCol = 0;
		var middleCol = 0;
		var rightCol = 0;
		for(i = 0; i < blocks.length; i++)
		{
			if(parseInt(blocks[i].style.left) == 0)
			{
				leftCol = leftCol + parseInt(blocks[i].offsetHeight) + 8;
			}
			if(parseInt(blocks[i].style.left) == 305)
			{
				middleCol = middleCol + parseInt(blocks[i].offsetHeight) + 8;
			}
			if(parseInt(blocks[i].style.left) == 610)
			{
				rightCol = rightCol + parseInt(blocks[i].offsetHeight) + 8;
			}
		}
		rightCol = rightCol + getSidebarHeight();

		if(leftCol > middleCol && leftCol > rightCol)
		{
			setFooter(leftCol);
		}
		if(middleCol > leftCol && middleCol > rightCol)
		{
			setFooter(middleCol);
		}
		if(rightCol > middleCol && rightCol > leftCol)
		{
			setFooter(rightCol);
		}
	}
}

function setFooter(newHeight)
{
	$('footer').style.top = blockHeader + newHeight + 'px';
	$('footer').style.display = 'block';
}

function getSidebarHeight()
{
	var returnHeight = 0;
	var blocks = new Array(7);
	blocks[0] = "advert";
	blocks[1] = "xbl-promo";
	blocks[2] = "cod4-promo";
	blocks[3] = "sidebar-fight";
	blocks[4] = "sidebar-forum";
	blocks[5] = "sidebar-stats";
	blocks[6] = "sidebar-giveaways";
	blocks[6] = "sidebar-skin-extras";
	
	for(i = 0; i < blocks.length; i++)
	{
		if($(blocks[i]))
		{
			returnHeight = returnHeight + parseInt($(blocks[i]).offsetHeight);
		}
	}
	return returnHeight + 56;
}