﻿var HomewardPath = {
	active: null
};

HomewardPath.init = function(limitWidths) {
	var p = $("#homeward-path");
	if(limitWidths) {
		var w = Math.floor(p.width() / 6);
		var as = p.find("A");
		for(var i = 0; i < as.length; i++) {
			var a = $(as[i]);
			if(!a.parent().hasClass(".selector")) continue;

			for(var t = a.html(); a.parent().width() > w && t.length > 5;) {
				t = t.substr(0, t.length - 1);
				a.html(t + "…");
			}
		}
	}

	p.bind("click", HomewardPath.handleClick);
}

HomewardPath.handleClick = function(evt) {
	function positionList(e, l) {
		var p = e.parent();
		var ep = e.position();
		var ed = {width: e.width(), height: e.height()};
		var pd = {width: p.width(), height: p.height()};
		l.style.minWidth = (pd.width + 4) + "px";
		var ld = {width: l.offsetWidth, height: l.offsetHeight};
		l.style.left = "";
		l.style.right = "0px";
		l.style.top = (ep.top + ed.height - 1) + "px";
		if(l.offsetLeft < 0) {
			l.style.right = "";
			l.style.left = "0px";
		}
	}

	function createList(e, type, typeId) {
		var id = type + "-drop-down";
		var list = $("#" + id)[0];
		var w = list.scrollWidth;
		if(w > list.offsetWidth) {
			list.style.width = (w + 4) + "px";
			positionList(e, list);
		}
		return list;
	}

	var prev = HomewardPath.active;
	if(HomewardPath.active) {
		$(HomewardPath.active).css("display", "none")
		$(HomewardPath.active.parentNode).removeClass("open");
		HomewardPath.active = null;

		$(document.body).unbind("click", HomewardPath.handleClick);
	}

	var p = $(evt.target).closest(".drop-down");
	var e = p.find("IMG[type]");
	if(e.length) {
		var typeAndId = e.attr("type").split(":");
		var type = typeAndId[0];
		var id = typeAndId[1];
		var list = createList(e, type, id);
		if(list != prev) {
			list.style.display = "block";
			positionList(e, list);

			HomewardPath.active = list;
			p.addClass("open");

			$(document.body).bind("click", HomewardPath.handleClick);
		}

		evt.preventDefault();
		return false;
	}
}

function DropDown(ids, limitWidths) {
	var active = null;

	function positionList(id) {
		var e = $(id);
		var ep = e.offset();
		var ed = {width: e.width(), height: e.height()};
		var l = $(id + "-list");
		l.style.minWidth = ed.width + "px";
		var ld = {width: l.width(), height: l.height()};
		l.style.left = (ep[0] + ed.width - ld.width - 2) + "px";
		l.style.top = (ep[1] + ed.height - 1) + "px";
	}

	function handleClick(evt) {
		var curActive = active;
		if(active) {
			active.addClass("closed");
			active = null;
			document.body.unbind("click", handleClick);
		}

		var e = evt.srcElement || evt.target;
		if(e && e.tagName != "A") {
			for(; e && !/\bdrop-down\b/.test(e.className); e = e.parentNode);
			if(e) {
				var l = $(e.id + "-list");
				if(l && curActive != l && l.hasClass("closed")) {
					l.removeClass("closed");
					active = l;
					positionList(e.id);
					document.body.bind("click", handleClick);
				}
			}

			return false;
		}
	}

	for(var i = 0; i < ids.length; i++) {
		var e = $("#" + ids[i]);
		if(limitWidths) {
			var a = e.children("A");
			if(a.length) {
				a = a[0];
				var w = Math.floor(e.parentNode.offsetWidth / 6);
				for(var t = a.innerHTML; e.offsetWidth > w && t.length > 5;) {
					t = t.substr(0, t.length - 1);
					a.innerHTML = t + "&hellip;";
				}
			}
		}
		e.bind("click", handleClick, false);
	}
}

