// Hover for other than <a> elements emulation
$().ready(function() {
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
		$('button, #page .drop-down li, .doubleform .button').hover(
	            function() {
	                $(this).addClass('chover');
	       }, function() {
	             $(this).removeClass('chover');
	       }
	    );
	}
	
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
		$('.tx-bewerberportal-pi1 .tbl-search-results tr').hover(
			    function() {
	                $(this).addClass('hover');
	       }, function() {
	             $(this).removeClass('hover');
	       }
		);
	}
	
	var selectBoxes = $('div.job_vacancies select');
	
	if(!jQuery.browser.msie || parseInt(jQuery.browser.version) != 6) {
		selectBoxes.each( function(i) {
			$(selectBoxes[i]).selectbox({debug: true} );
		});
	}
	
	var buttons = $("div.job_aplication div.upload_files input.i-file ");
	var spans = $("div.job_aplication div.upload_files a.button");
	
	buttons.each( function(i) {
		$(buttons[i]).mouseenter(function() {
			 $(spans[i]).addClass("button_hovered");
		});
		$(buttons[i]).mouseleave(function() {
			$(spans[i]).removeClass("button_hovered");
		});
	});
	
	var second_level = $("div.b-sitemap ul li > ul").not("div.b-sitemap ul li ul li ul");
	var third_level = $("div.b-sitemap ul li ul li > ul");
	var four_level = $("div.b-sitemap ul li ul li ul li > ul");
	four_level.hide();
	
	$("button.three-level").addClass("act");
	
	$("button.one-level").click( function() {
		$("button.one-level").addClass("act");
		$("button.two-level").removeClass("act");
		$("button.three-level").removeClass("act");
		$("button.four-level").removeClass("act");
		second_level.hide(400);
		third_level.hide(400);
		four_level.hide(400);
	});
	
	$("button.two-level").click( function() {
		$("button.one-level").removeClass("act");
		$("button.two-level").addClass("act");
		$("button.three-level").removeClass("act");
		$("button.four-level").removeClass("act");
		
		if(second_level.css("display")== "none"){
			second_level.show(400);
		}
		else{
			third_level.hide(400);
			four_level.hide(400);
		}                                 
	});
	$("button.three-level").click( function() {
		$("button.one-level").removeClass("act");
		$("button.two-level").removeClass("act");
		$("button.three-level").addClass("act");
		$("button.four-level").removeClass("act");
		
		if(third_level.css("display")== "none"){
			second_level.show(400);
			third_level.show(400);
			four_level.hide(400);
		}
		else{
			four_level.hide(400);
		}
	});
	
	$("button.four-level").click( function() {
		$("button.one-level").removeClass("act");
		$("button.two-level").removeClass("act");
		$("button.three-level").removeClass("act");
		$("button.four-level").addClass("act");
		
		if(four_level.css("display")== "none"){
			second_level.show(400);
			third_level.show(400);
			four_level.show(400);
		}
		else{}
	});
});

$(function() { 	
	 javaScriptOn();
	
	// Tabs functionality for .fce-tabs
	boxTabs();
	
	navStates();
	//Sliding menu functionality
	if(jQuery.browser.msie) {
		    menuSliderIE();
		} else {
			menuSlider();
		}
	
});

function javaScriptOn() { 
		$(".js-off").removeClass('js-off');
}

function navStates() { 
	$(".nav-sub li.active:has(ul)").find('a.active:first').addClass('multi');
}


function boxTabs () { 
	if ($('.fce-tabs').length) { 
		$('.fce-tabs-header > ul > li').click(function(){
			$(this).siblings().removeClass('active').end().addClass('active');
			$('#' + $(this).attr('id').substr(2)).siblings().removeClass('active').end().addClass('active');
		});	
	}
}

var currentMenuItem = null;

function expandMenu () {
	if (currentMenuItem && currentMenuItem.attr('mouse_hover')==1) {
		var cheight = currentMenuItem.data('height');
		if($('#page-index').length) { 
			currentMenuItem.find('ul:first').css({'visibility' : 'visible', 'opacity':'0'}).stop()
			.animate({ opacity: '1' }, 300);
		} else { 
			currentMenuItem.find('ul:first').css({'visibility' : 'visible' , 'height' : '0'}).stop()
			.animate({ height: cheight, opacity: '1' }, 300);			
		}	
	}
}

function expandMenuIE () {
	if (currentMenuItem && currentMenuItem.attr('mouse_hover')==1) {
		var cheight = currentMenuItem.data('height');
		if($('#page-index').length) { 
			currentMenuItem.find('ul:first').css({'visibility' : 'visible'});
		} else {
			currentMenuItem.find('ul:first').css({'visibility' : 'visible' , 'height' : '0'}).stop()
			.animate({ height: cheight }, 300);			
		}
	}
}




function menuSlider () { 
	var height = 0; // Calculating height for largest element inside
	$('.drop-down > li').each(function(){
		height = $('.drop-down > li').height();
	
		$(this).find('ul').each(function(){
			if ( $(this).height() > height ){ 
				height = $(this).height();
			}
		});
		//console.log('h:'+height);
		if(!$('#page-index').length && $(this).attr('id') != 'nav-main') { 
			$(this).find('ul').css('height', height);
		}
		$(this).data("height", height);
	});
	
	var width = 0; // Calculating width for each ul
	$('.drop-down ul').each(function(){
		width = $('.drop-down > li').width();
	
		$(this).find('li').each(function(){
			if ( $(this).width() > width ){ 
				width = $(this).width();
			}
		});

		$(this).css("width", width);
		$(this).data("width", width);
	});
	
	

	$('.drop-down > li').each(function(){ // Animation for secont level ul
		
		var cheight = $(this).data('height');
		
		$(this).hover(
			function(){
				$(this).attr('mouse_hover', 1);
				currentMenuItem = $(this);
				setTimeout('expandMenu()', 500);
			},
			function() {
				$(this).attr('mouse_hover', 0);
				if($('#page-index').length) { 
					$(this).find('ul:first').stop().animate({ opacity: '0' }, 200, function(){
						$(this).css('visibility','hidden');
					});
				} else { 
					$(this).find('ul:first').stop().animate({ height: '0px', opacity: '0' }, 200, function(){
						$(this).css('visibility','hidden');
					});
					
				}
		});
		
		
	
		$('.drop-down ul>li').hover(function(){ // Animation for rest of ul's inside
			if($('#page-index').length) { 
				$(this).children('ul').each(function(){}).stop().animate({ opacity: '1' }, 300, 'linear').css('visibility', 'visible');
			} else {
				$(this).children('ul').each(function(){
					$(this).stop().animate({ width: $(this).data('width'), opacity: '1' }, 300, 'linear').css('visibility', 'visible');
				});
			}
		}, function() {
				if($('#page-index').length) { 
					$(this).children('ul').stop().animate({ opacity: '0' }, 300, 'linear', function(){
						//$.fx.off = true;
						$(this).css('visibility','hidden');
					});
				} else {
					$(this).children('ul').stop(true, true).animate({ width: '0px', opacity: '0' }, 300, 'linear', function(){
						$(this).css('visibility','hidden');
					});
				}
		
		});		
	});
}

function menuSliderIE () { 
	
	var height = 0; // Calculating height for largest element inside
	$('.drop-down > li').each(function(){
		height = $('.drop-down > li').height();
	
		$(this).find('ul').each(function(){
			if ( $(this).height() > height ){ 
				height = $(this).height();
			}
		});
		
		if(!$('#page-index').length) { 
			$(this).find('ul').css('height', height);
		}
		$(this).data("height", height);
	});
	
	var width = 0; // Calculating width for each ul
		$('.drop-down ul').each(function(){
			width = $('.drop-down > li').width();

			$(this).find('li').each(function(){
				if ( $(this).width() > width ){ 
					width = $(this).width();
				}
			});
			
			if (parseInt(jQuery.browser.version) == 4) { 
				
				if($('#page-index').length == 0) {
					$(this).css("width", width);
				}
			
			} else {
				$(this).css("width", width);
			}
			
			//alert($('#page-index').length);
			
			$(this).data("width", width);
		});

	
	

	$('.drop-down > li').each(function(){ // Animation for secont level ul
		
		var cheight = $(this).data('height');
		
		$(this).hover(
			function(){
				$(this).attr('mouse_hover', 1);
				currentMenuItem = $(this);
				setTimeout('expandMenuIE()', 500);
			},
			function() {
				$(this).attr('mouse_hover', 0);
				if($('#page-index').length) { 
					$(this).find('ul:first').css('visibility','hidden');
				} else {
					$(this).find('ul:first').stop().animate({ height: '0px' }, 200, function(){
						$(this).css('visibility','hidden');
					});
					
				}
		});
		
		

		$('.drop-down ul>li').hover(function(){ // Animation for rest of ul's inside
			if($('#page-index').length) {
				$(this).children('ul').each(function(){
					$(this).css('visibility','visible');//
				});
			} else {
				$(this).children('ul').each(function(){
					$(this).stop().animate({ width: $(this).data('width')}, 300).css('visibility', 'visible');
				});
			}
		}, function() {
				if($('#page-index').length) { 
				
					$(this).children('ul').each(function(){
						$(this).css('visibility','hidden');//
					});
				
				} else {
				
					$(this).children('ul').stop().animate({ width: '0px' }, 300, function(){
						$(this).css('visibility','hidden');
					});
				
				}
		});
	});

}

/* Language selector
---------------------------------------- */

$().ready(function() {
	closeShader('#lang_selector');
});

$(window).resize( function() {
	$(window).unbind('scroll');
	position();
	resizeShader();
});

var currentPopupId = "";

function popup(popupId) {
	position();

	resizeShader();
	
	$('#shade').css('visibility', 'visible');
	
	currentPopupId = popupId;
	
	$('#shade-back').stop().fadeTo(400, 0.8, function() {
		$(popupId).stop().slideDown(400);
	});
	
	$(popupId).css("visibility", "visible");
	$('#shade-back').css("visibility", "visible");
}

function closeShader(popup) {
	$(popup).stop().slideUp(400, function () { 
			$(this).css("visibility", "hidden")
			$('#shade-back').stop().fadeTo(400, 0, function () { $(this).css("visibility", "hidden"); $(this).css("height", "1px")} );
			currentPopupId="";
	});
}

function position() {
	var selector = currentPopupId!=""?currentPopupId:"#lang_selector";
	if(document.documentElement.clientHeight > popupHeight(selector) + 40){
		v1 = function () { 
			var selector = currentPopupId!=""?currentPopupId:"#lang_selector";
			scrollTop = $(window).scrollTop();
			$(selector).css("top", scrollTop);
			$(selector).css("margin-top", 20);
		};
		v1();
		$(window).scroll( v1 );
	} else {
		scrollTop = $(window).scrollTop();
		$(selector).css("top", scrollTop);
		mTop = Math.round((document.documentElement.clientHeight-popupHeight(selector))/2);
		$(selector).css("margin-top", (mTop>0)?mTop:0);
	}
}

function resizeShader() {
	h = $('body').height();
	$('#shade-back').css("height",h);
}

function popupHeight(selector) {
	return $(selector).outerHeight() + 20;
}

$.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};

/* jQuery selectbox plugin
---------------------------------------- */

jQuery.fn.extend({
	selectbox: function(options) {
		return this.each(function() {
			new jQuery.SelectBox(this, options);
		});
	}
});


/* pawel maziarz: work around for ie logging */
if (!window.console) {
	var console = {
		log: function(msg) { 
	 	}
	}
}
/* */

jQuery.SelectBox = function(selectobj, options) {
	
	var opt = options || {};
	opt.inputType = opt.inputType || "input";
	opt.inputClass = opt.inputClass || "selectbox";
	opt.containerClass = opt.containerClass || "selectbox-wrapper";
	opt.hoverClass = opt.hoverClass || "current";
	opt.currentClass = opt.currentClass || "selected";
	opt.groupClass = opt.groupClass || "groupname"; //css class for group
	opt.maxHeight = opt.maxHeight || 200; // max height of dropdown list
	opt.loopnoStep = opt.loopnoStep || false; // to remove the step in list moves loop
	opt.onChangeCallback = opt.onChangeCallback || false;
	opt.onChangeParams = opt.onChangeParams || false;
	opt.debug = opt.debug || false;
	
	var elm_id = selectobj.id;
	var active = 0;
	var inFocus = false;
	var hasfocus = 0;
	//jquery object for select element
	var $select = jQuery(selectobj);
	// jquery container object
	var $container = setupContainer(opt);
	//jquery input object 
	var $input = setupInput(opt);
	// hide select and append newly created elements
	$select.hide().before($input).before($container);
	
	
	//init();
	setTimeout(function() {init();}, 100);
	
	$input
	.click(function(){
		if (!inFocus) {
			$container.toggle();
		}
	})
	.focus(function(){
		if ($container.not(':visible')) {
			inFocus = true;
			$('.'+opt.containerClass).hide();
			//setTimeout(function() {$container.show();}, 100);
			$container.show();
		}
	})
	.keydown(function(event) {	   
		switch(event.keyCode) {
			case 38: // up
				event.preventDefault();
				moveSelect(-1);
				break;
			case 40: // down
				event.preventDefault();
				moveSelect(1);
				break;
			//case 9:  // tab 
			case 13: // return
				event.preventDefault(); // seems not working in mac !
				$('li.'+opt.hoverClass).trigger('click');
				break;
			case 27: //escape
			  hideMe();
			  break;
		}
	})
	.blur(function() {
		return false;
		if ($container.is(':visible') && hasfocus > 0 ) {
			if(opt.debug) console.log('container visible and has focus')
		} else {
			// Workaround for ie scroll - thanks to Bernd Matzner
			if((jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 8) || jQuery.browser.safari){ // check for safari too - workaround for webkit
				if(document.activeElement.getAttribute('id').indexOf('_container')==-1){
					hideMe();
				} else {
					$input.focus();
				}
			} else {
				hideMe();
			}
		}
	});

	function hideMe() { 
		hasfocus = 0;
		$container.hide(); 
	}
	
	function init() {
		$container.append(getSelectOptions($input.attr('id'))).hide();
		var width = $input.css('width');
		if($container.height() > opt.maxHeight){
			$container.width(parseInt(width)+parseInt($input.css('paddingRight'))+parseInt($input.css('paddingLeft')));
			$container.height(opt.maxHeight);
		} else $container.width(width);
	}
	
	function setupContainer(options) {
		var container = document.createElement("div");
		$container = jQuery(container);
		$container.attr('id', elm_id+'_container');
		$container.addClass(options.containerClass);
        $container.css('display', 'none');
		
		return $container;
	}
	
	function setupInput(options) {
		if(opt.inputType == "span"){
			var input = document.createElement("span");
			var $input = jQuery(input);
			$input.attr("id", elm_id+"_input");
			$input.addClass(options.inputClass);
			$input.attr("tabIndex", $select.attr("tabindex"));
		} else {
			var input = document.createElement("input");
			var $input = jQuery(input);
			$input.attr("id", elm_id+"_input");
			$input.attr("type", "text");
			$input.addClass(options.inputClass);
			$input.attr("autocomplete", "off");
			$input.attr("readonly", "readonly");
			$input.attr("tabIndex", $select.attr("tabindex")); // "I" capital is important for ie
			$input.css("width", $select.css("width"));
        	}
		return $input;	
	}
	
	function moveSelect(step) {
		var lis = jQuery("li", $container);
		if (!lis || lis.length == 0) return false;
		// find the first non-group (first option)
		firstchoice = 0;
		while($(lis[firstchoice]).hasClass(opt.groupClass)) firstchoice++;
		active += step;
    		// if we are on a group step one more time
    		if($(lis[active]).hasClass(opt.groupClass)) active += step;
		//loop through list from the first possible option
		if (active < firstchoice) {
			(opt.loopnoStep ? active = lis.size()-1 : active = lis.size() );
		} else if (opt.loopnoStep && active > lis.size()-1) {
			active = firstchoice;
		} else if (active > lis.size()) {
			active = firstchoice;
		}
        	scroll(lis, active);
		lis.removeClass(opt.hoverClass);

		jQuery(lis[active]).addClass(opt.hoverClass);
	}
	
	function scroll(list, active) {
      		var el = jQuery(list[active]).get(0);
      		var list = $container.get(0);
      
		if (el.offsetTop + el.offsetHeight > list.scrollTop + list.clientHeight) {
			list.scrollTop = el.offsetTop + el.offsetHeight - list.clientHeight;      
		} else if(el.offsetTop < list.scrollTop) {
			list.scrollTop = el.offsetTop;
		}
	}
	
	function setCurrent() {	
		var li = jQuery("li."+opt.currentClass, $container).get(0);
		var ar = (''+li.id).split('_');
		var el = ar[ar.length-1];
		if (opt.onChangeCallback){
        		$select.get(0).selectedIndex = $('li', $container).index(li);
        		opt.onChangeParams = { selectedVal : $select.val() };
			opt.onChangeCallback(opt.onChangeParams);
		} else {
			$select.val(el);
			$select.change();
		}
		if(opt.inputType == 'span') $input.html($("<div/>").html($(li).html()).text());
		else $input.val($("<div/>").html($(li).html()).text());
		return true;
	}
	
	// select value
	function getCurrentSelected() {
		return $select.val();
	}
	
	// input value
	function getCurrentValue() {
		return $input.val();
	}
	
	function getSelectOptions(parentid) {
		var select_options = new Array();
		var ul = document.createElement('ul');
		select_options = $select.children('option');
		if(select_options.length == 0) {
			var select_optgroups = new Array();
			select_optgroups = $select.children('optgroup');
			for(x=0;x<select_optgroups.length;x++){
				select_options = $("#"+select_optgroups[x].id).children('option');
				var li = document.createElement('li');
				li.setAttribute('id', parentid + '_' + $(this).val());
				li.innerHTML = $("#"+select_optgroups[x].id).attr('label');
				li.className = opt.groupClass;
				ul.appendChild(li);
				select_options.each(function() {
					var li = document.createElement('li');
					li.setAttribute('id', parentid + '_' + $(this).val());
					li.innerHTML = $(this).html();
					if ($(this).is(':selected')) {
						$input.html($(this).html());
						$(li).addClass(opt.currentClass);
					}
					ul.appendChild(li);
					$(li)
					.mouseover(function(event) {
						hasfocus = 1;
						if (opt.debug) console.log('over on : '+this.id);
						jQuery(event.target, $container).addClass(opt.hoverClass);
					})
					.mouseout(function(event) {
						hasfocus = -1;
						if (opt.debug) console.log('out on : '+this.id);
						jQuery(event.target, $container).removeClass(opt.hoverClass);
					})
					.click(function(event) {
						var fl = $('li.'+opt.hoverClass, $container).get(0);
						if (opt.debug) console.log('click on :'+this.id);
						$('li.'+opt.currentClass, $container).removeClass(opt.currentClass); 
						$(this).addClass(opt.currentClass);
						setCurrent();
						$select.get(0).blur();
						hideMe();
					});
				});
			}
		} else select_options.each(function() {
			var li = document.createElement('li');
			li.setAttribute('id', parentid + '_' + $(this).val());
			li.innerHTML = $(this).html();
			if ($(this).is(':selected')) {
				$input.val($(this).html());
				$(li).addClass(opt.currentClass);
			}
			ul.appendChild(li);
			$(li)
			.mouseover(function(event) {
				hasfocus = 1;
				if (opt.debug) console.log('over on : '+this.id);
				jQuery(event.target, $container).addClass(opt.hoverClass);
			})
			.mouseout(function(event) {
				hasfocus = -1;
				if (opt.debug) console.log('out on : '+this.id);
				jQuery(event.target, $container).removeClass(opt.hoverClass);
			})
			.click(function(event) {
			  	var fl = $('li.'+opt.hoverClass, $container).get(0);
				if (opt.debug) console.log('click on :'+this.id);
				$('li.'+opt.currentClass, $container).removeClass(opt.currentClass); 
				$(this).addClass(opt.currentClass);
				setCurrent();
				$select.get(0).blur();
				hideMe();
			});
		});
		return ul;
	}
};

/* Browser detect 
---------------------------------------- */

var BrowserDetect = {
	init : function() {
		this.browser = this.searchString(this.dataBrowser)
				|| "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString : function(data) {
		for ( var i = 0; i < data.length; i++) {
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch
					|| data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			} else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion : function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1)
			return;
		return parseFloat(dataString.substring(index
				+ this.versionSearchString.length + 1));
	},
	dataBrowser : [ {
		string :navigator.userAgent,
		subString :"Chrome",
		identity :"Chrome"
	}, {
		string :navigator.userAgent,
		subString :"OmniWeb",
		versionSearch :"OmniWeb/",
		identity :"OmniWeb"
	}, {
		string :navigator.vendor,
		subString :"Apple",
		identity :"Safari"
	}, {
		prop :window.opera,
		identity :"Opera"
	}, {
		string :navigator.vendor,
		subString :"iCab",
		identity :"iCab"
	}, {
		string :navigator.vendor,
		subString :"KDE",
		identity :"Konqueror"
	}, {
		string :navigator.userAgent,
		subString :"Firefox",
		identity :"Firefox"
	}, {
		string :navigator.vendor,
		subString :"Camino",
		identity :"Camino"
	}, { // for newer Netscapes (6+)
				string :navigator.userAgent,
				subString :"Netscape",
				identity :"Netscape"
			}, {
				string :navigator.userAgent,
				subString :"MSIE",
				identity :"Explorer",
				versionSearch :"MSIE"
			}, {
				string :navigator.userAgent,
				subString :"Gecko",
				identity :"Mozilla",
				versionSearch :"rv"
			}, { // for older Netscapes (4-)
				string :navigator.userAgent,
				subString :"Mozilla",
				identity :"Netscape",
				versionSearch :"Mozilla"
			} ],
	dataOS : [ {
		string :navigator.platform,
		subString :"Win",
		identity :"Windows"
	}, {
		string :navigator.platform,
		subString :"Mac",
		identity :"Mac"
	}, {
		string :navigator.platform,
		subString :"Linux",
		identity :"Linux"
	} ]

};

BrowserDetect.init();

/* jQuery preloader
---------------------------------------- */

function imgpreload(imgs,settings)
{
	// settings = { each:Function, all:Function }
	if (settings instanceof Function) { settings = {all:settings}; }

	// use of typeof required
	// https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Instanceof_Operator#Description
	if (typeof imgs == "string") { imgs = [imgs]; }

	var loaded = [];
	var t = imgs.length;
	var i = 0;

	for (i; i<t; i++)
	{
		var img = new Image();
		img.onload = function()
		{
			loaded.push(this);
			if (settings.each instanceof Function) { settings.each.call(this); }
			if (loaded.length>=t && settings.all instanceof Function) { settings.all.call(loaded); }
		};
		img.style.display = 'none';
		img.src = imgs[i];
		document.body.appendChild(img);
	}
}

if (typeof jQuery != "undefined")
{
	(function($){

		// extend jquery (because i love jQuery)
		$.imgpreload = imgpreload;

		// public
		$.fn.imgpreload = function(settings)
		{
			settings = $.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings);

			this.each(function()
			{
				var elem = this;

				imgpreload($(this).attr('src'),function()
				{
					if (settings.each instanceof Function) { settings.each.call(elem); }
				});
			});

			// declare urls and loop here (loop a second time) to prevent
			// pollution of above closure with unnecessary variables

			var urls = [];

			this.each(function()
			{
				urls.push($(this).attr('src'));
			});

			var selection = this;

			imgpreload(urls,function()
			{
				if (settings.all instanceof Function) { settings.all.call(selection); }
			});

			return this;
		};

		// public
		$.fn.imgpreload.defaults =
		{
			each: null // callback invoked when each image in a group loads
			, all: null // callback invoked when when the entire group of images has loaded
		};

	})(jQuery);
}

/* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
---------------------------------------- */

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

/* New SSI home page slideshow
---------------------------------------- */

$(window).resize( function() {
	h = $(document).height();
	$('#bg-s').css("height",h);
	$('#bg-n').css("height",h);
	$('#bg-i').css("height",h);
});


$().ready(function() {
	h = $(document).height();
	$('#bg-s').css("height",h);
	$('#bg-n').css("height",h);
	$('#bg-i').css("height",h);
	
	if (jQuery.trim($(".index-page .flash-content").html())=="") {
		$(".index-page .flash-content").remove();
		$(".header-content-top").css('width', '100%');
	}

	/*random rearange small slideshow thumbnails - start*/
	
	(function($) {
		
		$.fn.randomize = function(childElem) {
		 return this.each(function() {
		 var $this = $(this);
		 var elems = $this.children(childElem);
		
		 elems.sort(function() { return (Math.round(Math.random())-0.5); });
		
		 $this.empty();
		
		 for(var i=0; i < elems.length; i++)
		 $this.append(elems[i]);
		
		 });
		}
	})(jQuery);
	
	$(".small_teaser_slideshow .slideshow-control ul").randomize("li"); 
	
	var $li = $(".small_teaser_slideshow .slideshow-control ul li");
	var $counter = 0;
	$li.each(function(i) {
		$counter = $counter + 1;
		if ($counter > 3){
			$(this).remove();
		}
	});
	
	/*random rearange small slideshow thumbnails - end*/
	
	var links = $(".slideshow-control li");
	$(".slideshow-control ul").removeClass("js-off");

	var main_menu_items = $("#page-index #nav-main li");
	var imgs = [];
	var img_links = [];
	var imgs_loaded = [];
	var current_index = -1;
	var paused = false;
	var pending_anim = false;
	var slide_delay = 6000;
	var back_speed = 0;
	var slide_speed = 1000;

	links.each( function(i) {
		img = $(links[i]).find('.img_link').html();
		img = img.replace('<!--', '');
		img = img.replace('-->', '');
		img = jQuery.trim(img).split(';');
		imgs.push(img[0]);
		img_links.push(img[1]);
		imgs_loaded.push(false);

		$(links[i]).find('.ss-i-h').css({'opacity': 0, visibility:'visible'});
		$(links[i]).find('.ss-t').css('opacity', 0);
		$(links[i]).mouseenter(function() {
			paused = true;

			showSlide(i);
			
			$(links[i]).find('.ss-t').stop().animate({
				opacity: 1,
				width: '280'
			}, slide_speed, "easeOutQuad", function() { });
		});
		$(links[i]).mouseleave(function() {
			paused = false;
			
			$(links[i]).find('.ss-t').stop().animate({
				opacity: 0,
				width: '260'
			}, 100, function() {
				$(this).css('display', 'none');
			});
		});
	});
	
	$.imgpreload(imgs,
	{
		each: function()
		{	
			for (var i = 0; i < imgs.length; i++) {
				regexp = new RegExp(imgs[i]+"$");
				if (this.src.search(regexp)>=0) {
					if (current_index==i && pending_anim) {
						pending_anim = false;
						$('#bg-i-custom-img-'+i).stop().animate({
							opacity: 1
						}, back_speed, "easeInQuad", function() {
							if (img_links[i]!='') {
								$('#slideshow-link').css('display', 'block');
								$('#slideshow-link').attr('href', img_links[i]);
							} else {
								$('#slideshow-link').css('display', 'none');
							}
						});
					}
					imgs_loaded[i] = true;
					break;
				}
			}
		}
	});	
	
	for (var i=0; i<imgs.length; i++) {
		$('#bg-i-custom').append('<div id="bg-i-custom-img-'+i+'" class="bg-i-custom-img" style="z-index:'+(10+i)+';visibility:hidden;background-image: url(\''+imgs[i]+'\');"></div>');
	}
	$('.slideshow-control').parent().append('<div class="slideshow-link"><a id="slideshow-link" href="" style="display:none;"><!-- --></a></div>');
	$('#bg-i-custom').append('<div id="bg-i-original-img" class="bg-i-custom-img" style="z-index:'+(10+imgs.length)+';visibility:hidden;background-image: '+$('#bg-i').css('background-image').replace(/"/g, "'")+';"></div>');
	
	/* small teaser part - start */
	if($('.small_teaser_slideshow')){
		$('#bg-i-original-img').remove();
	}
	/* small teaser part - end */
	
	
	
	var main_menu_items = $('.index-page #nav-main > li');
	main_menu_items.each( function(i) {
		$(main_menu_items[i]).append('<span class="highlight"></span>');
		$(main_menu_items[i]).mouseenter(function() {
			paused = true;

			showSlide(i);
		});
		$(main_menu_items[i]).mouseleave(function() {
			paused = false;
		});		
	});
	
	var next_zindex = 20;
	
	function showSlide(i) {
		if (current_index != i) {
			$('#slideshow-link').css('display', 'none');
			current_index = i;
			$("#page-index #nav-main > li .highlight").stop().css({opacity:0, visibility:'visible'});
			$(".slideshow-control li .ss-i-h").stop().css({opacity:0, visibility:'visible'});
			if (imgs[i]) {
				$("#page-index #nav-main > li:nth-child("+(i+1)+") .highlight").stop().animate({
					opacity: 1
				}, back_speed, "easeInQuad", function() {});
				$(".slideshow-control li:nth-child("+(i+1)+") .ss-i-h").stop().animate({
					opacity: 1
				}, back_speed, "easeOutQuad", function() {});
				$('#bg-i-custom-img-'+i).css({
					'z-index': next_zindex++,
					'opacity': 0,
					'visibility' : 'visible'
				});
				if (imgs_loaded[i]) {
					pending_anim = false;
					//$(".bg-i-custom-img").stop();
					$('#bg-i-custom-img-'+i).stop().animate({
						opacity: 1
					}, back_speed, "easeOutQuad", function() {
						if (img_links[i]!='') {
							$('#slideshow-link').css('display', 'block');
							$('#slideshow-link').attr('href', img_links[i]);
						} else {
							$('#slideshow-link').css('display', 'none');
						}
					});
				} else {
					pending_anim = true;
				}
			} else {
				//$(".bg-i-custom-img").stop();
				$('#bg-i-original-img').css({
					'z-index': next_zindex++,
					'opacity': 0,
					'visibility' : 'visible'
				});
				$('#bg-i-original-img').stop().animate({
					opacity: 1
				}, back_speed, "easeOutQuad", function() { });
			}
		}
		
		if (next_zindex>50) {
			var img_divs = $(".bg-i-custom-img");

			zindexes = [];

			img_divs.each( function(i) {
				zindexes.push($(img_divs[i]).css('z-index'));
			});
			function sortfunction(a, b){
				return (a - b) //causes an array to be sorted numerically and ascending
			}
			zindexes.sort(sortfunction);

			function findindex(arr, v){
				for (var i=0; i < arr.length; i++) {
					if (arr[i]==v) 
						return (i+1);
				}
				return false;
			}

			img_divs.each( function(i) {
				new_zindex = findindex(zindexes, $(img_divs[i]).css('z-index'));
				if (!new_zindex) {
					new_zindex = 0;
				}
				$(img_divs[i]).css('z-index', new_zindex);
			});
			
			next_zindex = 20;
		}
	}

    setTimeout( nextSlide, slide_delay );
	
	function nextSlide() {
	    if (!paused) {
			showSlide((current_index+1)%(imgs.length+1));
		}
		setTimeout( nextSlide, slide_delay );
	}
});

$(document).ready(function(){
	
	if ($("#tx_bewerberportal_occupational_land_id").length > 0){
	      updateRegions($("#tx_bewerberportal_occupational_land_id").val());
	}
});
