﻿//shortcutting YAHOO libs
var $D = $D || YAHOO.util.Dom;
var $E = $E || YAHOO.util.Event;
var $A = $A || YAHOO.util.Anim;
var $M = $M || YAHOO.util.Motion;
var $DD = $DD || YAHOO.util.DD;
var $CN = $CN || YAHOO.util.Connect;
var $EL = $EL || YAHOO.util.Element;
var $ = $D.get;
var $$ = YAHOO.util.Selector ? YAHOO.util.Selector.query : null;

window.Danbolig = window.Danbolig || {};

Danbolig.Collapsible = (function() {
	return {
		getTargetLoc: function(href) {
			return href.replace(/^[^#]*/, "");
		},
		onShow: function(e) {
			if (this.href) {
				if (e) YAHOO.util.Event.stopEvent(e);
				if (this.collapsibleCallback) {
					this.collapsibleCallback("show");
				}
				var elm = $$(Danbolig.Collapsible.getTargetLoc(this.href));
				if (elm) {
					$D.removeClass(elm, 'collapsible-hidden');
					var activators = $$(".collapsible-show");
					for (var j = 0; j < activators.length; j++) {
						if (activators[j].href && Danbolig.Collapsible.getTargetLoc(activators[j].href) == Danbolig.Collapsible.getTargetLoc(this.href)) {
							$D.addClass(activators[j], 'collapsible-hidden');
						}
					}
					var activators = $$(".collapsible-hide");
					for (var j = 0; j < activators.length; j++) {
						if (activators[j].href && Danbolig.Collapsible.getTargetLoc(activators[j].href) == Danbolig.Collapsible.getTargetLoc(this.href)) {
							$D.removeClass(activators[j], 'collapsible-hidden');
						}
					}
					
				}
			}
		},
		onHide: function(e) {
			if (this.href) {
				if (e) YAHOO.util.Event.stopEvent(e);
				if (this.collapsibleCallback) {
					this.collapsibleCallback("hide");
				}
				var elm = $$(Danbolig.Collapsible.getTargetLoc(this.href));
				if (elm) {
					$D.addClass(elm, 'collapsible-hidden');
					var activators = $$(".collapsible-hide");
					for (var j = 0; j < activators.length; j++) {
						if (activators[j].href && Danbolig.Collapsible.getTargetLoc(activators[j].href) == Danbolig.Collapsible.getTargetLoc(this.href)) {
							$D.addClass(activators[j], 'collapsible-hidden');
						}
					}
					var activators = $$(".collapsible-show");
					for (var j = 0; j < activators.length; j++) {
						if (activators[j].href && Danbolig.Collapsible.getTargetLoc(activators[j].href) == Danbolig.Collapsible.getTargetLoc(this.href)) {
							$D.removeClass(activators[j], 'collapsible-hidden');
						}
					}
				}
			}
		}
	}
})();

$E.onDOMReady(function() {
	var elms = $$(".collapsible");
	for (var i = 0; i < elms.length; i++) {
		var elm = elms[i];
		if (YAHOO.util.Selector.test(elm, '.collapsible-content')) {
			//$D.addClass(elm, 'collapsible-hidden');
		}
		if (YAHOO.util.Selector.test(elm, '.collapsible-hide')) {
			//$D.addClass(elm, 'collapsible-hidden');
			$E.addListener(elm, 'click', Danbolig.Collapsible.onHide, elm, true);
		}
		if (YAHOO.util.Selector.test(elm, '.collapsible-show')) {
			$E.addListener(elm, 'click', Danbolig.Collapsible.onShow, elm, true);
		}
	}
});
