Shadowbox.init({
	skipSetup: true,
	overlayOpacity: 0.75,
	overlayColor: '#FFF'
});

$(document).ready(function(){
	// Default-Texte in Formularen
	var defaultValues = new Array();
	$('#fwNewsletter form input#email, #fw.kontakt #fwBodyInner input[type=text], #fw.kontakt #fwBodyInner textarea').each(function(){
		var defaultVal = $(this).val();
		defaultValues[$(this).attr('id')] = defaultVal;
		$(this).focus(function(){
			if($(this).val()==defaultVal) {
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val()=='') {
				$(this).val(defaultVal);
			}
		});
		
	});

	// Newsletter-Formular
	$('#fwNewsletter form').submit(function(){
		$('#fwNewsletterReply').fadeOut('fast').load($(this).attr('action'), $(this).serialize(), function(){
			$(this).fadeIn();
		});
		return false;
	});

	// Karussell für Homepage Geschichten
	$('#fwStories ul').jcarousel({
		vertical: false,
		scroll: 1,
		auto: 12,
		wrap: 'circular'
	});

	// Weiter-Link für Geschichten
	$('a.nextStory').click(function(){
		var carousel = $('#fwStories ul').data('jcarousel');
		carousel.next();
		return false;
	});

	// Karussell für Homepage Teaser
	$('#fwTeaser ul').jcarousel({
		vertical: false,
		scroll: 1,
		auto: ($('#fwTeaser ul li').length>3 ? 6 : 0),
		wrap: 'circular'
	});

	// "Klick für mehr Infos" Hover-Label für Produktgruppenseiten
	$('#fwGroup a.m').hover(function(){
		$('#mapHover').show();
		var bodyPos = $('#fwBodyInner').position();
		$(this).mousemove(function(e){
			$('#mapHover').css({
				top: (e.pageY-bodyPos.top+10)+'px',
				left: (e.pageX-bodyPos.left+10)+'px'
			});
		});
	}, function(){
		$('#mapHover').hide();
		$(this).unbind('mousemove');
	});

	// Bildwechsel auf Produktseiten
	imgChanger('#fwProductImgSmall ul li a', '#fwProductImgLarge', '.imgLarge');
	imgChanger('#fwProductColors ul li a', '#fwProductImgMain', '.imgMain');

	// Kontakt Formular
	if($('#fw.kontakt #fwBodyInner form').length>0) {
		// Remove typo3's input validation, will be reused after own function
		var formValidate = $('#fw.kontakt #fwBodyInner form').attr('onsubmit');
		$('#fw.kontakt #fwBodyInner form').removeAttr('onsubmit');
		$('#fw.kontakt #fwBodyInner form').submit(function(){
			$('input[type=text], textarea', this).each(function(){
				if($(this).val() == defaultValues[$(this).attr('id')]) {
					$(this).val('');
				}
			});
			return eval(formValidate.substring(7)); // substring: Remove "return"
		});
	}
	
	$('form#shopSearch').submit(function(){
		var searchUrl = $(this).attr('action')+'?'+$(this).serialize();
		openShopSearchResult(searchUrl, 650);
		return false;
	});

	$('a#shopSearchOnline').click(function(){
		openShopSearchResult($(this).attr('href'), 300);
		return false;
	});
});

function openShopSearchResult(url, height) {
	Shadowbox.open({
		content:    url,
		player:     "iframe",
		title:      "Finden!",
		height:     height,
		width:      400
	});
}

function imgChanger(linkSelector, imageContainer, imageClass) {
	$(linkSelector).click(function(){
		var id = $(this).attr('rel');
		if(!$(imageContainer+' #'+id).is(':visible')) {
			$(imageContainer+' '+imageClass).fadeOut('slow');
			$(imageContainer+' #'+id).fadeIn('slow');
		}
		return false;
	});
	$(imageContainer+' '+imageClass).click(function(){
		var nextId = $(this).next().attr('id');
		if(nextId==undefined) {
			nextId = $(this).parent().children(':first-child').attr('id');
		}
		if(!$(imageContainer+' #'+nextId).is(':visible')) {
			$(imageContainer+' '+imageClass).fadeOut('slow');
			$(imageContainer+' #'+nextId).fadeIn('slow');
		}
	});
}
