﻿var postofficeshop;

if (!postofficeshop) {
	postofficeshop = {};
}

$.extend(postofficeshop, {
	mainNavigation: {
		timeout: -1,
		openNode: null,
		handler: function(submenuNode, visible) {
			var activeLink = submenuNode.prev();
			var initialheight = $(document).height();
			if(visible) {
				activeLink.addClass('active');
				$('.mainNavigation>dl>dd>ul').hide();
				clearTimeout(postofficeshop.mainNavigation.timeout);
				submenuNode.show();
				if( initialheight < $(document).height() ){
					if($(submenuNode).height()>initialheight){
						$(submenuNode).css( 'top', $(document).scrollTop() );
					}
					else {
						$(submenuNode).css( 'bottom', 0 );
					}
				}
				
			} else {
				postofficeshop.mainNavigation.openNode = submenuNode;
				postofficeshop.mainNavigation.timeout = setTimeout("postofficeshop.mainNavigation.openNode.hide();", 500);
				activeLink.removeClass('active');
			}
		}
	},
	
	toggleTooltip: {
		timeout: -1,
		openNode: null,
		handler: function(element, visible) {
			var formrow = element.parents('.formRow');
			var tooltip = element.parent().find('.tooltip');
			if(visible) {
				//Remove old changes
				$('.formRow').removeClass('active');
				$('.formRow .tooltip').hide();
				
				formrow.addClass('active');
				clearTimeout(postofficeshop.toggleTooltip.timeout);
				var top = parseInt(tooltip.height())-parseInt(tooltip.find('.content').css('paddingTop'));
				tooltip.css('top',-top);
				tooltip.show();
			} else {
				postofficeshop.toggleTooltip.openNode = tooltip;
				postofficeshop.toggleTooltip.timeout = setTimeout("postofficeshop.toggleTooltip.openNode.hide();", 500);
				formrow.removeClass('active');
			}
		}
	},
	
	focusElement: function(element, visible) {
		var formrow = element.parents('.formRow');
		var tooltip = element.parent().find('.tooltip');
		if(visible) {
			$('.formRow').removeClass('active');
			$('.formRow .tooltip').hide();
			
			formrow.addClass('active');
			var top = parseInt(tooltip.height())-parseInt(tooltip.find('.content').css('paddingTop'));
			tooltip.css('top',-top);
			tooltip.show();
		} else {
			$('.formRow').removeClass('active');
			tooltip.hide();
		}
	},
	
	clearForm: function(element){
		// iterate each matching form
		return element.each(function() {
			// iterate the elements within the form
			$(':input', this).each(function() {
				var type = this.type, tag = this.tagName.toLowerCase();
				if (type == 'text' || type == 'password' || tag == 'textarea')
					this.value = '';
				else if (type == 'checkbox' || type == 'radio')
					this.checked = false;
				else if (tag == 'select')
					this.selectedIndex = -1;
			});
		});
	},
	
	init: function() {
		$('.jsHide').hide();
	
		$('.mainNavigation>dl>dd').each(function() {
			$(this).hoverIntent(function(){
				postofficeshop.mainNavigation.handler($('>ul', this), true);
			}, function(){
				postofficeshop.mainNavigation.handler($('>ul', this), false);
			});
		});
		
		$('.formRow:not(.formRow_submit) input, .formRow:not(.formRow_submit) textarea, .formRow:not(.formRow_submit) select').each(function() {
			/*
			$(this).hoverIntent(function(){
				postofficeshop.toggleTooltip.handler($(this), true);
			}, function(){
				postofficeshop.toggleTooltip.handler($(this), false);
			});
			*/
			$(this).focus(function(){
				postofficeshop.focusElement($(this), true);
			});
			$(this).blur(function(){
				postofficeshop.focusElement($(this), false);
			});
		});
		
		$(".subNaviTable td:has('a')").hover(function() {
			$(this).siblings().removeClass('active');
			$(this).addClass('active');
		}, function() {
			$(this).removeClass('active');
		});
		
		$(".subSubNavi td:has('a')").hover(function() {
			$(this).siblings().removeClass('active');
			$(this).addClass('active');
		}, function() {
			$(this).removeClass('active');
		});

		$(".waretable td:has('a')").hover(function() {
			$(this).removeClass('active');
			$(this).siblings().addClass('active');
			$(this).addClass('active');
		}, function() {
			$(this).siblings().removeClass('active');
			$(this).removeClass('active');
		});
		
		//checkout_lieferdaten_aendern.shtml
		$('fieldset.lieferdaten input').click(function(){
			var showElement = $('#lieferdaten_'+$(this).attr('id'));
			var parentfieldset = $(this).parents('fieldset');
			var fieldsets = parentfieldset.siblings('fieldset:not(:last)');
			//debugger;
			//postofficeshop.clearForm(fieldsets);
			fieldsets.hide();
			showElement.show();
		});
		
		//checkout_lieferdaten_aendern.shtml
		$('fieldset.zahlweg input').click(function(){
			var showElement = $('#zahlweg_'+$(this).attr('id'));
			var parentfieldset = $(this).parents('fieldset');
			var fieldsets = parentfieldset.siblings('fieldset:not(:last)');
			//postofficeshop.clearForm(fieldsets); /* <- Alle Felder werden geleert */ 
			fieldsets.hide();
			showElement.show();
		});

		$('fieldset.formBoxCheck').each(function() { // div. needed as Jquery 1.1.1 has a bug!
			//var current = $(this);
			$(this).click(function() {
				$("input", $(this)).attr("checked","checked");
				$('fieldset.formBoxCheck label').removeClass("lieferboxChecked");
				$('label', $(this)).addClass("lieferboxChecked");
			})

		});
		
		$('fieldset.checkPass').find('input:password').blur(function() {
			if($(this).val().length < 6) {
				alert("Geben Sie in das Passwortfeld bitte mindestens 6 Zeichen ein.");
				$(this).val("");
			}
			else {
				var alphanum = /^([a-zA-Z0-9_-]+)$/;
					if(alphanum.test($(this).val())==false) {
					alert("Sonderzeichen sind nicht erlaubt. Verwenden Sie bitte nur Ziffern oder Buchstaben (klein/groß).");
					$(this).val("");
					}
				}
		});
		
		
		// start Warenkorb Staffelpreisanzeige
		
		$(".buttonMenge").hide();
	
		var pSelector = '.staffelpreis'
		$(".show-buttonMenge").click(function () {
			var p = $(this).parents(pSelector)
			p.find('.buttonMenge').show()
			p.find('.staffel-label').hide()
		})
		$(".hide-buttonMenge").click(function () {
			var p = $(this).parents(pSelector)
			p.find('.buttonMenge').hide()
			p.find('.staffel-label').show()
		})

		
		// end Warenkorb Staffelpreisanzeige
		
		
	}

});

$(postofficeshop.init);