/* ---------------------------------------------------------------------------------------------------- */
/* =fewo.js */
/* ---------------------------------------------------------------------------------------------------- */

var FEWO = {};


/* -------------------------------------------------- */
/* =sideteaser */

FEWO.sideteaser = {
	
	options: {
		safetyZone: 20,
		size: {x: 185, y: 740},
		offset: {x: 20, y: 100},
		displaytime: 8000
	},
	
	init: function () {
		this.container = $('sideteaser-container');
		this.container.setStyle('width', this.options.size.x);
		this.guide = document.getElement('.main-container');
		this.fx = new Fx.Tween(this.container, {property: 'opacity', link: 'cancel', transition: 'sine:out'}).set(0);
		this.isVisible = false;
		
		// load flash
		this.swiff = new Swiff('/_swf/teaser.swf', {
			width: this.options.size.x,
			height: this.options.size.y,
			vars: {
				serviceurl: '/_service/teaser.ashx',
				displaytime: this.options.displaytime
			},
			container: this.container
		});
		
		window.addEvent('resize', this.check.bind(this));
		this.check();
	},
	
	show: function () {
		this.container.setStyle('display', 'block');
		this.fx.start(1);
	},
	
	hide: function () {
		this.fx.start(0).chain(function () {
			this.container.setStyle('display', 'none');
		}.bind(this));
	},
	
	check: function () {
		var windowSize = window.getSize();
		var guideCoo = this.guide.getCoordinates();
		
		if (guideCoo.right + this.options.offset.x + this.options.size.x < windowSize.x - this.options.safetyZone) { // window is big enough to show sideteaser
			if (!this.isVisible) {
				this.show();
				this.isVisible = true;
			 }
		} else { // oops, to small: hide sideteaser
			this.hide();
			this.isVisible = false;
		}
		this.adjust();
	},
	
	adjust: function () {
		var guide = this.guide.getCoordinates();
		this.container.setStyles({
			'left': guide.right + this.options.offset.x,
			'top': guide.top + this.options.offset.y
		});
	}

};


/* -------------------------------------------------- */
/* =TextShadow */

FEWO.TextShadow = new Class({
	
	Implements: Options,
	
	options: {
		offset: {x: 1, y: 1},
		color: '#666'
	},
	
	initialize: function (elements, options) {
		this.elements = $$(elements);
		this.setOptions(options);
		this.elements.each(this.drawShadow, this);
	},
		
	wrapElement: function () {
		var styles = {
			'position': 'relative',
			'left': 0,
			'top': 0
		};
		styles.display = this.currentElement.getStyle('display');
		if (this.currentElement.getStyle('position') == 'absolute') {
			styles.position = this.currentElement.getStyle('position');
			styles['z-index'] = this.currentElement.getStyle('z-index');
			$extend(styles, this.currentElement.getCoordinates(this.currentElement.getParent()));
			if (Browser.Platform.mac && Browser.Engine.gecko) {
				styles.width += 1;
				styles.left -= 1;
			}
		}
		new Element('div', {
			'styles': styles
		}).wraps(this.currentElement);
	},
	
	positionElement: function (element, isClone) {
		var styles = {
			'position': 'relative',
			'left': 0,
			'top': 0,
			'right': 0,
			'bottom': 0,
			'z-index': 10
		};
		if (isClone) {
			styles.width = this.currentElement.getParent().getSize().x;
			styles.height = this.currentElement.getParent().getSize().y;
			$extend(styles, {'position' : 'absolute', 'left': this.options.offset.x, 'top': this.options.offset.y, 'z-index': 9, 'color': this.options.color});
		}
		element.set('styles', styles);
	},
	
	drawShadow: function (element) {
		this.currentElement = element;
		this.wrapElement();
		this.positionElement(this.currentElement, false);
		this.positionElement(this.currentElement.clone().inject(this.currentElement, 'after'), true);
	}
	
});

window.addEvent('domready', function () {
	new FEWO.TextShadow('.textshadow', {offset: {x: 2, y: 2}, color : '#000'});
});


/* -------------------------------------------------- */
/* =debug */

FEWO.debug = {
	
	log: function (text) {
		try {
			console.log(text);
		} catch (e) {
			return false;
		}
	}
	
};


/* -------------------------------------------------- */
/* =fireDefaultButton */

FEWO.fireDefaultButton = {
	
	init: function () {
		$$('.firedefaultbutton').each(function (area) {
			var target = area.get('title');
			area.erase('title');
			new FireDefaultButton(area, target);
		}, this);
	}
	
};

window.addEvent('domready', function () {
	FEWO.fireDefaultButton.init();
});



/* -------------------------------------------------- */
/* =ff2Fix */

FEWO.ff2Fix = {

	init: function () {
		if (Browser.Engine.gecko && (Browser.Engine.version < 19)) {
			this.styles = $$('.btn');
			this.styles.each(function (item) {
				item.setStyle('padding', '5px 0 6px 0');
			});
		}
	}
	
};

window.addEvent('domready', function () {
	FEWO.ff2Fix.init();
});