
/*
 * Анимация навигации
 * TimeZero <http://www.timezero.ru/>
 * 2007-12-24
 * Автор: Дмитрий Шкинёв <berkel@timezero.ru>
 */

var Navigation = function(id){

	this._mainContainer = $(id);

	this._subContainer = $(id + "-sub");

	this._activeSubNodeItem = 0;

	this._defaultNodeItemId = 0;

	var eThis = this;

	eThis.init();
}

Navigation.prototype = {

	init : function(){

		var eThis = this;

		var oItemList = eThis._mainContainer.childNodes;

		if (oItemList.length == 0)
			return false;

		var hideInterval = 800;

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.tagName != "DIV")
				continue;

			var id = oItem.id.replace(/^navigation-(\d+)$/, "$1");

			if (eThis.getChildrenTagsByUrl(document.location, id).length > 0 || eThis.getChildrenTagsBySelected(id).length > 0){

				var oImg = oItem.getElementsByTagName("IMG")[0];
				oImg.src = oImg.src.replace(/(.+)\.(.+)$/, "$1_over.$2");

				eThis._defaultNodeItemId = oItem.id.replace(/^navigation-(\d+)$/, "$1");

				eThis.showAllSubNode(eThis._defaultNodeItemId);
			}

			cmnAdd_event(oItem, "mouseover",

				function(){

					var id = this.id.replace(/^navigation-(\d+)$/, "$1");

					if (eThis.getChildrenTagsByUrl(document.location, id).length == 0 && eThis.getChildrenTagsBySelected(id).length == 0){

						var oImg = this.getElementsByTagName("IMG")[0];
						oImg.src = oImg.src.replace(/(.+)\.(.+)$/, "$1_over.$2");
					}

					eThis.showSubNode(id);
				}
			);

			cmnAdd_event(oItem, "mouseout",

				function(){

					var id = this.id.replace(/^navigation-(\d+)$/, "$1");

					if (eThis.getChildrenTagsByUrl(document.location, id).length == 0 && eThis.getChildrenTagsBySelected(id).length == 0){

						var oImg = this.getElementsByTagName("IMG")[0];
						oImg.src = oImg.src.replace(/_over\.(.+)$/, ".$1");
					}

					eThis.hideAllIntervalId = setTimeout(

						function(){

							eThis.hideAllSubNodes();

							if (eThis._defaultNodeItemId)
								eThis.showSubNode(eThis._defaultNodeItemId);

						}, hideInterval
					);
				}
			);
		}

		cmnAdd_event(eThis._subContainer, "mouseover",

			function(){

				clearInterval(eThis.hideAllIntervalId);
			}
		);

		cmnAdd_event(eThis._subContainer, "mouseout",

			function(){

				eThis.hideAllIntervalId = setTimeout(

					function(){

						if (eThis._defaultNodeItemId){

							eThis.showSubNode(eThis._defaultNodeItemId);

						} else {

							eThis.hideAllSubNodes();
						}

					}, hideInterval
				);
			}
		);



		var oItemList = eThis._subContainer.childNodes;

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.tagName != "DIV"){

				continue;
			}

			var id = oItem.id.replace(/^navigation-sub-(\d+)$/, "$1");

			var mainItem = $("navigation-" + id);

			var summaryWidth = eThis.summaryWidthChildNodes(oItem);

			var x = (mainItem.offsetLeft + (mainItem.offsetWidth / 2)) - (summaryWidth / 2);

			if ((x + summaryWidth) > (eThis._subContainer.offsetLeft + eThis._subContainer.offsetWidth - 11)){

				x = (eThis._subContainer.offsetLeft + eThis._subContainer.offsetWidth) - summaryWidth - 11;
			}

			oItem.style.width = summaryWidth + "px";
			oItem.style.left = x + "px";

			if (eThis.getChildrenTagsByUrl(document.location, id).length == 0 && eThis.getChildrenTagsBySelected(id).length == 0){

				oItem.style.display = "none";
			}

			var oItemSubList = oItem.childNodes;

			for (var i2=0, c2=oItemSubList.length; i2<c2; i2++){

				var oItemSub = oItemSubList[i2];

				if (oItemSub.tagName != "DIV")
					continue;

				cmnAdd_event(oItemSub, "mouseover",

					function(){

						this.className = this.className + " selected";
					}
				);

				cmnAdd_event(oItemSub, "mouseout",

					function(){

						this.className = this.className.replace(/ selected/, "");
					}
				);
			}
		}
	},

	showSubNode : function(id){

		var eThis = this;

		var subNode = $("navigation-sub-" + id);

		clearInterval(eThis.hideAllIntervalId);

		if (!subNode)
			return false;

		if (subNode.style.display == "block")
			return false;

		eThis.hideAllSubNodes();

		clearInterval(eThis.showSubNodeIntervalId);

		subNode.style.display = "block";


		eThis._activeSubNodeItem = 0;
		eThis.showSubNodeIntervalId = setInterval(

			function(){

				eThis.showSubNodeItem(id);

			}, 100);
	},

	showSubNodeItem : function(id){

		var eThis = this;

		var subNode = $("navigation-sub-" + id);

		var childNodes = subNode.childNodes;

		var oItemList = [];

		for (var i=0, c=childNodes.length; i<c; i++){

			var oItem = childNodes[i];

			if (oItem.tagName != "DIV")
				continue;

			oItemList[oItemList.length] = oItem;
		}

		if (eThis._activeSubNodeItem > (oItemList.length - 1)){

			clearInterval(eThis.showSubNodeIntervalId);
			eThis._activeSubNodeItem = 0;

		} else {

			var subNodeItem = oItemList[eThis._activeSubNodeItem];

			subNodeItem.style.display = "block";

			eThis._activeSubNodeItem++;
		}
	},

	getChildrenTagsByName : function(obj, tagName){

		var eThis = this;

		var oItemList = eThis._subContainer.childNodes;

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.tagName != "DIV")
				continue;

			oItem.style.display = "none";

			if (!oItem.hasChildNodes())
				continue;

			var oItemList2 = oItem.childNodes;

			for (var i2=0, c2=oItemList2.length; i2<c2; i2++){

				var oItem2 = oItemList2[i2];

				if (oItem2.tagName != "DIV")
					continue;

				oItem2.style.display = "none";
			}
		}
	},

	hideAllSubNodes : function(){

		var eThis = this;

		var oItemList = eThis._subContainer.childNodes;

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.tagName != "DIV")
				continue;

			oItem.style.display = "none";

			if (!oItem.hasChildNodes())
				continue;

			var oItemList2 = oItem.childNodes;

			for (var i2=0, c2=oItemList2.length; i2<c2; i2++){

				var oItem2 = oItemList2[i2];

				if (oItem2.tagName != "DIV")
					continue;

				oItem2.style.display = "none";
			}
		}
	},

	getChildrenTagsByUrl : function(url, id){

		var eThis = this;

		var node = $("navigation-sub-" + id);

		if (!node)
			return false;

		var oItemList = node.getElementsByTagName("A");

		var oList = [];

		var url1 = new String(url);

		var url2 = $("navigation-" + id).getElementsByTagName("A")[0].href;

		if (url1.match(url2))
			oList[oList.length] = $("navigation-" + id).getElementsByTagName("A")[0];

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.href == url)
				oList[oList.length] = oItem;
		}

		return oList;
	},

	getChildrenTagsBySelected : function(id){

		var eThis = this;

		var node = $("navigation-sub-" + id);

		if (!node)
			return false;

		var oItemList = node.getElementsByTagName("DIV");

		var oList = [];

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.className.match(/selected/))
				oList[oList.length] = oItem;
		}

		return oList;
	},

	showAllSubNode : function(id){

		var eThis = this;

		var node = $("navigation-sub-" + id);

		if (!node)
			return false;

		node.style.display = "block";

		var oItemList = node.childNodes;

		for (var i=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.tagName != "DIV")
				continue;

			oItem.style.display = "block";
		}
	}
	,

	summaryWidthChildNodes : function(oElem){

		if (!oElem){

			return false;
		}

		var eThis = this;

		var oItemList = oElem.childNodes;

		for (var i=0, w=0, c=oItemList.length; i<c; i++){

			var oItem = oItemList[i];

			if (oItem.tagName != "DIV"){

				continue;
			}

			w += oItem.offsetWidth + 2;
		}

		return w;
	}
}