/* ----------------------------------------------------------------------
	TOOL BOX
---------------------------------------------------------------------- */
function tollBoxVisibility()
{
	if(!window.getCookie)
		return;
	
	if(getCookie("showToolBox")=="none")
	{
		var tollBox = document.getElementById("toolBox");
		if(tollBox)
		{
			tollBox.style.display = "none";
			var button = document.getElementById("toolBoxButton");
			if(button)
				button.className += " off";
				button.title = "Show ToolBox";
		}
	}
}

function changeToolBoxVisiblity(button)
{
	var tollBox = document.getElementById("toolBox");
	if(!tollBox)
		return false;
	var displayStatus = tollBox.style.display == "none";
	tollBox.style.display = ( displayStatus ) ? "block" : "none";
	if(displayStatus)
	{
		button.className = button.className.replace("off","");
		button.title = "Hide ToolBox";
	}
	else
	{
		button.className += " off";
		button.title = "Show ToolBox";
	}
	setCookie("showToolBox", tollBox.style.display, "/");
	
	return false;
}

/* ----------------------------------------------------------------------
	MENU TOP HOVER
---------------------------------------------------------------------- */
function animateHoverMenuTop()
{
	var top = document.getElementById("top");
	if(!top)
		return;
	var menuItems = top.getElementsByTagName("ul").item(0).getElementsByTagName("a");
	for(var i = 0; i < menuItems.length; i++ )
	{
		setOverImg(menuItems[i]);
		menuItems[i].onmouseover = on;
		menuItems[i].onmouseout = off;
	}
	// load hover images
	function setOverImg(item)
	{
		item.firstChild.offImage = item.firstChild.src;
		var tmp = new Image();
		tmp.src = item.firstChild.src.replace("top_menu.svg","top_menu_hover.svg");
		item.firstChild.onImage = tmp.src;
	}
	
	function on()
	{
		var parent =  this.parentNode;
		this.firstChild.src = this.firstChild.onImage;
		if(checkFirst(parent))
			parent.parentNode.style.borderLeftColor = "#990505";
		switchBg(parent, true);
	}
	
	function off()
	{
		var parent =  this.parentNode;
		this.firstChild.src = this.firstChild.offImage;
		if(checkFirst(parent))
			parent.parentNode.style.borderLeftColor = "#eaeaea";
		switchBg(parent, false);
	}
	
	function checkFirst(element)
	{
		if(element.parentNode.firstChild.nodeType == 1 && element.parentNode.firstChild == element)
			return true;
		if(element.parentNode.firstChild.nodeType == 3 && element.parentNode.childNodes.item(1) == element)
			return true;
		return false;
	}
	
	function switchBg(element, status)
	{
		// if is already active, don't do anything
		if(element.className.indexOf("active")!=-1 && status && element.className.indexOf("selected")==-1)
			return;
		element.className = status ? element.className + " active" :
				element.className.replace("active","");
	}
}

/* ----------------------------------------------------------------------
	POPUPS LINK
---------------------------------------------------------------------- */
function makePopUp()
{
	var links = document.getElementsByTagName("a");
	
	var width = 640;
	var height = 480;
	
	for( var i = 0; i < links.length; i++ )
	{
		if(links[i].className.indexOf("popup_window") != -1 )
			links[i].onclick = function()
			{
				openWindow(this.href, width, height);
				return false;
			}
	}
}

function openWindow(href, width, height)
{
	var screenWidth=window.screen.width;
	var screenHeight=window.screen.height;
				
	var left = (screenWidth / 2) - width / 2;
	var top = (screenHeight / 2) - height / 2;
				
	var w = window.open(href, "popup", "height=" + height + ",width=" + width + 
		",left=" + left + ",top=" + top + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
}

/* ----------------------------------------------------------------------
	IE SELECT FIX - buuu ;(
---------------------------------------------------------------------- */
function fixToolTip()
{
	var UA = navigator.userAgent.toLowerCase();
	if( UA.indexOf("msie") == -1 || UA.indexOf("opera") != -1 )
		return;
	
	var toolTips = document.getElementsByTagName("span");
	for(var i = 0; i < toolTips.length; i++ )
	{
		if(toolTips[i].className.indexOf("tooltip")!=-1)
		{
			toolTips[i].onmouseover = fix;
			toolTips[i].onmouseout = fixOff;
		}
	}
	
	function fix()
	{
		var iframe;
		var iframeId = "toolTipIframe";
		
		var d = this.firstChild;
		
		this.className += " hover";
		this.parentNode.className += " hover";

		iframe = document.getElementById(iframeId);
		if(!iframe)
		{
			iframe = document.createElement("iframe");
			iframe.setAttribute("id",iframeId);
			iframe.setAttribute("frameBorder","0");
			iframe.style.border="none";
			iframe.style.position="absolute";
			iframe.style.top="22px";
			iframe.style.right="0";
			iframe.style.zIndex="8";
			iframe.style.width=d.offsetWidth + "px";
			iframe.style.height=d.offsetHeight + "px";
			this.appendChild(iframe);
		}
		else
		{
			iframe.parentNode.removeChild(iframe);
			iframe.style.width=d.offsetWidth + "px";
			iframe.style.height=d.offsetHeight + "px";
			this.appendChild(iframe);
		}
		
		iframe.style.display="block";
		return true;
	}
	
	function fixOff()
	{
		this.className = this.className.replace("hover","");
		this.parentNode.className = this.parentNode.className.replace("hover","");
	}
}

/* ----------------------------------------------------------------------
	SCREEN SIZE
---------------------------------------------------------------------- */
function getScreenSize(mode)
{
	if (self.innerHeight)
	{
        if(mode == 'width') return self.innerWidth;
        else return self.innerHeight;
        
	}
	else if (document.documentElement || document.body)
	{
        if(mode == 'width')
        	return document.documentElement.clientWidth ?
        		document.documentElement.clientWidth : document.body.clientWidth;
        else
			return document.documentElement.clientHeight ?
        		document.documentElement.clientHeight : document.body.clientHeight;
	}
	
	return 0;
}

/*=============================================================================
    RESIZE DOCUMENT IF IT'S TOO SHORT
==============================================================================*/
function resizeDoc()
{
	// something mess up when administration mode is on
	if(document.getElementById("preview_warning"))
		return;

	var offset = 165; // height of top and bottom
	
	var content = document.getElementById("middle");
	if(!content)
		return;

	var screenHeight = getScreenSize("height");
	
	var containerHeight = document.getElementById("container").offsetHeight;
	
	if(containerHeight < screenHeight)
		content.style.height =  (screenHeight - offset) + "px";
}

/*=============================================================================
    ELEMENT SWITCHER
==============================================================================*/
function switcher(buttonsContainer, elementContainer, element, animation)
{
	var animationIsOn = false;

	// options
	var optionsContainer = document.getElementById( buttonsContainer );
	if(!optionsContainer)
		return;

	var optionsArray = new Array();

	var links = optionsContainer.getElementsByTagName("a");
	for(var i = 0; i < links.length; i++)
	{
		optionsArray[i] = links[i];
		links[i].lp = i;
		links[i].onclick = switchElement;
	}
	
	//switch a single element
	function switchElement()
	{
		this.blur();
		
		// check if this eleme isn't active or animation isn't started
		if(this.className.indexOf("active") != -1 || animationIsOn)
			return false;

		var newElem, oldElem;
		var container = document.getElementById(elementContainer);
		if(!container)
			return true;

		var elements = container.getElementsByTagName(element);

		for(var i = 0; i < elements.length; i++ )
		{
			if(this.lp == i)
			{
				newElem = elements[i];
				optionsArray[i].className += " active";
			}
			else if(elements[i].className.indexOf("active") != -1)
			{
				oldElem = elements[i];
				optionsArray[i].className = optionsArray[i].className.replace("active","");
			}
		}
		
		replaceElements(newElem, oldElem, animation);
		return false;
	}

	function replaceElements(n,o,animation)
	{
		// if no animation, just switch class name
		if(animation == false)
		{
			n.className += " active";
			o.className = o.className.replace("active","");
			return;
		}
		else if(animation == 1)
		{
			animationIsOn = true;	
			animateAlpha(5, 10);
		}
		else if(animation == 2)
		{
			animationIsOn = true;	
			animateSlide(10, 10);
		}
		
		return false;
		
		function animateAlpha(step,speed)
		{
			var opacity = 0;
			
			n.style.opacity = opacity / 100;
			n.style.filter="alpha(opacity=" + opacity + ")";
			n.className += " active";
			n.zIndex = 10;
			o.zIndex = 0;
			
			
			var animation = setInterval(function ()
			{
				opacity += step;
				n.style.opacity = opacity / 100;
				n.style.filter="alpha(opacity=" + opacity + ")";	
				o.style.opacity = (100-opacity) / 100;
				o.style.filter="alpha(opacity=" + ( 100 - opacity ) + ")";	
				if(opacity >= 100)
				{
					clearInterval(animation);
					// turn on animation
					animationIsOn = false;
					o.className = o.className.replace("active","");
				}
			} , speed );
		}
		
		
		function animateSlide(step, speed)
		{
			var oLeft = parseInt(o.offsetLeft);
			var nLeft = parseInt(n.offsetLeft);
			
			var animation = setInterval(function ()
			{
				if(nLeft - step <= 0)
				{
					clearInterval(animation);

					n.className += " active";
					o.className = o.className.replace("active","");
					nLeft = 0;
					oLeft = parseInt(n.offsetWidth);
					n.style.left = nLeft + "px";
					o.style.left = oLeft + "px";
					animationIsOn = false;
					return false;
				}
			
				nLeft -= step;
				n.style.left = nLeft + "px";
				oLeft -= step;
				o.style.left = oLeft + "px";
			}, speed );
			
		}
		
	}
}

function changeStyle(element, styleName)
{
	element.className = element.className.indexOf(styleName) != -1 ?
		element.className.replace(styleName,"") : element.className + " " + styleName;
}

function clickOnTheRow()
{
	var table = document.getElementById("msg_table");
	if(!table)
		return;
	var cells = table.getElementsByTagName("tbody").item(0).getElementsByTagName("td");
	for(var i = 0; i < cells.length; i++)
		if(cells[i].className.indexOf("option") == -1)
			cells[i].onclick = function()
			{
				location.href = '/members/view_message.xml?message_id=' + this.parentNode.id.slice(3);
			}
}


function openWindow(href, width, height)
{
	var screenWidth=window.screen.width;
	var screenHeight=window.screen.height;
				
	var left = (screenWidth / 2) - width / 2;
	var top = (screenHeight / 2) - height / 2;
				
	var w = window.open(href, "popup", "height=" + height + ",width=" + width + 
		",left=" + left + ",top=" + top + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
	if(w)
		w.focus();
	return false; 
}

/*=============================================================================
    ADD MULTIPLE EVENTS
==============================================================================*/
function addEvent(obj, evType, fn)
{
	if(obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false); 
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	}
	else 
	{
		return false;
	}
}

function run()
{
	resizeDoc();
	animateHoverMenuTop();
	makePopUp();
	fixToolTip();
	switcher("news_list", "news_list_content", "li", 1); 
	switcher("search_forms_options", "search_forms", "form", 2);
	clickOnTheRow();
	window.setTimeout("tollBoxVisibility()", 5);
} 

addEvent(window, "load", run);

/* to del */
function debug(object)
{
	var t = "";
	for(var i in object)
		t+=i+" -> "+object[i]+"<br>";
	document.write(t);
}

