(function($, undefined) {
var B = {};

B.History = {

		// create a link
	make: function(url, anchorEl) {
		_gaq.push(['_trackPageview']);
		if (!url.match(/(product|zoom|zoomvideo)\.html/)) {
			window.location.hash = '#.' + url;
		}
		if (anchorEl && $(anchorEl).attr('id')) {
			// remove any previous links
			if (window.location.hash.length && window.location.hash.indexOf('--') != -1) {
				window.location.hash = window.location.hash.substr(0, window.location.hash.indexOf('--'));
			}
			if (!window.location.hash) {
				window.location.hash = '#';
			}
			window.location.hash += '--' + $(anchorEl).attr('id');
		}
	}, 

		// used when visiting the page
	check: function() {
		var historyItem = window.location.hash;
		var currentUrl = window.location.href;
		if (historyItem.length && historyItem.indexOf('#!') != -1) {
			// remove the #!
			historyItem = historyItem.substr(2);
			// redirect to that page
			if (historyItem.length) {
				window.location.href = historyItem;
			}
		} else if (historyItem.length && historyItem.indexOf('#.') != -1) {
			// remove the #.
			historyItem = historyItem.substr(2);
			// redirect to that page
			if (historyItem.length) {
				if (historyItem.indexOf('--') && historyItem.indexOf('#') == -1) {
					historyItem = historyItem.replace(/--/, '#--');
				}
				window.location.href = historyItem;
			}
		} else if (historyItem.length && (historyItem.indexOf('--') != -1 || historyItem.indexOf('#-') != -1)) {
	
			if (historyItem.indexOf('--') != -1) {
				historyItem = historyItem.substr(historyItem.indexOf('--')+2);
			} else {
				historyItem = historyItem.substr(historyItem.indexOf('#-')+2);
			}
	
			
			if (historyItem.indexOf('?') != -1) {
				historyItem = historyItem.substr(0, historyItem.indexOf('?'));
			}
			
			// click on the link
			if (historyItem.length && $('#' + historyItem).length) {
				$('#' + historyItem).click();
			}
		} else if (currentUrl.indexOf('--') != -1) {
			currentUrl = currentUrl.replace(/--/, '#--');
			window.location.href = currentUrl;
			return;
		}
	}
}


B.Menu = {
	// dynamically set the margin top of the window height
	checkHeight: function() {
		var windowHeight = $(window).height();
		var contentHeight = $('#main').outerHeight();
		if (windowHeight < contentHeight) {
			var maxMarginTop = $('#header').height();	// 71px
			var minMarginTop = 30;
			var diff = (windowHeight - contentHeight) + 100;
				// the margintop can be between 71px (min, starting) and 30px (max)
			if (diff < minMarginTop) {
				diff = minMarginTop;
			} else if (diff > maxMarginTop) {
				diff = maxMarginTop;
			}
/* 			console.debug(diff); */
			$('#header').addClass('right');
			$('.section:first', '#main').css({
				'marginTop': diff + 'px'
			});
		} else {
			$('#header').removeClass('right');
			$('.section:first', '#main').css({'marginTop': 0});
		}
	}
}



$(document).ready(function() {

	b_initFormValidation();
	b_postcontentload();
	b_checkFooterHeight();

	// execute it right away
	B.History.check();

	// check the video
	if ($('#startvideoImage').length) {
		var startvideo = $('#startvideoImage');
		b_resizeImageToFitScreen(startvideo);
		$(window).resize(function() {
			b_resizeImageToFitScreen(startvideo);
		});

		flowplayer('startvideoImage', {
			'src': '/fileadmin/templates/flash/flowplayer.commercial-3.2.2.swf',
			'wmode': 'transparent'
		}, {
			'key': '#@9aef5eb1e5c22fe7286',
			'buffering': false,
			'clip': {
				'autoPlay': true,
				'autoBuffering': true,
				'controls': false,
				'onPause': function() {
					this.getPlugin('play').hide();
				},
				'onFinish': function() {
					this.getPlugin('play').hide();
				}
			},
			'plugins': {
				'controls': {
					'bottom': -50,
					'opacity': 0
				}
			}
		});
	}

	B.Menu.checkHeight();
	$(window).resize(function() {
		B.Menu.checkHeight();
	});

	
	// make sure that every link in the first level of the accordion opens
	// content in its own section area
	$('.firstLevel').click(function(evt) {
		b_removeStartvideo();
		// only show the content, if there's nothing else loading right now, 
		// and this link isn't opened right now
		if (!$(this).hasClass('active') && !b_contentisloading()) {
			b_precontentload();

			var url = $(this).attr('href');
			var sectionHeadline = $(this).parent().parent();

				// hide all submenus for now (we'll open the active one later)
			$('.sectionHeadline .subMenu').hide();
			$('a.active').removeClass('active');
			
			showSectionContent = function() {
				if (sectionHeadline.next().length == 0) {
					sectionHeadline.after('<div class="sectionContent" style="display: none;"></div>');
				}
				
				$.get(url, {
					'type': 13
				}, function(data) {
					sectionHeadline.next().html(data).slideDown();
					sectionHeadline.find('ul').show();
					sectionHeadline.find('ul a').first().addClass('active');
					b_postcontentload();
				}, 'html');
			};
			
			if ($('.sectionContent:visible, .footerContent:visible').length) {
				$('.sectionContent:visible, .footerContent:visible').slideUp(function() {
					showSectionContent();
					b_checkFooterHeight();
				});
			} else {
				showSectionContent();
			}
			
			$(this).addClass('active');
			if (!$(this).hasClass('external-link') && !$(this).attr('target')) {
				B.History.make(url);
			}
		}
		if (!$(this).hasClass('external-link') && !$(this).attr('target')) {
			evt.preventDefault();
		}
	});


	/** second level content **/
	$('.subMenu a').click(function(evt) {
		// only show the content, if there's nothing else loading right now, 
		// and this link isn't opened right now
		if (!$(this).hasClass('active') && !b_contentisloading()) {
			b_precontentload();

			$(this).parents('.subMenu').find('.active').removeClass('active');
			var url = $(this).attr('href');
			var section = $(this).parents('.section');
			var sectionContent = $('.sectionContent', section);
			b_showloader(sectionContent);
			// let the "logout" button proceed with a refresh,
			// so the navigation is loaded again
			if (url.indexOf('logout') > 0) {
				return;
			}
			$.get(url, {
				'type': 13
			}, function(data) {
				var currentHeight = sectionContent.height();
				sectionContent.html(data);
				b_postcontentload();
			}, 'html');
			
			$(this).addClass('active');
			if (!$(this).hasClass('external-link') && !$(this).attr('target')) {
				B.History.make(url);
			}
		}
		if (!$(this).hasClass('external-link') && !$(this).attr('target')) {
			evt.preventDefault();
		}
	});


	/** footer content **/
	$('.footerMenu a').click(function(evt) {
		b_removeStartvideo();
		// only show the content, if there's nothing else loading right now, 
		// and this link isn't opened right now, and it's not the close button
		if ($(this).hasClass('footerClose')) {
			$('a.active').removeClass('active');
		} else if (!$(this).hasClass('active') && !b_contentisloading()) {
			if ($(this).hasClass('external-link') || $(this).attr('target')) {
				return;
			}
			var url = $(this).attr('href');
			b_precontentload();
			var footerMenu = $('.footerMenu');
			// hide all submenus for now (we'll open the active one later)
			$('.sectionHeadline .subMenu').hide();
			$('a.active').removeClass('active');
			
			showFooterContent = function() {
				if (footerMenu.next().length == 0) {
					footerMenu.after('<div class="footerContent" style="display: none;"></div>');
				}
				$('.sectionContent, .footerContent').hide();
				b_removeOverlays();

				$.get(url, {
					'type': 13
				}, function(data) {
					footerMenu.addClass('footerMenuActive');
					footerMenu.next().hide().html(data).slideDown(function() {
						b_checkFooterHeight();
					});
					b_postcontentload();
				}, 'html');
			}
			
			if ($('.sectionContent:visible, .footerContent:visible').length) {
				$('.sectionContent:visible, .footerContent:visible').slideUp(showFooterContent);
			} else {
				showFooterContent();
			}
			
			$(this).addClass('active');
			if (!$(this).hasClass('external-link') && !$(this).attr('target')) {
				B.History.make(url);
			}
		}
		if (!$(this).hasClass('external-link') && !$(this).attr('target')) {
			evt.preventDefault();
		}
	});
	
	if ($('.footerContent').length) {
		$('.footerMenu').addClass('footerMenuActive');
	}
	
});

b_contentisloading = function() {
	return ($('body').data('isloading') == '1');
}

b_precontentload = function() {
	if ($('#googlemap').length && $('#googlemap').css('visibility') == 'visible') {
		b_uninitgooglemaps();
	}
	$('body').removeClass();
	$('.footerMenu').removeClass('footerMenuActive');
	$('body').data('isloading', '1');
}

b_postcontentload = function() {
	$('body').data('isloading', '0');

	if ($('#googlemap').length && $('#googlemap').css('visibility') != 'hidden') {
		b_initgooglemaps();
	}

	b_updatetitletag();
	
	/*** forms ***/
	b_applyFormTransformation();
	b_applyFormValidation();

	// make powermail do the submit
	$('.tx_powermail_pi1_fieldwrap_html_submit a', $('.ajaxForm')).unbind('click').click(function(evt) {
		var submitForm = $(this).parents('form').first();
		var action = submitForm.attr('action');
		action = action.replace(/&type\=13/, '');
		action = action.replace(/content\.html/, '');
		submitForm.attr('action', action);
		submitForm.submit();
		evt.preventDefault();
	});
	
	b_sharebutton();

	/** create scrollables ***/
	if ($('.collectionOverview').length) {

		if ($('.itemContainer', '.collectionOverview').length > 1) {
			// check if we need to fill the last container in order to show the latest items as well
			$('.collectionOverview').children().first().scrollable({
				'speed': 1000,
				'items': '.collectionOverviewSlider',
				'disabledClass': 'boxNavigationDisabled',
				'prev': '.boxNavigationLeft',
				'next': '.boxNavigationRight',
				'onBeforeSeek': function(evt, nextPos) {
					// if we're hitting the last item, make sure that we scroll right
					if (this.getItems().size() > 1 && this.getItems().size() == nextPos+1) {
	
						// check if we need to scroll the full width (= having 4 looks in there)
						var lastItem = this.getItems().last();
						// only do that with non-accessory items
						if (!lastItem.hasClass('itemContainerAccessory')) {
							var looksInLastItem = lastItem.children().length;
							if (looksInLastItem < 4) {
								var speed = this.getConf().speed / 4 * looksInLastItem;
								var leftPos = -lastItem.position().left;
								leftPos += ((4 - looksInLastItem) * lastItem.children().first().width());
								this.getItemWrap().animate({left: leftPos}, speed, this.getConf().easing);
								this.current = lastItem;
								this.index = nextPos;
								evt.preventDefault();
							}
						}
					}
	
				}
			});
		
		} else {
			$('.boxNavigationLeft, .boxNavigationRight', '.collectionOverview').hide();
		}
		$('.collectionOverview').addClass('collectionOverviewActivated').removeClass('collectionOverview');
	}

	var blogContainer = $('.blogOverview');
	if (blogContainer.length) {

		// try and remove any empty blogPage elements
		$('.blogPage', blogContainer).each(function() {
			if ($(this).children().length == 0) {
				$(this).remove();
			}
		});
		
		if ($('.blogPage', blogContainer).length > 1) {
			$('.blogBoxNavigationRight', blogContainer).removeClass('boxNavigationDisabled');
		}

		var maxHeight = 0;
		$('.blogColumn', $('.blogOverviewSlider')).each(function() {
			if ($(this).height() > maxHeight) {
				maxHeight = $(this).height();
			}
		});
		$('.blogOverview').height(maxHeight).find('.multiBoxInnerWrap').height(maxHeight).find('.multiBoxInnerWrap2').height(maxHeight);
		$('.blogOverview').children().first().scrollable({
			'speed': 1000,
			'items': '.blogOverviewSlider',
			'disabledClass': 'boxNavigationDisabled',
			'prev': '.boxNavigationLeft',
			'next': '.boxNavigationRight'
		});
		$('.blogOverview').addClass('blogOverviewActivated').removeClass('blogOverview');
	}
	if ($('.startpageBox').length) {
		var maxHeight = 0;
		$('.box', $('.startpageBox')).each(function() {
			if ($(this).height() > maxHeight) {
				maxHeight = $(this).height();
			}
		});
		$('.startpageBox').height(maxHeight);
		$('.startpageBoxInnerWrap').height(maxHeight);
		$('.startpageSlider').height(maxHeight);

/* 	alert($('.startpageBox').find('.startpageSlideItem').length);		 */
 		if ($('.startpageSlider').children().length > 1) { 
			$('.startpageBox').children().first().scrollable({
				'speed': 1000,
				'items': '.startpageSlider',
				'disabledClass': 'boxNavigationDisabled',
				'prev': '.boxNavigationLeft',
				'next': '.boxNavigationRight',
				'onSeek': function() {
				b_checkForSlideshowVideo();
				}
			});
/* alert('if'); */
		} else {
			$('.startpageBox .boxNavigationLeft').addClass('boxNavigationDisabled');
			$('.startpageBox .boxNavigationRight').addClass('boxNavigationDisabled');
/* alert('else'); */
		}

		b_checkForSlideshowVideo();
		$('.startpageBox').addClass('startpageBoxActivated').removeClass('startpageBox');
	}

	// check for videos within the startpage
	if ($('#sliderboxVideo').length) {
		$('#sliderboxVideo').width(880).height(480);
		flowplayer('sliderboxVideo', {
			'src': '/fileadmin/templates/flash/flowplayer.commercial-3.2.2.swf',
			'wmode': 'transparent'
		}, {
			'key': '#@9aef5eb1e5c22fe7286',
			'buffering': false,
			'clip': {
				'autoPlay': true,
				'autoBuffering': true,
				'controls': false,
				'onPause': function() {
					this.getPlugin('play').hide();
				},
				'onFinish': function() {
					this.getPlugin('play').hide();
				}
			},
			'plugins': {
				'controls': {
					'bottom': -50,
					'opacity': 0
				}
			}
		});
	}



	
	b_hideloader();


	
	/**
	 expose (from jquery tools), but only for IE8+ and other browsers
	*/
/*
	browserVersion = parseFloat(navigator.appVersion.split('MSIE')[1]);
	if (navigator.appVersion.indexOf('MSIE') != -1 && browserVersion > 5 && browserVersion < 8) {
	} else {
		$('.boxCollection, .boxProduct').unbind('mouseenter').bind('mouseenter', function() {
			$(this).expose({
				closeOnEsc: false,
				closeOnClick: false,
				'loadSpeed': 250,
				'closeSpeed': 0
			});
		});
		$('.collectionOverviewActivated').unbind('mouseleave').bind('mouseleave', function() {
			$.mask.close();
		});
	}
*/


	/** overlay / popups / layers **/
	$('.close, #collectionBox, #blogBox').unbind('click').bind('click', b_removeOverlays);
	$('.collectionBoxInnerWrap, .blogBoxInnerWrap').unbind('click').bind('click', function(evt) { evt.stopPropagation(); });

	$('.ajaxOverlay').unbind('click').bind('click', function(evt) {
	
		if ($('#collectionBox').length) {
			b_showloader($('.boxCollectionDetail'));
		}
		var url = $(this).attr('href');

		B.History.make(url, this);
		$.get(url, {
			'type': 14
		}, function(data) {
			b_removeOverlays();
			$('body').addClass('hasAjaxOverlay');
			$('body').prepend(data);
			//$('body').append(data);
			$('.ajaxOverlay').unbind('click');
			b_postcontentload();
		}, 'html');
		
		evt.preventDefault();
		evt.stopPropagation();
	});


	/** zoom overlays **/
	$('.zoomOverlay').unbind('click').bind('click', function(evt) {
		if ($('#zoomBox').length) {
			b_showloader($('#zoomBoxImage'));
		}

		$.get($(this).attr('href'), {
			'type': 15
		}, function(data) {
			b_removeZoom();
			$('body').addClass('hasZoomOverlay');
			$('body').prepend(data);
			//$('body').append(data);
			$('.zoomOverlay').unbind('click');
			b_postcontentload();
		}, 'html');

		evt.preventDefault();
		evt.stopPropagation();
	});
	$('.zoomvideoOverlay').unbind('click').bind('click', function(evt) {

		if ($('#videoBox').length) {
			b_showloader($('#videoBoxImage'));
		}

		$.get($(this).attr('href'), {
			'type': 16
		}, function(data) {
			$('body').addClass('hasZoomvideoOverlay');
			b_removeZoom();
			$('body').prepend(data);
			//$('body').append(data);
		if ($('#videoBox').length) {
			$('.zoomvideoOverlay').unbind('click');
		}
			b_postcontentload();
		}, 'html');

		evt.preventDefault();
		evt.stopPropagation();
	});
	$('#zoomBoxClose, #videoBoxClose').unbind('click').bind('click', b_removeZoom);
	
	// lookbook funtionality
	$('.saveToLookbook').unbind('click').bind('click', function(evt) {
		$.get($(this).attr('href'), {
			'type': 17
		}, function(data) {
			$('#lookbookItemCount').html(data);
			$('#myLookbookItemCount').html(data);
		});
		if ($(this).next().length) {
			$(this).hide();
			$(this).next().show();
		}
		evt.preventDefault();
		evt.stopPropagation();
	});
	$('.deleteFromLookbook').unbind('click').bind('click', function(evt) {
		var item = $(this).parents('.lookBookItem');
		$.get($(this).attr('href'), {
			'type': 17
		}, function(data) {
			$('#lookbookItemCount').html(data);
			$('#myLookbookItemCount').html(data);
			item.remove();
		});
		evt.preventDefault();
		evt.stopPropagation();
	});

	$('.blogOverlay').unbind('click').bind('click', function(evt) {
		
		var currentBlogItem = $(this);
		var nextBlogItem = $(this).parents('.boxBlog').next();
		if (!nextBlogItem.length) {
			nextBlogItem = $(this).parents('.blogColumn').next();
		}
		var prevBlogItem = $(this).parents('.boxBlog').prev();
		if (!prevBlogItem.length) {
			prevBlogItem = $(this).parents('.blogColumn').prev();
		}
	
		if ($('#blogBox').length) {
			b_showloader($('.boxBlogDetail'));
		}
	
		var url = $(this).attr('href');
		B.History.make(url, this);
		$.get(url, {
			'type': 18
		}, function(data) {
			$('.blogOverlay').unbind('click');
			$('body').addClass('hasBlogOverlay');
			b_removeOverlays();
			$('body').prepend(data);
			//$('body').append(data);
			b_postcontentload();
			if (prevBlogItem.length) {
				$('.browserLeft', $('#blogBox')).click(function(evt) {
					evt.preventDefault();
					evt.stopPropagation();
					$(prevBlogItem).find('.blogOverlay').last().click();
				});
			} else {
				$('.browserLeft', $('#blogBox')).addClass('browserDisabled');
			}
			if (nextBlogItem.length) {
				$('.browserRight', $('#blogBox')).click(function(evt) {
					evt.preventDefault();
					evt.stopPropagation();
					$(nextBlogItem).find('.blogOverlay').first().click();
				});
			} else {
				$('.browserRight', $('#blogBox')).addClass('browserDisabled');
			}


		}, 'html');
		
		evt.preventDefault();
		evt.stopPropagation();
	});
	
	// image navigation for blog entries
	if ($('#blogBox').length) {
	
	
		// allow navigation through the small bubbles inside the image
		$('.blogNavigationItem').unbind('click').click(function(evt) {
			$('.navigationItemActive').removeClass('navigationItemActive');
			$('.singleImage').hide();
			$(this).addClass('navigationItemActive');
			var className = $(this).children().first().attr('class');
			var singleImage = className.replace(/navigationItem/, 'singleImage');
			$('.' + singleImage).show();

			var prevNavItem = $(this).prev();
			if (prevNavItem.length) {
				$('.blogBoxNavigationLeft', $('#blogBox')).removeClass('boxNavigationDisabled').unbind('click').click(function(evt) {
					prevNavItem.click();
					evt.preventDefault();
					evt.stopPropagation();
				});
			} else {
				$('.blogBoxNavigationLeft', $('#blogBox')).addClass('boxNavigationDisabled').unbind('click');
			}

			var nextNavItem = $(this).next();
			if (nextNavItem.length) {
				$('.blogBoxNavigationRight', $('#blogBox')).removeClass('boxNavigationDisabled').unbind('click').click(function(evt) {
					nextNavItem.click();
					evt.preventDefault();
					evt.stopPropagation();
				});
			} else {
				$('.blogBoxNavigationRight', $('#blogBox')).addClass('boxNavigationDisabled').unbind('click');
			}
	
			evt.preventDefault();
			evt.stopPropagation();
		});
		
		// blog post loaded, no other image selected for now
		$('.blogBoxNavigationLeft', $('#blogBox')).addClass('boxNavigationDisabled');
		var nextNavItem = $('.navigationItemActive').next();
		if (nextNavItem.length) {
			$('.blogBoxNavigationRight', $('#blogBox')).click(function(evt) {
				nextNavItem.click();
				evt.preventDefault();
				evt.stopPropagation();
			});
		} else {
			$('.blogBoxNavigationRight', $('#blogBox')).addClass('boxNavigationDisabled');
		}
	}
	
	// my lookbook
	$('.previewMylookbook').unbind('mouseenter').mouseenter(function(evt) {
		var img = $(this).attr('rel');
		$('.chooseAddImgWrap').children().first().attr('src', img);
		$('.chooseAddImgWrap').show();
	});
	$('.previewMylookbook').unbind('mouseleave').mouseleave(function(evt) {
		$('.chooseAddImgWrap').hide();
	});
	$('.downloadPdf').unbind('click').click(function(evt) {
		var href = $(this).attr('href');
		if (href.indexOf('http://') == -1) {
			href = 'http://' + window.location.host + href;
			href = 'http://pdf.b13.de/render.html?url=' + encodeURI(href);
			$(this).attr('href', href);
		}
	});
	
	// lookbook: choose additional looks,
	// every time a checkbox is clicked, we check if it was de-selected
	// then we need to remove it directly from the lookbook
	$('.chooseAdditionalLooksCheckbox').change(function() {
		if (!$(this).attr('checked')) {
			var lookUid = $(this).val();
			$.get('/index.php', {
				'type': 17,
				'tx_renelezard_pi2[controller]': 'Lookbook',
				'tx_renelezard_pi2[action]': 'remove',
				'tx_renelezard_pi2[look]': lookUid
			}, function(data) {
				$('#lookbookItemCount').html(data);
				$('#myLookbookItemCount').html(data);
			});
		}
	});


	
	// press area
	if ($('.lookListNaviWrap').length) {
		$('.lookListSelector').unbind('click').click(function(evt) {
			var container = $(this).attr('rel');
			$(this).addClass('lookListSelectorActive');
			$('#' + container).show();
			$(this).siblings().each(function() {
				var container = $(this).attr('rel');
				$(this).removeClass('lookListSelectorActive');
				$('#' + container).hide();
			});
			evt.preventDefault();
		});
		
			// open up the first item on each list
		$('.lookListNaviWrap').each(function() {
			$(this).children(':first').click();
		});
		
	}
	
	
	$('.blogBoxNavigationLeft a, .blogBoxNavigationRight a').unbind('click').click(function(evt) {
		evt.preventDefault();
	});
	$('.startpageBoxActivated .boxNavigationLeft a, .startpageBoxActivated .boxNavigationRight a').unbind('click').click(function(evt) {
		evt.preventDefault();
	});
	
}

b_removeStartvideo = function(evt) {
	if ($('#startvideoImage').length) {
		$('#startvideoImage').unload();
		$('#videoBox').unload();
		$('#videoBox').hide();
		$('#starvideoImage').hide();
		$('#startpage').children().unwrap();
	}
}


b_removeOverlays = function(evt) {
	$('#collectionBox, #collectionBoxOverlay, #blogBox, #blogBoxOverlay').remove();
	$('body').removeClass();

	if (evt) {
		evt.preventDefault();
		evt.stopPropagation();
	}
	if ($('.footerContent').length && $('.footerContent').css('display') == 'block') {
		$('.footerContent').slideUp();
		$('#main').animate({
			'paddingBottom': '50px'
		});
		$('.footerMenu').removeClass('footerMenuActive');
	}
}

b_removeZoom = function(evt) {
	if ($('#videoBoxImage').length) {
		$('#videoBoxImage').unload();
		$('#videoBoxImage').hide();
	}
	if ($('#blogVideo').length) {
		$('#blogVideo').unload();
		$('#blogVideo').hide();
	}
	

	$('#zoomBox, #videoBox').remove();
	$('body').removeClass();

	if (evt) {
		evt.preventDefault();
		evt.stopPropagation();
	}
}


b_checkFooterHeight = function() {
	// check the height of the footer
	if ($('#footer').height() > $(window).height()) {
		//$('#main').css('paddingBottom', (50 + $('#footer').height()) + 'px');
		$('#main').animate({
			'paddingBottom': (50 + $('#footer').height())
		});
	} else {
		$('#main').css('paddingBottom', '50px');
	}
}

b_applyFormTransformation = function() {
	$('.tx_powermail_pi1_fieldwrap_html_select').jqTransform();
	$('.tx_powermail_pi1_fieldwrap_html_radio').jqTransform();
	if (!$('#newsletterform').length) {
		$('.tx_powermail_pi1_fieldwrap_html_check').jqTransform();
	}
	$('.jqTransform').jqTransform();
	
	
	
	// check for 
	if ($('.storefinderForm').length) {
		$('.checkboxWrapAll input').change(function() {
			if ($(this).attr('checked')) {
				var otherCbs = $(this).parents('.checkboxWrap').siblings();
				otherCbs.find('input').attr('checked', false);
				otherCbs.find('.jqTransformChecked').removeClass('jqTransformChecked');
				otherCbs.find('.jqTransformLabelChecked').removeClass('jqTransformLabelChecked');
			}
		});
		$('.checkboxWrapAll').siblings('.checkboxWrap').find('input').change(function() {
			if ($(this).attr('checked')) {
				$('.checkboxWrapAll').find('input').attr('checked', false);
				$('.checkboxWrapAll').find('.jqTransformChecked').removeClass('jqTransformChecked');
				$('.checkboxWrapAll').find('.jqTransformLabelChecked').removeClass('jqTransformLabelChecked');
			}
		});
	}
	
}

b_resizeImageToFitScreen = function(el) {
	el.css('position', 'absolute');
	var windowRatio = b_getWindowRatio();
	if (windowRatio > 177) {
		var videoHeight = Math.ceil($(window).width() / 1.77);
		el.height(videoHeight).width($(window).width());
		el.css({
			'top': '-' + Math.floor((videoHeight - $(window).height()) / 2) + 'px',
			'left': 0
		});
		
	} else {
		var videoWidth = $(window).height() * 1.77;
		el.width(videoWidth).height($(window).height());
		el.css({
			'left': '-' + Math.floor((videoWidth - $(window).width()) / 2) + 'px',
			'top': 0
		});
	}
}


b_initFormValidation = function() {
	$.tools.validator.addEffect('wall', function(errors, event) {

		// disable all existing errors
		this.getInputs().each(function() {
			$(this).parents('.tx_powermail_pi1_fieldwrap_html').removeClass('error');
		});

		//var container = $(this.getConf().container);
		var container = this.getForm();
		var errorTextContainer = $('.errorText', container).children().show();

		$.each(errors, function(index, error) {

			// erroneous input
			$(error.input).parents('.tx_powermail_pi1_fieldwrap_html').addClass('error');
			$(error.input).parents('.formRow').addClass('error');

			// error messages for the input (Array),
			// individual error messages not used right now
			// var msg = error.messages;
			// $.each(msg, function() {
			// 	errorTextContainer.append(msg);
			// });
		});
	
		// "hide" function
	}, function(inputs)  {
		$('div.errorText').children().hide();
		this.getForm().unbind('submit').submit();
		return true;
	});
}

b_applyFormValidation = function() {
	//Validate all forms
	$('.required').attr('required', 'true');
	//$('.validate-email').attr('type', 'email');
	// we cannot make HTML5 "email" input types, thus, we make it on our own
	//$('.validate-email').attr('type', 'email');
	$.tools.validator.fn('.validate-email', function(el, value) {
		var emailRe = /^([a-z0-9_\.\-\+]+)@([\da-z\.\-]+)\.([a-z\.]{2,6})$/i
		return !value || emailRe.test(value);
	});

	if ($('.ajaxForm').length) {
		$('.ajaxForm').validator({
			'effect': 'wall',
			'inputEvent': null,
			'errorInputEvent': null
		});
	}

	if ($('#newsletterform').length) {
		$('#privacy').unbind('change').bind('change', function(evt) {
			evt.stopPropagation();
			evt.preventDefault();
			return false;
		});
		$('.tx_powermail_pi1_fieldwrap_html_check').jqTransform();
	}

}

b_initgooglemaps = function() {
	if(document.getElementById("stores_radiusform") != null) document.getElementById("stores_radiusform").style.display = "";
	if(document.getElementById("stores_sidebar") != null) document.getElementById("stores_sidebar").style.display = "";
	document.getElementById("stores").style.height = "400px";
	drawMap_stores();
	$('#googlemap').css('visibility', 'visible');
}

b_uninitgooglemaps = function() {
	$('#googlemap').css('visibility', 'hidden');
	GUnload();
}

b_closestorebubble = function() {
	WecMap.get('stores').closeExtInfoWindow();
}

b_showloader = function(container) {
	$(container).before('<div class="sectionLoader" style="display: none;"></div>');
	var loader = $(container).prev();
	loader.css({
		'width':  '100%',
//		'width':  $(container).width(),
		'height': $(container).height()
	});
	loader.show();
}

b_hideloader = function() {
	$('.sectionLoader').remove();
}

b_updatetitletag = function() {
	if ($('div.titletag').length) {
		var titleTag = $('div.titletag').first().html();
		document.title = titleTag;
		$('div.titletag').remove();
	}
	if ($('div.previewimage').length) {
		var imageSrc = $('div.previewimage').first().html();
		$('#facebookImage').attr('href', '/' + imageSrc);
		$('div.previewimage').remove();
	}
}

b_sharebutton = function() {
	
	// check if we have a share button
//	if ($('#buttonShare').length) {
//		addthis.button('#buttonShare');
//	}
	if ($('#buttonShareFavorites').length) {
		//addthis.button('#buttonShareFavorites');
		addthis.toolbox('#toolboxShareFavorites');
	}
	if ($('#buttonShareLook').length) {
		//addthis.button('#buttonShareLook');
		addthis.toolbox('#toolboxShareLook');
	}
	if ($('#buttonShareBlog').length) {
		//addthis.button('#buttonShareBlog');
		addthis.toolbox('#toolboxShareBlog');
	}
	
//	addthis.button('.addthis_share');
//	addthis.toolbox('.addthis_toolbox');

}


b_makehistory = function(url, anchorEl) {
	_gaq.push(['_trackPageview']);
	if (!url.match(/(product|zoom|zoomvideo)\.html/)) {
		window.location.hash = '#.' + url;
	}
	if (anchorEl && $(anchorEl).attr('id')) {
		// remove any previous links
		if (window.location.hash.length && window.location.hash.indexOf('--') != -1) {
			window.location.hash = window.location.hash.substr(0, window.location.hash.indexOf('--'));
		}
		if (!window.location.hash) {
			window.location.hash = '#';
		}
		window.location.hash += '--' + $(anchorEl).attr('id');
	}
}

b_checkhistory = function() {
	var historyItem = window.location.hash;
	var currentUrl = window.location.href;
	if (historyItem.length && historyItem.indexOf('#!') != -1) {
		// remove the #!
		historyItem = historyItem.substr(2);
		// redirect to that page
		if (historyItem.length) {
			window.location.href = historyItem;
		}
	} else if (historyItem.length && historyItem.indexOf('#.') != -1) {
		// remove the #.
		historyItem = historyItem.substr(2);
		// redirect to that page
		if (historyItem.length) {
			if (historyItem.indexOf('--') && historyItem.indexOf('#') == -1) {
				historyItem = historyItem.replace(/--/, '#--');
			}
			window.location.href = historyItem;
		}
	} else if (historyItem.length && (historyItem.indexOf('--') != -1 || historyItem.indexOf('#-') != -1)) {

		if (historyItem.indexOf('--') != -1) {
			historyItem = historyItem.substr(historyItem.indexOf('--')+2);
		} else {
			historyItem = historyItem.substr(historyItem.indexOf('#-')+2);
		}

		
		if (historyItem.indexOf('?') != -1) {
			historyItem = historyItem.substr(0, historyItem.indexOf('?'));
		}
		
		// click on the link
		if (historyItem.length && $('#' + historyItem).length) {
			$('#' + historyItem).click();
		}
	} else if (currentUrl.indexOf('--') != -1) {
		currentUrl = currentUrl.replace(/--/, '#--');
		window.location.href = currentUrl;
		return;
	}
}


// get the ratio, needed for the video on the startvideo
b_getWindowRatio = function() {
	return Math.round($(window).width() / $(window).height() * 100);
}


b_checkForSlideshowVideo = function() {
	var i = 0;
	$('.sliderboxVideo').width(880).height(480);
	if ($('.sliderboxVideo').length) {
		$('.sliderboxVideo').each(function() {
			if ($(this).attr('id') == '') {
				$(this).attr('id', 'sliderboxVideo-' + (i++));
			}
		});

		$('.sliderboxVideoActive').each(function() {
			// console.debug('stopping ' + $(this).attr('id'));
			flowplayer().stop();
			$(this).removeClass('sliderboxVideoActive');
		});
		
		$('.sliderboxVideo').each(function() {
			var leftOffset = $(this).offset().left;
			if ((leftOffset+880) > $(window).width() || leftOffset < 0) {
				// console.debug('out of sight ' + $(this).attr('id') + ' (left offset ' + leftOffset + ')');
				return true;
			}
			var hasAutoplay = $(this).hasClass('sliderboxVideoAutoplay');
			$(this).addClass('sliderboxVideoActive');
			if ($(this).eq('object').length) {
				// console.debug('resuming ' + $(this).attr('id') + ' (left offset ' + leftOffset + ')');
				flowplayer($(this).attr('id')).play();
			} else {
				// console.debug('playing ' + $(this).attr('id') + ' (left offset ' + leftOffset + ')');
				var itm = $(this);
				var html5Code = itm.html();
				itm.html('');	// unset this so autoplay works
				
				flowplayer($(this).attr('id'), {
					'src': '/fileadmin/templates/flash/flowplayer.commercial-3.2.2.swf',
					'onFail': function() {
						itm.html(html5Code);
					},
					'wmode': 'transparent'
				}, {
					'key': '#@9aef5eb1e5c22fe7286',
					'buffering': false,
					'clip': {
						'autoPlay': (hasAutoplay ? true : false),
						'autoBuffering': true,
						'controls': false,
						'onPause': function() {
							this.getPlugin('play').hide();
						},
						'onFinish': function() {
							this.getPlugin('play').hide();
						}
					},
					'plugins': {
						'controls': {
							'bottom': -50,
							'opacity': 0
						}
					}
				});

			}

		});

	}
}

})(jQuery);

// stub for powermail
var Validation = function() {
}
