/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);

function containerPos() {
	var screenWidth = $(window).width();
	var screenHeight = $(window).height();
	var topMargin = (screenHeight-500)/2;
	if (topMargin > 50) $('#container').css('margin-top', topMargin);
	}

function menu_off() {
	$("#navigation").slideUp(100);
	}
function menu_on() {
	$("#navigation").slideDown(300);
	}

function smooth_in(start_id) {
	if(start_id=="start start_praxis") $('.start_praxis').animate({top: "0px", left: "0px", width: "508px", height: "191px"},500);
	if(start_id=="start start_kontakt") $('.start_kontakt').animate({top: "0px", left: "508px",  width: "392px", height: "191px"},500);
	if(start_id=="start start_spektrum") $('.start_spektrum').animate({top: "258px", left: "0px", width: "339px", height: "191px"},500);
	if(start_id=="start start_philosophie") $('.start_philosophie').animate({top: "258px", left: "339px",  width: "561px", height: "191px"},500);
	}
function smooth_out(start_id) {
	if(start_id=="start start_praxis") $('.start_praxis').animate({top: "0px", left: "0px", width: "850px", height: "400px"},500);
	if(start_id=="start start_kontakt") $('.start_kontakt').animate({top: "0px", left: "50px",  width: "850px", height: "400px"},500);
	if(start_id=="start start_spektrum") $('.start_spektrum').animate({top: "50px", left: "0px", width: "850px", height: "400px"},500);
	if(start_id=="start start_philosophie") $('.start_philosophie').animate({top: "50px", left: "50px",  width: "850px", height: "400px"},500);
	}

var timeout = 300;
var timerref = 0;
var menueeintrag = 0;
function menue_oeffnen() {
	menue_canceltimer();
	menue_schliessen();
	$(this).css("z-index", "100");
	var start_id = $(this).attr('class');
	smooth_out(start_id);
	menueeintrag = $(this);
	}
function menue_schliessen() {
	if(menueeintrag) {
		var start_id = $(menueeintrag).attr('class');
		smooth_in(start_id);
		$(menueeintrag).css("z-index", "10");
		}
	}
function menue_timer() {
	timerref = window.setTimeout(menue_schliessen, timeout);
	}
function menue_canceltimer() {
	if(timerref) {
		window.clearTimeout(timerref);
		timerref = null;
		}
	}

$(document).ready(function() {
	containerPos();
	$("a[rel=external]").attr("target", "_blank");

//	$('.start').bind('mouseover', menue_oeffnen);
//	$('.start').bind('mouseout', menue_timer);
//	$(document).bind('onclick', menue_schliessen);

	$('.subnav').click(function() {
		var target = $(this).attr('href').replace("#", "");
		$(this).parents('div.sec_item').children('div.act_div').fadeOut("slow");
		$(this).parents('div.sec_item').children('div.act_div').removeClass('act_div');
		$(this).parents('div.sec_item').children('div').eq(target).fadeIn("slow");
		$(this).parents('div.sec_item').children('div').eq(target).addClass('act_div');
								});

	
	$('.start_praxis').click(function() { $("#screen").scrollTo( '.sec_item:eq(2)', 1200, function() { menu_on();pos_active=2; } ); });
	$('.start_kontakt').click(function() { $("#screen").scrollTo( '.sec_item:eq(1)', 1200, function() { menu_on();pos_active=1; } ); });
	$('.start_spektrum').click(function() { $("#screen").scrollTo( '.sec_item:eq(3)', 1200, function() { menu_on();pos_active=3; } ); });
	$('.start_philosophie').click(function() { $("#screen").scrollTo( '.sec_item:eq(4)', 1200, function() { menu_on();pos_active=4; } ); });
	
	var pos_active = 0;
	$('#container').serialScroll({
		target:'#screen',
		items:'.sec_item', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		axis:'xy',// The default is 'y' scroll on both ways
		navigation:'#navigation li',
		duration:300,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		onBefore:function( e, elem, $pane, $items, pos ){ 
			//alert (pos_active+' zu '+pos);
			if(pos==0) {
				menu_off();//alert(pos_active);
				$('#sections').children('div').eq(pos_active).children('div.act_div').fadeOut("slow"); 
				$('#sections').children('div').eq(pos_active).children('div.act_div').removeClass('act_div'); 
				$('#sections').children('div').eq(pos_active).children('div').eq(0).fadeIn("slow");
				$('#sections').children('div').eq(pos_active).children('div').eq(0).addClass('act_div');
				}
			var activediv = $('#sections').children('div').eq(pos).children('div.act_div').index();
			var   zerodiv = $('#sections').children('div').eq(pos).children('div').eq(0).index();
			if (activediv != 0) { 
				$('#sections').children('div').eq(pos).children('div.act_div').fadeOut("slow"); 
				$('#sections').children('div').eq(pos).children('div.act_div').removeClass('act_div'); 
				$('#sections').children('div').eq(pos).children('div').eq(0).fadeIn("slow");
				$('#sections').children('div').eq(pos).children('div').eq(0).addClass('act_div');
				}
			pos_active=pos;
			//if(pos==0) alert("Hallo");
			e.preventDefault();
			if( this.blur )
				this.blur();
		},
		onAfter:function( elem ){
		}
	});

	$('.off').hover(
		function() {
			$(this).stop().animate({backgroundPosition:"(0 28px)"}, {duration:500})
		},
		function() {
			$(this).stop().animate({backgroundPosition:"(-233px 28px)"}, {duration:500})
		}
	);

});

$(window).resize(function() {
	containerPos();
	});


