/**
 * @author Tobias Fuhlroth - Notch Interactive GmbH
 */


/**
 * Console
 */
 
if (window['console'] === undefined) window.console = { log: function () {} };
if (window['opera']) window.console.log = window.opera.postError;


/**
 * Site
 */
 
var Site = {};


/**
 * Mainnav
 */
 
Site.mainnav = {
	
	options: {
		'collapse-height': 169,
		'expand-height': 247
	},
	
	initialize: function () {
		this.container = document.id('mainnav');
		this.list = document.id('mainnav-list');
		this.items = this.list.getElements('> li');
		
		this.items.forEach(function (item, index) {
			if (!item.hasClass('selected') && !item.hasClass('ancestor')) {
				new Element('div', {'class': 'mousearea'}).inject(item);
				item.set('tween', {'transition': 'sine:out'});
				item.addEvent('mouseenter', this.mouseenter.pass(item, this));
				item.addEvent('mouseleave', this.mouseleave.pass(item, this));
			}
		}, this);
		
		new Element('div', {'id': 'header-cover'}).inject(document.id(document.body));
	},
	
	mouseenter: function (item) {
		var nested_list = item.getElement('ul');
		if (nested_list) {
			var height = nested_list.getSize().y + 197;
			item.tween('height', height);
		}
	},
	
	mouseleave: function (item) {
		item.tween('height', 169);
	}

};

window.addEvent('domready', function () {
	Site.mainnav.initialize();
});


/**
 * Footer
 */

Site.footer = {
	
	content: {},
	
	initialize: function () {
		if (Browser.Platform.ios || Browser.Platform.android || Browser.Platform.webos) {
			document.id('site-footer').setStyle('position', 'static');
			return false;
		}
		
		// Container
		this.container = document.id('site-footer');
		this.container.set('tween', {'duration': 1000, 'transition': 'sine:out'});
		//this.container.setStyle('position', 'absolute');
		
		// Wrapper
		//this.wrapper = new Element('div', {'id': 'site-footer-wrapper'}).wraps(this.container);
				
		// Metanav
		this.nav = document.id('metanav');
		this.nav_items = this.nav.getElements('> ul > li > a');
		this.nav_items.forEach(function (item, index) {
			item.addEvent('click', function (event) {
				event.stop();
				this.show(item);
			}.bind(this));
		}, this);
		
		// Print
		this.print_button = new Element('a', {'id': 'site-footer-print', 'html': '&nbsp;'});
		this.print_button.addEvent('click', this.print.bind(this));
		new Element('li').adopt(this.print_button).inject(this.nav.getElement('> ul'));
		
		// Close
		this.close_button = new Element('a', {'id': 'site-footer-close', 'html': '&nbsp;'});
		this.close_button.addEvent('click', this.collapse.bind(this));
		new Element('li').adopt(this.close_button).inject(this.nav.getElement('> ul'));
		
		// Content
		this.content_container = new Element('div', {'id': 'footer-content-container'}).inject(this.container);
		this.content_container.set('tween', {'duration': 1000, 'transition': 'sine:out'});
		this.content = new Element('div', {'id': 'footer-content'}).inject(this.content_container);
		
		// Setup
		this.bound = {
			'adjust_size_delayed': this.adjust_size_delayed.bind(this),
			'adjust_position': this.adjust_position.bind(this)
		};
		this.is_expanded = false;
		this.current_href = null;
		
		this.adjust_position();
		window.addEvent('scroll', this.bound.adjust_position);
		window.addEvent('resize', this.bound.adjust_position);
		
	},
	
	attach_events: function () {
		window.addEvent('resize', this.bound.adjust_size_delayed);
		window.addEvent('scroll', this.bound.adjust_size_delayed);
	},
	
	detach_events: function () {
		window.removeEvent('resize', this.bound.adjust_size_delayed);
		window.removeEvent('scroll', this.bound.adjust_size_delayed);
	},
	
	show: function (item) {
		this.current_href = item.get('href');
		var url = '/get/content' + this.current_href;
		if (this.content[url]) {
			this.render_page(url);
		} else {
			this.load(url);
		}
	},
	
	load: function (url) {
		this.request = new Request({
			'url': url,
			'onSuccess': function (responseText, responseXML) {
				this.content[url] = responseText;
				this.render_page(url);
			}.bind(this)
		}).send();
	},
	
	render_page: function (url) {
		this.content.set('html', this.content[url]);
		if (!this.is_expanded) this.expand();
	},
	
	expand: function () {
		this.adjust_size();
		this.adjust_position();
		this.attach_events();
		this.print_button.setStyle('display', 'block');
		this.close_button.setStyle('display', 'block');
	},
	
	collapse: function () {
		this.print_button.setStyle('display', 'none');
		this.close_button.setStyle('display', 'none');
		this.detach_events();
		this.container.tween('height', 30);
		this.content_container.tween('height', 0);
	},
	
	adjust_size: function () {
		var window_size = window.getSize();
		var window_scroll = window.getScroll();
		var content_scroll_size = this.content.getScrollSize();
		var height = window_size.y + window_scroll.y - 265;
		if (height < 30) height = 30; // min-height
		if (height > content_scroll_size.y) height = content_scroll_size.y; // max-height
		this.container.tween('height', height);
		this.content_container.tween('height', height - 30);
	},
	
	adjust_size_delayed: function () {
		this.timer = clearTimeout(this.timer);
		this.timer = this.adjust_size.delay(300, this);
	},
	
	adjust_position: function () {
		/*var window_size = window.getSize();
		var window_scroll = window.getScroll();
		this.wrapper.setStyle('top', window_size.y + window_scroll.y);*/
	},
	
	print: function () {
		window.open('/print' + this.current_href);
	}

};

window.addEvent('domready', function () {
	Site.footer.initialize();
});




/**
 * Team
 */

Site.team = {
	
	initialize: function () {
		this.list = document.id('team-list');
		this.list_items = this.list.getElements('li');
		this.email_anchors = this.list.getElements('.msg');
		this.email_anchors.setStyle('display', 'none');
		this.anchors = this.list.getElements('.image');
		this.anchors.forEach(function (anchor, index) {
			anchor.addEvent('click', function (event) {
				event.stop();
				this.show(index);
			}.bind(this));
		}, this);
		
		this.image_container = new Element('p', {'id': 'team-list-image'});
		this.image = new Element('img').inject(this.image_container);
		this.current_index = null;
		this.show(0);
		this.image_container.inject(this.list, 'before');
	},
	
	show: function (index) {
		if (index == this.current_index) return false;
		if (this.current_index != null) {
			this.anchors[this.current_index].removeClass('active');
			this.email_anchors[this.current_index].setStyle('display', 'none');
		}
		this.current_index = index;
		var anchor = this.anchors[this.current_index];
		anchor.addClass('active');
		if (anchor.get('href')) {
			this.image.set('src', anchor.get('href'));
			this.image.setStyle('visibility', 'visible');
		} else {
			this.image.setStyle('visibility', 'hidden');
		}
		this.email_anchors[this.current_index].setStyle('display', 'block');
	}

};


/**
 * CheckboxReplacement
 */

var CheckboxReplacement = new Class({
	
	Implements: Options,
	
	options: {
		'className': 'checkbox'
	},
	
	initialize: function (form, options) {
		this.form = document.id(form);
		this.setOptions(options);
		if (!this.form) return false;
		
		this.boxes = this.form.getElements('input[type=checkbox]');
		this.replacements = [];
		this.boxes.forEach(function (checkbox, index) {
			checkbox.setStyle('display', 'none');
			var replacement = new Element('a', {
				'id': checkbox.get('id') + '-replacement',
				'class': this.options.className
			});
			if (checkbox.get('checked')) replacement.addClass('checkbox-checked');
			replacement.addEvent('click', this.toggle.pass(index, this));
			replacement.inject(checkbox, 'after');
			this.replacements.push(replacement);
		}, this);
	},
	
	toggle: function (index) {
		var checkbox = this.boxes[index];
		checkbox.set('checked', !checkbox.get('checked'));
		
		var replacement = this.replacements[index];
		replacement.toggleClass('checkbox-checked');
	}

});



/**
 * MailtoRewrite
 */
 
var MailtoRewrite = new Class({
	
	Implements: Options,
	
	options: {
		className: 'msg',
		domain: 'vtwealth.ch'
	},
	
	initialize: function (options) {
		this.setOptions(options);
		$$('.{className}'.substitute(this.options)).forEach(function (item) {
			if (item.get('href')) {
				var account = item.get('href').match(/^mailto:(.*)$/)[1];
				var email = account + '@' + this.options.domain;
			} else {
				var email = item.get('text') + '@' + this.options.domain;
				item.set('text', email);
			}
			
			item.set('href', 'mailto:' + email);
		}, this);
	}
	
});

window.addEvent('domready', function () {
	new MailtoRewrite();
});




/**
 * TargetModifier
 */

var TargetModifier = new Class({

    Implements: Options,

    options: {
        target: '_blank',
        className: 'target:blank'
    },

    initialize: function(options) {
        this.setOptions(options);
        $$('a').forEach(function(item) {
            if (item.hasClass(this.options.className)) {
                item.set('target', this.options.target);
            }
        }, this);
    }

});

window.addEvent('domready', function() {
    new TargetModifier();
});
