(function($){
	var workingProject = false;
	var openedSize = "350px";
	var closedSize = "100px";
	var fadeTime = 600;
										
	$.fn.prepareProject = function() {
		var t = $(this);
		return t.each(function(index,element) {
			var e = $(element);
			e.css("width",openedSize);
			var width = e.find(".title").outerWidth() - 20;
//			console.log(width);
			var imgThumb = e.find("img.thumb");
// Change thumbnail size dynamically
			var origSrc = imgThumb.attr("src");
			var newSrc = origSrc.replace("width", width);
			imgThumb.attr("src", newSrc);
			imgThumb.fadeTo(0,0.8);
			e.css("width",closedSize);
		});
	}
	
	$.fn.showProject = function() {
		var t = $(this);
		return t.each(function(index,element) {
// Nur öffnen wenn noch nicht offen		
			var e = $(element);
			if(!e.hasClass("open") && !workingProject) {
				workingProject = true;
				e.css("width",openedSize);
				var left = e.find(".title").outerWidth() + 5;
				// Doing the animation
				e.find("div.content").fadeIn(fadeTime);
				e.find("div.title").fadeIn(fadeTime);
				e.find("img.rechteEcke").animate({"left": left}, fadeTime, function() {
					workingProject = false;
				});
// Offene Einträge schließen		
				$(".startEntry.open").hideProject();
// Neuen Eintrag als geöffnet kennzeichnen
				e.addClass("open");
			}
		});
	}
	
	$.fn.hideProject = function() {
		var t = $(this);
		return t.each(function(index,element) {
			var e = $(element);
			if (e.hasClass("open")) {
				e.find("div.content").fadeOut(fadeTime);
				e.find("div.title").fadeOut(fadeTime);
				e.find("img.rechteEcke").animate({"left": 22},fadeTime, function() {
					e.css("width",closedSize);
					e.removeClass("open");
				});
			}
		});
	}
})(jQuery);
 
$(document).ready(function() {
	
	// Supersized
	$.fn.supersized.options = {  
		startwidth: 1800,
		startheight: 1200,
		vertical_center: 1
	};
	$('#supersized').supersized(); 
	
	// Projects: Make videos almost fullscreen
	var screenWidth = window.innerWidth * 0.7;
	var screenHeight = screenWidth * 9 / 16;
	$("#sidebar").find(".youtube-player").attr("width",screenWidth).attr("height",screenHeight);
	$("#sidebar").find(".vimeo-player").attr("width",screenWidth).attr("height",screenHeight);
	
	// Fancybox configuration for images
	$("a.details").fancybox({
		"showCloseButton":true,
		"titleShow":true,
		"titlePosition":"over",
		"autoDimensions":"true"}
	);

	// Fancybox configuration for videos
	$("a.inline").fancybox({
		"showCloseButton":true,
		"titleShow":false,
		"autoDimensions":"true",
		"autoScale":true,
		"scrolling":"no",
		"hideOnContentClick":false}
	);
	
	// Fancybox hack to display close button only when hovering over right edge
	var fancyboxClose = $("#fancybox-close");
	var fancyboxLeft = $("#fancybox-left");
	var fancyboxRight = $("#fancybox-right");
	var backgroundImage = fancyboxClose.css("background-image");
	
	/*
	fancyboxClose.css("background-image","none");
	fancyboxClose.hover(function(){
		$(this).css("background-image",backgroundImage);
	},function(){
		$(this).css("background-image","none");
	});
	*/
	fancyboxLeft.css("height", "85%").css("top","0px");
	fancyboxRight.css("height", "85%").css("top","0px");
	
	// Fancybox hack for not showing arrows when there is only one element in the gallery
	var count = $(".thumbContainer").size();
	if(count == 1) {
		fancyboxLeft.css("height","0px");
		fancyboxRight.css("height","0px");
	}
	
	// Blog: Scroll to settings	
	$("#recentScrollLinks").localScroll({
		//target: "#blog", // could be a selector or a jQuery object too.
		axis:"y",
		hash:true,
		reset:false,
		margin:true,
		offset:{top:-175},
		duration:800
	});
	
	// Work: Slide in the description and thumbs
	$(".projectDescription").delay(800).slideDown(1400, function() {
	    $("div#thumbs").delay(200).slideDown(800);
	});
	
	// Blog: Slide in the blogbar
	$("#blogNavi").delay(800).slideDown(1400);
	
	// Work: Opacity hover effect
	$("div.workContainer ul a").hover(function() {
		$(this).parent().parent().parent().find("img").css("opacity","1");
	}, function() {
		$(this).parent().parent().parent().find("img").css("opacity","0.7");
	});
	$("div.workContainer a").hover(function() {
		$(this).parent().find("img").css("opacity","1");
	}, function() {
		$(this).parent().find("img").css("opacity","0.7");
	});
	
	// Work: Navigation titles
	$("#archive_navi span").hide();
	$("#archive_navi img").hover(function(){
		$(this).next().show();
	},function(){
		$(this).next().hide();
	});
	
	// Work: Project title tool tip
	$(".workContainer").hover(function(){
		var el = $(this).find("span").text();
		$.cursorMessage(el, {hideTimeout:1500});
		$("body").css("overflow-x","hidden");
	}, function(){
		$.hideCursorMessage;
		$("body").css("overflow","visible");
	});
	
	// Project: Opacity hover effect
	$(".thumbContainer a").hover(function() {
		$(this).parent().find("img").css("opacity","1");
	}, function() {
		$(this).parent().find("img").css("opacity","0.7");
	});
	
	// Project: Calculate dynamic width for projects
	dynWidth();
	$(".workContainer").fadeIn(800);
	function dynWidth() {
		var windowWidth = $(window).width();
		windowWidth = windowWidth - $("#header").width() - 50;
		var projectMaxCount = Math.floor(windowWidth / 180);
		$("#margin_top").width(2*109 + (projectMaxCount-1)*150);
		var correctedIndex = 1;
		var hideLeft = true;		// If false than hide right edged images
		$(".workContainer").each(function(index){
			if(correctedIndex <= projectMaxCount) {
				if(hideLeft) {
					$(this).find(".projRight").remove();
				} else {
					$(this).find(".projLeft").remove();			
				}			
				if(correctedIndex == projectMaxCount) {
					correctedIndex = 0;
					hideLeft = !hideLeft;
				}
			}
			correctedIndex++;
		});
	}

	// Menu: Contact slide toggle
	$("span#contactlink").click(function () {
		$("#contactbox").slideToggle("slow");
	});
	
	// Media animation
	var upTime = 500;
	var downTime = 500;
	var mediaFadeTime = 800;
	var workingMedia = false;
	$("#media").hover(function(){
		if (!workingMedia) {
			workingMedia = true;
			$("#media img").fadeTo(0,0.8);
			$("#media #data").animate({
				height:"335px"
				}, upTime, function(){
					workingMedia = false;
				});
		}
	},function(){
		if (!workingMedia) {
			workingMedia = true;
			$("#media #data").animate({
				height:"20px"
			}, downTime, function(){
					workingMedia = false;
				});
			$("#media img").fadeOut(0);
		}
	});
	
	$("#media img").hover(function(){
		$(this).fadeTo("fast",1);
	},function(){
		$(this).fadeTo("fast",0.8);
	});

	$(".startEntry").prepareProject();
	$(".startEntry").hover(function(){
		$(this).showProject();
	});

	$(".projectbox").hover(

		function () {
			$(".project_text img").show("1000", 
			function()
			{
				$(".overflow").show("1000");
			});
		}, 
		function(){
			$(".overflow").hide("fast", 
			function()
			{
				$(".project_text img").hide("fast");
			});
		}
	);
});

// Index: Auto open related functions
$(window).load(function() {
	window.setTimeout(autoOpenStartEntry, 1000);  
});

function autoOpenStartEntry() {
	$(".startEntry.autoOpen:first").showProject();
}

// Background related functions
function changeBackground() {
	$("#background").attr("src", "http://www.a38.at/2010/workspace/images/background_0"+rnd(3)+".jpg");
	
	var filename = $("#project_background").attr("alt");
	$("#project_background").attr("src", "http://www.a38.at/2010/workspace/project_files/" + filename);
}
     
function rnd(n) {
	return Math.floor(Math.random() * n) + 1;
}
 

