// quadracom standard functions
// Requires mootools 1.2.1 framework
// Use t3mootools-extensions for TYPO3!

// Init-Script for actions to be taken after dom-ready event uis triggered
function dom_ready() {
	add_ext_links_target();
}

/*
Adds target-attrib with value 'blank' to a-tags with classes *-new-window
*/
function add_ext_links_target() {
	$each($$('a.external-link-new-window', 'a.internal-link-new-window'), function(link_item) {
		link_item.set('target', 'blank');
	});
}
window.addEvent("domready", dom_ready);



 
 
 /*
 * Script for FAQ-Pages: Adds accordion funcionality to TYPO3 content elements with section_frame 'expandable'
 *
 */
	// Contains currently open expandable
	var expandable_open;
	// Contains all div with class="expandable"
	var expandables;

function init_expandables() {
	var a_id;

	// Contains current URL jump mark to a certain content element?
	// If so, that ce will be expanded automatically
	var ce = location.href.match(/#c[0-9]+/);
	if (ce != null) {
		// Yes, so store it
		a_id = ce[0].match(/c[0-9]+/);
	}

	expandables = $$('div.expandable');
	expandables.each(
		function(el) {
			// Actual expandable element
			var expandable = el.getElement('div.csc-textpic-text');
			if ( (expandable == undefined) || (expandable == null )) {
				expandable = el.getElement('div.csc-textpic-text');
			}
			if ( (expandable == undefined) || (expandable == null )) {
				return;
			}
			var el_height = expandable.getStyle('height');
			expandable.setStyle('height', '0px');
			expandable.setStyle('overflow', 'hidden');
			
			//var ce_id = el.getPrevious().id;
			var ce_id = el.id;
	
			var heading = el.getElementsByTagName('h1')[0];
	
			// create link to this ce inside heading
			ln_url = document.location.href.replace(/#c[0-9]+/, '') + '#' + ce_id;
			var ln = new Element('a', {
				'href' : ln_url,
				'title' : 'Text anzeigen'
			});
			if (document.location.href.match(/\/en\//)) ln.title = 'Show text';
			if (heading.get('text')) {
				ln.set('text', heading.get('text'));
				heading.set('text', '');
			}
			else {
				ln.set('text', heading.get('text'));
				heading.innerText = '';
			}
			ln.injectInside(heading);
			
			// Make ce's heading click sensitive
			ln.addEvent('click', function(el) {
				open_expandable(expandable, el_height);
			});
			ln.setAttribute('onclick', 'return false;');
			
			// match current ce's uid against jump mark and open it
			if (ce_id == a_id) {
				open_expandable(expandable, el_height);
			}
		}
	);

	var exp_tips = new Tips($$('.expandable h1 a'), {
        maxTitleChars: 50   //I like my captions a little long
    });
}

function open_expandable(expandable, el_height) {
	if (expandable_open) {
		var closeTween = new Fx.Tween(expandable_open, {
			property: 'height', 
			transition: Fx.Transitions.Cubic.easeInOut, 
			duration: 1000
		}).start(0);
	}
	if (expandable_open != expandable) {
		var openTween = new Fx.Tween(expandable, {
			property: 'height', 
			transition: Fx.Transitions.Cubic.easeInOut, 
			duration: 1000,
			onComplete: function(expandable) {
				var scroller = new Fx.Scroll(window, {
					wait:false,
					duration:500,
					transition: Fx.Transitions.Quad.easeInOut
				});
				scroller.toElement(expandable.getPrevious());
				expandable_open = expandable;
			}
		}).start(el_height);
	}
	else {
		expandable_open = false;
	}
}

window.addEvent('domready', init_expandables);



/*
 * Script für Teaserboxen auf der Startseite
 */

window.addEvent('domready', function() {
	teaser_boxes = $$('div.teaser-box');
	teaser_boxes.each(
		function(el) {
			el.textFx = new Fx.Tween(el.getElement('.teaser-box-text'), {
				duration:400, 
				transition: Fx.Transitions.Cubic.easeInOut, 
				wait:false,
				property: 'top'
			});
			el.picFx = new Fx.Tween(el.getElement('.teaser-box-image-normal'), {
				duration:400, 
				transition: Fx.Transitions.Cubic.easeInOut, 
				wait:false,
				property: 'opacity'
			});
			el.addEvent('mouseenter', function() {
				this.picFx.start('0');
				this.textFx.start('73px');
			});
			el.addEvent('mouseleave', function() {
				this.picFx.start('1');
				this.textFx.start('153px');
			});
			var url = el.getElement('a');
			if ( (url != undefined) && (url != null) ) {
				el.setStyle('cursor', 'pointer');
				el.addEvent('click', function(el) {
					location.href = url.href;
				});
			}
		}
	);
});



/*
 * Script für Suchfeld-Manipulation
 */

window.addEvent('domready', function() {
	var sInput = $('search_form_header').getElement('.search_input');
	if ( (sInput != undefined) && (sInput != null) ) {
		var sInputDefaultValue = sInput.value;
		sInput.addEvent('focus', function() {
			this.select();
		});
		sInput.addEvent('blur', function() {
			if (this.value == "") {
				this.value = sInputDefaultValue;
			}
		});
	}
});
