function SetActiveTab(tab)
{
	var listElement = tab.parentNode.parentNode;
	var arTabs = listElement.getElementsByTagName("LI");

	//Hide
	for (var i = 0; i < arTabs.length; i++)
	{
		arTabs[i].className = "";
		var tabBody = document.getElementById(arTabs[i].id + "-body");
		if (tabBody)
			tabBody.style.display = "none";
	}

	//Show
	var tabBody = document.getElementById(tab.parentNode.id + "-body");
	if (tabBody)
	{
		tab.parentNode.className = "active";
		tab.blur();
		tabBody.style.display = "block";
	}
}

function ShowSwf(sSwfPath, width1, height1)
{
	var scroll = 'no';
	var top=0, left=0;

	if(width1 > screen.width-10 || height1 > screen.height-28)
		scroll = 'yes';

	if(height1 < screen.height-28)
		top = Math.floor((screen.height - height1)/2-14);

	if(width1 < screen.width-10)
		left = Math.floor((screen.width - width1)/2);

	width = Math.min(width1, screen.width-10);  
	height = Math.min(height1, screen.height-28); 

	window.open('/bitrix/tools/swfpg.php?width='+width1+'&height='+height1+'&img='+sSwfPath,'','scrollbars='+scroll+',resizable=yes, width='+width+',height='+height+',left='+left+',top='+top);
}

function ShowImg(sImgPath, width, height)
{
	var scroll = 'no';
	var top=0, left=0;

	if(width > screen.width-10 || height > screen.height-28)
		scroll = 'yes';

	if(height < screen.height-28)
		top = Math.floor((screen.height - height)/2-14);

	if(width < screen.width-10)
		left = Math.floor((screen.width - width)/2);

	width = Math.min(width, screen.width-10); 
	height = Math.min(height, screen.height-28);  
	window.open('/bitrix/tools/imagepg.php?img='+sImgPath,'','scrollbars='+scroll+',resizable=yes,width='+width+',height='+height+',left='+left+',top='+top);
}

function SetPrintCSS(isPrint)
{
	var link;

	if (document.getElementsByTagName)
		link = document.getElementsByTagName('link');
	else if (document.all)
		link = document.all.tags('link');
	else
		return;

	for (var index=0; index < link.length; index++)
	{
		if (link[index].title != 'print')
			continue;

		if (isPrint)
		{
			link[index].disabled = false;
			link[index].rel = "stylesheet";
		}
		else
		{
			link[index].disabled = true;
			link[index].rel = "alternate stylesheet";
		}
	}
}

var jsPublicForms =
{
	SetFieldValue: function(id, value)
	{
		var field = document.getElementById(id);
		if(field)
		{
			field.value = value;
		}
	}
}

var jsPublicFilters =
{
	SetCookie: function(Name, Value, ArrName)
	{
		if(ArrName)
			document.cookie = ArrName + "[" + Name + "]=" + Value + "; path=/;";
		else
			document.cookie = Name + "=" + Value + "; path=/;";
	},
	
	ShowHide: function(id, action)
	{
		var filter = document.getElementById(id);
		if(filter)
		{
			if(filter.style.display == "none" || "show" == action)
			{
				filter.style.display = "block";
				jsPublicFilters.SetCookie(id,"on","PublicFilters");
			}
			else
			{
				filter.style.display = "none";
				jsPublicFilters.SetCookie(id,"off","PublicFilters");
			}
		}
	},
	
	GetCookie: function (Name, ArrName) 
	{
		if(ArrName)
			var prefix = ArrName + "[" + Name + "]=";
		else
			var prefix = Name + "=";
		var cookieStartIndex = document.cookie.indexOf(prefix);
		if (cookieStartIndex == -1) 
			return null;
		var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
		if (cookieEndIndex == -1) 
			cookieEndIndex = document.cookie.length;
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	}
}

var jsPublicTabController =
{
	SetTabCookieId: function(TabSec, TabId)
	{
		document.cookie = "BxTabs[" + TabSec + "]=" + TabId + "; path=/;";
	},
	
	SetTabCookieIdUnload: function()
	{
		//no more then 2 tablists
		for(i=0;i<2;i++)
		{
			var tab_ul = document.getElementById('tab-list-id'+i);
			if(tab_ul)
			{
				tab_li = tab_ul.getElementsByTagName('LI');
				for(j=0;j<tab_li.length;j++)
				{
					if(tab_li[j].className == 'active')
					{
						tab_a = tab_li[j].getElementsByTagName('A');
						tab_a[0].onclick();
					}
				}
			}
			else
				break;
		}
	},

	TabToTabAnchor: function(TabId)
	{
		tab = document.getElementById(TabId);
		if(tab)
		{
			tab_a = tab.getElementsByTagName('A');
			tab_a[0].onclick();
			SetActiveTab(tab_a[0]);
		}
	}
}

window.onbeforeunload = jsPublicTabController.SetTabCookieIdUnload;

//change load window
if (window.jsAjaxUtil)
{
	// show ajax visuality
	jsAjaxUtil.ShowLocalWaitWindow = function (TID, cont, bShadow)
	{
		if (typeof cont == 'string' || typeof cont == 'object' && cont.constructor == String)
			var obContainerNode = document.getElementById(cont);
		else
			var obContainerNode = cont;
		
		if (obContainerNode.getBoundingClientRect)
		{
			var obRect = obContainerNode.getBoundingClientRect();
			var obWndSize = jsAjaxUtil.GetWindowSize();

			var arContainerPos = {
				left: obRect.left + obWndSize.scrollLeft, 
				top: obRect.top + obWndSize.scrollTop, 
				right: obRect.right + obWndSize.scrollLeft, 
				bottom: obRect.bottom + obWndSize.scrollTop
			};
		}
		else
			var arContainerPos = jsAjaxUtil.GetRealPos(obContainerNode);
		
		var container_id = obContainerNode.id;
		
		if (!arContainerPos) return;
		
		if (null == bShadow) bShadow = true;
		
		if (bShadow)
		{
			var obWaitShadow = document.body.appendChild(document.createElement('DIV'));
			obWaitShadow.id = 'waitshadow_' + container_id + '_' + TID;
			obWaitShadow.className = 'waitwindowlocalshadow';
			obWaitShadow.style.top = (arContainerPos.top - 5) + 'px';
			obWaitShadow.style.left = (arContainerPos.left - 5) + 'px';
			obWaitShadow.style.height = (arContainerPos.bottom - arContainerPos.top + 10) + 'px';
			obWaitShadow.style.width = (arContainerPos.right - arContainerPos.left + 10) + 'px';
		}
		
		var obWaitMessage = document.body.appendChild(document.createElement('DIV'));
		obWaitMessage.id = 'wait_' + container_id + '_' + TID;
		obWaitMessage.className = 'waitwindow';
		
		var div_top = arContainerPos.top + 5;
		if (div_top < document.body.scrollTop) div_top = document.body.scrollTop + 5;
		
		obWaitMessage.style.top = div_top + 'px';
		obWaitMessage.style.left = (arContainerPos.left + 5) + 'px';
		obWaitMessage.innerHTML = 'Загрузка...';
		
		if(jsAjaxUtil.IsIE())
		{
			var frame = document.createElement("IFRAME");
			frame.src = "javascript:''";
			frame.id = 'waitframe_' + container_id + '_' + TID;
			frame.className = "waitwindow";
			frame.style.width = obWaitMessage.offsetWidth + "px";
			frame.style.height = obWaitMessage.offsetHeight + "px";
			frame.style.left = obWaitMessage.style.left;
			frame.style.top = obWaitMessage.style.top;
			document.body.appendChild(frame);
		}
		
		function __Close(e)
		{
			if (!e) e = window.event
			if (!e) return;
			if (e.keyCode == 27)
			{
				jsAjaxUtil.CloseLocalWaitWindow(TID, cont);
				jsEvent.removeEvent(document, 'keypress', __Close);
			}
		}
		
		jsEvent.addEvent(document, 'keypress', __Close);
	}
}



if (document.location.hash == '#print')
	SetPrintCSS(true); 

if (document.location.hash.indexOf("#tab-") != -1)
{
	var selectedTabID = document.location.hash.substr(5,document.location.hash.length-10);
	window.onload = function() 
	{
		var tab = document.getElementById("tab-" + selectedTabID);

		if (tab && tab.childNodes[0])
			SetActiveTab(tab.childNodes[0]);
	};
}



/* banner - KP tmp */
function CPublicRotateBanner(items, params)
{
	this.Inited = false;
	this.ItemList = items;	

	this.AutoRotation = params.autorotate ? true : false;
	this.CurrentBunner = (params.current && this.ItemList.length >= params.current) ? params.current : 1;

	this.ActiveObj = params.aObj ? document.getElementById(params.aObj) : null;
	this.ListObj = params.lObj ? document.getElementById(params.lObj) : null;
	this.RotateTime = params.rTime ? params.rTime : 1000;
	
	if(this.ItemList.length>0 && this.ActiveObj && this.ListObj)
		this.Inited = true;

	this.ChangeBanner(this.CurrentBunner);
}

CPublicRotateBanner.prototype.ChangeBanner = function(id, handle)
{	
	if(!id || id<=0 || id>this.ItemList.length || !this.Inited)
		return;
		
	if(handle===true)
		this.AutoRotation = false;

	ExCurrent = this.CurrentBunner;
	this.CurrentBunner = id;
	
	Citem = this.ItemList[id-1];
	if(Citem)
	{
		imgs = this.ActiveObj.getElementsByTagName('IMG');
		btns = this.ListObj.getElementsByTagName('LI');

		if(imgs && btns[id-1])
		{
			imgs[0].src = Citem.image;
			this.ActiveObj.href = Citem.link;
			
			btns[ExCurrent-1].className = "";
			btns[this.CurrentBunner-1].className = "item-selected";
			
			if(Citem.functions)
			{
				for(i = 0; i<Citem.functions.length; i++)
				{
					if(Citem.functions[i].func && Citem.functions[i].arg && typeof(Citem.functions[i].func) == 'function')
						Citem.functions[i].func.apply(null, Citem.functions[i].arg);
				}
			}
		}
	}
}

CPublicRotateBanner.prototype.Rotation = function(start)
{
	if(!this.Inited || !this.AutoRotation)
		return;
	
	if(!start)
		this.ChangeBanner((this.CurrentBunner + 1) > this.ItemList.length ? 1 : (this.CurrentBunner + 1));
	
	_this = this;
	setTimeout(function(){_this.Rotation()}, this.RotateTime);
}

CPublicRotateBanner.prototype.PreLoad = function(skip)
{
	for (var imageIndex = 0; imageIndex < this.ItemList.length; imageIndex++)
	{
		if(skip!=imageIndex)
		{
			var imageObj = new Image();
			imageObj.src = this.ItemList[imageIndex].image;
		}
	}	
}
