/* $Id */

/**
 * class Fizon
 */
Fizon = new Class({

	content: null,
	frtlogin: false,
	contentLayer: null,
	startpage: false,
	eventWords: {
		words: ['ledif', 'tnempoleved dipar', 'suar'],
		active: {}
	},
	popups: {},
	url: {},
	kontaktform: false,

	/**
	 * initialize()
	 */	
	initialize: function()
	{
		document.fizon = this;
		
		this.content = $('content');
		this.contentLayer = new Element('div', {id: 'contentlayer'});
		this.contentLayer.fade(0);
		$(document.body).grab(this.contentLayer);

		var url = /^(https?):\/\/(([a-zA-Z0-9-]+\.)+[a-zA-Z]+)(\/.*)?$/.exec(window.location.href);
		this.url = {proto: url[1], domain: url[2], path: url[4]};
		if (this.url.path == '')
			this.url.path = '/';

		this.startpage = this.url.path == '/';

		this.frtlogin = $('frtlogin');
		if (this.frtlogin)
			this.frtlogin.addEvent('submit', this.onFrtLogin.bind(this));

		this.iniMenu();
		this.contentLeyerSetPosition();
		this.iniEvents();
		var karte = $('fizonmap');
		if (karte)
			this.initFizonKarte(karte);
		this.kontaktform = $('kontaktform');
		if (this.kontaktform)
			this.initKontaktform();

	},

	/**
	 * initFizonKarte()
	 */
	initFizonKarte: function(div)
	{
		this.map = {
			map:	new GMap2(div),
			point:	new GLatLng(53.8422172,10.6391902),
			click:	(function() {
				this.map.marker.openInfoWindowHtml(
					"<h4>FIZON GmbH</h4>"
					+"<p>Niendorfer Straße 4<br />23560 Lübeck</p>"
					+"<img src='/images/frontview_tn.jpg' alt='FIZON' width='218' height='145'/>"
					+"<div style='text-align:right'><a href='http://maps.google.de/maps?f=d&source=s_d&hl=de&geocode=&saddr=&daddr=Niendorfer+Stra%C3%9Fe+4,+23560+Moisling,+L%C3%BCbeck&sll=51.151786,10.415039&sspn=22.176349,53.657227&ie=UTF8&z=16'>Route berechnen</a></div>"
				)
			}).bind(this)
		};
		this.map.marker = new GMarker(this.map.point);

		this.map.map.setCenter(this.map.point, 15, G_HYBRID_MAP);
		this.map.map.setUIToDefault();
		this.map.map.addOverlay(this.map.marker);
		this.map.click();

		GEvent.addListener(this.map.marker, "click", this.map.click);
	},

	/**
	 * initKontaktform(()
	 */
	initKontaktform: function()
	{
		this.kontaktform.addEvent('submit',
			this.onSubmitKontaktform.bind(this));
		return this;
	},

	/**
	 * onSubmitKontaktform()
	 */
	onSubmitKontaktform: function()
	{
		var errors = [];

		if (this.kontaktform.name.value == '') 
			errors.push('Bitte geben Sie einen Namen ein.');
		if (this.kontaktform.text.value == '')
		       errors.push('Bitte geben Sie eine Nachricht ein.');
		
		if (errors.length == 0)
			return true;

		alert(errors.join("\n"));
		return false;
	},

	/**
	 * iniMenu()
	 */
	iniMenu: function()
	{
		var ie6fuck = 0;

		$('topmenu').getElements('li').each(function(li) {
			if (! document.fizon.startpage && ! Browser.Engine.trident4) {
				li.addEvents({
					mouseenter: function() {
						document.fizon.menuMouseenter(this);
					},
					mouseleave: function() {
						document.fizon.menuMouseleave(this);
					}
				}).getElement('div').set({
					tween: {
						duration	: 1000,
						transition	: Fx.Transitions.Bounce.easeOut
					},
					styles: { height: 4 }
				});
			}
			if (ie6fuck++ == 0 && Browser.Engine.trident4)
				li.setStyle('margin-left', 0);
		});

		return this;
	},

	/**
	 * iniEvents()
	 */
	iniEvents: function()
	{
		window.addEvent('keydown', function(e) {
			document.fizon.keydown(e);
		});
		return this;
	},

	/**
	 * keydown()
	 */
	keydown: function(e)
	{
		var i, j, s, k;
		var tmpActive = [];

		k = e.key;
		if (k == 'space')
			k = ' ';

		for (i in this.eventWords.active) {
			if (this.eventWords.words[i][this.eventWords.active[i]--] == k)
				tmpActive.push(i);
			else 
				delete this.eventWords.active[i]
		}

		for (i in this.eventWords.words) {
			s = true;
			for (j = 0; j < tmpActive.length; j++)
				if (tmpActive[j] == i)
					s = false;
			if (s && this.eventWords.words[i][this.eventWords.words[i].length - 1] == k)
				this.eventWords.active[i] = this.eventWords.words[i].length - 2;
		}

		for (i in this.eventWords.active) {
			if (this.eventWords.active[i] != -1)
				continue;
			delete this.eventWords.active[i];
			this.fireWordEvenet(this.eventWords.words[i]);
		}

		return this;
	},

	/**
	 * fireWordEvenet()
	 */
	fireWordEvenet: function(w)
	{
		switch (w) {
		case 'ledif':
			this.createPopup('eefc', 320, 260, {
				title: 'Bacardi vs. Havana Club',
				loadContent: '/dynhtml/popup_eefc.html'
			});
			break;
		case 'tnempoleved dipar':
			this.createPopup('rpdl', 312, 416, {
				title: 'Rapid Development',
				loadContent: '/dynhtml/popup_rpdl.html'
			});
			break;
		case 'suar':
			this.createPopup('suar', 517, 256, {
				title: 'Raus',
				loadContent: '/dynhtml/popup_raus.html'
			});
			break;
		}

		return this;
	},

	/**
	 * menuMouseenter()
	 */
	menuMouseenter: function(li)
	{
		if (! this.startpage)
			li.getElement('div').tween('height', 110);
		return this.contentLeyerDisplay(true);
	},

	/**
	 * menuMouseleave()
	 */
	menuMouseleave: function(li)
	{
		if (! this.startpage)
			li.getElement('div').tween('height', 4);
		return this.contentLeyerDisplay(false);
	},

	/**
	 * contentLeyerSetPosition()
	 */
	contentLeyerSetPosition: function()
	{
		this.contentLayer.setStyles({
			width:		this.content.getWidth() - 10,
			height:		this.content.getHeight() - 10
		});
		this.contentLayer.setPosition({
			relativeTo:	this.content,
			position:	'upperLeft',
			offset:		{x: 5, y: 5}
		});
		return this;
	},

	/**
	 * contentLeyerDisplay()
	 */
	contentLeyerDisplay: function(action) 
	{
		if (action) {
			this.contentLeyerSetPosition();
			this.contentLayer.fade(0.8);
		}
		else {
			this.contentLayer.fade(0);
		}

		return this;
	},

	/**
	 * createPopup()
	 */
	createPopup: function(id, width, height, options)
	{
		if (this.popups[id])
			return this.popups[id];

		return new FizonPopup(id, width, height, options);
	},

	/**
	 * onFrtLogin()
	 */
	onFrtLogin: function(event)
	{
		var login = this.frtlogin.login;
		if (login.value.test(/^\d{4}$/))
			login.value += '@kunden.fizon.de';
		return this;
	}
});

/**
 * class FizonPopup
 */
FizonPopup = new Class({

	id: null,
	element: null,
	header: null,
	close: null,
	title: null,
	content: null,
	options: null,

	/**
	 * initialize()
	 */
	initialize: function(ident, width, height, options)
	{
		this.ident = ident;
		this.options = options || {};

		document.fizon.popups[this.ident] = this;
		
		this.element = new Element('div', {id: this.ident}).addClass('popup').setStyle('width', width);

		this.header = new Element('div').addClass('header');
		this.close = new Element('div').addClass('close').store('popupobj', this);
		this.title = new Element('div').addClass('title');
		this.content = new Element('div').addClass('content').setStyles({
			width: width,
			height: height
		});

		this.header.adopt(this.title, this.close);
		this.element.adopt(this.header, this.content);

		this.close.addEvent('click', function() {
			this.retrieve('popupobj').destroy();
		});

		$(document.body).grab(this.element);

		this.element.setPosition({
			relativeTo:	document.fizon.content,
			position:	'center'
		});

		if (this.options.title)
			this.setTitle(this.options.title);

		if (this.options.loadContent)
			this.loadContent(this.options.loadContent);
	},

	/**
	 * destroy()
	 */
	destroy: function()
	{
		this.element.dispose();
		delete document.fizon.popups[this.ident];
		delete this;
	},

	/**
	 * setTitle()
	 */
	setTitle: function(title)
	{
		this.title.set('text', title);
		return this;
	},

	/**
	 * loadContent()
	 */
	loadContent: function(url)
	{
		this.content.set('html',
			new Request({
				url:	url,
				method:	'get',
				async:	false
			}).send().response.text
		);
	}
});

// load fizon onject
window.addEvent('load', function() { new Fizon; });
