// JavaScript Document

var pWidth = 0;
var nWidth = 0;
var refactive = 1;
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);

$(document).ready(function() {	
//--Fokus für Links unterbinden
	$('a').focus(function(){
		$(this).blur();
	})
	
//--Clientabhängige Funktionen an- und auschalten
	if (agentID) {
		//--Ausrichtung abfragen
		if(window.orientation != 0){
			//--Body auf Bildschirmbreite bringen
			$('body').width(screen.height);
		}
	}
	
//--allgemeine Werte abfragen
	pWidth = $('.wrapper:first-child').width();
	nCount = $('#navi ul:first-child > li').length;
	
//--Hauptmenü
	//--zentrieren und kein Trenner für letzten Menüpunkt
		$('#navi ul:first-child > li').siblings().each(function(index){
			nWidth += $(this).width();	
			if(index == (nCount-1)){
				$(this).addClass('last');	
			}
			//--Submenü
				//--Submenü und HG bei Mousover einblenden
				if(!agentID){
					$(this).mouseover(function(){
						$('#navi ul li ul').css('display','none');
						if($(this).find('ul').length > 0){
							$(this).find('ul').css('display','block');
							$('#header').addClass('sshow');
						} else {
							$('#header').addClass('nosub');	
						}
					});
					//--ausblenden
					$(this).mouseout(function(){
						if(!$(this).find('a:first-child').hasClass('aktiv')){
							$(this).find('ul').css('display','none');
						}
						$('#header').removeClass('sshow').removeClass('nosub');
						$('#navi ul.aktiv').css('display','block');
					});
				}
				//--Submenüs zentrieren
				var snWidth = 0;
				var snCount = $(this).find('li').length;
				$(this).find('li').each(function(sindex){
					snWidth += $(this).width();
					if(sindex == (snCount-1)){
						$(this).addClass('last');	
					}
				})
				$(this).find('ul').css({
					paddingLeft : (pWidth - snWidth)/2,
					display     : 'none'
				});
		});
		$('#navi ul:first-child').css('paddingLeft',(pWidth - nWidth)/2);
		
		//--Submenü onLoad einblenden
		showsub = function(){
			$('#navi ul:first-child > li').siblings().each(function(){
				if($(this).find('a').hasClass('aktiv') && ($(this).find('ul').length > 0)){
					$('#header').addClass('sub');
					$(this).find('ul').fadeIn('fast').addClass('aktiv');
				}
			});
		}
		showsub();
		
//--Footer je nach Template beeinflussen
	if($('#content').css('backgroundImage') == 'none'){
		$('#footer').addClass('hg');
	}
	if($('#content').hasClass('galerie')){
		$('#footer').addClass('galerie');
	}
});

$(window).load(function() {
//--Clientabhängige Funktionen an- und auschalten
	if(agentID && window.orientation == 0){
		//--Hochformat-Warnung ausgeben			
		$('<a id="portraiterror" href="images/error_portrait.gif">').appendTo('body');
		$('#portraiterror').delay(500).fancybox({overlay:false}).delay(500).click();
	}	
	
	//--Referenzdarstellung 
	if($('#refshow').length > 0){
		var refcount = $('#refshow .refwrap .refs ul li').length;
		
		//--Gesamtbreite festlegen, Inhalte zentrieren
		$('#refshow .refwrap .refs').width((refcount * pWidth));
		$('#refshow .refwrap .refs ul li').each(function(index){
			lWidth = 220 + $(this).find('img').width();
			$(this).css({
				'paddingLeft' : (pWidth - lWidth) / 2,
				'width'       : pWidth - ((pWidth - lWidth) / 2)
			}).addClass('nr'+(index+1));
			
			//--Toggles ausschalten
			if(index == 0){
				$(this).find('.toggleleft > a').css('display','none');
			}
			if(index == (refcount - 1)){
				$(this).find('.toggleright > a').css('display','none');
			}
		});
		
		//--Scrolling
		refscroll = function(dir){
			if(dir == 'left'){
				if(refactive > 1){
					refactive -= 1;				
				}
			} else {
				if(refactive < refcount){
					refactive += 1;
				}
			}
			$('#refshow .refwrap').scrollTo('.nr'+refactive,500);			
		}
		
		$('#refshow .refwrap .refs ul li .toggleleft a').each(function(){
			$(this).click(function(){
				clearInterval(ani);
				refscroll('left');
			});
		});
		$('#refshow .refwrap .refs ul li .toggleright a').each(function(){
			$(this).click(function(){
				clearInterval(ani);
				refscroll('right');
			});
		});
		
		$('#gallery').width($('body').width());
		
		//--Desktop Superzoom Bildausrichtung
		imgmove = function(e){
			//--verfügbare Maximaldimensionen ermitteln			
			var aWidth = $('#imgbig').width();
			var aHeight = $('#imgbig').height();
			var biWidth = $('#imgbig > img').width();
			var biHeight = $('#imgbig > img').height();

			xpos = (aWidth / 2) - (xmouse * (biWidth / aWidth));
		   	ypos = (aHeight / 2) - (ymouse * (biHeight / aHeight));
			$("#imgbig > img").css({
				position : 'absolute',
				left     : xpos+'px',
				top      : ypos+'px'
			})
		}
		
		//--Fullscreen		
		setimage = function(){
			$('#content').css('opacity',0);
			//--verfügbare Maximaldimensionen ermitteln			
			var aWidth = $('#imgbig').width();
			var aHeight = $('#imgbig').height();
			//--Originaldimensionen ermitteln
			var piWidth = $('.nr'+refactive).find('img').width();
			var piHeight = $('.nr'+refactive).find('img').height();
			var biWidth = aWidth;
			var biHeight = aHeight;
			
			$('#imgbig').fadeOut('fast',function(){
				fullsize = false;
				$('#imgbig > img').removeAttr("style");				
				$('#imgbig > img').attr('src',$('.nr'+refactive).find('img').attr('rel'));
				$('#description .txtr').html($('.nr'+refactive).find('.txtr').html());
				$('#description .txtp').html($('.nr'+refactive).find('.txtp').html());
				//--großes Bild in verfügbaren Rahmen einpassen
				if(((aWidth * piHeight) / piWidth) > aHeight){
					biWidth = ((biHeight * piWidth) / piHeight);
				} else {
					biHeight = ((aWidth * piHeight) / piWidth);
				}
				$('#imgbig > img').width(biWidth).height(biHeight);
				
				//--Superzoom
				$('#imgbig > img').click(function(){
					if(!$.fn.browserTouchSupport.touches){
						$(this).width('auto').height('auto');
						fullsize = true;
					} else {
						$(this).width('auto').height('auto');
						$('#bt_right, #bt_left, #description').css('display','none');
					}
				});						

				//--Rechts-Links-Buttons in Fullsize aus-/einblenden				
				if(refactive == refcount){
					$('#bt_right').fadeOut('fast');
				} else {
					$('#bt_right').fadeIn('fast');
				}
				if(refactive == 1){
					$('#bt_left').fadeOut('fast');
				} else {
					$('#bt_left').fadeIn('fast');
				}
				$('#description').css('display','block');		
				$(this).fadeIn('fast');								
			});			
		}
		
		$('#refshow img').each(function(){
			$(this).click(function(){
				clearInterval(ani);
				$('#gallery').fadeIn('fast').click(function(){
					return true;
				});
				setimage();
			});
		});
		
		$('#bt_close').click(function(){
			$('#content').css('opacity',1);
			fullsize = false;
			$('#imgbig > img').removeAttr("style").width(0).height(0);			
			$('#gallery, #imgbig').fadeOut('fast');
		});
		
		$('#bt_left').click(function(){
			refscroll('left');
			setimage();
		});
		
		$('#bt_right').click(function(){
			refscroll('right');
			setimage();
		});
		
		var xmouse = 0;
		var ymouse = 0;
		var fullsize = false;
	
		$(document).bind('mousemove',function(e){ 
       		xmouse = e.pageX;
	   		ymouse = e.pageY; 
			
			if(fullsize){
				imgmove();	
			}
		}); 
		
		autoani = function(){
			if(refactive < refcount){
				refactive += 1;
			} else {
				refactive = 1;	
			}
			$('#refshow .refwrap').scrollTo('.nr'+refactive,500);
		}
		
		autoplay = function(){
			ani = window.setInterval('autoani()',5000);
		}
		
		autoplay();		
	}
	
	//--Textboxhöhe dem nebenstehenden Bild angleichen
	$('.text').find('.txt').css({
		'minHeight' : $('.text').find('.image').height()
	});
});
