panele = {};
panele.initialize = function() {
	bannerfixed.initialize();
}
Event.observe(window, "load", panele.initialize);

bannerfixed = function(container) {
	this.timeAppear = 5;
	this.timeHide = 5;
	this.container = container;
	this.nodeTitle = this.container.getElementsBySelector(".bannerfixed_title")[0];
	this.hidden = true;
	this.hiddenHeight = 44;
	this.fullHeight = null;	
	this.container.getElementsBySelector(".bannerfixed_close")[0]
		.observe("click", this.handleClose.bind(this));	
	this.nodeTitle.observe("click", this.handleHide.bind(this));
	setTimeout(this.appear.bind(this), this.timeAppear * 1000);
}

bannerfixed.prototype.appear = function() {
	this.setVisible(true);
	this.setHidden(false);
}

bannerfixed.prototype.autoHide = function() {
	this.setHidden(true);
}

bannerfixed.prototype.handleHide = function() {
	this.setHidden(!this.hidden);
}

bannerfixed.prototype.setHidden = function(hide, instant) {
	this.hidden = hide;
	var height = (this.hidden ? this.hiddenHeight : this.fullHeight) + "px";
	if (instant) {
		this.container.style.height = height;
	} else {
		this.container.morph("height: " + height, {duration: 1, transition: Effect.Transitions.sinoidal}); 
	}
	this.nodeTitle.className = this.nodeTitle.className.split("bannerfixed_collapsed").join("") + 
		(this.hidden ? " bannerfixed_collapsed" : "");
	if (this.timeout) {
		clearTimeout(this.timeout);
	}
	if (!this.hidden) {
		this.timeout = setTimeout(this.autoHide.bind(this), this.timeHide * 1000);
	}
}

bannerfixed.prototype.handleClose = function() {
	this.setVisible(false);
}

bannerfixed.prototype.setVisible = function(show) {	
	var className = this.container.className;
	className = className.split("bannerfixed_shown").join("");
	if (this.timeout) {
		clearTimeout(this.timeout);
	}
	if (show) {
		className += " bannerfixed_shown";
		if (this.fullHeight === null) {
			var nodeBanner = this.container.getElementsBySelector(".bannerfixed_content_banner img")[0];
			if (!nodeBanner) {
				nodeBanner = this.container.getElementsBySelector(".bannerfixed_content_banner object")[0];
			}
			this.container.style.width = (Number(nodeBanner.getAttribute("width")) + 28) + "px";
			this.container.style.height = (Number(nodeBanner.getAttribute("height")) + 54) + "px";
			this.fullHeight = this.container.getHeight();			
		}
		this.setHidden(this.hidden, true);
	}
	this.container.className = className;
}

bannerfixed.initialize = function() {
	var el = $("bannerfixed");
	if (el) {
		new bannerfixed(el);
	}
}
