

jQuery(function($){
	
	
	if(!$.browser.webkit){
		$(".projectimage").each(
		function()
		{
			$(this).attr('original', $(this).attr('src'));
			$(this).attr('src','http://eggger.com/files/gimgs/spaceball.gif');
		});
	};
	
	
	//----------- FULLWINDOW-BILD --------------------------------

	function supersex() 
	{
		var imageAR = $(".projectimage").width()/$(".projectimage").height();
		var windowwidth = $(window).width();
		var windowheight = $(window).height();

		var windowAR = windowwidth/windowheight;
		if(windowAR > imageAR)
		{
			$(".projectimage").width(windowwidth);
			$(".projectimage").height($(".projectimage").width()/imageAR);
		}	else
			{
				$(".projectimage").height(windowheight);
				$(".projectimage").width($(".projectimage").height()*imageAR);
			}
	}
	
	
	//----------- RANDOM TITLE --------------------------------
	
		var arrName = new Array(10);
		arrName[0] = "Hier! Tobias' graphische Arbeiten!";
      	arrName[1] = "Jetzt! Tobias' graphische Arbeiten!";
      	arrName[2] = "Neu! Tobias' graphische Arbeiten!";
     	arrName[3] = "100%! Tobias' graphische Arbeiten!";
     	arrName[4] = "Hier! Tobias' graphische Arbeiten!";
		arrName[5] = "Hier! Tobias' graphische Arbeiten!";
      	arrName[6] = "Jetzt! Tobias' graphische Arbeiten!";
      	arrName[7] = "Neu! Tobias' graphische Arbeiten!";
     	arrName[8] = "100%! Tobias' graphische Arbeiten!";
     	arrName[9] = "Hier! Tobias' graphische Arbeiten!";
      	
      	var firstRandomNumber = (Math.floor(Math.random()*10));	
		$("title").html(arrName[firstRandomNumber]);
		
    
    
    //----------- HOME BUTTON --------------------------------
    
    $("#overlay+a").hover(function(){
    	$("#home").animate({ height:'226', top:'15', left:'50' }, 70);
    }, function(){
    	$("#home").animate({ height:'247', top:'5', left:'40' }, 70);
    });
    
	$("#overlay+a").mousedown(function(){		
    	$("#home").animate({ height:'247', top:'5', left:'40' }, 70);
	}).mouseup(function(){
		$("#home").animate({ height:'226', top:'15', left:'50' }, 70);
	});
	
	
    //----------- PFEILE --------------------------------
	
	function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
	};

	preload([
    	'http://www.eggger.com/files/gimgs/next-down2.png',
    	'http://www.eggger.com/files/gimgs/prev-down2.png'

    ]);
	
	
    
    $("#next>img").mousedown(function(){		
    	$(this).attr('src', 'http://www.eggger.com/files/gimgs/next-down2.png');
	}).mouseup(function(){
    	$(this).attr('src', 'http://www.eggger.com/files/gimgs/next.png');
	});
	
	$("#prev>img").mousedown(function(){		
    	$(this).attr('src', 'http://www.eggger.com/files/gimgs/prev-down2.png');
	}).mouseup(function(){
    	$(this).attr('src', 'http://www.eggger.com/files/gimgs/prev.png');
	});
	
	//----------- PREV/NEXT HOVER --------------------------------

	$("#prev>img").hide();

	$("#next").mouseenter(function(){		
		$("#next>img").show();
		$("#prev>img").hide();
	}).mouseleave(function(){
		$("#next>img").hide();
	});
	
	
	$("#prev").mouseenter(function(){		
		$("#prev>img").show();
		$("#next>img").hide();
	}).mouseleave(function(){
		$("#prev>img").hide();
	});
	
	
	
	//----------- INIT / LOAD --------------------------------

	$("#load").hide();
	
	$(".projectimage:first").attr('src', $(".projectimage:first").attr('original')).load(
	function()
	{
		$(this).removeAttr('original').addClass("loaded").addClass("active").show();
		$("#overlay").css({}).delay(1000).animate({ height:'0'}, 350);	
		$("#home").css({'height':'2px'});
		$("#home").css({}).delay(1500).animate({ height:'247', top:'5', left:'40' }, {duration: 2000, easing: 'easeOutElastic'});
		//$("#projectcount").html($(".active").index()+1 + "/" + $("article").length + " Projects");			
		supersex();
	});
	
	
	//----------- IMAGE CAROUSEL PREV/NEXT --------------------------------


	$("#img-container").width($(window).width()*$(".projectimage").length);


	$(window).resize(function(){
		supersex();
		$("#img-container").width($(window).width()*$(".projectimage").length);
	});	


	$("#next>img").click(
	function()
	{
		
		erstes = $(".projectimage:first");
		letztes = $(".projectimage:last");
		aktives = $(".active");
		voriges = $(".active").prev();
		naechstes = $(".active").next();		

		if (!naechstes.hasClass('loaded'))
		{	
			$("#load").show();
			naechstes.attr('src', naechstes.attr('original')).load(
			function()
			{	
				$("#load").hide();
				$("#img-container").animate({left:"-="+ $(window).width()},500);
				naechstes.addClass('active').addClass('loaded').removeAttr('original');
				aktives.removeClass('active');
				setTimeout(function(){letztes.after(erstes).removeClass('active'); $("#img-container").animate({left:"+="+ $(window).width()},0);},500);
			});
		}	else
			{
				$("#img-container").animate({left:"-="+ $(window).width()},500);
				naechstes.addClass('active');
				aktives.removeClass('active');
				setTimeout(function(){letztes.after(erstes).removeClass('active'); $("#img-container").animate({left:"+="+ $(window).width()},0);},500);
			}
	
	});
	
	
	$("#prev>img").click(
	function()
	{

		erstes = $(".projectimage:first");
		letztes = $(".projectimage:last");
		aktives = $(".active");
		voriges = $(".active").prev();
		naechstes = $(".active").next();		

		if (!letztes.hasClass('loaded'))
		{
			$("#load").show();
			letztes.attr('src', letztes.attr('original')).load(
			function()
			{	
				$("#load").hide();
				erstes.before(letztes).removeClass('active');
				$("#img-container").animate({left:"-="+ $(window).width()},0);
				$("#img-container").animate({left:"+="+ $(window).width()},500);
				letztes.addClass('active').addClass('loaded').removeAttr('original');
				aktives.removeClass('active');
			});
		}	else if (letztes.hasClass('loaded'))
			{
				erstes.before(letztes).removeClass('active');
				$("#img-container").animate({left:"-="+ $(window).width()},0);
				$("#img-container").animate({left:"+="+ $(window).width()},500);
				letztes.addClass('active').addClass('loaded').removeAttr('original');
				aktives.removeClass('active');
			}
	
	});
	
	

	
	//----------- PROJECT LIST HOVER STUFF --------------------------------
	
	
	$("#liste>li").hover(function(){
		$(this).css({'text-decoration':'underline'});
	}, function(){
		if ($(this).hasClass("aktiv")){
			$(this).css({'text-decoration':'underline'});
		} else {
			$(this).css({'text-decoration':'none'});
		};
	});
	

	//----------- BILD VERGRÖSSERN BEI RESIZE WINDOW --------------------------------


	
	
	
		
		
	$("#current").width($("#liste").width());	
	$("#liste").slideUp(0);
	

	
	$("#current").toggle(function()
	{
		$("#liste").slideDown(150);
	},function()
	{
		$("#liste").slideUp(150);
	});

	


});






