$(document).ready( function() {
	
	$('div.vertical-menue-counter-4,div.vertical-menue-counter-5').addClass('flip-tooltip');
	
	// apply tootip function only to menu entries with sub menues
	$('#menue-main li').each( function(index, elem) {
		//if ($(elem).find('.submenue-row') && $(elem).find('.submenue-row').length) {
		if ($(elem).find('.trigger-submenues-box') && $(elem).find('.trigger-submenues-box').length) {			
			// apply only to first link
			$(elem).find('a').first().tooltip({ effect: 'toggle',  relative: true,
			position: 'top left',
				onBeforeShow: function() {
					var wrapperMainOffsetLeft=$('#wrapper-content-main').offset().left;
					var offsetLeft=-20;
					var offsetTop=25;
					var offsetLeftFlippedTooltip=169;
					var submenuWidth=this.getTip().width();
					var submenuHeight=this.getTip().height();

					this.getConf().offset[1]=submenuWidth+offsetLeft;
					this.getConf().offset[0]=submenuHeight+offsetTop;
					
					// use different offset if tool tip is flipped (this prevents tooltips to be positioned out of viewport)
					var tooltip=this.getTip();
					if (tooltip.hasClass('flip-tooltip')) {
						this.getConf().offset[1]=offsetLeftFlippedTooltip;
					}
					
					
				}
			});

		}
	});
	
	// add hover state for main menu if submenu is expanded
	$('.menue-vertical').mouseenter( function() {
		$(this).addClass('hover');
	});

	$('.menue-vertical').mouseleave( function() {
		$(this).removeClass('hover');
	});
	
	
});

function followUrl(url) {
	var aMatches=url.match(/http:\/\//);

	if (aMatches && aMatches.length>0) {
		window.location.href=url;
	}
	else {
		if (!url.match(/^\//)) {
				url='/'+url;
		}

		url=document.location.protocol+"//"+document.location.host+url;

		window.location.href=url;

	}

}

$(document).ready( function() {
	// target=_blank replacement
    $('a.new-window,a[rel="new-window"]').click( function() {
    	var link=$(this).attr('href');
    		// fix missing / for IE and base href
    		if (!link.match(/^http:|https:|ftp:|mailto|#/i)) {
    			  if (!link.match(/^\//i)) { 
    			     link='/'+link;
    			  }
    			}
            window.open(link, 'new_window');
            return false;
    });
    
    // tracking of external manufacturer links via google
    $('.block-hersteller a[class!="geraete-ratgeber"][class!="kuechen-ratgeber"][class!="zubehoer-ratgeber"]').attr('onclick', 'return false;');
    $('.block-hersteller a[class!="geraete-ratgeber"][class!="kuechen-ratgeber"][class!="zubehoer-ratgeber"]').click( function(ev) {
		if (typeof globalGaAccount!='undefined') {
    		_gaq.push(['_setAccount', globalGaAccount.getAccount()]);
    	}
    	_gaq.push(['_trackEvent', 'Outbound Links', this.title]);
    	followUrl(this.href)
   	});

});

