var top_timer;
var hash_array = [];
var act;

var scroll_offset;
var nav_offset;
var nav_offset_orig;

$(document).ready(function () {
	//topnav_height = $('#topnav').height();
	init();
	$('form').jqTransform({imgPath:'js/jqtransform/img/'});

	// bind 'myForm' and provide a simple callback function 
				
	nav_offset_orig = parseInt($('#nav').css('top'));

	$(window).bind('resize', function() {
		init();
	});
	
	$('.top_cont').each(function() {
		$(this).height($(this).find('div.top_cont_wrap').outerHeight(true)).hide();
	});
	
	//Blur Links
	$('a').click(function() {
		this.blur();
	});
	
	/********************** TOP NAVIGATION *************************/
	
	//Open Topnav
	$('a.openTop').click(function(event) {
		var id = $(this).attr('href');
		openTop(id);
		return false;
	});
	
	//Close Topnav
	$('.top_cont .close').click(function() {
		closeTop();
	});
	
	$('#topnav').mouseover(function(){
		if(top_timer) {
			$.clearTimer(top_timer);
		}
	}).mouseout(function(){
		top_timer = $.timer(1000,function(){
			closeTop();
		});
	});
	
	//Hover
	$('#menu a').mouseenter(function() {
		$(this).stop().animate({backgroundColor:'#d6d6d6'},200);
	}).mouseleave(function() {
		$(this).stop().animate({backgroundColor:'#fbfbfb'},600);
	}).click(function() {
		closeTop();
	});
	
	/********************** NAVIGATION *************************/
	$('#nav a').each(function(){
		$(this).append('<span class="link_back"></span>');
		$(this).find('.link_back').css({'opacity':0});
		
		var href = $(this).attr('href');
		if(typeof $(href).position() != 'undefined') {
			var pos = $(href).position().top - $(href).outerHeight(true) - $('body').height() -30;
			pos = (pos>0) ? Math.round(pos) : 1;
			hash_array[pos] = href;
		}
	});
	$('#nav a').mouseenter(function() {
		$(this).find('.link_back').stop().fadeTo(400,1);
	}).mouseleave(function() {
		$(this).find('.link_back').stop().fadeTo(700,0);
	}).click(function() {
		closeTop();
	});
	
	/********************** PROJECTS *************************/
	$('div.projekt').mouseenter(function() {
		$(this).find('.projektinfo').slideDown(400,'easeInOutQuad');
	}).mouseleave(function() {
		$(this).find('.projektinfo').slideUp(400,'easeInOutQuad');
	});
	
	checkAct();
	/********************** ONSCROLL *************************/
	
	$('body').scroll( function (){
		//realign navigation
		if(!$('#nav').is(':animated')){
			$('#nav').animate({'top':nav_offset+'px'},1000,'easeInOutQuad');
		}
		//reset Navigation
		if($('body').scrollTop() < 20){
			$('#nav').stop().animate({'top':nav_offset_orig+'px'},1000,'easeInOutQuad');
		}
	});
	
	
	// validate signup form on keyup and submit
	$("#mailform").validate({
		submitHandler: function(form){
			$(form).ajaxSubmit();
			openTop('#thx_mail');
		},
		errorElement: "div",
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: '<img src="img/error.png" title="E-Mail invalide!">',
			name: '<img src="img/error.png" title="Sagen Sie uns ihren Namen!">'
		}
	});
	
	$('.video').media({ 
		width:     450, 
		height:    250, 
		autoplay:  true,
		attrs:     { 
			image:  'videos/preview.jpg',
			backcolor: "#666666",
			frontcolor: "#f3f3f3",
			lightcolor: "#a5ef08",
			screencolor: "#666666"
		},
		params:    { 
			//wmode: 'transparent'
		},
		caption:   false // supress caption text 
	}); 

	
});

openTop =function(id) {
	var slideDownTime = 0;
	$('.top_cont').each(function(){
		if('#'+$(this).attr('id') != id) {
			$(this).slideUp(500,'easeInOutQuad');
			slideDownTime = 500;
		}
	});
	$.timer(slideDownTime,function(){
		$(id).slideToggle(500,'easeInOutQuad');
	});
}

closeTop = function(){
	$('.top_cont').each(function(){
		$(this).slideUp(500,'easeInOutQuad');
	});
}

checkAct = function() {
	var pos = $('body').scrollTop() + $('#nav').position().top;
	$.each(hash_array,function(i,val) {
		if(typeof val != 'undefined'){
			var hash_pos = $(val).position().top;
			if(pos > hash_pos) {
				act = val;
			}
		}
	});
	if(act) {
		$('#nav a').removeClass('act');
		$('#nav a[href='+act+']').addClass('act');
	}
	$.timer(800,function(){
		checkAct();
	});
}

init = function() {
	scroll_offset = -($('body').height()/2);
	nav_offset = (-scroll_offset) - ($('#nav').height())/2;
	$('span.scrollTo').localScroll({offset:scroll_offset,align:'center',target:'body'});
}


// global defautls; override as needed 
$.fn.media.defaults.flvPlayer = 'flvplayer/player.swf';
$.fn.media.defaults.flashvars = {skin:'flvplayer/skins/stylish_slim/stylish_slim.swf'};
