var g_bScrollRunning = false;

function easeInOut(minValue, maxValue, totalSteps, actualStep,powr) 
{
	var delta = maxValue - minValue;
	var stepp = minValue + (Math.pow(((1 / totalSteps) * actualStep), powr) * delta);
	return Math.ceil(stepp)
}

function heightFolderExpand() 
{
    var elem = document.getElementById("cat_area");
    if(!elem.currentHeight) 
    {
        elem.currentHeight = 0;
    }
	doHeightChangeMem(
	    elem, 
	    elem.currentHeight, 
	    document.getElementById("photosets").offsetHeight, 
	    20, 
	    20, 
	    0.6);
}

function heightFolderRetract() 
{
    var elem = document.getElementById("cat_area");
    if(!elem.currentHeight) 
    {
        elem.currentHeight = document.getElementById("photosets").offsetHeight;
    }
	doHeightChangeMem(elem, elem.currentHeight, 0, 20, 20, 0.6);
}

function heightPhotoChange()
{
    var elem = document.getElementById("photo");
    if(!elem.currentHeight)
    {
        elem.currentHeight = elem.offsetHeight;
    }
        
    doHeightChangeMem(
        elem,
        elem.currentHeight,
        document.getElementById("photo_img").offsetHeight,
        20,
        20,
        0.6);
}

function doHeightChangeMem(elem, startHeight, endHeight, steps, intervals, powr) 
{
	if (elem.heightChangeMemInt) 
	{
	    window.clearInterval(elem.heightChangeMemInt);
	}
	
	var actStep = 0;
	elem.heightChangeMemInt = window.setInterval(
		function() 
		{
			elem.currentHeight = easeInOut(startHeight, endHeight, steps, actStep, powr);
			elem.style.height = elem.currentHeight + "px";
			actStep++;
			if(actStep > steps) 
			{
			    window.clearInterval(elem.heightChangeMemInt);
			}
		},
		intervals);
}

function findTopPosition(obj)
{
    var curTop = 0;
    
    if (obj.offsetParent)
    {
        do
        {
            curTop += obj.offsetTop;
        } 
        while (obj = obj.offsetParent);
    }
    
    return curTop;
}

function animateScroll(to, dest, steps, intervals, bDown)
{
    if(g_bScrollRunning)
    {
        return;
    }

    g_bScrollRunning = true;
    var timerId = setInterval(
        function()
        {
            if(!g_bScrollRunning || (bDown && to >= dest) || (!bDown && to <= dest)) 
            {
                clearInterval(timerId);
                g_bScrollRunning = false;                
                return;
            }
                        
            if(bDown)
            {
                to += steps;
            }
            else
            {
                to -= steps;
            }
            window.scrollTo(0, to);
        },
        intervals);
}

function fadeIn(obj, steps, intervals)
{
    var curOpacity = 0;
    var timerId = setInterval(
        function()
        {
            curOpacity += steps;
            if(curOpacity >= 100)
            {
                clearInterval(timerId);
                obj.style.opacity = 1;
                obj.style.filter = "alpha(opacity=" + 100 + ")";
                return;
            }
            else
            {
                obj.style.opacity = curOpacity / 100;
                obj.style.filter = "alpha(opacity=" + curOpacity + ")";
            }
        },
        intervals);
}
