Element.implement({

	// gets anchor from link href
	getAnchor: function(){
		try {
			var href = this.get('href');
			return href.substr(href.indexOf('#')+1);
		} catch (e) {
			return null;
		}
	},

	// show/hide methods, based on 'x-none' class
	show: function(){
		return this.removeClass('x-none');
	},
	hide: function(){
		return this.addClass('x-none');
	},

	// this methods disable text selection
	disableTextSelection: function(){
		return this.addEvents({
			'selectstart': $lambda(false),
			'mousedown': $lambda(false)
		});
	}
});



Window.implement({
	// this method load JQuery and highcharts.js together
	loadHighchartsStuff: function(){
		if(!window.highchartsStuff){
			window.highchartsStuff = new Asset.javascript('/x/highcharts/js/jQueryHighcharts.js');
			return true;
		} else {
			return false;
		}
	}
});
window.highchartsStuff = false;

window.addEvent('domready', function(){
	column_text = $('column');
	if (column_text && column_text.get('html') == '' && Browser.Engine.trident){
		column_text.dispose();
	}
	// run markup hacks
	new Exm();


	// print links
	try {
		$('print').addEvent('click', function(){
			Document.print('body');
			return false;
		});
	} catch(e){}


	// Show & Hide block with group businesses
	try {
		$('groupbusiness').addEvents({
			mouseover: function(){
				this.getElement('span').removeClass('x-none');
			},
			mouseout: function(){
				this.getElement('span').addClass('x-none');
			}
		});
	} catch(e) {}


	// Prevent Default Event for Hyperlink which causes appearance for group businesses block
	try {
		$$('#groupbusiness > a').addEvent('click', function(event){
			event.preventDefault();
		});
	} catch(e) {}


	// release tabs
	try {
		if ($('tabs')) {
			var links = $$('a.tab').dispose().inject($('tabs'));
			//links[0].addClass('this');
			$('tab-' + links[0].getAnchor()).removeClass('x-none');
			links
				.disableTextSelection()
				.addEvent('click', function(){
					links.removeClass('this');
					$$('div.tab').hide();
					this.addClass('this');
					$('tab-'+this.getAnchor()).show();
					return false;
				});
		}
	} catch(e) {}


	// release filter
	try {
		if ($('filters')) {
			$$('#filters select').addEvent('change', function(){
				window.location.href = this.value;
				return false;
			});
		}
	} catch(e) {}


	// release teaser on home page
	try {

		var Teaser = new Hash({
			images: $$('#teaser > img'),
			nav: $$('#teaser-nav > a'),
			key: $$('#teaser-key > p'),
			content: $$('#teaser-content > div'),
			map: $$('#teaser-map > div'),
			fxcontent: new Object,
			position: null,

			// method poses teaser to an aimed state
			moveto: function(to){
				this.position = to;

				this.images.fade('hide');
				this.images[to-1].fade('in');

				this.nav.removeClass('this');
				this.nav[to-1].addClass('this');

				this.key.removeClass('this');
				this.key[to-1].addClass('this');

				this.content.getParent('div').setStyle('bottom', '-400px');
				this.content.removeClass('this');
				this.content[to-1].addClass('this');
				this.fxcontent.start('bottom', 0);

				this.map.removeClass('this');
				this.map[to-1].addClass('this');

				return this;
			},
			init: function() {
				$('teaser').disableTextSelection();
				this.fxcontent = new Fx.Tween('teaser-content', {
					fps: 60,
					unit: 'px',
					duration: 300,
					link: 'chain',
					transition: 'circ:out'
				});
				var _this_ = this;
				this.images
					.fade('hide')
					.removeClass('x-none');
				this.nav.addEvents({
					'mouseover': function(){
						if ( !this.hasClass('this') ) this.tween('width', 250);
					},
					'mouseout': function(){
						if ( !this.hasClass('this') ) this.tween('width', 175);
					},
					'click': function(){
						_this_.nav.fireEvent('mouseout');
						_this_.moveto(+this.get('href').substr(1));
						return false;
					}
				});
				return this.moveto(1);
			}
		});
		Teaser.init();

	} catch(e) {}


	// release new publications
	try {
		if ($('newpublications')) {
			var np = $('newpublications');
			np.first = np.getElements('.i')[0]

			np.getElements('.i').addEvents({
				'mouseover': function(e){
					$$('#newpublications .i').each(function(e){
						if (e.opened) {
							e.getElements('.ca').tween('height', 30);
							e.getElements('p').tween('height', 0);
							e.opened = false;
						};
					});
					this.opened = true;
					this.getElements('.ca').tween('height', 80);
					this.getElements('p').tween('height', 48);
					return false;
				},
				'mouseout': function(e){
					$('accord-2').getElements('.ca').tween('height', 30);
					$('accord-2').getElements('p').tween('height', 0);
					$('accord-2').opened = false;

					$('accord-3').getElements('.ca').tween('height', 30);
					$('accord-3').getElements('p').tween('height', 0);
					$('accord-3').opened = false;

					$('accord-4').getElements('.ca').tween('height', 30);
					$('accord-4').getElements('p').tween('height', 0);
					$('accord-4').opened = false;

					$('accord-1').getElements('.ca').tween('height', 80);
					$('accord-1').getElements('p').tween('height', 48);
					$('accord-1').opened = true;

					return false;
				},
				'click': function(e){
					window.location.href = this.getElements('a')[0].get('href');
					return false;
				}
			});

			//np.getElements('p').fade('hide');
			np.first.fireEvent('mouseover');
		}
	} catch(e){}

	// release sitemap
	try {
		if ($('sitemap')) {

			$$('#sitemap h3').disableTextSelection();
			$$('#sitemap h3 a').addEvent('click', function(e){
				e.stop();
				this.getParent('h3').fireEvent('click');
			});
			new Fx.Accordion($$('#sitemap h3'), $$('#sitemap div.node'), {
				display: 1,
				opacity: true,
				alwaysHide: false
			});
		}
	} catch(e) {}

	// release webcast toggler
	try {
		if ($('webcast-toggler')) {
			$('webcast-toggler').addEvent('click', function(){
				$('webcast-text').toggleClass('x-none');
				return false;
			});
		}
	} catch(e) {}

	// daterange toggler
	try {
		if ($('daterange-toggler')) {
			$('daterange-toggler').addEvent('click', function(){
				$('daterange-search').toggleClass('x-none');
				return false;
			});
		}
	} catch(e) {}

	// typerange toggler
	try {
		if ($('typerange-toggler')) {
			$('typerange-toggler').addEvent('click', function(){
				$('typerange-search').toggleClass('x-none');
				return false;
			});
		}
	} catch(e) {}

	// advanced-search toggler
	try {
		if ($('advanced-search-toggler')) {
			$('advanced-search-toggler').addEvent('click', function(){
				$('search-top-advanced').toggleClass('x-none');
				return false;
			});
		}
	} catch(e) {}

	// advanced-search close button
	try {
		if ($('advanced-search-close')) {
			$('advanced-search-close').addEvent('click', function(){
				$('search-top-advanced').addClass('x-none');
				return false;
			});
		}
	} catch(e) {}

	window.addEvent('load', function() {
		new DatePicker('.date_toggled', {
			pickerClass: 'datepicker',
			allowEmpty: true,
			toggleElements: '.date_toggler',
			format: 'd.m.Y',
			inputOutputFormat: 'd.m.Y',
			months: _['media_local_months'],
			days: _['media_local_days']
		});
	});

	// milkbox
	try {
		$$('a[rel=milkbox:only]').addEvents({
		'mouseover':function(){
			return false;
		}
		});
		milkbox = new Milkbox();
	} catch(e) {}

	// add row & Delete row
	try {
		$$('.cv_add').addEvent('click', function(){
			var clone = this.getParent('dl').clone().inject(this.getParent('dl').getNext(),'bottom');
			clone.addClass('cv_education_new');
			clone.getElements('[name]').each(function(obj){
				obj.set('name', 'cv_'+obj.get('name'));
				obj.set('value', '');
			});
			clone.getElements('select').each(function(obj){
				obj.getElements('option[value=0]').set('selected', 'selected')
			});
			clone.getElements('em').dispose();
			clone.getElements('.cv_add').dispose();
			clone.getElements('ins').dispose();
			clone.getElements('.cv_remove').removeClass('x-none').addEvent('click', function(){
				this.getParent('dl').dispose();
			});
		});

		$$('.cv_remove').addEvent('click', function(){
			this.getParent('dl').dispose();
		});
	} catch(e) {}

});

