jQuery(document).ready(function() {
	
	jQuery('#topmenu ul li.hasSubMenu > a').each(function() {
		if (jQuery(this).attr('href') == '') {
			jQuery(this).attr('href', jQuery(this).parent().find('.subMenu a:first').attr('href'));
		}
	});
	
	jQuery('#topmenu ul li.hasSubMenu').mouseover(function() {
		var sub = jQuery(this).find('.subMenu');
		if (sub.css('display') == 'none') {
			jQuery(this).addClass('open');
			sub.show();
		}
	});

	jQuery('#topmenu ul li.hasSubMenu').mouseout(function(e) {
		var target = jQuery(e.relatedTarget);
		if (!target.hasClass('open') && target.parents('.open').length == 0) {
			jQuery(this).removeClass('open');
			jQuery(this).find('.subMenu').hide();
		}
	});
	
	jQuery('form[name=productsearchform] input[type=text], form#searchform input[type=text]').each(function() {
		var inp = jQuery(this);
		var lbl = inp.attr('title');
		
		if (lbl) {
			if (inp.val() == '') {
				inp.val(lbl);
			}
			inp.focus(function() {
				if (inp.val() == lbl) {
					inp.val('');
				}
			});
			inp.blur(function() {
				if (inp.val() == '') {
					inp.val(lbl);
				}
			})
		}
	});
	
	var teaserPanes = jQuery('#teaser .left .big');
	var currentTeaserPane = 0;
	var teaserInterval = false;

	initTeaserInterval = function() {
		if (teaserInterval) {
			clearInterval(teaserInterval);
			teaserInterval = false;
		}
		
		teaserInterval = setInterval(function() {
			showTeaser(currentTeaserPane + 1);
		}, 5000);
	}
	
	showTeaser = function(index) {
		if (index < 0) {
			index = teaserPanes.length - 1;
		}
		if (index > teaserPanes.length - 1) {
			index = 0;
		}
		if (index == currentTeaserPane) {
			return false;
		}
		
		jQuery(teaserPanes[currentTeaserPane]).fadeTo('fast', 0);
		currentTeaserPane = index;
		jQuery(teaserPanes[currentTeaserPane]).fadeTo('fast', 1);
		jQuery('#teaserNav a').removeClass('active');
		jQuery('#teaserNav a').eq(index).addClass('active');
	}
	
	if (teaserPanes.length > 0) {
		teaserPanes.hide();
		jQuery('#teaser .left .big:first').show();
		jQuery('#teaserNav a:first').addClass('active');
		
		initTeaserInterval();
	}
	
	jQuery('#teaserNav a').click(function() {
		showTeaser(parseInt(jQuery(this).attr('rel')));
		initTeaserInterval();
		
		return false;
	});
	
	jQuery('a.overlay').click(function() {
		jQuery('#gmcoverlay').remove();
		jQuery('body').append('<div id="gmcoverlay"></div>');
		var href = jQuery(this).attr('href');
		var title = jQuery(this).text();
		jQuery("#gmcoverlay").load(href + ' #content .page', function() {
			jQuery("#gmcoverlay").dialog({
				title: title,
				modal: true,
				height: 'auto',
				width: 'auto'
			});
		});
		return false;
	});

});
