// StartUp Jobs
$(document).ready(function() {
	bws5MainNav.init();
	countrySelector.init();
	$('#msKeyVisual').css('background-image','url(/fileadmin/image/default.jpg)');
	$('#msKeyVisual').css('background-position','0 0');
	$('#msKeyVisual').css('background-repeat','no-repeat');
});


/* Navigation */
var bws5MainNav = {

    init: function() {
		
		// leere Bildunterschriften entfernen
		$('.M503 .itemBox-caption:empty').remove();
		
		// leere Bild-Container entfernen
		$('.M503 .cell-01 .itemBox-image:not(:has(img))').parent().remove();
		
		// Teaser Gallery
		$('.M201 .itemBox-image a').lightBox();
		
		// jedes dritte Bild hat kein margin
		// **********************************************************************
		$('.M209 .cell-01 .itemBox-image').each(function(intIndex) {
			var counting = intIndex % 3;
			if (counting === 2) {
				$(this).css('margin', '0 0 10px 0');
			} else {
				$(this).css('margin', '0 9px 10px 0');
			}
		});
		// **********************************************************************
		
		
		// Hauptmenü
		// **********************************************************************
        $('#msMenuShowMain .level1 ul.subLayerNav').each(function(i, jObj) {			
			var parent = $(this).parent();
			if ($(parent).hasClass('level1') && $(parent).hasClass('hasChildren') !== true) {

				parent.addClass('hasChildren');
				var subNavCount = parent.find('ul.subLayerNav').size();
				
				if (subNavCount > 0 && subNavCount < 4) {
					parent.addClass('has'+subNavCount+'Children');
				} else {
					parent.addClass('has3Children');
				}
				
				parent.append('<div class="subLayer"> </div>');
				parent.find('div.subLayer').append(parent.find('ul.subLayerNav'));
				parent.find('div.subLayer').append('<div class="clearing"> </div>');
				bws5MainNav.setHandler();
			}
        });
		
		// leere Kategorien im Flyout-Menü entfernen
		// **********************************************************************
		$("#msMenuShowMain ul.subLayerNav li h2:contains('blank')").remove();
		// **********************************************************************
		
		
		// Hauptmenüpunkte mit Untermenüpunkten nicht klickbar machen
		// **********************************************************************
		$("#msMenuShowMain ul li.hasChildren > a").attr('href', '#').css('cursor', 'default');		
		// **********************************************************************
		
		
		// automatische Startseiten Teaser Anpassung
		// **********************************************************************
		var teaserCount =  $('.page_home .M103').size();
		if (teaserCount > 1) {
			if (teaserCount == 2) {
				$('.page_home .M103').removeClass('M103').addClass('M102');
			} else if (teaserCount == 3) {
				$('.page_home .M103:not(:first)').removeClass('M103').addClass('M102');
			} else if (teaserCount == 4) {
				$('.page_home .M103').removeClass('M103').addClass('M101');
				
				$('.M101').parent().wrapInner('<div class="MR101 modulRow"></div>')
			} else if (teaserCount == 5) {
				$('.page_home .M103:not(:first)').removeClass('M103').addClass('M101');
			}
		}
		// **********************************************************************
		
		
		// Breadcrumb + Seitentitel in Key-Visual anzeigen
		// **********************************************************************
		var kvContentCountryPage = $('body').not('.page_home').find('#msKeyVisualAdditional').html();
		$('#msKeyVisual .itemBox-kvText').html(kvContentCountryPage).show();
		// **********************************************************************
		
		
		// zufällige KV-Ausgabe
		// **********************************************************************
		var kvCount = $('#msKeyVisual img').size();
		if (kvCount > 1) {
			var teaser	= new Array();
			$('#msKeyVisual img').each(function(intIndex) {
				teaser[intIndex] = this;
			});
			var teaserCypher = (Math.floor(Math.random() * teaser.length));
			$(teaser[teaserCypher]).addClass('showThis');
			$('#msKeyVisual img[class!=showThis]').remove();
		}
		// **********************************************************************
		
		
		// Deutlandkarte
		// **********************************************************************
		$('.container-locations .itemBox-text:not("#location_default")').hide();
		$('#map_germany area').hover(function() {
			$('.container-locations .itemBox-text:not(".active")').hide();
			$('.container-locations .itemBox-text').removeClass('active'); 
    		var locationName 			= $(this).attr('href');
			var locationOverlayposition = $(this).attr('rel');  
			$('.container-locations #location_'+locationName.replace('#', '')).show();
			
			$('.container-mapBubble').css('background-position', locationOverlayposition);
			
		}, function() {
			$('.container-locations .itemBox-text:not(".active")').hide();
		});
		
		$('#map_germany area').click(function() {
			var locationName = $(this).attr('href');
			$('.container-locations .itemBox-text').removeClass('active');
			$('.container-locations #location_'+locationName.replace('#', '')).show().addClass('active');
			return false;
		});
		// **********************************************************************
		
    },
	
    setHandler: function() {
		$('#msMenuShowMain .level1.hasChildren').each(function(i, jObj){
			 $(this, jObj).hover(function() {
			 	subLayer = $(this).find('.subLayer');
				positionLeft = $(this).position().left;
				if((positionLeft + subLayer.width()) > 975) {
					transformer = (positionLeft + subLayer.width()) - 970;
					subLayer.css('left', '-'+transformer+'px');
				}
            	$(this).find('.subLayer').show();
        	}, function() {
				$(this).find('.subLayer').hide();
			});
		});
				
    }
}

var countrySelector = {
	
	init: function() {
		countrySelector.setHandler();
	},
	
	setHandler: function() {
		
		$('.info-imagemap area').hover(function() { 
    		var url = $(this).attr('alt'); 
			$('.info-layer').html($('.info-data #info-'+url).html());
			$('.info-layer').fadeIn(250);
			$('.info-arrowlayer').attr('id', 'bubble-'+url);
			$('.info-arrowlayer').fadeIn(250);
			return false;
		}, function() {
			$('.info-layer').html(' ').hide();
			$('.info-arrowlayer').hide();
			return false; 
		});
		
		$('.country-websites ul li a').hover(function(){
			var url = $(this).attr('alt'); 
			$('.info-layer').html($('.info-data #info-'+url).html());
			$('.info-layer').fadeIn(250);
			$('.info-arrowlayer').attr('id', 'bubble-'+url);
			$('.info-arrowlayer').fadeIn(250);
			return false;
		}, function() {
			$('.info-layer').html(' ').hide();
			$('.info-arrowlayer').hide();
		});
		
		$('.page_global #country-selector').click(function() {
			var button = $(this);
			var layer = $('.country-select-layer');
			var layerHeight = $(layer).height();
			if (layerHeight > 0) {
				layer.animate({
					height: '0px'
				}, 750, function(){
					button.toggleClass('active');
				});
			}
			else {
				layer.animate({
					height: '273px'
				}, 750, function(){
					button.toggleClass('active');
				});;
			}
		
			return false;;
		});
		
		$('.country-select-layer .close-x, .country-select-layer .close-text').click(function() {
			countrySelector.close();
			return false;
		});
		
	},
	
	close: function() {
		$('#country-selector').removeClass('active');
		var layer = $('.country-select-layer');
		var layerHeight = $(layer).height();
		layer.animate({
			height: '0px'
		}, 750);

	}
}


//check for flash
var flashversionMin = 8;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;

if (plugin) {
	for (f = 0; f < 20; f++) {
		var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
		if (plugin.description.indexOf(f+".")>=0){
			flashversion = f;
			
			if (flashversion >= flashversionMin){
				plugin = true;
				break;
			}
		}
	}
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.userAgent.indexOf("Windows 95")>=0 || navigator.userAgent.indexOf("Windows 98")>=0 || navigator.userAgent.indexOf("Windows NT")>=0)) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('plugin = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+flashversionMin+'")))\n');
	document.write('</SCRIPT\> \n');
}

function playSWF(file,id,vars,width,height,wmode,bgcolor,baseurl,wLName) {
	fsource='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
	fsource+=' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ';
	fsource+=' ID='+id+' WIDTH="'+width+'"; HEIGHT="'+height+'">';
	fsource+=' <PARAM NAME="allowScriptAccess" VALUE="always" /> <PARAM NAME=movie VALUE="'+file+'"> <PARAM NAME=FlashVars VALUE="'+vars+'"> <PARAM NAME=menu VALUE=false>  <PARAM NAME="BASE" VALUE='+baseurl+'> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode value='+wmode+'> <PARAM NAME=bgcolor VALUE='+bgcolor+'>';
		if(navigator.userAgent.indexOf("Opera")==-1) {
			fsource+=' <EMBED src="'+file+'" FlashVars="'+vars+'" wmode='+wmode+' menu=false quality=high bgcolor='+bgcolor+'';
			fsource+=' swLiveConnect=FALSE allowScriptAccess="always" NAME='+id+' WIDTH="'+width+'" HEIGHT="'+height+'"';
			fsource+=' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" BASE='+baseurl+'>';
			fsource+=' </EMBED>';
		}
		else	{
			fsource+=' <EMBED src="'+file+'" FlashVars="'+vars+'" wmode='+wmode+' menu=false quality=high bgcolor='+bgcolor+'';
			fsource+=' swLiveConnect=FALSE allowScriptAccess="always" ID='+id+' WIDTH="'+width+'" HEIGHT="'+height+'"';
			fsource+=' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" BASE='+baseurl+'>';
			fsource+=' </EMBED>';
		}
	fsource+=' </OBJECT>';

	if (document.getElementById){
		document.getElementById(wLName).innerHTML=fsource;
	}

}

