// JavaScript Document

$(document).ready(function(){
	
	proportion();
	aktionpos();
	linkclick();
	aktionclick();
	dead();
	redirect();
	
	//fonts();
	//changebg();
	// loadxml();
	//mainposition();
	//activelink();
	//menu();
	//menuposition();
	//karte();
	//galery();
});


$(window).resize(function(){
	proportion();
	mainposition();
	contentposition();
	menuposition();
	aktionpos();
});

function proportion(){
	imgrel = 1280 / 747;
	bodyrel = $('body').width() / $('body').height();
	
	if (bodyrel < imgrel){
		$('#new').height($('body').height());
		$('#new').width($('body').height()*imgrel);
		//$('#old').height($('body').height());
		//$('#old').width($('body').height()*imgrel);
		korr = ($('body').width() - $('#new').width()) / 2;
		$('#background').css('left', korr);
		$('#background').css('top', 0);
		$('#background').width($('body').width() - korr);
		$('#background').height($('body').height());
	}
	else {
		$('#new').width($('body').width());
		$('#new').height($('body').width()/imgrel);
		//$('#old').width($('body').width());
		//$('#old').height($('body').width()/imgrel);
		korr = ($('body').height() - $('#new').height()) / 2;
		$('#background').css('top', korr);
		$('#background').css('left', 0);
		$('#background').height($('body').height() - korr);
		$('#background').width($('body').width());
	}
}

function aktionpos(){
	aktleft = ($('body').width() - $('#aktion').width()) / 2;
	akttop = ($('body').height() - $('#aktion').height()) / 2;
	$('#aktion').css('left', aktleft);
	$('#aktion').css('top', akttop);
}

function aktionclick(){
	$('#aktion').click(function(){
		$(this).hide();
		$('#main').show(/*, function(){
			$('.scroll-pane').jScrollPane({dragMaxHeight:400, arrowSize:9, showArrows:true});}*/
			);
		karteclick();
		kartehover();
		opengalery();
		menuposition();
		mainposition();
		contentposition();
		clearTimeout(red);
		//menu();
		return false;
	});
}

function linkclick(){
	$('.intern').click(function(){
		$('body').attr('id', $(this).attr('rel'));
		activelink();
		changebg();
		changecontent();
		return false;
	});
}

function dead(){
	$('.dead').click(function(){
		return false;
	});
}

function menuposition(){
	$('#menu').css('top', (($('#main').height() * 0.4) - 2));
	$('.submenu').css('top', (($('#main').height() * 0.4) + 175));
}

function mainposition(){
	$('#main').css('margin-left', ($('body').width() - $('#main').width()) * 0.776);
}

function contentposition(){
	//$('#content_wrap').css('margin-top', ($('#content').height() * 0.4));
	$('.scroll-pane').jScrollPane({dragMaxHeight:400, arrowSize:9, showArrows:true});
}

function karteclick(){
	$('.accordeon').click(function(){
		sparte=$(this).attr('rel');
		if($('#'+sparte).css('display') == 'none'){
			$('.stand').slideUp();
			$('#'+sparte).slideDown('', function(){
			$('.scroll-pane').jScrollPane({dragMaxHeight:400, arrowSize:9, showArrows:true});});
		}
		else {
			$('#'+sparte).slideUp();
		}
	});
}

function kartehover(){
	$('.karte img').hover(function(){
		$('.karte div').stop(true, true).fadeOut('slow');
		$('#'+$(this).attr('rel')).fadeIn('slow');
	}, function(){
		$('#'+$(this).attr('rel')).fadeOut('slow');
	});
}

function opengalery(){
	$('.galery').click(function(){
		galcont = $(this).attr('rel');
		actpic = 1;
		$('#lightbox td').load('content/galery/'+galcont+'.htm', 'div', function(){
			galery();
			$('#lightbox').show();
			$('#galeryhead').width($('#pic'+actpic).width()+14);
		});
		return false;
	});
}
	
function galery(){
	$('#lightbox #hider').click(function(){
		$('#lightbox').hide();
		return false;
	});
	
	$('#galery_right').click(function(){
		actpic = actpic + 1;
		next = actpic + 1;
		$('.mainpic').hide();
		$('#pic'+actpic).show();
		$('#galeryhead').width($('#pic'+actpic).width()+14);
		$('#galery_left img').show();
		if ($('#pic'+next).length == 0){
			$('#galery_right img').hide();
		}
		return false;
	});
	
	$('#galery_left').click(function(){
		actpic= actpic - 1;
		next = actpic - 1;
		$('.mainpic').hide();
		$('#pic'+actpic).show();
		$('#galeryhead').width($('#pic'+actpic).width()+14);
		$('#galery_right img').show();
		if ($('#pic'+next).length == 0){
			$('#galery_left img').hide();
		}
		return false;
	});
}

function activelink(){
	$('.intern').removeClass('active');
	$('.intern[rel="'+$('body').attr('id')+'"]').addClass('active');
	group = $('.submenu .intern[rel="'+$('body').attr('id')+'"]').attr('rev');
	$('#menu .intern[rev="'+group+'"]').addClass('active');
	$('.submenu').hide();
	$('#'+group).show();
	//fonts();
}

function changebg(){
	here = $('body').attr('id');
	if (here == 'programm_m'){here = 'programm_l';}
	if (here == 'standorte'){here = 'unternehmen_g';}
	if (here == 'philosophie_m'){here = 'philosophie_l';}
	if (here == 'presse'){here = 'unternehmen_s';}
	if (here == 'archiv'){here = 'unternehmen_s';}
	if (here == 'kontakt'){here = 'unternehmen_s';}
	if (here == 'newsletter'){here = 'unternehmen_s';}
	if (here == 'impressum'){here = 'mitarbeiter';}
	if (here == 'shop'){here = 'philosophie_l';}
	if (here == 'standorte_vlbg'){here = 'mitarbeiter';}
	if (here == 'standorte_trl'){here = 'programm_l';}
	if (here == 'standorte_ch'){here = 'philosophie_l';}
	$('#background #new').attr('src', 'images/background/'+here+'.jpg');
	/*$('#background #new').hide().attr('src', 'images/background/'+here+'.jpg').fadeIn('1000', function(){
		$('#background #old').attr('src', 'images/background/'+here+'.jpg');
		});*/
}

function changecontent(){
	$('#leftcol').load('content/'+$('body').attr('id')+'.htm', 'div', function(){
		$('.scroll-pane').jScrollPane({dragMaxHeight:400, arrowSize:9, showArrows:true});
		fonts();
		});
}

function fonts(){
	//Cufon.replace('#menu a');
	//Cufon.replace('.submenu a');
	Cufon.replace('h1');
}

function standorte(){
	$('#standorte .borderlink, #standorte_vlbg .borderlink, #standorte_trl .borderlink, #standorte_ch .borderlink').click(function(){
		$(this).siblings('.borderlink').removeClass('active');
		$(this).addClass('active');
		$(this).siblings('.addresse').hide();
		$('#'+$(this).attr('rel')).show();
		return false;
	});
}

function newsletter(name, email, phone, message, action){
	$.post('php/newsletter.php', {name:name, email:email, phone:phone, message:message, action:action}, function(answer){
		alert(answer);
		document.newsform.name.value='';
		document.newsform.email.value='';
		document.newsform.phone.value='';
		document.newsform.message.value='';
	}); 	 
}

function check(){
	$('input[type=checkbox]').checkbox({
  	cls:'jquery-checkbox',
  	empty: 'images/template/empty.png'
	});
}

function kontakt(name, company, street, country, email, phone, message, products, brochure, newsletter){
	
	if (products == true) {products = 'JA';}
	else {products='nein';}
	
	if (brochure == true) {brochure = 'JA';}
	else {brochure='nein';}
	
	if (newsletter == true) {newsletter = 'JA';}
	else {newsletter='nein';}
	
	
	$.post('php/kontakt.php', {name:name,  company:company, street:street, country:country, email:email, phone:phone, message:message, products:products, brochure:brochure, newsletter:newsletter}, function(answer){
		alert(answer);
		document.newsform.name.value='';
		document.newsform.company.value='';
		document.newsform.street.value='';
		document.newsform.country.value='';
		document.newsform.email.value='';
		document.newsform.phone.value='';
		document.newsform.message.value='';
		document.newsform.email.checked=false;
		document.newsform.email.checked=false;
		document.newsform.email.checked=false;
	}); 	 
}

function openemployee(){
	$('.employees').click(function(){
		empcont = $(this).attr('rel');
		actpic = 1;
		$('#lightbox td').load('content/employees/'+empcont+'.htm', 'div', function(){
			employees();
			fonts();
			$('#lightbox').show();
			//$('#employeeshead').width($('#pic'+actpic).width()+14);
		});
		return false;
	});
}

function employees(){
	
	$('#lightbox #hider').click(function(){
		$('#lightbox').hide();
		return false;
	});
	
	$('.thumbnail').click(function(){
		$('.focus').hide();
		$('#'+$(this).attr('rel')).show();
		return false;
	});
}

/*function menu(){
	// menu
	$('#menu a').hover(function(){
		$(this).addClass('hover');
		fonts();
	}, function(){
		$(this).removeClass('hover');
		fonts();
	});
	
	// submenu
	$('.submenu a').hover(function(){
		$(this).addClass('hover');
		fonts();
	}, function(){
		$(this).removeClass('hover');
		fonts();
	});
}*/

function redirect(){
	if (phpsite == 0){
		/*red=setTimeout(function(){*/
			$('#aktion').hide();
			$('#main').show(/*, function(){
				$('.scroll-pane').jScrollPane({dragMaxHeight:400, arrowSize:9, showArrows:true});}*/
				);
			karteclick();
			kartehover();
			opengalery();
			menuposition();
			mainposition();
			contentposition();
			$('body').attr('id', 'unternehmen_g');
			activelink();
			changebg();
			changecontent();
		/*}, 10000);*/
	}
	else {
		$('#aktion').hide();
			$('#main').show(/*, function(){
				$('.scroll-pane').jScrollPane({dragMaxHeight:400, arrowSize:9, showArrows:true});}*/
				);
			karteclick();
			kartehover();
			opengalery();
			menuposition();
			mainposition();
			contentposition();
			$('body').attr('id', phpsite);
			activelink();
			changebg();
			changecontent();
	}
}

function flashclick(newsite){
		$('body').attr('id', newsite);
		activelink();
		changebg();
		changecontent();
		//return false;
}