// Copyright (c) 2008, Волынцев Павел ( pavel_volyntsev@mail.ru )
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/*
	Скрипт для анимации блоков текста с картинкой
		Блок должен состоять из картинки и трёх слоёв текста
	При наведении и удержании мыши на области, изменяется размер и адрес картинки, а тексты заменяют друг друга
	Аналогично при отводе указателя с блока
*/

var debug = false; // включение отладки
var debugObj = '' // ID области для вывода отладки
var ResizeSteps = 10 // Количество шагов при анимации
var ResizableBoxes =  { } // Массив описаний блоков

var grow_timeout = 500 // таймаут роста при наведении мыши
var shrink_timeout = 500 // таймаут сжатия при уходе мыши
var regrow_timeout = 10000 // таймаут продолжения роста, если мышь не двигалась
var reshrink_timeout = 250 // таймаут продолжения сжатия, если мышь не двигалась

function dbg(str)
{
	if (debug && null!=$(debugObj)) $(debugObj).innerText = str + "\n" + $(debugObj).innerText
}

function Box_over(boxid)
{
	ResizableBoxes[boxid].state = "over"
	dbg(boxid + " over: inprogress=" + (ResizableBoxes[boxid].inprogress||false))

	if (null!=ResizableBoxes[boxid].timer) { clearTimeout(ResizableBoxes[boxid].timer); ResizableBoxes[boxid].timer = null; }
	ResizableBoxes[boxid].timer = setTimeout("Box_checkState('" + boxid +"','over')",grow_timeout);
}

function Box_out(boxid)
{
	ResizableBoxes[boxid].state = "out"
	dbg(boxid + " out : inprogress=" + (ResizableBoxes[boxid].inprogress||false))

	if (null!=ResizableBoxes[boxid].timer) { clearTimeout(ResizableBoxes[boxid].timer); ResizableBoxes[boxid].timer = null; }
	ResizableBoxes[boxid].timer = setTimeout("Box_checkState('" + boxid +"','out')",shrink_timeout);
}

function Box_checkOver(boxid)
{
	dbg("Box_checkOver('" + boxid + "')")
	var cur = ResizableBoxes[boxid].size
	if (ResizableBoxes[boxid].state=="over" && (cur == "little" || cur == "middle"))
	{
		var next = cur == "little" ? "middle" : "big"
		if (null != ResizableBoxes[boxid][next])
		{ 
			ResizableBoxes[boxid].inprogress = true;
			dbg(boxid + " - grow starts, inprogress=true")
			Box_grow(boxid,cur,next,0);
		}
	}
}

function Box_checkOut(boxid)
{
	dbg("Box_checkOut('" + boxid + "')")
	var cur = ResizableBoxes[boxid].size
	if (ResizableBoxes[boxid].state=="out" && (cur == "big" || cur == "middle"))
	{
		var next = cur == "big" ? "middle" : "little"
		ResizableBoxes[boxid].inprogress = true;
		dbg(boxid + " - shrink starts, inprogress=true")
		Box_shrink(boxid,cur,next,0);
	}
}

function Box_checkState(boxid,oldstate) // check if mouse pointer not moved
{
	dbg(boxid + " - checking for mouse hold : new = " + ResizableBoxes[boxid].state + ", old = " + oldstate + ", inprogress=" + (ResizableBoxes[boxid].inprogress||false))
	if (ResizableBoxes[boxid].state==oldstate)
	{
		if (oldstate=="over")
			Box_checkOver(boxid)	
		else
			Box_checkOut(boxid)
	}
	else
	{
		if (oldstate=="out")
			Box_checkOver(boxid)	
		else
			Box_checkOut(boxid)
	}
}

function Box_grow(boxid,from,to,phase)
{
	var box = $(boxid)
	var img = document.getElementsByClassName("th",box)[0]

	if (phase==0) img.src = ResizableBoxes[boxid][to][0]

	var img_delta = ( ResizableBoxes[boxid][to][1] - ResizableBoxes[boxid][from][1] ) / ResizeSteps

	var img_width = ResizableBoxes[boxid][from][1] + img_delta * phase

	var box_delta = ( ResizableBoxes[boxid][to][3] - ResizableBoxes[boxid][from][3] ) / ResizeSteps
	var box_width = ResizableBoxes[boxid][from][3] + box_delta * phase 

	if (phase==ResizeSteps)
	{
		img.width = ResizableBoxes[boxid][to][1]
		box.style.width = ResizableBoxes[boxid][to][3]
		dbg(boxid + " : " + phase + " / " + img.src + " " + img.width + " " + img.height + "; " + box.style.width + ", stop!")

		Box_changeText(boxid,from,to)
		ResizableBoxes[boxid].size = to;
		ResizableBoxes[boxid].inprogress = false;

		if (to=="middle" && null!=ResizableBoxes[boxid]["big"])
			setTimeout("Box_checkState('" + boxid +"','over')",regrow_timeout);
		return;
	}
	img.width = img_width
	box.style.width = box_width
	dbg(boxid + " : " + phase + " / " + img.src + " " + img.width + " " + img.height + "; " + box.style.width)
	setTimeout("Box_grow('" + boxid +"','" + from + "','" + to + "'," + (phase+1) + ")",5)
}


function Box_shrink(boxid,from,to,phase)
{
	var box = $(boxid)
	var img = document.getElementsByClassName("th",box)[0]

	var img_delta = ( ResizableBoxes[boxid][to][1] - ResizableBoxes[boxid][from][1] ) / ResizeSteps

	var img_width = ResizableBoxes[boxid][from][1] + img_delta * phase

	var box_delta = ( ResizableBoxes[boxid][to][3] - ResizableBoxes[boxid][from][3] ) / ResizeSteps
	var box_width = ResizableBoxes[boxid][from][3] + box_delta * phase 

	if (phase==ResizeSteps)
	{
		img.width = ResizableBoxes[boxid][to][1]
		box.style.width = ResizableBoxes[boxid][to][3]
		dbg(boxid + " : " + phase + " / " + img.src + " " + img.width + " " + img.height + "; " + box.style.width + ", stop!")

		img.src = ResizableBoxes[boxid][to][0]
		Box_changeText(boxid,from,to)
		ResizableBoxes[boxid].size = to;
		ResizableBoxes[boxid].inprogress = false;

		if (to!="little")
			setTimeout("Box_checkState('" + boxid +"','out')",reshrink_timeout);
		return;
	}
	img.width = img_width
	box.style.width = box_width
	dbg(boxid + " : " + phase + " / " + img.src + " " + img.width + " " + img.height + "; " + box.style.width)
	setTimeout("Box_shrink('" + boxid +"','" + from + "','" + to + "'," + (phase+1) + ")",5)
}

function Box_changeText(boxid,from,to)
{
	var _tTo = document.getElementsByClassName(to,$(boxid))[0]
	var _tFrom = document.getElementsByClassName(from,$(boxid))[0]

	if ('undefined' == typeof Effect)
	{
		_tFrom.hide()
		_tTo.show()
	}
	else
	{
		new Effect.Appear(_tTo);
		new Effect.SwitchOff(_tFrom);
	}
}