is_dom = document.getElementById //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
is_opera = is_opera5 = window.opera && is_dom //Opera 5+
is_opera6 = is_opera && window.print //Opera 6+
is_opera7 = is_opera && document.readyState //Opera 7+
is_ie = document.all && document.all.item && !is_opera //Microsoft Internet Explorer 4+
is_ie5 = is_dom && is_ie //MSIE 5+
is_n4 = document.layers //Netscape 4.*
is_gecko = is_dom && navigator.appName=="Netscape" //Mozilla или Netscape 6.*
is_ie7 = (is_ie) && (window.navigator.userAgent.indexOf("MSIE 7.0") != -1)

function show(id, hidetext, showtext)
{
	var AllTexts = $$('textpages_contents');
	var AllLabes = $$('textpages_lables');

	for (var i = 0; i < AllTexts.length; i++)
	{
		var item = AllTexts[i];
		if (item.id == 'sub' + id)
			continue;
		item.style.display = 'none';
		var label = AllLabes[i];
		label.innerHTML = showtext;

	}

	var span = $('text' + id);
	
	if (document.getElementById('sub' + id).style.display == 'none')
	{
		document.getElementById('sub' + id).style.display = 'block';
		span.innerHTML = hidetext;
	}
	else 
	{
		document.getElementById('sub' + id).style.display = 'none';
		span.innerHTML = showtext;
	}
}

function over(id)
{
	if (id == -1)
		return;
	
	document.getElementById('cl_' + id).className='clientsWindowHoverActive';
    //var win = document.getElementById('cl_' + id + '_mes');

	document.getElementById('cl_' + id + '_mes').style.display='block';
}

function out(id)
{
	if (id == -1)
		return;
	
	document.getElementById('cl_' + id).className='clientsWindowHover';
	document.getElementById('cl_' + id + '_mes').style.display='none';
}


function $(name)
{
	if (!name)
		return false;
	if (document.getElementById)
	{
		var obj = document.getElementById(name);
		if (!obj)
			return false;
		if (obj.getAttribute('id') != name)
			return false;
		return obj;
	}
	return false;
}
function $$(name)
{
	if (!name)
		return false;
	
	if (document.getElementsByName)
	{
		var objs = document.getElementsByName(name);
		
		if (!objs)
		{
			return false;
		}
		if (objs.length == 0)
		{
			// IE 6 & 7 could not find elements with names, that was created dynamicaly
			// for more info see http://msdn2.microsoft.com/en-us/library/ms534184.aspx
			if (is_ie)
			{
				var aElements = [];
				for (var i = 0; i < document.all.length; i++)
				{
					var el = document.all[i];
					var el_name = el.getAttribute('name');
					if (el_name)
					{
						if (el_name === name)
						{
							aElements.push(el);
						}
					}
				}
				if (aElements.length == 0)
					return false;
				else
					return aElements;
			}
			return false;
		}
		return objs;
	}
	return false;
}

/**
 * Add event to body onload
 * @param {Function} eL Fuction to run on body onLoad event
 */
function xAddMultiOnLoadEvent(eL)
{
	if (typeof window.onload == 'function')
	{
		var oldFunction = window.onload;
		window.onload = function()
		{
			oldFunction();
			eL();
		}
	}
	else
	{
		window.onload = eL;
	}
}
/**
 * Add event to Element el
 * @param {Object} e Element
 * @param {String} eT Event name without ON
 * @param {Function} eL Function to add
 * @param {Boolean} cap [Optional] If need to you capture
 */
function xAddEventListener(e,eT,eL,cap)
{
  if ((typeof e) == 'string')
  	e=$(e);
  if(!e)return;
  eT=eT.toLowerCase();
  if(e.addEventListener)e.addEventListener(eT,eL,cap||false);
  else if(e.attachEvent)e.attachEvent('on'+eT,eL);
  else {
    var o=e['on'+eT];
    e['on'+eT]=typeof o=='function' ? function(v){o(v);eL(v);} : eL;
  }
}

/**
 * Removes event from Element el
 * @param {Object} e Element
 * @param {String} eT Event name without ON
 * @param {Function} eL Function to add
 * @param {Boolean} cap [Optional] If need to you capture
 */
function xRemoveEventListener(e,eT,eL,cap)
{
  if ((typeof e) == 'string')
  	e=$(e);
  if(!e)return;
  eT=eT.toLowerCase();
  if(e.removeEventListener)e.removeEventListener(eT,eL,cap||false);
  else if(e.detachEvent)e.detachEvent('on'+eT,eL);
  else e['on'+eT]=null;
}

var Mover = null;
var aGallerySrc = [];
xAddMultiOnLoadEvent(
	function()
	{
		Mover = new Scroller();
		
		
		xAddEventListener($('unistockObject'), 'click', function() { location.href='?section=index'; }, false);
		/*if (typeof aGallery == 'undefined')
			return;
			
		if (aGallery.length > 0)
		{
			
			for (var i  = 0; i < aGallery.length; i++)
			{
				aGallerySrc[i] = new Image();
				aGallerySrc[i].src = aGallery[i];
			}
		}*/
	}
);

function xGetBounds(element)
{
	var left = element.offsetLeft;
	var top = element.offsetTop;
	for (var parent = element.offsetParent; parent; parent = parent.offsetParent) 
	{
		left += parent.offsetLeft;
		top += parent.offsetTop;
	}
	
	return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}


function Scroller(divID)
{
	this.div = $('scrollArea');
	this.touch = $('touchBar');
	this.bar = $('navBar');
	this.mouseDown = false;
	this.touchMaxRight = 10;
	this.touchMaxLeft = 208;
	this.currentAreaPos = 0;
	this.movePosition = 0;
	
	if (!this.div || !this.touch)
		return false;
	
	xAddEventListener(this.bar, 'mousedown', function(e) 
		{
			Mover.mouseDown = true;
//			e||(e=window.event);
			
			
		}
	, true);
	
	xAddEventListener(this.bar, 'mouseup', function() 
		{
			Mover.mouseDown = false;
		}
	, false);
	
	xAddEventListener(this.bar, 'mouseout', function(e) 
		{
			e||(e=window.event);
			
			e.target || (e.target = e.srcElement);
			
			if (e.target.getAttribute('id') != 'navBar')
				Mover.mouseDown = false;
		}
	, false);
	
	
	xAddEventListener(this.touch, 'mousedown', function(e) 
		{
			e||(e=window.event);
			
			if (Mover.movePosition == 0)
				Mover.movePosition = e.clientX;
		}
	, false);
		

	xAddEventListener(this.touch, 'mousemove', function(e) 
		{
			if (Mover.mouseDown == false)
				return;
			
			var TouchPosPx = 0;
			
			if (is_n4)
				TouchPosPx = Mover.touch.left;
			else if (is_ie)
				TouchPosPx = Mover.touch.style.pixelLeft;
			else
				TouchPosPx = parseInt(Mover.touch.style.left);
			
			e||(e=window.event);
			
			var newPos = Mover.touchMaxRight + e.clientX - Mover.movePosition;
						
			if (newPos < Mover.touchMaxRight || newPos > Mover.touchMaxLeft)
				return;
			
			if (is_ie)
				Mover.div.scrollLeft = Math.floor((newPos - Mover.touchMaxRight) * 1.26 * ArchiveItemCount);
			else
			{
				Mover.div.scrollLeft = Math.floor((newPos - Mover.touchMaxRight) * 1.14 * ArchiveItemCount);
			}
			if (is_n4)
				Mover.touch.left = newPos;
			else if (is_ie)
				Mover.touch.style.pixelLeft = newPos;
			else
				Mover.touch.style.left = newPos + "px";
		}
	, false);
	
	this.moveTo = function(Position, button)
	{
		if (is_ie7)
		{
			var offset_button = 292;
			if (button == 1)
				scrollLeft = 0;
			else if (button == 2)
				scrollLeft = offset_button;
			else if (button == 3)
				scrollLeft = offset_button * 2;
			else if (button == 4)
				scrollLeft = offset_button * 3;
		}
		else if (is_ie)
		{
			var offset_button = 312;
			if (button == 1)
				scrollLeft = 0;
			else if (button == 2)
				scrollLeft = offset_button;
			else if (button == 3)
				scrollLeft = offset_button * 2;
			else if (button == 4)
				scrollLeft = offset_button * 3;
		}
		else
		{
			var offset_button = 292;
			if (button == 1)
				scrollLeft = 0;
			else if (button == 2)
				scrollLeft = offset_button;
			else if (button == 3)
				scrollLeft = offset_button * 2;
			else if (button == 4)
				scrollLeft = offset_button * 3;
		}
		
		if (Mover.movePosition == 0)
			Mover.movePosition = xGetBounds($('touchBar')).left + (50/2);
		
//		alert(['moveTo function', Mover.movePosition]);
//		Mover.div.scrollLeft = Math.floor((Position - Mover.touchMaxRight) * 1.2 * ArchiveItemCount);
		Mover.div.scrollLeft = scrollLeft;
			
		if (is_n4)
			Mover.touch.left = Position;
		else if (is_ie)
			Mover.touch.style.pixelLeft = Position;
		else
			Mover.touch.style.left = Position + "px";
	}
}

var index = 0;
function closeImageDiv(DIV_ID)
{
	// close popup div with photo
	var addon = $(DIV_ID);
	if (addon)
	{
		document.body.removeChild(addon);
	}		
}
function quickImage(gallery_index, start_index)
{
	index = start_index;
	var IMG_ID = 'addon_popup_image_element';
	var DIV_ID = 'addon_popup_image';
	
	var aGallery = eval('aGallery_'+gallery_index)
	var imgSrc = aGallery[index];
	

	function appendAndFloat()
	{
		function updateDiv()
		{
			var div = $(DIV_ID);
			var img = $(IMG_ID);
			
			if (is_n4)
			{
				div.height = (img.height + 20 + 30);
				div.width = (img.width + 20);
			}
			else 
			{
				div.style.height = (img.height + 20 + 30) + "px";
				div.style.width = (img.width + 20) + "px";
			}
			img.style.display = 'block';
			var floatImage = new Float();
			floatImage.floatBy(5, div);
		}
		
		var div = document.createElement('div');
		div.style.cssText = 
		"height:		" + (img.height + 20 + 30) + "px;" + 
		"width:			" + (img.width + 20) + "px;" + 
		"display:		none;" + 
		"top:			10px;" +
		"z-index: 		450;" + 
		"background-color: white;" + 
		"border:		1px solid #C0C0C0;";
		
		div.setAttribute('id', DIV_ID);

		div.appendChild(img);
		
		var navBar = document.createElement('div');
		
		var prevA = document.createElement('a');
		prevA.style.cssText = 
			"cursor: 		pointer;" + 
			"position: 		absolute;" + 
			"padding:		2px 10px;" + 
			"border:		1px solid grey;" + 
			"left: 			10px;";
		
		xAddEventListener(prevA, 'click', function()
			{
				if (typeof aGallery[index - 1] == "undefined")
				{	
					//return;
					index = aGallery.length - 1;
					closeImageDiv(DIV_ID);
					quickImage(gallery_index, index);
				}
				else
				{
					index--;
					closeImageDiv(DIV_ID);					
					quickImage(gallery_index, index);
					/*
					$(IMG_ID).style.display = 'none';
					
					$(IMG_ID).src = aGallery[index];
					
					if ($(IMG_ID).height != 0)
						updateDiv();
					else
						$(IMG_ID).onload = updateDiv;*/
				}
			}, false);
			
		prevA.className = "links";
		//nextA.appendChild(document.createTextNode(' >> '));
		prevA.style.fontSize = "10pt";
		prevA.innerHTML = "&laquo"
		
		var nextA = document.createElement('a');
		nextA.style.cssText = 
			"cursor: 		pointer;" + 
			"position: 		absolute;" + 
			"border:		1px solid grey;" + 
			"padding:		2px 10px;" + 
			"right: 		10px;";
		
		xAddEventListener(nextA, 'click', function()
			{
				if (typeof aGallery[index + 1] == "undefined")
				{	
					//return;
					index = 0;
					closeImageDiv(DIV_ID);
					quickImage(gallery_index, index);
				}
				else
				{
					index++;
					closeImageDiv(DIV_ID);
					quickImage(gallery_index, index);
					/*
					var newImage = $(IMG_ID);
					newImage.src = aGallery[index];
					
					if (newImage.height != 0)
						updateDiv();
					else
						newImage.onload = updateDiv;*/
				}
			}, false);
			
		nextA.className = "links";
		//nextA.appendChild(document.createTextNode(' >> '));
		nextA.style.fontSize = "10pt";
		nextA.innerHTML = "&raquo"
		
		navBar.appendChild(prevA);
		navBar.appendChild(nextA);
		
		div.appendChild(navBar);
		document.body.appendChild(div);
		
		var floatImage = new Float();
		floatImage.floatBy(5, $(DIV_ID));
	}
	
	closeImageDiv(DIV_ID);
		
	var img = document.createElement('img');
	img.setAttribute('title', 'close image');
	img.style.cssText = 
		"cursor:		pointer;" + 
		"padding:		10px;";
	img.setAttribute('id', IMG_ID);
	
	img.src = imgSrc;
	
	xAddEventListener(img, 'click', function()
	{
		closeImageDiv(DIV_ID);
	}, false);
	
	if (img.height != 0)
		appendAndFloat();
	else
		img.onload = appendAndFloat;
}


function showMap(imgSrc)
{
	var IMG_ID = 'addon_popup_image';
	
	function closeImageDiv()
	{
		// close popup div with photo
		var addon = $(IMG_ID);
		if (addon)
		{
			if (img)
				img = null;

			document.body.removeChild(addon);
		}		
	}

	function appendAndFloat()
	{
		img.style.cssText = 
		"cursor:		pointer;" + 
		"height:		" + img.height + "px;" + 
		"width:			" + img.width + "px;" + 
		"display:		none;" + 
		"border:		3px solid #C0C0C0;";

		document.body.appendChild(img);
		
		var floatImage = new Float();
		floatImage.floatBy(5, $(IMG_ID));
	}
	
	closeImageDiv();
		
	var img = new Image();
	img.setAttribute('title', 'close image');
	img.setAttribute('id', IMG_ID);
	
	img.src = imgSrc;
	
	xAddEventListener(img, 'click', closeImageDiv, false);
	
	if (img.height != 0)
		appendAndFloat();
	else
		img.onload = appendAndFloat;
		
}
