//IE5+, Mozilla 1.0+, Opera 7+
//var logtxt = "";

function  getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight)
	{ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight)
	{ // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	}
	else
	{
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{
		pageWidth = windowWidth;
	}
	else
	{
		pageWidth = xScroll;
	}

	return [pageWidth,pageHeight,windowWidth,windowHeight];
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function elementPosition(element)
{
    var offsetLeft = 0, offsetTop = 0;
    do {
        offsetLeft += element.offsetLeft;
        offsetTop  += element.offsetTop;
    } while (element = element.offsetParent);
    return [offsetLeft, offsetTop];
}

// Линейная функция
function delta1(progress) {
    return progress;
}

// Степенная функция
function delta2(progress) {
    return Math.pow(progress, 2);
}

function delta2_ob(progress) {
     function d(progress) {
        return Math.pow(progress, 3);
     }
     return 1 - d(1 - progress);
}

// Функция Circ
function delta3(progress) {
    return 1 - Math.sin(Math.acos(progress));
}

function delta3_ob(progress) {
     function d(progress) {
        return 1 - Math.sin(Math.acos(progress));
     }
     return 1 - d(1 - progress);
}

// Функция Sine
function delta4(progress) {
    return 1 - Math.sin((1 - progress) * Math.PI/2);
}

// Функция Back
function delta5(progress, x) {
    return Math.pow(progress, 2) * ((x + 1) * progress - x);
}

function delta5_ob(progress, x) {
     function d(progress, x) {
        return Math.pow(progress, 2) * ((x + 1) * progress - x);
     }
     return 1 - d(1 - progress, x);
}

// Функция Bounce
function delta6(progress) {
     for(var a = 0, b = 1, result; 1; a += b, b /= 2) {
        if (progress >= (7 - 4 * a) / 11)
            return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2) + Math.pow(b, 2);
     }
}

// Функция Elastic
function delta7(progress, x) {
    return Math.pow(2,10 * (progress - 1)) * Math.cos(20 * progress * Math.PI * x / 3);
}

// Обратные функции
function delta6_ob(progress) {
     function d(progress) {
        for(var a = 0, b = 1, result; 1; a += b, b /= 2) {
           if (progress >= (7 - 4 * a) / 11)
               return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2) + Math.pow(b, 2);
        }
     }
     return 1 - d(1 - progress);
}

function picPreviewShow(panid, picid, picsrc, pw, ph)
{
	var splashobj = uh_get_object(panid + "splash");
	var panobj = uh_get_object(panid);
	var picobj = uh_get_object(picid);
	if( panobj && picobj && splashobj )
	{
		psize = getPageSize();

		//alert( psize[0] );

		//alert( document.documentElement.scrollTop );
		//alert( document.body.scrollTop );

		splashobj.style.width = psize[0];
		splashobj.style.height = psize[1];

		showItem(panid + "splash");

		browser_w = getClientWidth();
		browser_h = getClientHeight();

		//alert("h: " + browser_w + "; h: " + browser_h + "pw: " + pw + "; ph: " + ph);

		vert_off = 0;
		if( document.body.scrollTop )
		{			vert_off = document.body.scrollTop;		}

		pan_x = Math.round( (browser_w - pw)/2 );
		pan_y = Math.round( (browser_h - ph)/2 ) + vert_off;

		//alert("pan_x: " + pan_x + "; pan_y: " + pan_y);

		showItem(panid);

		panobj.style.left = pan_x + "px";
		panobj.style.top = pan_y + "px";

		pan_w = (26 + parseInt(pw));
		pan_h = (54 + parseInt(ph));

		//panobj.style.width = pan_w  + "px";
		panobj.style.width = 10  + "px";
		panobj.style.height = 10  + "px";

		picobj.src = picsrc;
		picobj.style.width = pw + "px";
		picobj.style.height = ph + "px";

		//////////////////////////////////////////////////
		var from = 0;			// Начальная координата X
		var to = 500;			// Конечная координата X
		var duration = 1000;	// Длительность - 1 секунда
		var start = new Date().getTime(); // Время старта
		var delay = 20;
		//////////////////////////////////////////////////

		//logtxt = "";

		//drawShadow('blockShadow', 2, 6, 10, 1, 1, pan_w, pan_h, pan_x, pan_y);

		//setTimeout("picPreviewResize('" + panid + "', 15, "+pan_w+", 10, "+pan_w+", "+pan_h+")", 50);
		//setTimeout("picPreviewResizeEx('" + panid + "', "+start+", "+duration+", "+pan_w+", 10, "+pan_w+", "+pan_h+", "+delay+")", delay);
		setTimeout("picPreviewResizeEx('" + panid + "', "+start+", "+duration+", 10, 10, "+pan_w+", "+pan_h+", "+delay+","+pan_x+","+pan_y+")", delay);
	}
}

function picPreviewResize(panid, steph, w, h, maxw, maxh)
{
	var panobj = uh_get_object(panid);
	if( panobj )
	{
		if( (steph + h) < maxh )
		{
			panobj.style.height = (steph+h) + "px";
			setTimeout("picPreviewResize('" + panid + "', "+steph+", "+w+", "+(steph+h)+", "+maxw+", "+maxh+")", 50);
		}
		else
		{
			panobj.style.height = maxh + "px";
		}
	}
}

function picPreviewResizeEx(panid, start_time, dur, w, h, maxw, maxh, animdelay, left, top)
{
	var panobj = uh_get_object(panid);
	if( panobj )
	{
		//alert(maxh);
		/////////////////////////////////////////////////////////////////
		var now = (new Date().getTime()) - start_time;	// Текущее время
		var progress = now / dur;						// Прогресс анимации

		//alert( progress );
		//logtxt = logtxt + "S1: " + progress + "; ";

		var result_w = (maxw - w) * delta3_ob(progress) + w;
		var result_h = (maxh - h) * delta3_ob(progress) + h;

		//element.style.left = result + "px";
		panobj.style.height = result_h + "px";
		panobj.style.width = result_w + "px";

		if (progress < 1) // Если анимация не закончилась, продолжаем
		{
		    setTimeout("picPreviewResizeEx('" + panid + "', "+start_time+", "+dur+", "+w+", "+h+", "+maxw+", "+maxh+", "+animdelay+","+left+","+top+")", animdelay);
		}
		else
		{
			panobj.style.height = maxh + "px";
			panobj.style.width = maxw + "px";

			drawShadow('blockShadow', 2, 6, 10, 1, 1, maxw, maxh, left, top);
			//alert(logtxt);
		}
		/////////////////////////////////////////////////////////////////
	}
}

function picPreviewClose(panid)
{
	hideShadow("blockShadow");

	hideItem(panid);
	hideItem(panid + "splash");
}

function getNameBrowser()
{
	var ua = navigator.userAgent.toLowerCase();
	// Определим Internet Explorer
	if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
		return "msie"
	}
	// Opera
	if (ua.indexOf("opera") != -1) {
		return "opera"
	}
	// Gecko = Mozilla + Firefox + Netscape
	if (ua.indexOf("gecko") != -1) {
		return "gecko";
	}
	// Safari, используется в MAC OS
	if (ua.indexOf("safari") != -1) {
		return "safari";
	}
	// Konqueror, используется в UNIX-системах
	if (ua.indexOf("konqueror") != -1) {
		return "konqueror";
	}
	// Netscape
	if (ua.indexOf("Netscape6/") != -1) {
    	return "ns";
  	}
	return "unknown";
}

function hideShadow(shadow_id)
{
	var shadow_obj = uh_get_object( shadow_id );
	if( !shadow_obj )
		return;

	shadow_obj.innerHTML = "";}

// stype: 1 - простая, 2 - сложная
// size in pixels [1..10] - Размер тени
// opacity in % [10..100] - Плотность тени
// displace: 1 - без смещения, 2 - вниз, 3 - вверх, 4 - влево, 5 - вправо
// strength in pixels [1..3] - сила смещения
// width - ширина блока
// height - высота блока
function drawShadow(shadow_id, stype, size, opacity, displace, strength, w, h, left, top)
{
	var browser = getNameBrowser();
	var shadow_obj = uh_get_object( shadow_id );
	if( !shadow_obj )
		return;

	shadowArea = size;
	shadowDepth = opacity;

	if (browser == "msie")
	{		shadowStep	= shadowDepth/shadowArea*1;
		shadowDepth	= shadowDepth*1;
	}
	else
	{
		shadowStep	= shadowDepth/shadowArea*0.01;
		shadowDepth	= shadowDepth*0.01;
	}

	shadow_obj.innerHTML = "";

	displace_x = 0;
	displace_y = 0;
	switch(displace)
	{
		case 1:
			displace_x=0;
			displace_y=0;
			break;

		case 2:
			displace_x=0;
			displace_y=1*strength;
			break;

		case 3:
			displace_x=0;
			displace_y=-1*strength;
			break;

		case 4:
			displace_x=-1*strength;
			displace_y=0;
			break;

		case 5:
			displace_x=1*strength;
			displace_y=0;
			break;
	}

	if( stype == 1 )
	{
		op = shadowStep;
		for(i=1; i<=shadowArea; ++i)
		{
			shadowWidth	= w + i*2;
			shadowHeight= h + i*2;

			shadowLeft	= left - i + displace_x;
			shadowTop	= top - i + displace_y;

			shadow_obj.innerHTML += "<div id='shadow_"+i+"' style='position:absolute; left:"+shadowLeft+"px; top:"+shadowTop+"px; width:"+shadowWidth+"px; height:"+shadowHeight+"px; background:#000000'>&nbsp;</div>";

			//if ((browser == "ns")||(browser == "opera"))	document.getElementById("shadow_"+i).style.opacity = op;
			if (browser == "msie")							document.getElementById("shadow_"+i).style.filter="alpha(opacity="+op+")"
			else											document.getElementById("shadow_"+i).style.opacity = op;
		}
	}

	if( stype == 2 )
	{
		for(i=1; i<=shadowArea; ++i)
		{
			for(j=1; j<=(shadowArea-i+1); ++j)
			{
				shadowWidth	= w + i*2;
				shadowHeight= h + j*2;
				shadowLeft	= left - i + displace_x;
				shadowTop	= top - j + displace_y;

				shadow_obj.innerHTML += "<div id='shadow_"+i+"_"+j+"' style='position:absolute; left:"+shadowLeft+"px; top:"+shadowTop+"px; width:"+shadowWidth+"px; height:"+shadowHeight+"px; background:#000000'>&nbsp;</div>";

				//if ((browser == "ns")||(browser == "opera")) {
				//	op = shadowStep;
				//	document.getElementById("shadow_"+i+"_"+j).style.opacity = op;
				//}
				if (browser == "msie")
				{
					op = Math.floor(shadowStep/2)+1;
					document.getElementById("shadow_"+i+"_"+j).style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity="+op+")";
				}
				else
				{
					op = shadowStep;
					document.getElementById("shadow_"+i+"_"+j).style.opacity = op;				}
			}
		}
	}
}