
/*
 * Класс для анимации навигации
 * TimeZero <http://www.timezero.ru/>
 * 2008-01-18
 * Автор: Дмитрий Шкинёв <berkel@timezero.ru>
 */

/*Конструктор объекта*/
var Navigation = function(id){

	this._mainContainerId = id;

	this._mainContainer = $(this._mainContainerId);

	if (!this._mainContainer){

		return false;
	}

	this._containerChangeHeightInterval = 10;
	this._containerChangeHeightDefaultStep = 7;

	this._containerTempDefaultPositionInterval = 4;

	this._containerTempPositionInterval = this._containerTempDefaultPositionInterval;

	this._containersVars = [];

	Common.preloadImages("/i/l-menu-plus.jpg", "/i/l-menu-minus.jpg");

	this.init();
}

/*Добавляем новые методы*/
Navigation.prototype = {

	/*Инициализация класса*/
	init : function(){

		var eThis = this;

		var oItemList = eThis._mainContainer.getElementsByTagName("DIV");

		if (!oItemList){

			return false;
		}

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			var pattern = new RegExp("^" + eThis._mainContainerId + "-switcher-([\\w]+)$");

			if (!pattern.test(oItem.id)){

				continue;
			}

			var id = oItem.id.replace(pattern, "$1");

			var container = eThis.getContainerById(id);

			var containerTop = eThis.getContainerTopById(id);

			var switcher = eThis.getSwitcherById(id);

			if (!(container || switcher)){

				continue;	
			}

			eThis._containersVars[id] = {

				maxHeight : container.offsetHeight,
				intervalId : null,
				tremblingIntervalId : null,
				isHide : true
			};

			if (eThis.checkSelected(container)){

				eThis._containersVars[id].isHide = false;

				switcher.className = "minus";

				containerTop.className = "l-menu-top-1";

			} else {

				container.style.height = "1px";
				container.style.display = "none";
			}

			Common.Event.add(oItem, "click",

				function(){

					var id = this.id.replace(pattern, "$1");

					var container = eThis.getContainerById(id);

					if (eThis._containersVars[id].isHide){

						eThis.show(id);

					} else {

						eThis.hide(id);
					}
				}
			);
		}
	},

	/*Метод, скрывающий блок навигации*/
	hide : function(id){
		
		var eThis = this;

		if (eThis._containersVars[id].tremblingIntervalId){

			return false;
		}

		eThis._containersVars[id].isHide = true;

		var switcher = eThis.getSwitcherById(id);

		switcher.className = "plus";

		if (eThis._containersVars[id].intervalId){

			clearInterval(eThis._containersVars[id].intervalId);	
			eThis._containersVars[id].intervalId = null;
		}

		eThis._containersVars[id].intervalId = setInterval(

			function(){

				eThis.changeHeight(id, "hide");

			}, eThis._containerChangeHeightInterval
		);
			},

	/*Метод, открывающий блок навигации*/
	show : function(id){

		var eThis = this;

		if (eThis._containersVars[id].tremblingIntervalId){

			return false;
		}

		eThis._containersVars[id].isHide = false;

		var switcher = eThis.getSwitcherById(id);

		var containerTop = eThis.getContainerTopById(id);

		switcher.className = "minus";

		containerTop.className = "l-menu-top-1";

		if (eThis._containersVars[id].intervalId){

			clearInterval(eThis._containersVars[id].intervalId);	
			eThis._containersVars[id].intervalId = null;
		}

		var container = eThis.getContainerById(id);

		container.style.display = "block";

		eThis._containersVars[id].intervalId = setInterval(

			function(){

				eThis.changeHeight(id, "show");

			}, eThis._containerChangeHeightInterval
		);
	},

	/*Метод, изменяющий высоту блока навигации*/
	changeHeight : function(id, mode){

		var eThis = this;

		var container = eThis.getContainerById(id);
		var containerTop = eThis.getContainerTopById(id);
		var containerHeight = container.offsetHeight;
		var containerMaxHeight = eThis._containersVars[id].maxHeight;

		var step = eThis._containerChangeHeightDefaultStep;

		if (mode == "hide"){

			if ((containerHeight - step) <= 0){

				container.style.height = "1px";
				container.style.display = "none";
				clearInterval(eThis._containersVars[id].intervalId);
				eThis._containersVars[id].intervalId = null;

				containerTop.className = "l-menu-top";

			} else {

				container.style.height = (containerHeight - step) + "px";
			}

		} else if (mode == "show"){

			if ((containerHeight + step) >= containerMaxHeight){

				container.style.height = (containerMaxHeight) + "px";
				clearInterval(eThis._containersVars[id].intervalId);
				eThis._containersVars[id].intervalId = null;

//				eThis.tremblingInit(id);

			} else {

				container.style.height = (containerHeight + step) + "px";
			}
		}
	},

	/*Метод, проверяющий наличие активных подпунктов*/
	checkSelected : function(oElem){

		var oItemList = oElem.getElementsByTagName("LI");

		if (!oItemList){

			return false;
		}

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.className == "act"){

				return true;
			}
		}

		return false;
	},

	/*Метод, инициализирующий дрожание*/
	tremblingInit : function(id){

		var eThis = this;

		eThis._containersVars[id].tremblingIntervalId = setInterval(

			function(){

				eThis.trembling(id);

			}, 70);
	},

	/*Метод, для дрожание :)*/
	trembling : function(id){

		var eThis = this;

		var container = eThis.getContainerById(id);

		var height = eThis._containersVars[id].maxHeight;

		if (eThis._containerTempPositionInterval < 0){

			eThis._containerTempPositionInterval = Math.abs(eThis._containerTempPositionInterval);
			height -= --eThis._containerTempPositionInterval;

		} else if (eThis._containerTempPositionInterval > 0){

			eThis._containerTempPositionInterval = -eThis._containerTempPositionInterval;
			height -= ++eThis._containerTempPositionInterval;

		} else {

			clearInterval(eThis._containersVars[id].tremblingIntervalId);
			eThis._containersVars[id].tremblingIntervalId = null;
			eThis._containerTempPositionInterval = eThis._containerTempDefaultPositionInterval;
		}

		container.style.height = (height) + "px";
	},

	/*Метод, возвращающий объект контейнера по ID*/
	getContainerById : function(id){

		var eThis = this;

		return $(eThis._mainContainerId + "-container-" + id);
	},

	/*Метод, возвращающий объект переключателя по ID*/
	getSwitcherById : function(id){

		var eThis = this;

		return $(eThis._mainContainerId + "-switcher-" + id);
	},

	/*Метод, возвращающий объект переключателя по ID*/
	getContainerTopById : function(id){

		var eThis = this;

		return $("id_menu_top-" + id);
	}
}