// JavaScript Document

try { document.domain = 'naunet.ru'; }
catch (e) {}

Event.observe(document, "mousemove", function(event) {
	if(state.menuShown == "") return true;
	if (Position.within($(state.menuShown), Event.pointerX(event), Event.pointerY(event)) || Position.within($(state.menuShown + "DDMenu"), Event.pointerX(event), Event.pointerY(event))) return true;
	else
	{
		ddHide(state.menuShown);
		return true;
	}
});

function ddHide (which)
{
	var dd = $(which + "DDMenu");
	//dd.style.overflowY = "hidden";
	//new Rico.Effect.Size(dd, 0, 1, 1, 1, {complete: function() { dd.style.display = "none"; }});	
	dd.hide();
	$(state.menuShown).style.backgroundImage = 'url(/i/mainMenu/bgBlack.gif)';
	state.menuShown = "";
}


function hilion(which)
{	
	var oldShown = state.menuShown;
	if (which == state.menuShown) return true;
	$(which).style.backgroundImage = 'url(/i/ddMenu/topBg.gif)';
	if ((which == "actions") || (which == "news")) return true;
	var dd = $(which + "DDMenu");
	var left = Position.cumulativeOffset($(which))[0] - 3;	
	var top  = Position.cumulativeOffset($(which))[1];
	dd.style.left = (left) + "px";
	dd.style.top  = top + 24 + "px";
	
	dd.style.display = "block";
	//var origHeight = dd.scrollHeight;
	//dd.style.height = "1px";
	//dd.style.overflowY= "hidden";
	
	if (oldShown != "")
	{
		ddHide(oldShown);
		$(oldShown).style.backgroundImage = 'url(/i/mainMenu/bgBlack.gif)';
	}
	
	//new Rico.Effect.Size(dd, 0, origHeight, 1, 1, {complete: function() { dd.style.overflowY = "visible"; }});
	state.menuShown = which;
}

function ssLogin()
{
	var sus = window.frames[0].document;
	$('sslogin').getElements().each(function(s) { sus.getElementById(s.id).value = s.value; });
	sus.getElementById('enter').submit();
}

function hilioff(which)
{
	if (state.menuShown == which) return true;
	$(which).style.backgroundImage = 'url(/i/mainMenu/bgBlack.gif)';
}

function pngload() 
{
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseFloat(navigator.appVersion) >= 4.0))
	{
		$('logo').style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/i/logo.png", sizingMethod="crop")';
		$('logo').style.backgroundImage = "none";
	}
}


function init()
{
	if (currentTabNumber == 0) currentTabNumber++;
	var accordionExample = {
		panelWidth:"100%",
		panelHeight:"auto",
		tabLocation:'inline',
		onLoadShowTab:currentTabNumber-1,
		expandedBg:"",
		collapsedBg:"",
		hoverBg:"",
		hoverTextColor:"#ffffff",
		collapsedTextColor:"#ffffff",
		borderColor:"#9ab2c8",
		expandedTextColor:"#ffffff",
		expandedFontWeight    : "normal",
		expandedBorderBottom  : "0 solid #9ab2c8",
		collapsedBorderBottom : "1px solid #9ab2c8",
		collapsedBorderLeft   : "3px solid #9ab2c8",
		expandedBorderLeft    : "3px solid #9ab2c8",
		duration: 40,
		fixborders:false		//look at this whe you apply padding
	}	
	
	if ($('accordionExample') != null ) new Rico.Accordion('accordionExample', accordionExample );
	
	pngload();	

	if (state.zsid != "") 
	{
		//$('sus').src = 'http://client.naunet.ru/n/' + state.zsid + '/site_test';
		$('sus').src = 'http://client.naunet.ru/n/site_test';
	}
}

  function getXMLHandler () {

    var xmlHandler = false;

    try { xmlHandler = new XMLHttpRequest (); }
    catch (e) {

      // the list of xmlHTTP versions
      var xmlHttpVersions = Array ( 'Msxml2.XMLHTTP.6.0',
                                    'Msxml2.XMLHTTP.5.0',
                                    'Msxml2.XMLHTTP.4.0',
                                    'Msxml2.XMLHTTP.3.0',
                                    'Msxml2.XMLHTTP',
                                    'Microsoft.XMLHTTP' );

      for ( var i = 0; i < xmlHttpVersions.length; i++ ) {

         try { xmlHandler = new ActiveXObject( xmlHttpVersions[ i ] ); }
         catch (e) {}

         if ( typeof( xmlHandler ) == 'object' ) break;
      }

      // last try
      if ( typeof( xmlHandler ) != 'object' ) {
        try { xmlHandler = window.createRequest(); }
        catch (e) {}
      }
    }

    return xmlHandler;
  }


function simpleRequest ()
{
    return Try.these(
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
      function() {return new ActiveXObject('Microsoft.XMLHTTP')},
      function() {return new XMLHttpRequest()}
    ) || false;
}

function postData(url, data)
{
	var xmlhttp = getXMLHandler(); // simpleRequest ();
	if (xmlhttp)
	{
		xmlhttp.open("POST", url, false);
		xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
                xmlhttp.send(data||'');
		return xmlhttp.responseText
	}
	else { return false; }
}	


function ssLogin()
{
	var sus = window.frames[0].document;
	$('sslogin').getElements().each(function(s) { sus.getElementById(s.id).value = s.value; });
	sus.getElementById('enter').submit();
	return false;
}

function ssLogout()
{
        doLogout();
	state.zsid = '';
	postData('/tools/session/', 'zsid=');	
	$('sus').src = 'http://client.naunet.ru/c/json-login';
	$('personalInformation').update("<table cellspacing='0' id='loginContainer'><tr><td class='left'><label for='login'>логин:</label></td><td style='width:63%; padding-right:4px;'><input type='text' name='login' id='login' /></td></tr><tr><td class='left'><label for='password'>пароль:</label></td><td style='padding-right:4px;'><input type='password' name='password' id='password' /></td></tr><tr><td><br /></td><td colspan='2'><input type='submit' value='войти' class='submit' id='loginSubmit' /></td></tr><tr><td colspan='2'><a href='https://client.naunet.ru/c/newclient2' target='_blank'>зарегистрироваться!</a></td></tr></table>");
}

function doLogout () {

  try {

    var frame = document.createElement( 'IFRAME' );

    frame.setAttribute( 'width', '0' );
    frame.setAttribute( 'height', '0' );
    frame.setAttribute( 'src', 'http://client.naunet.ru/d/' + state.zsid + '/logout_ajax?logoutSubmit=1' );

    frame.style.display = 'none';
    frame.style.visibility = 'hidden';

    document.body.appendChild( frame );
  }
  catch (e) {}
}

function isAuth()
{
try {
	document.domain = "naunet.ru";
	if(!state.zsid)
	{
		authCapture();
	}
	var sus = window.frames[0].document;
	if ((sus.getElementById('WRONG_PASSWORD') != null) && (sus.getElementById('WRONG_PASSWORD').innerHTML != ""))
	{
		$('modalBlock').style.display = 'block';
		$('error').style.display = 'block';
	}
	
	if ((sus.getElementById('zsid') != null) && (sus.getElementById('zsid').innerHTML != ""))
	{
		postData('/tools/session/', 'zsid=' + sus.getElementById('zsid').innerHTML);
		state.zsid = sus.getElementById('zsid').innerHTML;
		//$('sus').src = 'http://client.naunet.ru/n/' + state.zsid + '/site_test';
		$('sus').src = 'http://client.naunet.ru/n/site_test';
	}
	if (state.zsid)
	{
		$('personalInformation').update("<table cellspacing='0' class='topLogin'><tr><td style='text-align:center; color:#ffffff;'>Владелец договора:</td></tr><tr><td style='text-align:center; color:#ffffff;'>" + sus.getElementById('client').innerHTML + "(" + sus.getElementById('email').innerHTML.strip() + ")</td></tr><tr><td style='text-align:center;'><a href='javascript:void()' style=' color:#ffffff;' onclick='ssLogout()'>Выйти</a></td></tr></table>");
	}

	//if ($('sus').src == 'http://client.naunet.ru/n/' + state.zsid + '/site_test')
	if ($('sus').src == 'http://client.naunet.ru/n/site_test')
	{
		while (sus.getElementById('pin').innerHTML == "") { continue; }
		updateSSLinks();
	}
}
catch(e){}
}

function modalUnblock()
{
	$('modalBlock').style.display = 'none';
        if ( $('error') ) $('error').style.display = 'none';
        if ( $('success') ) $('success').style.display = 'none';
}

function updateSSLinks()
{
	var pin = window.frames[0].document.getElementById('pin').innerHTML;
	var links = document.links;
	var forms = document.forms;
	for(var i = 0; i < links.length; i++)
	{
		if (links[i].href.include("https://client.naunet.ru/c/login"))
		{
			//links[i].href = "https://client.naunet.ru/n/" + state.zsid + "/statp?any=" + pin;
			links[i].href = "https://client.naunet.ru/n/statp?any=" + pin;
		} 
		else if (links[i].href.include("https://client.naunet.ru"))
		{
			//links[i].href = links[i].href.sub("https://client.naunet.ru/n/", "https://client.naunet.ru/n/" + state.zsid + "/");	
			links[i].href = links[i].href.sub("https://client.naunet.ru/n/", "https://client.naunet.ru/n/");	
		}
	}	
	var any = document.getElementsByName('any');
	if (any != null) { for (var i = 0; i < any.length; i++) { any[i].value = pin; } }
	for(var i = 0; i < forms.length; i++)
	{
		if (forms[i].action.include("https://client.naunet.ru")) 
		{ 
			if (forms[i].action == "https://client.naunet.ru/c/login")
			{
				//forms[i].action = "https://client.naunet.ru/n/" + state.zsid + "/" + $('redirect' + i).value; 
				forms[i].action = "https://client.naunet.ru/n/" + $('redirect' + i).value; 
			}
			else
			{
				//forms[i].action = forms[i].action.sub("https://client.naunet.ru/n/", "https://client.naunet.ru/n/" + state.zsid ); 
				forms[i].action = forms[i].action.sub("https://client.naunet.ru/n/", "https://client.naunet.ru/n/"); 
			}
		}
	}
}

function imgZoom(which)
{
	Position.absolutize(which);
	var x = which.style.left;
	var y = which.style.top;
	Position.relativize(which);
	
	if (state.zoom != null) 
	{
		document.body.removeChild(state.zoom);
		state.zoom = null;
		return true;
	}
	var img = which.cloneNode(false);
	img.src = img.src.sub("thumb/", "full/");
	img.title = "Кликните, чтобы убрать увеличенное изображение";
	document.body.appendChild(img);
	img.style.zIndex = "100";
	img.style.position = "absolute";
	img.style.top = y;
	img.style.left = "20%";
	//img.scrollTo();
	state.zoom = img;
	img.onclick = function () { document.body.removeChild(this); state.zoom = null; }
}

function authCapture ()
{

	var links = document.links;
	var type = "generic";
	if (document.location.toString().include("order")) type = "order";
	for(var i = 0; i < links.length; i++)
	{
		if ( links[i].href.include( 'https://client.naunet.ru/n/' ) )
		{ 
			links[i].href = "https://client.naunet.ru/?redirect=" + links[i].href.sub("https://client.naunet.ru/n/", "") + "&nointredirect=1";
		}
	}
	var forms = document.forms;
	for(var i = 0; i < forms.length; i++)
	{
		if ( forms[i].action.include( 'https://client.naunet.ru/n/' ) )
		{
			new Insertion.Top(forms[i], "<input type='hidden' name='type' value='" + type +"'><input type='hidden' id='redirect" + i + "' name='redirect' value='" + forms[i].action.sub("https://client.naunet.ru/n/", "") + "'>");
			forms[i].action = "https://client.naunet.ru/c/login";  
			forms[i].innerHTML = forms[i].innerHTML.sub("/n//", "");
			new Insertion.Bottom(forms[i], "<input type='hidden' name='nointredirect' value='1'>");
		}
	}
}
//////////////////////////////////////////////////////////////////////////////





