var onOutMainMenu_delay = false; // setTimeout
var onOutMainMenu_delay_time = 2000; // milisecs

function placeFunctions()
{
	
	// Do fix only if its Internet explorer 
	if (/MSIE/.test(navigator.userAgent))
	{
		for (i = 2; i <= 6; i++)
		{
			var li = document.getElementById('nav'+i);
			try
			{
				li.onmouseover = function()
				{
					var num = this.id.split("v");
					num = num[1];
					
					if (num!=6)
					{
						document.getElementById("dropdown_"+num).style.left = '-40px';
					}
					
					// only for IE6
					if (/MSIE 6.0/.test(navigator.userAgent))
					{
						document.getElementById("dropdown_"+num).style.display = 'block';
					}
				};
				if (/MSIE 6.0/.test(navigator.userAgent))
				{
					li.onmouseout = function()
					{
						var num = this.id.split("v");
						num = num[1];
						document.getElementById("dropdown_"+num).style.display = 'none';
					};
				}
			}
			catch(err)
			{
			}
		}
		/*
		if (document.getElementById('co_quickMenu'))
		{
			var div = wcne('co_quickMenu', 'div', 'co_row');

			for (i = 0; i < div.length; i++)
			{
				if (co_hasClassName(div[i],'co_seperator'))
				{
					div[i].style.position = 'absolute';
					if (div[i+1])
					{
						co_addClassName(div[i+1],'co_postseperator');
					}
				}
			}
		}/**/
	}
	
	if (document.getElementById('co_bodyArea'))
	{
		var co_file = wcne('co_bodyArea', 'input', 'co_file');
		for (i = 0; i < co_file.length; i++)
		{
			co_file[i].size = 32;
		}
	}
	
	if (document.getElementById('co_pBillingCreditCardPaymentReceipt'))
	{
		document.getElementById('co_bodyArea').className += ' cPaymentReceipt';
		wcne('co_billingCreditCardPaymentReceipt', 'div', 'ccReceiptCompany')[1].style.display = 'none';
	}
	
	// console.log('('+$('co_navArea').innerHTML+')');	
	if (!$('co_navArea') || ($('co_navArea').innerHTML == '\n' || $('co_navArea').innerHTML == '' || $('co_navArea').innerHTML == '&nbsp;'))
	{
		$('co_contentArea').className = 'full';
		
		if ($('co_navArea') && ($('co_navArea').innerHTML == '\n' || $('co_navArea').innerHTML == '' || $('co_navArea').innerHTML == '&nbsp;'))
		{	
			$('co_navArea').remove();
		}
	}
	//else if($('co_navArea') && !(/MSIE 6.0/.test(navigator.userAgent)))
	else if($('co_navArea'))
	{
		var div = document.createElement('div');
		div.className = 'toggle_left_nav';
		
		var toggleoff = function()
		{
			$('co_navArea').addClassName('co_hidden');
			$('co_navArea').removeClassName('ontop');
			
			$('co_contentArea').className = 'hidednav';
			div.className = 'toggle_left_nav_off';
			
			createCookie('left_nav', 'hide', 1);
			
			Event.observe
			(
				div,
				"mouseover",
				function()
				{
					if ($('co_navArea').hasClassName('co_hidden'))
					{
						$('co_navArea').removeClassName('co_hidden');
						$('co_navArea').addClassName('ontop');
						
						Event.observe
						(
							$('co_navArea'),
							"mouseout",
							function(e)
							{
								onOutMainMenu(e, true);
							}
						);
						
						// aki //co_navArea //co_contentArea
						$('co_contentArea').style.minHeight = $('co_navArea').clientHeight+'px';
					}
				}
			);
			
			if ($('co_employees')) 
				$('co_employees').removeClassName('full_width');
			
			if (/MSIE 6.0/.test(navigator.userAgent))
						$('original_cd_nav').style.marginLeft = '52px';
						
			$('co_contentArea').style.minHeight = '10px';
		}
		
		
		if (readCookie('left_nav') == 'hide')
		{
			toggleoff();
		}
		else
		{
			if ($('co_employees')) 
				$('co_employees').addClassName('full_width');

			if (/MSIE 6.0/.test(navigator.userAgent))
				$('original_cd_nav').style.marginLeft = '-137px';	
		}

		Event.observe
		(
			div,
			"click",
			function()
			{
				if (div.className == 'toggle_left_nav')
				{
					toggleoff();
				}
				else
				{
					$('co_navArea').removeClassName('co_hidden');
					$('co_navArea').removeClassName('ontop');
					
					$('co_contentArea').className = '';
					div.className = 'toggle_left_nav';
					
					createCookie('left_nav', 'show', 1);
					
					div.stopObserving('mouseover');
					$('co_navArea').stopObserving('mouseout');
					
					if ($('co_employees')) 
						$('co_employees').addClassName('full_width');
					
					if (/MSIE 6.0/.test(navigator.userAgent))
						$('original_cd_nav').style.marginLeft = '-137px';
				}
			}
		);
		
		$('co_bodyArea').appendChild(div);
		
		
	}

}

function wcne (main, element, classname)
{
	var elements = Array();
	var obj = document.getElementById(main).getElementsByTagName(element); //


	for (i = 0; i < obj.length; i++)
	{
		if(co_hasClassName(obj[i], classname))
		{
			elements.push(obj[i])
		}
	}

	return elements;
}


function footerAdjustment(returning_value_only, only_minHeight_adjustment)
{
	var height = document.documentElement.clientHeight;
	/*alert(height);*/
	if (height) // && (!document.body.clientHeight || height > document.body.clientHeight )

	{
		if (!returning_value_only && document.getElementById('co_bodyArea'))
		{
			if (/MSIE/.test(navigator.userAgent))
			{
				document.getElementById('co_bodyArea').style.minHeight = (height-221)+'px';
				document.getElementById('co_bodyArea').style.paddingTop = '21px';
			}
			else
			{
				document.getElementById('co_bodyArea').style.minHeight = (height-206)+'px';
			}
		}
		else
		{
			return (height-206)+'px';
		}
	}
	
	if (only_minHeight_adjustment)
	{
		return true;
	}
	
	if ($('co_pLogin') || $('co_pForgotPassword'))
	{
		$('co_loginLinkArea').remove();
		//$('co_search').remove();
		$('co_menuArea').remove();
	}
	
	
}

function legendAdjustment()
{
	/*
	//if (!(/MSIE/.test(navigator.userAgent)))
	{
		var h2 = document.getElementsByTagName('h2');
		var len = h2.length;
		for (i=0; i<len; i++)
		{
			if (h2[i].innerHTML != '' && h2[i].parentNode.tagName.toLowerCase() == 'fieldset')
			{
				var span_content = document.createElement('span');
				span_content.className = 'h2content';
				span_content.appendChild(document.createTextNode(h2[i].innerHTML));
				
				//var span_end = document.createElement('span');
				//span_end.className = 'h2end';
				
				//alert(h2[i].innerHTML);
				h2[i].innerHTML = ''; //'<span class="h2content">'+h2[i].innerHTML+'</span><span class="h2end"></span>';
				
				h2[i].appendChild(span_content);
				//h2[i].appendChild(span_end);
				
				//alert(h2[i].innerHTML);
			}
		}
	}
	*/
}

function onOutMainMenu(e, left_nav) 
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	
	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
	var width = document.documentElement.clientWidth;
	
	if (left_nav)
	{
		var wMenu = 934;
		if (posx < (((width-wMenu)/2)) || posx > (((width-wMenu)/2)+159) || posy > ($('co_navArea').clientHeight+$('co_navArea').offsetTop-30) || posy < $('co_navArea').offsetTop)
		{
			$('co_navArea').addClassName('co_hidden');
			$('co_navArea').removeClassName('ontop');
			$('co_navArea').stopObserving('mouseout');
			$('co_contentArea').style.minHeight = '10px';
		}
	}
	else
	{
		var wMenu = 830;
	
		if (posx < ((width-wMenu)/2) || posx > (((width-wMenu)/2)+wMenu) || posy > 210 || posy < 152)
		{
			// console.log('out');
			onOutMainMenu_delay = setTimeout
			(
				function()
				{
					// console.log('out action');
					li = getMainOptionsMenu();
					
					for (i = 0; i<li.length; i++)
					{
						if (li[i].hasClassName('co_menuActive'))
						{
							li[i].addClassName('co_menuHover');
						}
						else
						{
							li[i].removeClassName('co_menuHover');
						}
					}
				},
				onOutMainMenu_delay_time
			);
		}
	}
}

function onOverMainMenu(e)
{
	// console.log('_ _ _ _ _ _');
	clearInterval(onOutMainMenu_delay);
}

function getMainOptionsMenu()
{
	var menu = document.getElementById('co_mainMenu');
	if (menu)
	{
		var li = menu.getElementsByTagName('li');
		var vli = Array();
		
		for (i = 0; i<li.length; i++)
		{
			if (li[i].parentNode == menu)
			{
				vli.push(li[i]);
			}
		}
		return vli;
	}
	return null;
}

function ibizMenuSettings()
{
	vli = getMainOptionsMenu();
	liHoverAction(vli);
	subMenusAdjustments(vli);
}

function liHoverAction(li)
{
	if (li)
	{
		for (i = 0; i<li.length; i++)
		{
			Event.observe
			(
				li[i],
				"mouseover",
				function()
				{
					for (i = 0; i<li.length; i++)
					{
						if (li[i] == this)
						{
							li[i].addClassName('co_menuHover');
						}
						else
						{
							li[i].removeClassName('co_menuHover');
						}
					}
				}
			);
		}
	}
	if (document.getElementById('co_menuArea'))
	{
		document.getElementById('co_menuArea').onmouseout = onOutMainMenu;
		document.getElementById('co_menuArea').onmouseover = onOverMainMenu;
	}
}

function subMenusAdjustments(li)
{
	if(li)
	{
		var wMenu = 966;
		var width = document.documentElement.clientWidth;
		//		alert(width);

		var left = (((width-wMenu)/2) < 18) ? 18: ((width-wMenu)/2);
	
		for (i = 0; i<li.length; i++)
		{
			var ul = li[i].getElementsByTagName('ul')[0];
			if (ul)
			{
				ul.style.left = left+'px';
			}
		}
	}
}

function createCookie (name, value, days) 
{
	if (days) 
	{
		var date = new Date ();
		date.setTime (date.getTime ()+ (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString ();
	}
	else 
	{
		var expires = "";
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie (name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split (';');
	for (var i=0; i < ca.length; ++ i) 
	{
		var c = ca[i];
		while (c.charAt(0) == ' ')
		{
			c = c.substring (1, c.length);
		}
		if (c.indexOf (nameEQ) == 0) 
		{	
			return c.substring (nameEQ.length, c.length);
		}
	}
	return null;
}

function eraseCookie (name) 
{
	createCookie (name, "", - 1);
}

window.onload = placeFunctions;
window.onresize = function()
	{
		
		footerAdjustment(false, true);
		vli = getMainOptionsMenu();
		subMenusAdjustments(vli);
		/**/
	};

