/*=============================================================================
    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 );
			
		}
		
	}
}
/*=============================================================================
    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()
{
	switcher("search_forms_options", "search_forms", "form", 2);
} 

addEvent(window, "load", run);

/* to del */
function debug(object)
{
	var t = "";
	for(var i in object)
		t+=i+" -> "+object[i]+"<br>";
	document.write(t);
}