/**
 * @author tfuhlroth maxomedia - agentur für crossmedia-kommunikation bsw
 */


/* -------------------------------------------------- */
/* =Site */

var Site = {};
$extend(Site, new Events());


/* -------------------------------------------------- */
/* =Background */

Site.background = {
	
	init: function () {
		this.backgroundBottom = $(document.body).getElement('.background-wrapper-bottom');
		this.paddingBottom = this.backgroundBottom.getStyle('padding-bottom').toInt();
		window.addEvent('resize', this.check.bind(this));
		this.check();
	},
	
	check: function () {
		if (window.getScrollSize().y <= window.getSize().y) {
			this.backgroundBottom.setStyle('height', window.getSize().y - this.paddingBottom);
		} else {
			this.backgroundBottom.setStyle('height', 'auto');
		}
	}
	
};

window.addEvent('domready', function () {
	Site.background.init();
});


/* -------------------------------------------------- */
/* =Mainnav */

Site.mainnav = {
	
	options: {
		spriteWidth: 200,
		opacity: 1	
	},
	
	init: function () {
		this.element = $(document.body).getElement('.mainnav');
		
		this.menu = new Menu.HTML(this.element, {
			customTransition: true,
			onShow: function (list) {
				list.setStyles({
					display: 'block',
					opacity: 0,
					zIndex: 1000
				});
				list.get('tween', {link: 'cancel', transition: 'sine:out'}).start('opacity', this.options.opacity);
			}.bind(this),
			onHide: function (list) {
				list.tween('opacity', 0);
			},
			onSelect: function (obj) {
				console.log(obj);
			}
		});
		
		this.items = $$('.mainnav > li');
		this.items.forEach(function (item, index) {
			if (item.get('class').search(/-active/) < 0) {
				item.set('tween', {link: 'cancel', transition: 'sine:out', duration: 300});
				item.addEvent('mouseenter', this.enter.pass(index, this));
				item.addEvent('mouseleave', this.leave.pass(index, this));
				if (!item.getStyle('background-position')) item.setStyle('background-position', 10 - (index * this.options.spriteWidth) + 'px 0'); // fix .getStyle('background-position')
			 }
		}, this);
	},
	
	enter: function (index) {
		var item = this.items[index];
		item.tween('background-position', this.getPos(item, '-17px'));
	},
	
	leave: function (index) {
		var item = this.items[index];
		item.tween('background-position', this.getPos(item, '0'));
	},
	
	getPos: function (item, y) {
		var pos = item.getStyle('background-position').split(' ');
		pos[1] = y;
		return pos.join(' ');
	}
		
};

if (Browser.Engine.trident) Site.mainnav.options.opacity = 0.9;

window.addEvent('domready', function () {
	Site.mainnav.init();
});

/* -------------------------------------------------- */
/* =Form Disabler */

Site.frmdisable = {
	
	init: function () {
		this.container = $('frm-disable');
		this.state = 0;
		this.trigger = $('frm-trigger').getElement('input');
		this.trigger.addEvent('click', this.toggle.bind(this));
		
		if(this.trigger.get('checked') == true) this.check();
	},
	
	toggle: function () {
		if(this.state == 0) {
			this.container.setStyle('display', 'none');
			this.state = 1;
		} else {
			this.container.setStyle('display', 'block');
			this.state = 0;
		}
	},
	
	check: function () {
		this.container.setStyle('display', 'none');
		this.state = 1;
	}
	
};

window.addEvent('domready', function () {
	if($('frm-disable')) {
		Site.frmdisable.init();
	}
});


/* -------------------------------------------------- */
/* =Flash Loader */

var FlashLoader = new Class({

	initialize: function (containers) {
		this.containers = containers;
		this.containers.forEach(function (item) {
			this.loadParams(item);	
		}.bind(this));
	},

	loadParams: function (item) {
		this.container = $(item);
		this.objTag = this.container.getElement('object');
		
		this.params = new Hash();
		this.paramElements = this.container.getElements('param');
		this.paramElements.each(function (param) {
			this.params.include(param.get('name'), param.get('value'));
		}, this);
		
		this.loadSwiff();
	},
	
	loadSwiff: function () {
		new Swiff(this.params.get('movie'), {
			width: this.objTag.width,
			height: this.objTag.height,
			params: this.params.getClean(),
			container: this.container
		});
	}

});

/* -------------------------------------------------- */
/* =Target Modifier */

var TargetModifier = new Class({
	
	Implements: Options,
	
	options: {
		target: '_blank',
		className: 'target:blank'
	},

	initialize: function (options) {
		this.setOptions(options);
		$$('a').each(function (item) {
			if(item.hasClass(this.options.className)) {
				item.set('target', this.options.target);
			}
		}, this);
	}

});

window.addEvent('domready', function () {
	new TargetModifier();
});

/* -------------------------------------------------- */
/* =Flash Teaser Loader */

Site.flashTeaserLoader = {
	init: function () {
		// scan for elements
		this.elements = document.body.getElements('div[rel*=titleswf::]');
		if(this.elements.length != 0) {
			this.elements.each(function (item) {
				var url = item.get('rel').split('::')[1];
				var width = 710;
				var height = item.get('rel').split('::')[2];
				this.renderSWF(item, url, width, height);
			}, this);
			
		}
	},
	renderSWF: function (item, url, width, height) {
		new Swiff(url, {
			width: width,
			height: height,
			container: item
		});
	}
}

window.addEvent('domready', function () {
	Site.flashTeaserLoader.init();
});