

/**
 * Global Settings
 */
var _timeoutImgLoad = 8000
	, _imgAnimationLowResWidth = 300
	, _imageViewZoomInDuration = 300
	, _imageViewZoomOutDuration = 500
	, _imageViewZoomEasing = 'swing'
	, _imageViewTransitionDuration = 300
	, _imageViewTransitionEasing = 'linear'
	, _autoHideTimeout = 1000  //1300  //1500
	, _autoHideSpeed = 400, _autoShowSpeed = 200
	, _thumbW = 193
	, _thumbH = 133
	;



/**
 * Global Variables and simple helper Functions
 */
var _doc, _win, _body
	, _winW, _winH
	, winResize = function(func) { if(arguments.length > 0) { if($.isFunction(arguments[0])) winResizeStack.push(func); } else for(var f in winResizeStack) winResizeStack[f](); }, winResizeStack=[]
	;








/**
 * Mobile Browser Detection
 * sets jQuery.browser.mobile = false|true
 **/
//** by http://detectmobilebrowser.com
(function(a){jQuery.browser.mobile=/android.+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))})(navigator.userAgent||navigator.vendor||window.opera);
//** also for Tablets/Pads by http://blog.mobileesp.com
jQuery.browser.tablet = DetectTierTablet();
if(jQuery.browser.tablet) jQuery.browser.mobile = true;



/**
 * Load conditional CSS and JS browser specific
 */
if(!$.browser.mobile) {
	document.write('<link href="script.css" rel="stylesheet" type="text/css" />');
	//document.write('<script src="js/json2.js" type="text/javascript"></script>');
	document.write('<script src="js/preload.js.php" type="text/javascript"></script>');
	document.write('<script src="js/deeplink.js" type="text/javascript"></script>');
	document.write('<script src="js/slides.js" type="text/javascript"></script>');
	document.write('<script src="js/fullscreen.js" type="text/javascript"></script>');
} else {
	document.write('<link href="inc/mobile.css.php" rel="stylesheet" type="text/css" />');
}





/**
 * The jQuery Main, DOM Ready
 */
$(function() {
	_doc = $(document);
	_win = $(window); _winW = _win.width(); _winH = _win.height();
	_body = $('body');
	


	/****
	 **** Desktop only stuff (not for mobiles)
	 ***/
	if(!$.browser.mobile) {



		/**
		 * fadein IMGs when they're completely loaded
		 */
		$('#main img').each(function(i, e) {
		  var img = $(e);
			img.load(function() {
			  if($(this).css('display') == 'none' && (!$.browser.mobile && !$(this).hasClass('slides'))) {
				  $(this).fadeIn(700, function() {
				    //** to fix some bad ie behavior **
						$(this).css({ filter:'', zoom:'' });
					});
				}
			});
			if(e.complete) img.load();
			setTimeout(function() {
			  if(img.css('display') == 'none') {
					img.load();
				}
				img = null;  //** supposedly better garbage collection in ie
			}, _timeoutImgLoad);
		});
		_win.load(function() {
      $('#main img').each(function(i, e) {
        if($(e).css('display') == 'none') $(e).load();
			});
			

			//$('input:first').focus();
			

			/**
			 * display CMS errors
			 */
			var errorPos = location.search.lastIndexOf('e=');
			if(errorPos != -1) {
			  alert(decodeURIComponent(location.search.substr(errorPos+2)));
				location.search = location.search.substr(0, errorPos-1);
			}
		});




		/**
		 * IE6,7 Optimizations
		 */
		if($.browser.msie && $.browser.version < 8.0 /*&& typeof(document.documentMode) == 'undefined'*/) {
			//** repair IEs bad menu hover **
//old:
// 			$('.menu').css({ overflow:'hidden' })
// 			.find('ul').hover(function() {
// 			  $(this).css({ backgroundColor:'#2a2a2a' })
// 				.parent().css({ overflow:'visible', zIndex:10 });
// 			}, function() {
// 				$(this).css({ backgroundColor:'transparent' })
// 				.parent().css({ overflow:'hidden' });
// 			});

//TODO
// 			$('div.menu ul li.highlight').css({ background:"transparent url('inc/arrow-b01.png') 188px 50% no-repeat" });
// 			$('div.menu').hover(function() {
// 			  $('div.menu ul li.over').css({ display:'list-item' });
// 			}, function() {
// 			  $('div.menu ul li.over').css({ display:'none' });
// 			});
		}










		/**
		 * Window Size Adaptions
		 *
		 * /!\ event calls seems to run async so use winResize()
		 */
		_win.resize(function() {
		  _winW = _win.width(); _winH = _win.height();


			//** adjust CMS editors **
			$('iframe#mce_0_ifr').height(_winH - 270);
			//.css({ 'overflow':'hidden', 'overflow-x':'hidden', 'overflow-y':'auto' }).attr('scrolling', 'no')
			//.find('html, body').css({ 'overflow':'hidden', 'overflow-x':'hidden', 'overflow-y':'auto' }).attr('scroll', 'no');
			$('table.mceLayout').height(_winH - 242);


			winResize();
		});
		//** for synchronious calls
		winResize(function() {
			//** adapt footline to scrollbar **
			$('#foot, .cmsTopBar').css('width', $('div.measurer').innerWidth());
		});











		/**
		 * Scrolling with fixed Elements for all Desktop Browsers
		 */
		$('html, body').css({ width:'100%', height:'100%', overflow:'hidden' });
		$('div.scroller').css({
			position:'absolute', overflow:'auto',
			left:0, top:0, width:'100%', height:'100%',
			textAlign:'center'
		});
		//** adapt footline size for simulated fixed scrolling **
		$('#foot').css({
			position:'absolute',
			bottom:0,
			width:'98%',
			padding:'7px 0', margin:0,
			textAlign:'center'
		});
		_win.resize();










		/**
		 *	Menu Hover Animations
		 */

		//** Unfold Menu on Hover **
		$('div.menu').append('<div style="position:absolute; left:0;top:-10px;width:197px;height:20px; background:'+_body.css('background-color')+'; z-index:100;">&nbsp;</div>');
		$('div.menu>ul>li.over').css({
			display:'list-item'
		});
		$('div.menu>ul').css({
			height:68
			,overflow:'hidden'
//,paddingTop:10
//,border:'1px dotted yellow'
		}).hover(function() {
			var ul = $(this)
				, h = 0;
			ul.children('li').each(function(i,li) { h += $(this).height(); });

			ul.css({
				overflow:'visible'
			}).stop(true, false).animate({
				height:h
			}, 200)/*.children('li.over').css({
				display:'list-item'
			})*/;

			ul.children('li:first').stop(true, false).animate({
				marginTop:0
			}, 200);
		}, function() {
			var ul = $(this)
				, h1 = ul.children('li:first').height()
				, highlightY = (ul.children('li.highlight').length == 1) ? ul.children('li.highlight').position().top : 0
				, shrinkH = 0
				, shrinkY = 0;
			ul.children('li:not(.over)').each(function() { shrinkH += $(this).height(); });
			ul.children('li').each(function() {
				if($(this).hasClass('over')) shrinkY += $(this).height();
				else return false;
			});

			//if(highlightY+h1 > shrinkH) {
			if(shrinkY > 0) {
				ul.children('li:first').stop(true, false).animate({
					//marginTop:(ul.parent('div.menu').height()-2 -h1) -highlightY
					marginTop:-shrinkY
				}, 200);
			}

			ul.css({
				overflow:'hidden'
			}).stop(true, false).animate({
				height:68  //shrinkH-2
			}, 200, function() {
// 				$(this).children('li.over').css({
// 					display:'none'
// 				});
			});
		}).mouseleave();


		//** Unfold Submenu on MenuItem Hover **
//TODO



		//** Expand for too long Submenu Items **
		$('div.menu ul ul li a').css({
			position:'absolute'
			,overflow:'hidden'
			,width:145
			,padding:0
//,border:'1px dotted yellow'
		}).append('<div class="fader">&nbsp;</div>');
		$('div.menu ul ul li').hover(function() {
			var a = $(this).find('a')
				, w = a.attr('origW');
			if(typeof w == 'undefined') {
				var tmp = a.clone().css({
					position:'absolute'
					,display:'none'
					,width:'auto'
					,padding:0
					,whiteSpace:'nowrap'
//,border:'1px dotted yellow',background:'#000'
					}).addClass('smaller lighter').appendTo(_body);
				w = tmp.width()+10;
				tmp.remove();
				a.attr('origW', w);
			}
			a.animate({
				width:w
			}, 200);
		}, function() {
			$(this).find('a').animate({
				width:145
			}, 200);
		});










		/**
		 *  Stuff to do when page and all IMGs are completely loaded
		 */
		_win.load(function() {
			_win.resize();


			if($('form.login input[name="u"]').val() != '') {
				$('form.login input[name="u"]').change();
				$('form.login input[name="p"]').focus(); //.select();
			}
		});


	} //****END Desktop only Stuff END****



	/****
	 **** Mobile only stuff
	 ***/
	else {


		//** remove the navi that's hidden behind IMGview **
		if($('div.imageView:not(.dummy)').length > 0) {
			$('div#head, div#main').remove();
		}


		//** Hide the Addressbar
		//** problematic because the IMGview adapts to the
		//** available screen, so that scrolling is impossible

		//$('div.imageView').height(screen.height+30);
// alert(
// 	'IMGview '+screen.width+'x'+screen.height
// 	+'\nscrH '+$('div.imageView').height()
// 	+'\nscrAvailH '+$('div.imageView').height()
// 	+'\nwinH '+_winH
// );

		window.scrollTo(0,1);
		//$('div.imageView').height(screen.height-30);
		//_win.resize();




	} //****END Mobile only Stuff END****






	$('#main .description').css({ display:'none' })

	if($('#main .description').text() != '') {
		$('#main .description').addClass('tooltip lighter').removeClass('description darker');
		$('.tooltip').appendTo(_body);

		$('#main.gallery, #main.start').append(
			'<div style="'
			//+'position:relative; text-align:right; margin:0;'
			+'position:relative; text-align:left; margin:0;'
			+'"><span class="info_button smallest thin darker" style="'
			//+'position:absolute; right:2px; bottom:2px; cursor:pointer;'
			+'position:absolute; left:2px; bottom:2px; cursor:default;'
			+'">info</span></div>'
		);

		//$('#main div.slides, .info_button')
		$('.info_button').fadeTo(0,.1).click(function(e) {
		  e.stopPropagation();
		  e.stopImmediatePropagation();
		  e.preventDefault();
			var tooltip = $('.tooltip');
			if(tooltip.css('display') == 'none') {
				var w = tooltip.width()
					, h = tooltip.height()
					, x = e.pageX - (w / 2)
					, y = e.pageY - (h / 2);
				if(x + w > _winW) x = _winW - w*1.2;
				if(y + h > _winH) y = _winH - h*1.2;
				if(x < 0) x = w*0.2;
				if(y < 0) y = h*0.2;
				tooltip.css({
					left:x
					,top:y
				}).fadeIn();
			} else {
				tooltip.fadeOut();
			}
		});
		_body.click(function(e) {
			var tooltip = $('.tooltip');
			if(tooltip.css('display') != 'none') {
				tooltip.fadeOut();
			}
		});
	}


	$('div#foot .contact').append(
		'<span class="phone mobile_block"><span class="mobile_none">&nbsp;/&nbsp;</span>'
		+'		  <a href="call&#116;&#111;:&#43;&#52;9&#49;&#55;&#50;5160502">'
		+'				&#109;&#111;b&#105;&#108; &#48;&#49;&#55;&#50; 51 60 50 2'
		+'			</a>'
		+'		</span>'
		+'	  <span class="mail mobile_block"><span class="mobile_none">&nbsp;/&nbsp;</span>'
		+'			<a href="m&#97;i&#108;&#116;&#111;:&#109;&#97;&#105;&#108;&#64;vangerven&#46;&#100;e">'
		+'				&#109;&#97;&#105;&#108;&#64;vangerven&#46;&#100;e'
		+'			</a>'
		+'		</span>'
	);



	$('table span.replace.phone').replaceWith(
		'<a href="call&#116;&#111;:&#43;&#52;9&#49;&#55;&#50;5160502">'
		+'&#48;&#49;&#55;&#50; 51 60 50 2'
		+'</a>'
	);


	$('table span.replace.contact').replaceWith(
		'<a class="highlight" href="m&#97;i&#108;&#116;&#111;:&#109;&#97;&#105;&#108;&#64;vangerven&#46;&#100;e">'
		+'&#109;&#97;&#105;&#108;&#64;vangerven&#46;&#100;e'
		+'</a>'
	);






	/**
	 * CMS Login
	 */
	$('form.login .input input').bind('click focus blur focusin focusout change keyup', function() {
	  $('form.login .input input').each(function() {
		  if(this.value == '') $(this).css('background-color', 'transparent');
		  else $(this).css('background-color', '#fff');
		});
	});

	$('form.login input.submit').removeAttr('disabled');
	$('form.login').submit(function() {
	  //this.m.value = $.md5(this.u.value); this.u.value = '';
	  this.d.value = $.md5(this.p.value); this.p.value = '';
		return true;
	});










});



