var d = document;
var offsetfromcursorY=-7 // y offset of cursor
var offsetfromcursorX=-35 // x offset of cursor
var ie=d.all && !window.opera;
var ns6=d.getElementById && !d.all;
var curobj,op,overX,top;
function cursor(el)
{
	curobj=d.getElementById('cursor');
	op = 0.1;  
	curobj.style.opacity = op;
	//curobj.style.visibility="visible";
	el.onmousemove=curposition;
	appear();
}
function hide_cur(el)
{
	d.getElementById('cursor').style.visibility='hidden';
	el.onmousemove='';
}
function ietruebody()
{
	return (d.compatMode && d.compatMode!="BackCompat")? d.documentElement : d.body
}
function curposition(e)
{
	curobj.style.visibility="visible";
	var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
	var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
	curobj.style.left=curX+offsetfromcursorX+"px";
	curobj.style.top=curY+offsetfromcursorY+"px";
}
function appear()
{
	if(op < 1)
	{
		op += 0.1;
		curobj.style.opacity = op;
		setTimeout('appear()', 40);
	}
}

//-----------------------

function autoIframe(frame)
{
	try{
		frame.style.height="100px";
		innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
		objToResize = (frame.style) ? frame.style : frame;
		objToResize.height = (innerDoc.body.scrollHeight+20)+"px";
    }
    catch(err){
        alert (err.message);
		window.status = err.message;
    }
}

var i=0;
var a=0;
function fade_in()
{

	var fader=document.getElementById("fader");
	i=i+0.05;
	a=a+5;
	fader.style.opacity=i;
	fader.style.filter = "alpha(opacity="+a+")";
	if(i<0.50)
	{
		window.setTimeout(function() {fade_in("0")}, 30);
	}
	
}
function fade_out()
{

	var fader=document.getElementById("fader");
	i=i-0.05;
	a=a-5;
	fader.style.opacity=i;
	fader.style.filter = "alpha(opacity="+a+")";
	if(i>0.05)
	{
		window.setTimeout(function() {fade_out("0")}, 30);
	}
}


function ImageExpander(oThumb, sImgSrc)
{
	// store thumbnail image and overwrite its onclick handler.
	this.oThumb = oThumb;
	this.oThumb.expander = this;
	this.oThumb.onclick = function() { this.expander.expand(); }
	
	// record original size
	this.smallWidth = oThumb.offsetWidth;
	this.smallHeight = oThumb.offsetHeight;	
	
	var left = this.oThumb.offsetLeft;
	var top = this.oThumb.offsetTop;
	var parent = this.oThumb.offsetParent;
	while(parent && parent.tagName != "BODY")
	{
		left += parent.offsetLeft;
		top += parent.offsetTop;
		parent = parent.offsetParent;
	}
	this.coordx = left;
	this.coordy = top;

		
	this.bExpand = true;
	this.bTicks = false;
	

	// self organized list
	if ( !window.aImageExpanders )
	{
		window.aImageExpanders = new Array();
	}
	window.aImageExpanders.push(this);

	// create the full sized image.
	this.oImg = new Image();
	this.oImg.expander = this;
	this.oImg.onload = function(){this.expander.onload();}
	this.oImg.src = sImgSrc;
	
	var loading=document.getElementById("loading");
	loading.style.left=left+127+"px";
	loading.style.top=top+196+"px";
	loading.style.visibility="visible";

	var wind_height=document.body.clientHeight;
	if(wind_height<1250)
		wind_height=1250;
	var fader=document.getElementById("fader");
	fader.style.height=wind_height+"px";
	
}

ImageExpander.prototype.onload = function()
{
	this.oDiv = document.createElement("div");
	document.body.appendChild(this.oDiv);
	this.oDiv.appendChild(this.oImg);
	this.oDiv.style.position = "absolute";
	this.oDiv.style.border = "1px solid gray";
	this.oDiv.style.zIndex = 11;
	this.oDiv.expander = this;
	this.oImg.title = "кликните для уменьшения";
	this.bigWidth = this.oImg.width;
	this.bigHeight = this.oImg.height;
	
	if ( this.bExpand )
	{
		this.expand();
	}
	else
	{
		this.oDiv.style.visibility = "hidden";
		this.oImg.style.visibility = "hidden";
	}
}
ImageExpander.prototype.toggle = function()
{
	this.bExpand = false;
	this.oDiv.onclick = "";
	fade_out();
	
	
	if ( this.bExpand )
	{
		for ( var i in window.aImageExpanders )
			if ( window.aImageExpanders[i] !== this )
				window.aImageExpanders[i].reduce();
	}
}
ImageExpander.prototype.expand = function()
{
	var fader=document.getElementById("fader");
	fader.style.display="block";
	fade_in();

	// set direction of expansion.
	this.bExpand = true;

	// set all other images to reduce
	for ( var i in window.aImageExpanders )
		if ( window.aImageExpanders[i] !== this )
			window.aImageExpanders[i].reduce();
	

	// if not loaded, don't continue just yet
	if ( !this.oDiv ) return;
	// hide the thumbnail
	this.oThumb.style.visibility = "hidden";
	
	// calculate initial dimensions
	//this.x = this.oThumb.offsetLeft;
	//this.y = this.oThumb.offsetTop;
	
	this.x = this.coordx;
	this.y = this.coordy;
	
	this.w = this.oThumb.clientWidth;
	this.h = this.oThumb.clientHeight;
	
	this.oDiv.style.left = this.x + "px";
	this.oDiv.style.top = this.y + "px";
	this.oImg.style.width = this.w + "px";
	this.oImg.style.height = this.h + "px";
	this.oDiv.style.visibility = "visible";
	this.oImg.style.visibility = "visible";
	
	// start the animation engine.
	if ( !this.bTicks )
	{
		
		this.bTicks = true;
		var pThis = this;
		window.setTimeout(function(){pThis.tick();},25);	
	}
}
ImageExpander.prototype.reduce = function()
{
	// set direction of expansion.
	this.bExpand = false;
}
ImageExpander.prototype.tick = function()
{
	// calculate screen dimensions
	var cw = document.body.clientWidth;
	var ch = this.bigHeight;
	var cx = document.body.scrollLeft + cw / 2;
	var cy = 0 + ch / 2;
	
	// calculate target
	var tw,th,tx,ty;
	if ( this.bExpand )
	{
		tw = this.bigWidth;
		th = this.bigHeight;
		if ( tw > cw )
		{
			th *= cw / tw;
			tw = cw;
		}	
		if ( th > ch )
		{
			tw *= ch / th;
			tw *= ch / th;
			th = ch;
		}
		tx = cx - tw / 2;
		ty = cy - th / 2; 
	}
	else
	{
		tw = this.smallWidth-2;
		th = this.smallHeight-2;
		tx = this.coordx;
		ty = this.coordy;
		
	}	
	// move 5% closer to target
	var nHit = 0;
	var fMove = function(n,tn) 
	{
		var dn = tn - n;
		if ( Math.abs(dn) < 3 )
		{
			nHit++;
			return tn;
		}
		else
		{
			return n + dn / 3; // скорость
		}
	}
	this.x = fMove(this.x, tx);
	this.y = fMove(this.y, ty);
	this.w = fMove(this.w, tw);
	this.h = fMove(this.h, th);
	
	if(nHit==4)	
	{
		this.oDiv.onclick = function() {this.expander.toggle();};
	}	
		
	this.oDiv.style.left = this.x + "px";
	this.oDiv.style.top = this.y + "px";
	this.oImg.style.width = this.w + "px";
	this.oImg.style.height = this.h + "px";
	
	// if reducing and size/position is a match, stop the tick	
	if ( !this.bExpand && (nHit == 4) )
	{
		this.oImg.style.visibility = "hidden";
		this.oDiv.style.visibility = "hidden";
		this.oThumb.style.visibility = "visible";
		this.bTicks = false;
		this.oDiv.onclick = "";
		var loading=document.getElementById("loading");
		loading.style.visibility="hidden";
		var fader=document.getElementById("fader");
		fader.style.display="none";
	}
	
	if ( this.bTicks )
	{
		var pThis = this;
		window.setTimeout(function(){pThis.tick();},25);
	}
}
