﻿(function(){
  /*Use Object Detection to detect IE6*/
  var  m = document.uniqueID /*IE*/
  && document.compatMode  /*>=IE6*/
  && !window.XMLHttpRequest /*<=IE6*/
  && document.execCommand ;
  try{
    if(!!m){
      m("BackgroundImageCache", false, true) /* = IE6 only */
    }
  }catch(oh){};
})();

function GetByID(id)
{
	return document.getElementById(id);
}
var loaded = false;
function activateTab(id, num, t_id, aload)
{
	var path = window.location.pathname;
	var hsh = window.location.hash;
	if(!hsh)
	{
		var hsh = getCookie('anchor');
		window.location.hash = hsh;
		setCookie('anchor', '');
	}

	if(aload && aload===true && !loaded)
	{
		if(path.substr(0,3)=="/ru") var ch_path = ["/ru"+path.substr(3), path.substr(3), "/en"+path.substr(3)];
		else if(path.substr(0,3)=="/en") var ch_path = ["/en"+path.substr(3), path.substr(3), "/ru"+path.substr(3)];
		else var ch_path = [path,"/ru"+path,"/en"+path];
		for(p in ch_path)
		{
			var r_cookie = getCookie(ch_path[p]);
			if(r_cookie && r_cookie>-1)
			{
				num = r_cookie;
				break;
			}
		}
	}

	var tab_c = GetByID(id);
	var tab_t_p = GetByID(t_id);
	if(tab_c && tab_t_p)
	{
		var tabs_t = tab_t_p.getElementsByTagName('A');
		var cnt = 0;
		if(hsh && hsh.length>0 && !loaded)
		{
			for(j=0;j<tabs_t.length;j++)
			{
				if(tabs_t[j].nodeName=='A' && tabs_t[j].href && tabs_t[j].href.indexOf(hsh)>-1)
				{
					num = j;
					break;
				}
			}
		}

		var tabs = tab_c.getElementsByTagName('DIV');
		if(!num) num = 0;
		var cnt = 0;
		for(i in tabs)
		{
			if(tabs[i].className=="tab")
			{
				if(cnt==num) tabs[i].style.display = "block";
				else tabs[i].style.display = "none";
				cnt++;
			}
		}

		var cnt = 0;
		for(j=0;j<tabs_t.length;j++)
		{
			if(tabs_t[j].nodeName=='A')
			{
				//alert(tabs_t[j]);
				if(cnt==num) {
					tabs_t[j].className = "sel";
					tabs_t[j].blur();
					setCookie(path, j);
				}
				else tabs_t[j].className = "";
				cnt++;
			}
		}
	}
	loaded = true;
	return false;
}

function SetStyles(obj, to, replace)
{
	obj.className = (replace ? to : obj.className+' '+to);
}

function UnSetStyles(obj, to)
{
	obj.className = obj.className.replace(to, '');
}

function SetEvents(/* what, from, to, replace */)
{
	var what = 'div';
	var from = 'item';
	var to = 'hs';
	var replace = false;
	if(what && from && (to || (!to && replace)))
	{
		var res = document.getElementsByTagName(what);
		for(i=0;i<res.length;i++)
		{
			if(res[i].className.indexOf(from+" ")>-1 || res[i].className.indexOf(" "+from)>-1 || (res[i].className.length==from.length && res[i].className==from))
			{
				var to_old = res[i].className;
				if(document.all)
				{
					res[i].onmouseover = function() { SetStyles(this, to, replace); };
					res[i].onmouseout = function() { UnSetStyles(this, (replace ? "" : " ")+to); };
				}
				else
				{
					res[i].setAttribute("onMouseOver", "SetStyles(this, '"+to+"', "+replace+")");
					res[i].setAttribute("onmouseout", "UnSetStyles(this, '"+(replace ? "" : " ")+to+"')");
				}
			}
		}
	}

	//var frms = frames['googleSearchFrame'];
	//frms.style.width = '100%';
	//frms.setAttribute("height", "");
}

function sForm(f_name)
{
	document[f_name].submit();
}

 // Установить куки
function setCookie(name, value) {
      var valueEscaped = escape(value);
      var expiresDate = new Date();
      expiresDate.setTime(expiresDate.getTime() + 365 * 24 * 60 * 60 * 1000); // срок - 1 год, но его можно изменить
      var expires = expiresDate.toGMTString();
      var newCookie = name + "=" + valueEscaped + "; path=/; expires=" + expires;
      if (valueEscaped.length <= 4000) document.cookie = newCookie + ";";
}

// Получить куки
function getCookie(name) {
      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));
} 

function CheckAnchors()
{
	setCookie('anchor', window.location.hash);
}
