// Ajax
function prepareRequest() 
{
	var tran = null;
	try { tran = new XMLHttpRequest(); }
	catch(e) { tran = null; }
	try { if(!tran) tran = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) { tran = null; }
	try { if(!tran) tran = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch(e) { tran = null; }
	return tran;
}

function makeRequest(url,action,params) 
{
	http_request = prepareRequest();
	
	http_request.onreadystatechange = function() 
	{
		if (action && http_request.readyState == 4 && http_request.status == 200) 
		{
			var data = document.createElement("div");
			data.innerHTML = http_request.responseText;	
			action.appendChild(data);
		}
	}
	http_request.open('POST', url);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=windows-1251');
	http_request.send(params);
}


// Client Sizes
function ClientWidth() {
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function ClientHeight() {
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}	

function ScrollLeft() {
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.scrollLeft:document.body.scrollLeft;
}

function ScrollTop() {
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.scrollTop:document.body.scrollTop;
}	

function loadImage(zone,mimg,img,imgWidth,imgHeight) {
	
	cwidth=ClientWidth();
	cheight=ClientHeight();
	
	/*
	widthPrecent=0;
	if (imgWidth>cwidth) {
		rzc=(imgWidth-cwidth)+50;
		prc=imgWidth/100;
		widthPrecent=Math.ceil(rzc/prc);
	}

	heightPrecent=0;
	if (imgHeight>cheight) {
		rzc=(imgHeight-cheight)+50;
		prc=imgHeight/100;
		heightPrecent=Math.ceil(rzc/prc);
	}

	if (widthPrecent>0 || heightPrecent>0) {
		if (widthPrecent>heightPrecent) {
			prec=widthPrecent;
		} else {
			prec=heightPrecent;
		}
		
		imgWidth=Math.ceil(imgWidth-((imgWidth/100)*prec));
		imgHeight=Math.ceil(imgHeight-((imgHeight/100)*prec));
	}	
	*/
	
	resLeft=((cwidth-imgWidth)/2)+ScrollLeft();
	resTop=((cheight-imgHeight)/2)+ScrollTop();

	// add 13-11-09
	if (resLeft<0) resLeft=0;
	if (resTop<0) resTop=0;
	
	
	box=document.getElementById(zone);
	
	box.style.position='absolute';
	box.style.top=resTop+'px';
	box.style.left=resLeft+'px';
	box.style.width=imgWidth;
	box.style.height=imgHeight;
	box.style.display="block";
	box.innerHTML='';
	
	
	var close = document.createElement("a");
	close.href="javascript:void(0)";
	close.className="close";
	close.innerHTML="Закрыть";
	close.onclick = function() { 
		box.style.display='none';
		box.innerHTML='';
	}	
	
	
	data='';
	if (mimg.nodeName=='A')
	{
		simg = mimg.getElementsByTagName('img')[0].alt;
		if (simg) data=simg; 
	}
	else
	{
		data=mimg;
	}
	
	
	
		
	image=document.createElement("img");	
	image.src=img;
	image.width=imgWidth;
	image.height=imgHeight;
	image.style.display='none';
	image.alt=data;
	
	
	var comm = document.createElement("div");			
	comm.style.textAlign='left';
	comm.style.width=(imgWidth-10)+'px';
	comm.innerHTML=data;
	
	
	checkimage(image);
	
	box.appendChild(close);
	box.appendChild(image);
	box.appendChild(comm);
}


function checkimage() {
	if(image.complete)
		image.style.display = 'block';
	else 
		setTimeout("checkimage()",100);
}



function loadMiniWin(id) {
	
	if (id > 0) 
	{
		var window = document.getElementById('mini_win');

		window.innerHTML='';
		window.style.display='block';
		window.appendChild(closeButton(window));	
		
		makeRequest('/ajax.php',window,'action=gloss&id='+id);
		
		resLeft=((ClientWidth()-window.clientWidth)/2)+ScrollLeft();
		resTop=((ClientHeight()-window.clientHeight)/2)+ScrollTop();
		
		window.style.left=resLeft+'px';
		window.style.top=resTop+'px';
	}
}

function closeButton(window) 
{
	var close = document.createElement("a");
	close.href="javascript:void(0)";
	close.className="close";
	close.innerHTML="Закрыть";
	close.onclick = function() { 
		window.style.display='none';
		window.innerHTML='';
	}
	
	return close;
}