$(document).ready(function() {

	//redirectIfBrowserIsMobileClient(navigator.userAgent);
	
	setSubnavigationWidth();
	initializeNavigation();
	
	initializeCloseButton();
	
	initializeLawyerHoverEffects();
	
	hideNewsletterFormField();
	
	$("a.email").each(function(i) {
		
		$sMailto = $(this).attr("href");
		$sEmail  = $(this).html();

		$(this).attr("href", "mailto:" + rot13($sMailto));
		$(this).html(rot13($sEmail));		
	
	});
	
	$("span.emailwarning").remove();
	
});

var rot13map;

function isBrowserMobileClient(userAgent)
{
	var mobileClients = ["iphone","midp","240x320","blackberry","netfront","nokia","panasonic","portalmmm","sharp","sie-","sonyericsson","symbian","windows ce","benq","mda","mot-","opera mini","philips","pocket pc","sagem","samsung","sda","sgh-","vodafone","xda"];

	userAgent=userAgent.toLowerCase();
	for (var i=0; i < mobileClients.length; i++)
	{
		if (userAgent.indexOf(mobileClients[i]) != -1)
			return true;
	}
	
	return false;

}

function redirectIfBrowserIsMobileClient(userAgent)
{
	if(isBrowserMobileClient(userAgent))
		document.location.href="http://mobil.oppenhoff.eu";
	return;
}

function initializeLawyerHoverEffects()
{
	$('div#lawyer div.lawyer_port').mouseover(function() {
		
		$sId = $('a.hover_effect', this).attr('id');
		
		$('div#lawyer div.hovered:not(#'+$sId+')').trigger('mouseout');	
		
		$(this).addClass("hovered");
		
		$('> div.info', this).show();
		$("ul#lawyer_list li a#r"+ $sId).toggleClass("highlight");
		
		var eImgActive = $('img.imageActive',this);
		var eImgInActive = $('img.imageInactive',this);
		
		var oldSrc = $(eImgActive).attr('src');
		$(eImgActive).attr('src', $(eImgInActive).attr('src'));
		$(eImgInActive).attr('src', oldSrc);

	}).mouseout(function() {	
		if($(this).hasClass("hovered")){		
			$(this).removeClass("hovered");
			
			$sId = $('a.hover_effect', this).attr('id');
			
			var eImgActive = $('img.imageActive',this);
			var eImgInActive = $('img.imageInactive',this);
			
			var oldSrc = $(eImgActive).attr('src');
			$(eImgActive).attr('src', $(eImgInActive).attr('src'));
			$(eImgInActive).attr('src', oldSrc);
			
			$('> div.info',this).hide();
			$("ul#lawyer_list li a#r"+ $sId).toggleClass("highlight");
		}
	});
	
	$('ul#lawyer_list a').mouseover(function() {

		$(this).addClass("hovered");
	
		$sId = this.id.substr(1);
				
		var $targetElement = $("div#lawyer div.lawyer_port a#"+ $sId + " ~ div.info");
		$targetElement.show();
		
		var $targetDiv = $targetElement.parent();
		
		var eImgActive = $('img.imageActive',$targetDiv);
		var eImgInActive = $('img.imageInactive',$targetDiv);
		
		var oldSrc = $(eImgActive).attr('src');
		$(eImgActive).attr('src', $(eImgInActive).attr('src'));
		$(eImgInActive).attr('src', oldSrc);
		
	}).mouseout(function() {		
		if($(this).hasClass("hovered")){		
			$(this).removeClass("hovered");
			$sId = this.id.substr(1);
					
			var $targetElement = $("div#lawyer div.lawyer_port a#"+ $sId + " ~ div.info");
			
			$targetElement.hide();
			
			var $targetDiv = $targetElement.parent();
			
			var eImgActive = $('img.imageActive',$targetDiv);
			var eImgInActive = $('img.imageInactive',$targetDiv);
			
			var oldSrc = $(eImgActive).attr('src');
			$(eImgActive).attr('src', $(eImgInActive).attr('src'));
			$(eImgInActive).attr('src', oldSrc);
		}
	});
}

/**
 * Calculates custom width for subnavigations
 * 
 * @return null
 */
function setSubnavigationWidth()
{
	var width_left = 0;
	var sub_width = 0;
	
	$('li.level0').each(function(i) {
		
		$(this).removeClass();
		
		width_left += (this.offsetWidth + 30);

		sub_width = 0;
		
		$('ul li', this).each(function(i) {
		
			$(this).parent('ul').css('visibility', 'hidden');
			$(this).parent('ul').css('display', 'block');
			sub_width += (this.offsetWidth+15);
			$(this).parent('ul').css('display', 'none');
			$(this).parent('ul').css('visibility', 'visible');
		});
		
		if((930 - width_left) <= sub_width)
		{
			$('ul', this).css('width', (930 - width_left));	
		}
		else
		{
			$('ul', this).css('width', sub_width);
		}

	});

}

/**
 * Initialize Navigation
 * @return null
 */
function initializeNavigation()
{
	$('#navigation ul li').mouseover(function() {
		
		$('.current').addClass('current_hidden');
		$('.current').removeClass('current');

		$('#navigation ul li ul').hide();
		
		$('ul',this).show();
		
	}).mouseout(function() {
		$('ul',this).hide();

		$('.current_hidden').addClass('current');
		$('.current_hidden').removeClass('current_hidden');
	
	});
}
 

function initializeCloseButton()
{
	$('#close').css('visibility', 'visible');
	
	$('#close').live('click', function() {
		
		$('#contentpage').hide();
	
	});
}

function hideNewsletterFormField()
{
	$("#username").hide();		
}

function rot13init()
{
	var map = new Array();
	var s   = "abcdefghijklmnopqrstuvwxyz";

	for (i=0; i<s.length; i++)
	map[s.charAt(i)]			= s.charAt((i+13)%26);
	for (i=0; i<s.length; i++)
	map[s.charAt(i).toUpperCase()]	= s.charAt((i+13)%26).toUpperCase();
	return map;
}

function rot13(sString)
{
	try	{
		if(sString.substr(0, 7) == "mailto:")
			sString = sString.substr(7);

		if (!rot13map)
			rot13map = rot13init();

		s = "";
		for (i=0; i<sString.length; i++)
		{
			var b = sString.charAt(i);
			s	+= (b>='A' && b<='Z' || b>='a' && b<='z' ? rot13map[b] : b);
		}
		return s;
	} catch (e) {}
}


