/**
 * Functions.js - Bootstrap for all Javascript functionality
 * 
 * @author  Webstores <info at webstores dot nl>
 *           Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */

document.observe('dom:loaded', function() {
	var homeSMToggler = new SMToggler('sm-togglers');
});


/**
 * Set visual height
 */
var minVisualHeight = 360;

function resizeVisualContainer() {
	bodyHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
	headerHeight = $('header').offsetHeight;
	contentHeight = $('content').offsetHeight;
	footerHeight = $('footer').offsetHeight;
	visualHeight = bodyHeight - headerHeight - contentHeight - footerHeight;
	
	WS.DOM.getChild('visual', 1).style.height = ((visualHeight > minVisualHeight) ? visualHeight : minVisualHeight) + 'px';
	WS.DOM.getChild('visual', 1).style.lineHeight = ((visualHeight > minVisualHeight) ? visualHeight : minVisualHeight) + 'px';
}

WS.Event.addEvent(window, 'load', resizeVisualContainer);
WS.Event.addEvent(window, 'resize', resizeVisualContainer);


/**
 * Social media togglers
 * 
 * @param {Mixed} el The ID or object reference of the element containing the toggler links
 */
function SMToggler(el) {
	this.el = $(el);
	this.active = null;
	this.zIndex = 0;
	
	this.init();
};

SMToggler.prototype = {
	
	/**
	 * Initialize
	 */
	init: function() {
		if(this.el) {
			var self = this;
			var anchors = this.el.select('a');
			
			this.zIndex = anchors.length;
			
			anchors.each(function(anchor) {				
				if(anchor.up().hasClassName('selected')) {
					self.active = anchor;
				}
				
				anchor.observe('click', function(e) {
					Event.stop(e);
					self.activateHandler(this);
				});
			});
			
			$$('.sm-window-close').each(function(anchor) {
				anchor.observe('click', function(e) {
					Event.stop(e);
					self.closeHandler(this);
				});
			});
			
			$$('.sm-window').each(function(window) {
				window.observe('click', function(e) {
					self.activate(this);
				});
			});
		}
	},
	
	/**
	 * Activate window event listener
	 * 
	 * @param {Object} anchor
	 */
	activateHandler: function(anchor) {
		this.active.up().removeClassName('selected');
		anchor.up().addClassName('selected');
		this.active = anchor;
		
		this.activate($(anchor.href.split('#')[1]));
	},
	
	/**
	 * Close window event listener
	 * 
	 * @param {Object} anchor
	 */
	closeHandler: function(anchor) {
		this.active.up().removeClassName('selected');
		
		this.close($(anchor.href.split('#')[1]));
	},
	
	/**
	 * Activate the given window
	 * 
	 * @param {Object} window
	 */
	activate: function(window) {
		if(!window.visible()) {
			Effect.Appear(window, {
				duration: 0.25
			});
		}
		
		window.style.zIndex = ++this.zIndex;
	},
	
	/**
	 * Close the given window
	 * 
	 * @param {Object} window
	 */
	close: function(window) {
		Effect.Fade(window, {
			duration: 0.25
		});
	}
};
