// 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 für Teaserboxen auf der Startseite
 */

window.addEvent('domready', function() {
	
	boxcounter = 0
	
	teaser_boxes = $$('div.teaserbox');
	teaser_boxes.each(
		function(box) {
			
			box.titleFx = new Fx.Tween(box.getElement('h2'), {
				duration:400, 
				transition: Fx.Transitions.Cubic.easeInOut, 
				wait:false,
				property: 'top'
			});

			box.textFx = new Fx.Tween(box.getElement('.teaserbox-text'), {
				duration:400, 
				transition: Fx.Transitions.Cubic.easeInOut, 
				wait:false,
				property: 'left'
			});

			var url = box.getElement('a');
			if ( (url != undefined) && (url != null) ) {
				box.setStyle('cursor', 'pointer');
				box.addEvent('click', function(el) {
					location.href = url.href;
				});
			}

			box.pics = box.getElements('.teaserbox-image');
			
			if (box.pics.length > 1) {
			
				box.getElement('.teaserbox-content').setStyle('z-index', box.pics.length+10);
				
				var i=1;
				box.pics.each(function(pic) {
					pic.setStyle('z-index', i++);
					pic.fx = new Fx.Tween(pic, {
						duration:1000, 
						transition: Fx.Transitions.Cubic.easeInOut, 
						wait:false,
						property: 'opacity'
					});
				});
	
	
				box.addEvent('mouseenter', function() {
					this.titleFx.start('10px');
					this.textFx.start('190px');
				});
				box.addEvent('mouseleave', function() {
					this.titleFx.start('30px');
					this.textFx.start('10px');
				});
				
				// Timeout used to create a animation cascade
				window.setTimeout(function() {
					window.setInterval(function(){
					
						fadePic = box.pics[0];
						
						// Retrieve top pic (the one to be faded out)
						for(i=0; i<box.pics.length; i++) {
							if (box.pics[i+1]) {
								if (parseInt(box.pics[i+1].getStyle('z-index')) > parseInt(box.pics[i].getStyle('z-index')) ) {
									fadePic = box.pics[i+1];
								}
							}
						}
					
						// Start fade...
						fadePic.fx.start(0).chain(function(){
							//... and adjacently put faded pic to the bottom of the pic stack, fade it in again and move all pics one index up
							this.setStyle('z-index', 0);
							this.fx.set(1);
							box.pics.each(function(pic) {
								pic.setStyle('z-index', parseInt(pic.getStyle('z-index'))+1);
							});
						}.bind(fadePic));
							
					}, 10000);
				}, (boxcounter++)*400);
			}

		}
	);
});


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

	listen_boxes = $$('div.listenbox');
	listen_boxes.each(function(el) {
		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 = $('header').getElement('.tx-solr-q');
	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;
			}
		});
	}
});


window.addEvent('domready', function() {
	var cTables = $('wrap_main_content').getElements('table');
	cTables.each(function(tb) {
		if (tb.getParent('div.tx-qcombfhurteile-single') == null) {
			var zebraTable = new HtmlTable(tb, {zebra: true});
			zebraTable.updateZebras();
		}
	});
});

/*
 * Toggle Articles Extra Description in Shop List
 */
 
 window.addEvent('domready', function() {
 	var articleTitles = $('wrap_main_content').getElements('h3.com-list-art-title');
 	articleTitles.each(function(article) {
 		var desc = article.getParent().getElement('div.com-list-art-extradesc');
 		if (desc != undefined) {
 			var fx = new Fx.Reveal(desc);
 			//article.setStyle('cursor', 'pointer');
 			article.addClass('com-list-art-extradesc-trigger');
 			article.addEvent('click', function() {
 				this.toggleClass('com-list-art-extradesc-trigger-open');
 				fx.toggle();
 			});
 		}
 	});
});



/*
 * Script for FAQ-Pages: Adds accordion funcionality to TYPO3 content elements with section_frame 'expandable'
 *
 */
 
function init_expandables() {

	// controls slide options
	var slide_options = new Object({
		mode: 'vertical',
		duration: 1000,
    	transition: Fx.Transitions.Pow.easeOut
    });

	var toggleExp = function() {
				cw_slide.toggle();
				if (anriss_orig != null) {
					anriss_slide.toggle();
				}
	}

	expandables = $$('div.expandable');
	expandables.each(
		function(el) {

			// Get content to be revealed
			var content_wrap = '';
			if (el.getElement('.csc-textpic') != null) {
				content_wrap = el.getElement('.csc-textpic');
			}
			else if (el.getElement('.csc-text') != null) {
				content_wrap = el.getElement('.csc-text');
			}
			else if (el.getElement('.csc-textpic-text') != null) {
				content_wrap = el.getElement('.csc-textpic-text');
				content_wrap.setStyle('padding-top','18px');
				content_wrap.setStyle('margin-bottom','18px');
			}
			else {
				return;
			}

			var cw_slide = new Fx.Slide(content_wrap, slide_options);
			cw_slide.hide();

			var header = el.getElement('div.csc-header');
			var anriss_orig = el.getElement('p.anriss');

			if ( (header == null) && (anriss_orig == null) ) {
				return;
			}
			
			
			
			if (anriss_orig != null) {

				var anriss = anriss_orig.clone();
				anriss_orig.dispose();
				anriss.setStyle('margin-bottom','0px');
				if (header == null) {
					header = new Element('div', {
						'class': 'csc-header'
					});
					header.inject(el, 'top');
				}

				anriss.set('text', anriss.get('text') + ' ');
				more_link = new Element('a', {
					'text': 'mehr...',
					'class': 'exp-open',
					'style': 'cursor:pointer'
				});
				more_link.inject(anriss, 'bottom');
				anriss.inject(header, 'bottom');
					
				
				var anriss_slide = new Fx.Slide(anriss, slide_options);
				
				anriss.getElement('a.exp-open').addEvent('click', function() {
					cw_slide.toggle();
					if (header.getElement('h1') != null) {
						header.getElement('h1').toggleClass('exp-header-open');
					}
					anriss_slide.toggle();
				});
			}

			if (header.getElement('h1') != null) {
				header.getElement('h1').setStyle('cursor', 'pointer');
				header.getElement('h1').addEvent('click', function() {
					cw_slide.toggle();
					header.getElement('h1').toggleClass('exp-header-open');
					if (anriss_orig != null) {
						anriss_slide.toggle();
					}
				});
			}
			
			hide_link = new Element('a', {
				'text': 'weniger',
				'class': 'exp-close',
				'style': 'cursor:pointer'
			});
			hide_link_wrap = new Element('p');
			hide_link.inject(hide_link_wrap, 'bottom');
			hide_link_wrap.inject(content_wrap, 'bottom');
			el.setStyle('margin-bottom', '10px');
			content_wrap.getElement('a.exp-close').addEvent('click', function() {
				cw_slide.toggle();
				if (header.getElement('h1') != null) {
					header.getElement('h1').toggleClass('exp-header-open');
				}
				if (anriss_orig != null) {
					anriss_slide.toggle();
				}
			});
			
		}
	);
}

window.addEvent('domready', init_expandables);


function manipulateCommerceOutput() {
	
	// Tipps für Preis-Infos
	var infoTitle = "Hinweis zu unseren Produkten:";
	var infoText = "<p>Bei Erstbestellungen im laufenden Jahr wird nur die tatsächliche Nutzungsdauer ab dem Folgemonat berechnet,<br />Beispiel: Bestellung im Sept, Berechnung der Monate Okt bis Dez.</p>";
	var infoLinks = $$('div.com-single-article th a');
	infoLinks.store('tip:title', infoTitle);
	infoLinks.store('tip:text', infoText);
	var infoTips = new Tips(infoLinks);

	// 0,00-Preise in den Artikellisten durch Striche ersetzen
	var priceTds = $$('.com-list-article-info');
	priceTds.each( function(td) {
		if (td.get('text').match(/^0,00 €$/)) {
			td.set('text','-/-');
		}
	});

	// Wurde beim Hinzufügen eines Artikels zum Warenkorb eine Anzahl eingegeben?
	// Wenn nein: Anzahl auf "1" setzen
	var addToBasketSubmits = $$('.com-submit-add-to-basket');
	addToBasketSubmits.each( function(sbt) {
		var form = sbt.getParent('form');
		form.addEvent('submit', function() {
			var artCountInput = form.getElement('.com-input-qty');
			if (artCountInput.get('value') == '') {
				artCountInput.set('value', '1');
			}
		});
	});
	if ($(document.body).getElement('div.com-chkout-steps') != null) {
		var checkOutPaymentTab = $(document.body).getElement('div.com-chkout-steps').getElements('div')[2];
		checkOutPaymentTab.setStyle('display', 'none');
	}
}

window.addEvent('domready', manipulateCommerceOutput);

function addTipsToLinks() {
	var updateDownloadLinks = $$('.voks-update-download');
	updateDownloadLinks.store('tip:title', 'Info');
	updateDownloadLinks.store('tip:text', 'Dieses Update herunterladen');
	var updateDownloadTips = new Tips(updateDownloadLinks);

	var updateInfoLinks = $$('.voks-update-info');
	updateInfoLinks.store('tip:title', 'Info');
	updateInfoLinks.store('tip:text', 'Detaillierte Informationen zu diesem Update');
	var updateInfoTips = new Tips(updateInfoLinks);

	var addToBasketLinks = $$('.com-submit-add-to-basket');
	addToBasketLinks.store('tip:title', 'Info');
	addToBasketLinks.store('tip:text', 'Artikel zum Warenkorb hinzufügen');
	var addToBasketTips = new Tips(addToBasketLinks);
	
	var removeFromBasketLinks = $$('td.com-basket-actions a');
	removeFromBasketLinks.store('tip:title', 'Info');
	removeFromBasketLinks.store('tip:text', 'Artikel aus Warenkorb entfernen');
	var removeFromBasketTips = new Tips(removeFromBasketLinks);

	var updateBasketLinks = $$('input.com-input-update-basket');
	updateBasketLinks.store('tip:title', 'Info');
	updateBasketLinks.store('tip:text', 'Warenkorb aktualisieren');
	var updateBasketTips = new Tips(updateBasketLinks);
	
	var productLists = $$('.com-single-article');
	productLists.each( function(list) {
		var specsLink = new Element('a', {
			'text': 'Systemvoraussetzungen'
		});
		specsLink.inject(list, 'top');
		specsLink.set('class', 'com-single-specs');
	});

	var specLinks = $$('.com-single-article .com-single-specs');
	specLinks.store('tip:title', 'Systemvoraussetzungen für voks-Software');
	specLinks.store('tip:text', '<p>PC ab 2,5 GHz<br />ab 2 GB Arbeitsspeicher<br />Windows ab XP (SP3)<br />Auflösung ab 1.024 x 768<br />Internet-Zugang empfohlen</p>');
	var specLinksTips = new Tips(specLinks);

}
window.addEvent('domready', addTipsToLinks);

function toggleUrteilsTexte() {
	var slide_options = new Object({
		mode: 'vertical',
		duration: 1000,
    	transition: Fx.Transitions.Pow.easeOut
    });
    
	if ($$('div.tx-qcombfhurteile-pi1') != null) {
		var urteilsEls = $$('div.tx-qcombfhurteile-pi1 p.tatbestand, div.tx-qcombfhurteile-pi1 p.entschgruende');
		urteilsEls.each(function(el) {
			el.addClass('slide-toggle');
			var elTextEl = el.getNext('div');

			var urteilsSlide = new Fx.Slide(elTextEl, slide_options);
			urteilsSlide.hide();
			
			el
			
			el.addEvent('click', function() {
				urteilsSlide.toggle();
			});
		});


		urteilsEls.store('tip:title', 'Hinweis');
		urteilsEls.store('tip:text', '<p>Klicken Sie hier, um den Inhalt anzuzeigen bzw. zu verbergen.</p>');
		var urteilsElsTips = new Tips(urteilsEls);
	}
}

//window.addEvent('domready', toggleUrteilsTexte);

// Umfrage nach Einkauf --------------------------------------------------------------------------------------

function hideUmfrage() {
	myMask.hide();
	if ($('umfrageContainer')) {
		$('umfrageContainer').fade('out');
	}
}

function repositionDialogue() {

	if ($('umfrageMask')) {
		$('umfrageMask').setStyles({
			'width': window.getSize().x, 
			'height': window.getSize().y
		});
	}

	if($('umfrageContainer')) {
		var newX = (window.getSize().x - $('umfrageContainer').getSize().x)/2;
		var newY = (window.getSize().y - $('umfrageContainer').getSize().y)/2;
		$('umfrageContainer').setPosition({x: newX, y: newY});
	}
}

function showUmfrage() {
	
	myMask = new Element('div', {
		'id': 'umfrageMask',
		'styles': {
			'position':'absolute',
			'top': '0px',
			'left': '0px',
			'width': window.getSize().x,
			'height': window.getSize().y,
			'background-color': '#fff'
		}
	});
	myMask.inject($(document.body));
	myMask.addEvent('click', hideUmfrage);
	myMask.fade('hide');
	myMask.fade('0.7');
	umfrageContainer = new Element('div', {
		'id': 'umfrageContainer',
		'text': 'Lade Daten...'
	});
	umfrageContainer.inject($(document.body), 'bottom');
	umfrageContainer.fade('hide');
	umfrageContainer.fade('in');
	repositionDialogue();
	window.addEvent('resize', repositionDialogue);

	var umfrageRequest = new Request.HTML({
		'onSuccess': function (responseTree, responseElements, responseHTML, responseJavaScript) {
			//this.showNewPage(url, responseHTML);
			$('umfrageContainer').set('html', responseHTML);
			
			$('umfrageCloseBtn').addEvent('click', hideUmfrage);
			
			$('quelle_anders').addEvent('focus', function() {
				$('quelle_6').set('checked', true);
			});
				

			var frq = new Form.Request($('umfrageContainer').getElement('form'), $('umfrageContainer'), {
				'onSuccess': function () {
					$('umfrageCloseBtn').addEvent('click', hideUmfrage);
				},
				'onFailure': function(responseTree, responseElements, responseHTML, responseJavaScript){
					console.log('Could not load');
				}

    		});
			
		},
		'onProgress': function(event) {
			var loaded = event.loaded, total = event.total;
			console.log(loaded);
		 },
		'onFailure': function(responseTree, responseElements, responseHTML, responseJavaScript){
			console.log('Could not load');
		}
	});

	umfrageRequest.get('/fileadmin/voks/templates/scripts/umfrage.php');
}
window.addEvent('load', function() {
	if ($('showCustomerSurvey')) {
		window.setTimeout("showUmfrage();", 1000);
	}
});


