$(document).ready(function()
{
	$("#philipp").hide();
	$("#philipp .description #mailLink").replaceWith('<a href="mailto:philipp'+'@'+'rockolo'+'.'+'com" id="mailLink">philipp'+'@'+'rockolo'+'.'+'com</a>');
	$("#contact").toggle( function() { $("#philipp").slideDown(200); }, function() { $("#philipp").slideUp(200); } );
	$.lastButton = $("<div></div>");
	$("div.project ul.selection li:first-child a").addClass("select");
	$("div.project ul.selection li:first-child a").addClass("a_last");
	$("div.project ul.selection li:last-child a").addClass("a_last");
	
	var scrollToElement = function( element ) {
		$('html, body').animate({
		    scrollTop: element.offset().top - 30
		}, 300);
	};
	
	if( location.hash.indexOf( "/" )  == 1 )
	{
		var hashID = location.hash.substr(2);
		var elem = $( "#" + hashID );
		scrollToElement( elem );
	}
	
	$("#aerofluxLink").css('cursor', 'pointer').click( function()
	{
		location.hash = "/aeroflux";
		scrollToElement( $("#aeroflux") );
	});
	
	
	$("div.project ul.selection li a").each(function()
	{
		var urlChunks = $(this).attr("id").split("-");
		var projectID = "#" + urlChunks[0];
		var projectNUM = parseInt( urlChunks[1] );
		var projectPartID = urlChunks[0] + urlChunks[1];
		
		$(this).data("button", {
			"urlChunks" : urlChunks,
			"projectID" : projectID,
			"projectNUM" : projectNUM,
			"projectPartID" : projectPartID
		});
	});

	$("div.project ul.selection li a").mouseover(function()
	{
		var button = $(this).data("button");
		$( button.projectID + " div.contentSet").stop().animate(
		{ 
	        marginTop: button.projectNUM * -323
	    }, 800 );
	
		$(button.projectID + " ul.selection a.select").removeClass("select");
	
		$(this).addClass("select");
		
		if( ( $.lastButton && $.lastButton.hasClass("pause") || $.lastButton.hasClass("play")
		|| $.lastButton.hasClass("pauseFlash") || $.lastButton.hasClass("playFlash") )
		&& $.lastButton.attr("id") != $(this).attr("id") )
		{
			$(button.projectID + " ul.selection a.pause").addClass("play");
			$(button.projectID + " ul.selection a.pause").removeClass("pause");
			
			$(button.projectID + " ul.selection a.pauseFlash").addClass("playFlash");
			$(button.projectID + " ul.selection a.pauseFlash").removeClass("pauseFlash");
			
			var projectPartID = $.lastButton.data("button").projectPartID;
			$("#"+projectPartID).replaceWith("<img src='images/projects/"+projectPartID+".jpg' id='"+projectPartID+"'/>");
			
			$.lastButton = $("<div></div>");
		}	
	});

	$("div.project ul.selection li a.play").mouseup( function()
	{
			
		var button = $(this).data("button");
		
		//movie is loaded and paused
		if( $("#"+button.projectPartID).is("div") && document[button.projectPartID+'Movie'].GetRate() == 0 )
		{
			document[button.projectPartID+'Movie'].Play();
			
			$(this).removeClass("play");
			$(this).addClass("pause");
		}
		//movie is loaded and playing
		else if( $("#"+button.projectPartID).is("div") && document[button.projectPartID+'Movie'].GetRate() == 1 )
		{
			document[button.projectPartID+'Movie'].Stop();
	        
			$(this).removeClass("pause");
			$(this).addClass("play");
		}
		//movie is not loaded
		else
		{
			var args = ['images/projects/'+button.projectPartID+'.mov', '431', '323', '',
					'emb#name', button.projectPartID+'Movie',
					'id', button.projectPartID+'Movie',
					'enablejavascript', 'true',
					'controller', 'false',
					'autoplay', 'true',
					'bgcolor', 'black',
					'scale', 'aspect'];

			var txt = _QTGenerate("QT_WriteOBJECT_XHTML", true, args);
			$("#"+button.projectPartID).replaceWith( "<div id='"+button.projectPartID+"'>" +  txt + "</div>" );
			
			$(this).removeClass("play");
			$(this).addClass("pause");
		}
	
		$.lastButton = $(this);

	});
	
	$("div.project ul.selection li a.playFlash").mouseup( function()
	{
			
		var button = $(this).data("button");
		var projectPartID = button.projectPartID;
		
		//flash is not loaded
		if( $("#"+button.projectPartID).is("img") )
		{
			swfobject.embedSWF('images/projects/'+button.projectPartID+'.swf', projectPartID, "431", "323", "9.0.0");
			
			$(this).removeClass("playFlash");
			$(this).addClass("pauseFlash");
		}
		//flash is loaded
		else
		{
			$("#"+projectPartID).replaceWith("<img src='images/projects/"+projectPartID+".jpg' id='"+projectPartID+"'/>");
	        
			$(this).removeClass("pauseFlash");
			$(this).addClass("playFlash");
		}
	
		$.lastButton = $(this);

	});
	
	$("div.project div.description h2").mouseup( function()
	{
		var clickedID =  $(this).parent().parent().attr("id");
		location.hash = "/" + clickedID;
		scrollToElement( $(this).parent().parent() );

	});

})
