/*
Dependencies: jquery
*/

/*
	Navigation bar, Mega Drop down and language chooser
*/

var baseURL = window.location.protocol+'//'+window.location.host+'/';
var currentURL = window.location.href

Nav = {}

Nav.Init = function (){
	Nav.MegaDropDown = $("#MegaDropDown");
	Nav.LangDropDown = $("#LangDropDown");
	Nav.LangChooser = $('#LangChooser');
	
	Nav.LangChooser.hover(Nav.ShowLanguages, Nav.HideLanguages);
	Nav.LangDropDown.hover(Nav.ShowLanguages, Nav.HideLanguages);
	Nav.LangDropDown.find('li').hover(Nav.addHover, Nav.removeHover);
	
	
	Nav.MegaDropDown.hover(Nav.ShowDropDown, Nav.HideDropDown);
	
	
	// select additional Columns for init
	var additionalCols = $('#MegaDropDownSource .additionalCols');
	
	
	Nav.PageContainer = $('#PageContainer');
	var NavItems = $("#SiteNav li");
	var MegaDropDownSources = $('#MegaDropDownSource .DropDownContents');
	for (var i=0;i<NavItems.length;i++){
	
		//additionalColsHtml = $(additionalCols[i]).html();
		var additionalCol = $(additionalCols[i]).find('div.additionalCol');
		
		// delete empty additional cols(if no content element was given for that)
		for (var k=0;k<additionalCol.length;k++){
			// remove empty col container
			if($(additionalCol[k]).html() == '') {
				$(additionalCol[k]).remove();
			}
			// add class if text with picture was chosen
			textpic = $(additionalCol[k]).find('div.csc-default').find('div.csc-textpic');
			if(textpic.length > 0) {
				$(additionalCol[k]).addClass('Highlight');
			}
		}
		
		// update col data, maybe some ".additionalCol"-elements have been removed
		var additionalCol = $(additionalCols[i]).find('div.additionalCol');
		
		// add class "Last" to last additional column
		if(additionalCol.length == 1) {
			if($(MegaDropDownSources[i]).find('div.ColumnContainer').find('div.First').html() != null) {
				$(MegaDropDownSources[i]).find('div.ColumnContainer').addClass('TwoColumnsHighlight');
			}
			else {
				$(MegaDropDownSources[i]).find('div.ColumnContainer').addClass('test');
			}
			$(additionalCol[0]).addClass('Last');
		}
		else if(additionalCol.length == 2){
			if($(MegaDropDownSources[i]).find('div.ColumnContainer').find('div.First').html() != null) {
				$(MegaDropDownSources[i]).find('div.ColumnContainer').addClass('ThreeColumns');
			}
			else {
				$(MegaDropDownSources[i]).find('div.ColumnContainer').addClass('TwoColumns');
			}
			$(additionalCol[1]).addClass('Last');
		}
		
		// check if there is at least 1 column in the current dropdowncontent div
		allCols = $(MegaDropDownSources[i]).find('div.ColumnContainer').find('div.Col');
		if(allCols.length == 0) {
			$(MegaDropDownSources[i]).html('');
		}
		
		$(MegaDropDownSources[i]).find('div.Clear').before($(additionalCols[i]).html());
		
		$(NavItems[i]).hover(RolloverFuncMaker(NavItems[i], MegaDropDownSources[i]), Nav.HideDropDown)
	}
	
	function RolloverFuncMaker (NavItem, Source){
		return function (){
			Nav.OpenMegaDropDown($(NavItem), $(Source))
		}
	}
}

Nav.ShowLanguages= function (){
	Nav.LangDropDown.show();
	Nav.LangChooser.find('li').addClass('over');
	Nav.LangDropDown[0].style.left = (Nav.LangChooser.offset().left) + "px";
	Nav.LangDropDown[0].style.top = (Nav.LangChooser.offset().top + Nav.LangChooser.outerHeight()) + "px";
}

Nav.HideLanguages = function (){
	Nav.LangChooser.find('li').removeClass('over');
	Nav.LangDropDown.hide();
}

Nav.addHover = function() {
	$(this).addClass('over');
}

Nav.removeHover = function() {
	$(this).removeClass('over');
}


Nav.OpenMegaDropDown = function(theNavItem, theSource) {
	
	//Reset
	var TargetContentDiv = $("#MegaDropDown .DropDownContents");
	
	TargetContentDiv.children().remove();
	var SourceContents = theSource.children();
	if (SourceContents.length == 0) {
		// No contents for this drop down, simply don't show it
		return;
	}
	else {
		SourceContents.clone().appendTo(TargetContentDiv);
	}

	
	//position layer
	var PageRight = Nav.PageContainer.offset().left + Nav.PageContainer.outerWidth();
	var ParentLeft = $('#Top').offset().left;
	
	//alert(Nav.MegaDropDown.outerWidth() + ' + ' + theNavItem.offset().left + ' < ' + PageRight);
	if (Nav.MegaDropDown.outerWidth() + theNavItem.offset().left < PageRight) {
		Nav.MegaDropDown[0].style.left = theNavItem.offset().left - ParentLeft + "px";
		//alert('if left: ' + theNavItem.offset().left + ' - ' + ParentLeft + ' = ' + Nav.MegaDropDown[0].style.left);
	}
	else {
		//Stop it poking off the page
		Nav.MegaDropDown[0].style.left = PageRight - Nav.MegaDropDown.outerWidth()- ParentLeft + "px";
		//alert('else left: ' + Nav.MegaDropDown[0].style.left);
	}
	Nav.MegaDropDown[0].style.top = theNavItem.offset().top +  theNavItem.outerHeight() - 1  + "px";
	
	// Set nav item active
	Nav.activeNavItem = theNavItem;
	if (theNavItem.hasClass('act')) {
		Nav.activeNavItem.RestoreToAct = true;
	}
	Nav.ShowDropDown ();
}


Nav.ShowDropDown = function (){
	if (!Nav.activeNavItem.hasClass('act')){
		Nav.activeNavItem.addClass("act");
	}
	Nav.activeNavItem.addClass("ShowDropDown");
	Nav.MegaDropDown.show();
}

Nav.HideDropDown = function (){
	if(!Nav.activeNavItem.RestoreToAct) {
		Nav.activeNavItem.removeClass("act");
	}
	Nav.activeNavItem.removeClass("ShowDropDown");
	Nav.MegaDropDown.hide();
}



/*
	Homepage 
*/

Home = {}

Home.Init = function (){
	if (!($('body').hasClass('Home'))) return; // Not the homepage
							 
	Home.ValueCycleInit();
	Home.SlideShow = $('.Home #Slides');
	Home.Slides = Home.SlideShow.find('.Slide');
	Home.SlideShowNav = Home.SlideShow.find('.HorizontalNav li');
	Home.SlideShowNav.hover(Home.SlidesNavOver, Home.SlidesNavOut);
	for(var i=0;i<Home.SlideShowNav.length;i++) {
		$(Home.SlideShowNav[i]).click(NavClickHandlerMaker (i));
	}
	
	function NavClickHandlerMaker (pos){
		return function () {
			Home.ShowSlide(pos);
		}
	}
}

Home.SlidesNavOver = function(){
	$(this).addClass('over');
}

Home.SlidesNavOut = function(){
	$(this).removeClass('over');
}

Home.ShowSlide = function(pos){
	Home.Slides.hide();
	$(Home.Slides[pos]).show();
	
	Home.SlideShowNav.removeClass('act');
	Home.SlideShowNav.removeClass('over');

	var theItem = $(Home.SlideShowNav[pos])
	theItem.addClass('act');
	
	var theMarker = Home.SlideShow.find('.CurrentMarker');
	theMarker.css('left', (theItem.outerWidth() * (pos + 0.5)) - (theMarker.outerWidth() / 2) + "px");
}

Home.ValueCycleInit = function (){
	var hoverImg = new Image();
	hoverImg.src = "/fileadmin/sys/gfx/value_cycle_over.png";
	$('#ValueCircle a').append(hoverImg);
	$(hoverImg).addClass('hoverImg').hide();
	$('#ValueCircle').hover(function(){$(hoverImg).fadeIn(200)}, function () {$(hoverImg).fadeOut(200)})
}

/*
	Accordeon 
*/

Accordeon = {}

Accordeon.Init = function(){

	// first add neccessary classes to content elements
	// add header classes
	$('.Accordeon .csc-header h2').addClass('AccordeonHeader');
	$('.Accordeon .csc-header h3').addClass('AccordeonHeader');
	$('.Accordeon .csc-header h4').addClass('AccordeonHeader');
	// move AccordeonHeadline before AccordeonContent
	$('.Accordeon').each( function() {
		$(this).prepend( $(this).find('div.csc-header') );
	});

	Accordeon.Accordeons = $('.Accordeon');
	for (var i=0;i<Accordeon.Accordeons.length;i++) {
		$(Accordeon.Accordeons[i]).find('.AccordeonHeader').click(MakeClickHandler(i));
		// if accordeon cookie value is set and current accordeon element has been opened last time -> open it from beginning
		if( document.cookie.search('Accordeon_' + pid) != -1 && $(Accordeon.Accordeons[i]).attr('id') == readCookieValue('Accordeon_' + pid)) {
			ToggleAccordeon(i);
		}
	}
	
	function MakeClickHandler (pos) {
		return function (){
			ToggleAccordeon(pos);
		}
	}

	function ToggleAccordeon (pos) {
		var theAccordeon = $(Accordeon.Accordeons[pos]);
		var theAccordeonContent = theAccordeon.find('.AccordeonContent');
		var accCookieKey = 'Accordeon_' + pid;
		if(document.cookie.search(accCookieKey) != -1) {
			removeCookieValue(accCookieKey);
		}
		
		if (theAccordeon.hasClass('AccordeonOpen')) {
			theAccordeon.removeClass('AccordeonOpen');
			theAccordeonContent.css('display', 'block');
			theAccordeonContent.slideUp('fast');
		}
		else {
			$('.AccordeonOpen .AccordeonContent').css('display', 'block');
			$('.AccordeonOpen .AccordeonContent').slideUp('fast');
			$('.Accordeon').removeClass('AccordeonOpen');
			theAccordeon.addClass('AccordeonOpen');
			theAccordeonContent.css('display', 'none');
			theAccordeonContent.slideDown('fast');
			addCookieValue(accCookieKey, $(theAccordeon).attr('id'));
		}
	}
	
	// each accordeon can be opened independently of each other
	/*function ToggleAccordeon (pos) {
		var theAccordeon = $(Accordeon.Accordeons[pos]);
		var theAccordeonContent = theAccordeon.find('.AccordeonContent');
		
		if (theAccordeon.hasClass('AccordeonOpen')) {
			theAccordeon.removeClass('AccordeonOpen');
			theAccordeonContent.css('display', 'block');
			theAccordeonContent.slideUp('fast');
		}
		else {
			theAccordeon.addClass('AccordeonOpen');
			theAccordeonContent.css('display', 'none');
			theAccordeonContent.slideDown('fast');
		}
	}*/
}


/*
	Cookies 
	Read, Write and Delete cookie values. Used to determine if an accordeon or gptab element
	has been left open, when the user has left the page before.
*/

/*
 *	Adds a cookie key/value pair
 */
function addCookieValue (key, value) {
	var currDate = new Date();
	var expiringDate = new Date( currDate.getTime() + ( 7 * 86400 ) ); // 1 week
	document.cookie = key + "=" + value + ";expires=" + expiringDate.toGMTString() + ";";
}

/*
 *	Deletes the value for a given cookie key
 */
function removeCookieValue (key) {
	var expiringDate = new Date(70,1,1,0,0,1); // in past so value will be deleted
	document.cookie = key + "=0;expires=" + expiringDate.toGMTString() + ";";
}

/*
 *	Returns the value for a given cookie key
 */
function readCookieValue (key) {
	var cookieContent = '';
	var cookieValue = '';
	
	if(document.cookie) {
		cookieContent = document.cookie;
		cookieContent = cookieContent.slice(cookieContent.indexOf(key + '=')+key.length+1, cookieContent.length);
		cookieValue = cookieContent.slice(0, cookieContent.indexOf(';'));
	}
	
	return cookieValue;
}


/*
	Adapt size 
	Checks the size of the screen and adds an extra class to the body if necessary
*/

SizeManager = {}
SizeManager.config = {}
SizeManager.PreviousHeight = 0;
SizeManager.PreviousWidth = 0;
/*
	Styles need to be ordered by smallest to largest, as JSON array, eg
	
	SizeManager.config.heightStyles = {
		400 : 'hgt400px',
		600 : 'hgt600px'
	}	
*/
SizeManager.config.heightStyles = {
	800 : 'hgt800px'
}

SizeManager.config.widthStyles = {
	
}

SizeManager.init = function(){
	$(window).resize(SizeManager.AdaptSize);
	SizeManager.AdaptSize();
}

SizeManager.AdaptSize = function () {
	var WinHeight = $(window).height();
	var WinWidth= $(window).height();
	
	//IE7 fires resize a bit too often and crashes. This prevents it.
	if (WinHeight == SizeManager.PreviousHeight && WinWidth == SizeManager.PreviousWidth) {
		return;
	}
	
	SizeManager.PreviousHeight = WinHeight;
	SizeManager.PreviousWidth = WinWidth;
	
	SizeManager.ClearStyles();
	
	addStyle(SizeManager.config.heightStyles, WinHeight);
	addStyle(SizeManager.config.widthStyles, WinWidth);
	
	function addStyle(theCollection, WindowSize) {
		for (theMeasure in theCollection) {
			if (WindowSize <= theMeasure) {
				$('body').addClass(theCollection[theMeasure]);
				break;
			}
		}
	}
}

SizeManager.ClearStyles = function (){
	RemoveStyles (SizeManager.config.heightStyles);
	RemoveStyles (SizeManager.config.widthStyles);
	
	function RemoveStyles (theCollection) {
		for (theMeasure in theCollection) {
			$('body').removeClass(theCollection[theMeasure]);
		}
	}
	
} 

/*
	GP Tabs 
	JS click event handling and CSS adaption for gptabs elements.
*/
function GPTabsInit() {
	// styling of the teaser tab conteiner (adjust height that each container has the same height)
	cfHeight = $('.tx-gptabs-pi1 .TabContainer .TabCFTeaser').height() + 50;
	mcHeight = $('.tx-gptabs-pi1 .TabContainer .TabMCTeaser').height() + 50;

	if(cfHeight > mcHeight) {
		$('.tx-gptabs-pi1 .TabContainer .TabCFTeaser').css('height', cfHeight+'px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTeaser').css('height', cfHeight+'px');
	}
	else {
		$('.tx-gptabs-pi1 .TabContainer .TabCFTeaser').css('height', mcHeight+'px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTeaser').css('height', mcHeight+'px');
	}

	var gptabCookieKey = 'GPTab_' + pid;
	
	// "more"-link functionality in Corporate Finance TeaserTab
	$('.tx-gptabs-pi1 .TabContainer .TabCFTeaser .TabButton').click( function(){
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').addClass('TabMCTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').addClass('TabMCBottomInactive');
		
		$('.tx-gptabs-pi1 .TabContainer .TabCFContent').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabCFTeaser').toggle();
		$('.tx-gptabs-pi1 .TabContainer .TabMCTeaser').toggle();
		$('.tx-gptabs-pi1 .TabContainer .TabBorder').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').fadeIn('fast').css('bottom', '2px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').css('top', '1px');
		addCookieValue(gptabCookieKey, 'cf');
	});
	// "more"-link functionality in Managenent Consultants Teaser Tab
	$('.tx-gptabs-pi1 .TabContainer .TabMCTeaser .TabButton').click( function(){
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').addClass('TabCFTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').addClass('TabCFBottomInactive');
		
		$('.tx-gptabs-pi1 .TabContainer .TabMCContent').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabCFTeaser').toggle();
		$('.tx-gptabs-pi1 .TabContainer .TabMCTeaser').toggle();
		$('.tx-gptabs-pi1 .TabContainer .TabBorder').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').fadeIn('fast').css('bottom', '2px');
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').css('top', '1px');
		addCookieValue(gptabCookieKey, 'mc');
	});
	
	// links for switching the tabs
	// Top Tab Links
	// showing CF tab
	$('.tx-gptabs-pi1 .TabContainer .TabCFTop').click(function(){
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').removeClass('TabCFTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').removeClass('TabCFBottomInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').addClass('TabMCTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').addClass('TabMCBottomInactive');
		
		$('.tx-gptabs-pi1 .TabContainer .TabCFTeaser').hide();
		$('.tx-gptabs-pi1 .TabContainer .TabMCTeaser').hide();
		$('.tx-gptabs-pi1 .TabContainer .TabCFContent').fadeIn('fast');;
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').css('top', '1px');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').css('bottom', '2px');	
		$('.tx-gptabs-pi1 .TabContainer .TabMCContent').hide();
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').css('top', '1px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').css('bottom', '2px');
		$('.tx-gptabs-pi1 .TabContainer .TabBorder').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').show();
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').show();
		addCookieValue(gptabCookieKey, 'cf');
	});
	// showing MC tab
	$('.tx-gptabs-pi1 .TabContainer .TabMCTop').click(function(){
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').addClass('TabCFTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').addClass('TabCFBottomInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').removeClass('TabMCTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').removeClass('TabMCBottomInactive');
	
		$('.tx-gptabs-pi1 .TabContainer .TabCFTeaser').hide();
		$('.tx-gptabs-pi1 .TabContainer .TabMCTeaser').hide();
		$('.tx-gptabs-pi1 .TabContainer .TabCFContent').hide();
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').css('top', '1px');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').css('bottom', '2px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCContent').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').css('top', '4px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').css('bottom', '2px');
		$('.tx-gptabs-pi1 .TabContainer .TabBorder').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').show();
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').show();
		addCookieValue(gptabCookieKey, 'mc');
	});
	
	// Bottom Tab Links
	// showing CF tab
	$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').click(function(){
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').removeClass('TabCFTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').removeClass('TabCFBottomInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').addClass('TabMCTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').addClass('TabMCBottomInactive');
		
		$('.tx-gptabs-pi1 .TabContainer .TabCFContent').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').css('top', '1px');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').css('bottom', '2px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCContent').hide();
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').css('top', '1px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').css('bottom', '2px');
		addCookieValue(gptabCookieKey, 'cf');
	});
	// showing MC tab
	$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').click(function(){
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').addClass('TabCFTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').addClass('TabCFBottomInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').removeClass('TabMCTopInactive');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').removeClass('TabMCBottomInactive');
		
		$('.tx-gptabs-pi1 .TabContainer .TabCFContent').hide();
		$('.tx-gptabs-pi1 .TabContainer .TabCFTop').css('top', '1px');
		$('.tx-gptabs-pi1 .TabContainer .TabCFBottom').css('bottom', '2px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCContent').fadeIn('fast');
		$('.tx-gptabs-pi1 .TabContainer .TabMCTop').css('top', '4px');
		$('.tx-gptabs-pi1 .TabContainer .TabMCBottom').css('bottom', '2px');
		addCookieValue(gptabCookieKey, 'mc');
	});
	
	// if accordeon cookie value is set and current accordeon element has been opened last time -> open it from beginning
	if( document.cookie.search('GPTab_' + pid) != -1 && readCookieValue('GPTab_' + pid) == 'cf') {
		$('.tx-gptabs-pi1 .TabContainer .TabCFTeaser .TabButton').trigger('click');
	}
	if( document.cookie.search('GPTab_' + pid) != -1 && readCookieValue('GPTab_' + pid) == 'mc') {
		$('.tx-gptabs-pi1 .TabContainer .TabMCTeaser .TabButton').trigger('click');
	}
	
}

function LinkButtonInit() {
	$('.LinkBtn').wrapInner('<span />');
	$('.LinkBtn').addClass('LinkBtnShadow');
	$('.LinkBtnUrl').wrapInner('<span />');
	$('.LinkBtnUrl').addClass('LinkBtnShadow');
	
	$('.LinkBtnLight').wrapInner('<span />');
	$('.LinkBtnLight').addClass('LinkBtn');
	$('.LinkBtnLight').addClass('LinkBtnLightShadow');
	$('.LinkBtnLight').removeClass('LinkBtnLight');
	$('.LinkBtnLightUrl').wrapInner('<span />');
	$('.LinkBtnLightUrl').addClass('LinkBtnUrl');
	$('.LinkBtnLightUrl').addClass('LinkBtnLightShadow');
	$('.LinkBtnLightUrl').removeClass('LinkBtnLightUrl');
}

function LightboxInit() {
	$('.layerBG').click(function(){
		$('.layerLightbox').fadeOut('fast');
		$('.layerBG').fadeOut('fast');
	});

	$('.lightboxClose').click(function(){
		$('.layerLightbox').fadeOut('fast');
		$('.layerBG').fadeOut('fast');
	});
	
	bodyHeight = $('body').height();
	$('.layerBG').height(bodyHeight);
}

/* fetches content from page, adds it to the div.layercontent
 * and displays layer + layerBG
 *
 * !!! for some unknown reason it doesn't call the success function in IE,
 * !!! although the AJAX call returns the status 200(always error function with parsererror)
 * !!!  -> WORKAROUND: insert and display content in error function too
 */
function showServiceLineContent(id, type, lang) {
	lang = sys_language_uid;
	$.ajax({
		dataType: 'text',
  		url: baseUrl+'index.php?id='+id+'&type='+type+'&L='+lang,
  		success: function(data,status) {
  			//alert('success xhr Status:'+status);
    			$('.lightboxContent').html(data);
    			//$('.lightboxPrev').html('<a href="javascript:changeServiceLineContent('+id+', '+type+', '+lang+');">Previous<a/>');
    			//$('.lightboxNext').html('<a href="javascript:changeServiceLineContent('+id+', '+type+', '+lang+');">Next<a/>');
    			$('.layerBG').fadeIn('fast');
    			$('.layerLightbox').fadeIn('fast');
				GPTabsInit();
				LinkButtonInit();
  		},
  		error: function(x,e,errorThrown){
 			if(x.status==0){
 				//alert('You are offline!!\n Please Check Your Network.');
 			}else if(x.status==404){
 				alert('Requested URL not found.');
 			}else if(x.status==500){
 				alert('Internel Server Error.');
 			}else if(e=='parsererror'){
 				//alert('Error:Parsererror\nStatus:'+x.status+'\nResponse text:\n'+x.responseText+'\nError thrown:'+errorThrown.number);
 				$('.lightboxContent').html(x.responseText);
 				//$('.lightboxPrev').html('<a href="javascript:changeServiceLineContent('+id+', '+type+', '+lang+', '+prev+', '+next+');">Previous<a/>');
    				//$('.lightboxNext').html('<a href="javascript:changeServiceLineContent('+id+', '+type+', '+lang+', '+prev+', '+next+');">Next<a/>');
    				$('.layerBG').fadeIn('fast');
    				$('.layerLightbox').fadeIn('fast');
					GPTabsInit();
					LinkButtonInit();
 			}else if(e=='timeout'){
 				//alert('Request Time out.');
 			}else {
 				//alert('Unknown Error.\n'+x.responseText);
 			}
 		}
	});
}

function changeServiceLineContent(id, type, lang) {
	lang = sys_language_uid;
	$.ajax({
		dataType: 'text',
  		url: baseUrl+'index.php?id='+id+'&type='+type+'&L='+lang,
  		success: function(data,status) {
    			$('.lightboxContent').html(data);
				GPTabsInit();
				LinkButtonInit();
    			//$('.lightboxPrev').html('<a href="javascript:changeServiceLineContent('+id+', '+type+', '+lang+', '+prev+', '+next+');">Previous<a/>');
    			//$('.lightboxNext').html('<a href="javascript:changeServiceLineContent('+id+', '+type+', '+lang+', '+prev+', '+next+');">Next<a/>');
  		},
  		error: function(x,e,errorThrown){
 			if(x.status==0){
 				//alert('You are offline!!\n Please Check Your Network.');
 			}else if(x.status==404){
 				alert('Requested URL not found.');
 			}else if(x.status==500){
 				alert('Internel Server Error.');
 			}else if(e=='parsererror'){
 				//alert('Error:Parsererror\nStatus:'+x.status+'\nResponse text:\n'+x.responseText+'\nError thrown:'+errorThrown.number);
 				$('.lightboxContent').html(x.responseText);
				GPTabsInit();
				LinkButtonInit();
 				//$('.lightboxPrev').html('<a href="javascript:changeServiceLineContent('+id+', '+type+', '+lang+', '+prev+', '+next+');">Previous<a/>');
    				//$('.lightboxNext').html('<a href="javascript:changeServiceLineContent('+id+', '+type+', '+lang+', '+prev+', '+next+');">Next<a/>');
 			}else if(e=='timeout'){
 				//alert('Request Time out.');
 			}else {
 				//alert('Unknown Error.\n'+x.responseText);
 			}
 		}
	});
}

function StandardBoxRightInit() {
	$('.ListBox .BoxContents ul').addClass('NavStyleList ListWithArrow');
}

function adjustHome() {
	// HRs in #HighlightedSections on homepage
	$('#HighlightedSections .csc-default').append('<hr />');
	
	// h3 in #HighlightedSections on homepage
	//$('#HighlightedSections .ColumnContainer .csc-default:first h3').css('padding-bottom', '10px');
	//$('#HighlightedSections .ColumnContainer .csc-default:last h3').css('padding-bottom', '10px');
}

function adjustIE() {
	// buttons on homepage
	$('#HighlightedSections .csc-textpic-text p:last-child').css('position','absolute');
	$('#HighlightedSections .csc-textpic-text p:last-child').css('bottom','-10px');
	
	// sitenav on top
	$('#SiteNav li a').css('padding-right', '20px');
	
	// correct Searchfield style on top
	$('#Search .SearchField').css('top', '0');
	
	// add transparency for IE
	$('.layerBG').css('filter', 'Alpha(opacity=80)');
}

function countrySelectboxInit() {
	$('.tx_bzdstaffdirectory_countrySelectbox').change(function() {
		newCountryUid = $('.tx_bzdstaffdirectory_countrySelectbox option:selected').val();
		oldCountrySelector = $('.tx-bzdstaffdirectory-pi1 .ActCountry');
		newCountrySelector = $('.tx-bzdstaffdirectory-pi1 .country' + newCountryUid);

		$(oldCountrySelector).slideUp('fast');
		$(oldCountrySelector).removeClass('ActCountry');
		$(newCountrySelector).addClass('ActCountry');
		$(newCountrySelector).slideDown('fast');
	});
}

function adjustBzdstaffdirectorySingeView() {
	// PageTitle
	if($('.tx_bzdstaffdirectory_PageTitle').html() != null) {
		$('.PageTitle').html( $('.tx_bzdstaffdirectory_PageTitle').html() );
		$('.tx_bzdstaffdirectory_PageTitle').replaceWith('');
	}
	
	// Sidebar
	if( $('.tx_bzdstaffdirectory_detail_sidebar').html() != null ) {
		$('#SideBar').height( $('.tx_bzdstaffdirectory_detail_sidebar').height() );
	}
}

function valueCycleSelectNavInit() {
	// Servicelines CF
	if( $('.ServiceLinesSelectFormCF').html() != null ) {
		$('.ServiceLinesSelectNavCF').change(function() {
			if($('.ServiceLinesSelectNavCF').val() > 0) {
				showServiceLineContent($('.ServiceLinesSelectNavCF').val(), 250, sys_language_uid);
			}
		});
	}
	
	// Servicelines MC
	if( $('.ServiceLinesSelectFormMC').html() != null ) {
		$('.ServiceLinesSelectNavMC').change(function() {
			if($('.ServiceLinesSelectNavMC').val() > 0) {
				showServiceLineContent($('.ServiceLinesSelectNavMC').val(), 250, sys_language_uid);
			}
		});
	}
	
	// Industry Lines
	if( $('.IndustryLinesSelectForm').html() != null ) {
		$('.IndustryLinesSelectNav').change(function() {
			if(!$('.IndustryLinesSelectNav').val() == 0) {
				$('.IndustryLinesSelectForm').attr('action', $('.IndustryLinesSelectNav').val());
				$('.IndustryLinesSelectForm').submit();
			}
		});
	}
}

function peopleAtGPInit() {

	$('.peopleAtGPBox p').first().removeClass('bodytext');
	var peoplePreviewImageListItems = $('.peopleAtGPGallery li');
	var peoplePreviewAltTexts = $('.peopleAtGPGallery img').attr('alt');
	var previewImageWidth = $('.peopleAtGPGallery img').width();
	var previewImageHeight = $('.peopleAtGPGallery img').height();
	
	// create all blue Layers for the preview images
	for(var j=0;j<peoplePreviewImageListItems.length;j++) {
		$(peoplePreviewImageListItems[j]).append(
			'<div class="previewImageLayer" style="height:'+(previewImageHeight-10)+'px; width:'+(previewImageWidth-10)+'px;">'
			+$(peoplePreviewImageListItems[j]).find('img').attr('alt')
			+'</div>'
		);
		
		// link to page when layer is shown
		$(peoplePreviewImageListItems[j]).click( function() {
			window.location.href = baseURL+$(this).find('a').attr('href');
		});
		
		// show layer instead of the image of the current person
		if( currentURL == baseURL+$(peoplePreviewImageListItems[j]).find('a').attr('href') ) {
			$(peoplePreviewImageListItems[j]).find('img').toggle();
			$(peoplePreviewImageListItems[j]).find('.previewImageLayer').toggle();
		}
		
		// hover effect other images
		else {
			$(peoplePreviewImageListItems[j]).css('cursor','pointer');
			$(peoplePreviewImageListItems[j]).hover( function(){
				$(this).find('img').toggle();
				$(this).find('.previewImageLayer').toggle();
			});
		}
	}
	
}

function emailInTextInit() {
	var emailLinks = $('.mailLinkGP');
	for(var l=0;l<emailLinks.length;l++) {
		hrefAttr = $(emailLinks[l]).attr('href');
		titleAttr = $(emailLinks[l]).attr('title');
		$(emailLinks[l]).before('<a class="mailLinkGPImage" href="'+hrefAttr+'" title="'+titleAttr+'"><img src="fileadmin/sys/images/global/email.gif" /></a>&nbsp;');
	}
}

function gpLocationsShowDetail(locationId) {
	javascript:goToGpLocation(locationId);
	$('#tx_gplocations_worldmap').css('display','none');
}

function adjustHeaderTextPosition() {
	topImageHeight = $('#TopImage').innerHeight();
	sloganHeight = $('.TopImageSlogan').height();
	$('.TopImageSlogan').css('top', ( ( topImageHeight-sloganHeight ) / 2 ) + 'px');
}

function initMosaicGallery() {
	if($("#jmg").length) {
		$("#jmg").mosaic({popup: true});
	}
}

function initImageFrame() {
	var imageFrameObjects = $(".ImageFrame .ImageFrameContent .csc-textpic .csc-textpic-imagewrap");
	for(var l=0;l<imageFrameObjects.length;l++) {
		imageFrameHtml = $(imageFrameObjects[l]).html();
		$(imageFrameObjects[l]).html('<div>' + imageFrameHtml + '</div>');
	}
}

function initVC () {
	!!document.createElement('svg').getContext;
	var theCircle = new valueCircle (slices, buttons);
	theCircle.init();
	$('#valueCircle svg').css('padding', '0 0 0 60px');
	breakhere = true;
}

function Init() {
	SizeManager.init();
	StandardBoxRightInit();
	Nav.Init();
	Accordeon.Init();
	Home.Init();
	GPTabsInit();
	LinkButtonInit();
	LightboxInit();
	adjustHome();
	adjustHeaderTextPosition();
	countrySelectboxInit();
	adjustBzdstaffdirectorySingeView();
	valueCycleSelectNavInit();
	peopleAtGPInit();
	emailInTextInit();
	initMosaicGallery();
	initImageFrame();
	
	// IE specific adjustments
	if( $.browser.msie ) {
		adjustIE();
	}
}

$(document).ready(Init);

