var sitevar = parseInt(0);
var oldsite = 0;

$(function(){ 
    $('div').click(function(){ 
        $(this).css({'outline' : 'none'}); 
    }); 
});

$(document).keydown(function(event) {

	var keycode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	if(keycode == 37) {
		if(sitevar > 0) {
			sitevar -= 1;		
			moveTo(sitevar);
		} 
	}
	
	if (keycode == 39) {
		if(sitevar < 4) {
			sitevar += 1;		
			moveTo(sitevar);
		}
	}
});



$(document).ready(function(){
	$('div#background').onImagesLoaded(function(_this){
		$('#site').fadeIn(500);
    	$(_this).fadeIn(3000);
    	$('.row').jScrollPane({hideFocus : true});
    	$('.tworow').jScrollPane({hideFocus : true});
	});
	
	
	$.address.externalChange(function(event) {  
		var target = (event.value).substr(1);
		if(is_numeric(target) && target < 5) { 
			sitevar = parseInt((event.value).substr(1));
			moveTo(target);		
		}
	});  

	pfeilePositionieren();
	hoeheAnpassen();
	
	$("#navul li").click(function(){
		moveTo($(this).attr("id"));
	});
	
	
	$(window).resize(function() {
		hoeheAnpassen();
		pfeilePositionieren()
	});


	$(".links").click(function() {
		if(sitevar > 0) {
			sitevar -= 1;		
			moveTo(sitevar);
		} 
	});

	$('.rechts').click(function() {
		if(sitevar < 4) {
			sitevar += 1;		
			moveTo(sitevar);
		}
	});
	
	$('#memberlist h1').click(function() {
		$(this).toggleClass('arrowup');
		$(this).toggleClass('arrowdown');

		$(this).next('ul').slideToggle('fast', function() {
	    	$('.row').jScrollPane({hideFocus : true});
    		$('.tworow').jScrollPane({hideFocus : true});
		});
	});
	
	//$('#memberlist ul').hide();
	
	$('.gotoanmeldung').click(function() {
		moveTo(3);
	});
});

function loadNews(id){
	$("#newsHolderLeft").fadeOut("slow", function() {
		$("#newsHolderLeft").load("blog.php?id="+id, function() {
			$("#newsHolderLeft").fadeIn("slow");
		});
	});
};

function kickDaScrollbar(){
	$('.row').jScrollPane({hideFocus : true});
	$('.tworow').jScrollPane({hideFocus : true});
}

function moveTo(site) {
		
		sitediff = Math.abs(site - oldsite);
		oldsite = site;

		var timevar = 400*sitediff;
		var easing = "easeOutCubic";

		// move background
		
		var bgstep = 150;
		var bgdistance = (-1) * site * bgstep;
		
		$("#background img").stop().animate({
			left: bgdistance + "px"
		}, timevar, easing);

		
		// move content
		
		var fgstep = 900;
		
		var fgdistance = (-1) * site * fgstep;
	
		$("#content").stop().animate({
			left: fgdistance + "px"
		}, timevar, easing);
		
		
		// move menuslider
		
		var navstep = 140;
		var navdistance = site * navstep;
		
	
		$("#navslider").stop().animate({
			left: navdistance + "px"
		}, timevar, easing);
		
		sitevar = parseInt(site);

		$.address.value(site);
		
		// toggle menu arrows
		
		if (site == 0) {
			$(".links").fadeOut("fast");
		} else {
			$(".links").fadeIn("fast");
		}
		
		if (site == 4) {
			$(".rechts").fadeOut("fast");
		} else {
			$(".rechts").fadeIn("fast");
		}

}


function hoeheAnpassen() {
	var hoehe = $(window).height();
	var breite = $(window).width();

	if (breite > 2600 || hoehe > 1200) { 
		var newheight = hoehe*2;
		var newwidth = hoehe * 6;
		$("#background img").css({'height' : newheight, 'width' : newwidth});
	} else {
		$("#background img").css({'height' : 1200, 'width' : 3600});
	}
	
	if (breite < 1200) {
		$("#head img").css({'margin-left': 0})
	} else {
		$("#head img").css({'margin-left': '-100px'})
	}
}


function is_numeric(value) {
  if (value == null || value == "" || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}


function pfeilePositionieren() {
	var breite = $(window).width();
	var position = ((breite-900)/2)-80;
	if (position < 0) position = 0;
	$('.links').css({'left' : position});
	$('.rechts').css({'right' : position});
}


jQuery.fn.onImagesLoaded = function(_cb) { 
  return this.each(function() {
 
    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
        _cont = this,
            i = 0,
    _done=function() {
      if( typeof _cb === 'function' ) _cb(_cont);
    };
 
    if( $imgs.length ) {
      $imgs.each(function() {
        var _img = this,
        _checki=function(e) {
          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
          {
            if( ++i===$imgs.length ) _done();
          }
          else if( _img.readyState === undefined ) // dont for IE
          {
            $(_img).attr('src',$(_img).attr('src')); // re-fire load event
          }
        }; // _checki \\
 
        $(_img).bind('load readystatechange', function(e){_checki(e);});
        _checki({type:'readystatechange'}); // bind to 'load' event...
      });
    } else _done();
  });
};

