// Copyright (c) 2007 Vilesse Ltd. All Rights Reserved.
// http://www.vilesse.com

function EventBinder (elements, eventName, eventFunction, elemClassName)
{
	for (var i = 0, len = elements.length; i < len; ++i)
	{
		if (!elemClassName || elements [i].className.search (elemClassName) != -1)
		{
			elements [i] [eventName] = eventFunction;
		}
	}
}

function RemoveFocusSelection ()
{
	var linksElements = document.getElementsByTagName ('a');
	EventBinder
	(
		linksElements,
		'onfocus',
		function ()
		{
			this.blur()
		},
		/nofocus/
	);
}

function BindOpenInNewWindowClass ()
{
	// We want to centralize 'new window' behavior here, instead of 'target' attribute in links on each page
	var linksElements = document.getElementsByTagName ('a');
	EventBinder
	(
		linksElements,
		'onclick',
		function ()
		{
			window.open (this.href);
			return false;
		},
		/new_window/
	);
}

var OnLoadIE = function ()
{
	var liElements = document.getElementById ('menu_inner').getElementsByTagName ('li');

	EventBinder
	(
		liElements,
		'onmouseover',
		function ()
		{
			this.className += ' jshover';
		}
	);

	EventBinder
	(
		liElements,
		'onmouseout',
		function ()
		{
			this.className = this.className.replace (/ jshover/, '');
		}
	);

	RemoveFocusSelection ();
}

function DetectBrowserType ()
{
	var userAgent = navigator.userAgent.toLowerCase();

	if (userAgent.indexOf ('opera') != -1) return 'Opera';
	if (userAgent.indexOf ('firefox') != -1) return 'Firefox';
	if (userAgent.indexOf ('safari') != -1) return 'Safari';
	if (userAgent.indexOf ('netscape') != -1) return 'Netscape';
	if (userAgent.indexOf ('mozilla/5.0') != -1) return 'Mozilla';

	var msieIndex = userAgent.indexOf ('msie');
	if (msieIndex > 0)
	{
		return 'IE' + parseInt (userAgent.substring (msieIndex + 5, userAgent.indexOf ('.', msieIndex)));
	}

	return navigator.userAgent;
}

function OnClickDetails (obj)
{
	var hEls = document.getElementById ('d' + obj.id);

	if (hEls.style.display == 'block')
	{
			hEls.style.display = 'none';
			obj.innerHTML = detailsShow;
	}
	else
	{
			hEls.style.display = 'block';
			obj.innerHTML = detailsHide;
	}
}

function InitializeDetails ()
{
	var linksElements = document.getElementsByTagName ('a');
	EventBinder
	(
		linksElements,
		'onclick',
		function ()
		{
			OnClickDetails (this);
		},
		/details_link/
	);
}

function AdjustHeight ()
{
	var browser = DetectBrowserType ();
	
	if (browser == 'IE6' || browser == 'IE5')
	{
		document.getElementById ('content').style.height = '750px';
	}
	else if (browser == 'Opera')
	{
		// Opera doesn't support overflow-y:scroll style but we have to force vertical scrollbar somehow
		document.body.style.height = (screen.height - 200) + 'px';
	}
}

function PreloadImage (imageName)
{
	var image = new Image ();
	image.src = imageName;
}

function FixCompatibilityIssues ()
{
	switch (DetectBrowserType ())
	{
		case 'IE5':
		case 'IE6':
		case 'IE7':
			// IE doesn't support hover for all elements
			window.attachEvent ('onload', OnLoadIE);
		break;

		case 'Opera':
			// Preloading logo on white background to prevent printing problems (Opera doesn't load images with display:none)
			PreloadImage ('/images/logo_wb.gif');
		break;
	}
}

function InitializePage ()
{
	InitializeDetails ();
	FixCompatibilityIssues ();
	BindOpenInNewWindowClass ();
}

function GoToSection (section)
{
	// Using AJAX to test whether the requested section exists. If it does - redirect user.

	if (section == '')
	{
		alert (msgSpecifySection);
		return;
	}
	
	function GetSectionFromName (section_name)
	{
		return '/clients/' + section_name;
	}
	
	function RedirectClient (section_name)
	{
		window.location.href = GetSectionFromName (section_name);
	}
	
	var xhrq;
	
	if (window.XMLHttpRequest)
	{ 
		xhrq = new XMLHttpRequest ();
	}
	else if (window.ActiveXObject)
	{
		// IE ActiveX
		xhrq = new ActiveXObject ('Microsoft.XMLHTTP');
	}
	
	if (xhrq)
	{
		// Supported by browser
		xhrq.onreadystatechange = function ()
			{
				if (xhrq.readyState == 4)
				{
					if (xhrq.status == 404)
					{
						alert (msgNoSection.replace (/%%section%%/, section));
						return;
					}
					else
					{
						RedirectClient (section);
					}
				}
			}

		xhrq.open('GET', GetSectionFromName (section), true);
		xhrq.send(null);
	}
	else
	{
		// Not supported, just trying it blindly
		RedirectClient (section);
	}
}

