/**
 * @requires   pots.js
 * @version    1.02
 * @updated    2007/11/12
 * @author     inamorix &lt;<a href="mailto:inamorix@metatype.jp">inamorix@metatype.jp</a>&gt;
 * @copyright  Copyright (c) 2007, <a href="http://metatype.jp/">metatype</a>.
 * @license    The MIT-style license.
 */



port.extend({
	/**
	 * @private
	 */
	init: function () {
		port.make('mootools');
		this.fx_drawer = null;
		this.re_drawer = null;
		
		return this;
	},
	
	
	
	/**
	 * @private
	 */
	standby: function () {
		new SmoothScroll();
		this.addEventsDrawer();
		this.addEventsFooter();
		this.fixIndexBoxes();
	},
	
	
	
	/**
	 * @private
	 */
	addEventsDrawer: function () {
		$$('div#header li').each(this.bind(function(scope, el) {
			if (/app|lib|wiki/.test(el.className)) {
				el.firstChild.onclick = this.bind(this.onHeader);
			}
		}));
		$('drawer_close').firstChild.onclick = this.bind(this.closeDrawer);
		
		var $this = this;
		this.fx_drawer = new Fx.Slide('drawer_proj', {
			mode      : 'vertical',
			duration  : 360,
			onComplete: function (el) {
				if (parseFloat(el.style.marginTop) < 0) {
					$('header_open').id   = '';
					$('drawer').className = '';
					
					if ($this.re_drawer) {
						$this.onHeader($this.re_drawer);
						$this.re_drawer = null;
					}
				}
			}
		});
		this.fx_drawer.hide();
	},
	
	
	
	/**
	 * @private
	 */
	addEventsFooter: function () {
		$('footer_and_more').onclick = function () {
			this.parentNode.style.display  = 'none';
			$('footer_more').style.display = 'inline';
			return false;
		};
	},
	
	
	
	/**
	 * @private
	 */
	fixIndexBoxes: function () {
		if ($('index')) {
			var padding = (($('index').className == 'withimg') ? 160 : 0) + 40;
			var cnt     = 1;
			var h_max   = 0;
			$$('div#index div.box').each(function (el) {
				var h = el.getSize().size.y;
				if (cnt == 3) {
					if (h < h_max) {
						h_max -= padding;
						el.style.height = (h_max + 1) + 'px';
					}
					cnt   = 0;
					h_max = 0;
				}
				else if (h > h_max) {
					h_max = h;
				}
				
				if (cnt == 2) {
					el.style.margin = '0px 25px 0px 25px';
				}
				cnt++;
			});
		}
	},
	
	
	
	/**
	 * @private
	 * @param   {element}  el
	 * @return  {boolean}
	 */
	onHeader: function (el) {
		var mode   = el.parentNode.className;
		var drawer = $('drawer');
		if (mode != drawer.className) {
			if (drawer.className == '') {
				el.id            = 'header_open';
				drawer.className = mode;
				this.openDrawer();
			}
			else {
				this.re_drawer = el;
				this.closeDrawer();
			}
		}
		else {
			this.closeDrawer();
		}
		
		el.blur();
		return false;
	},
	
	
	
	/**
	 * @private
	 * @return  {boolean}
	 */
	openDrawer: function () {
		this.fx_drawer.slideIn();
		return false;
	},
	
	
	
	/**
	 * @private
	 * @return  {boolean}
	 */
	closeDrawer: function () {
		this.fx_drawer.slideOut();
		return false;
	}
});