
var MM_contentVersion = 6;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	    for (var i = 0; i < words.length; ++i)
	    {
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i]; 
	    }
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
   && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}








// the project that is currently open
var currentProject = -1;

// this remembers how often you open and close the same thing
// - you can't get the state of the JQuery accordion so we have to watch for it.
var toggleCount = 0;
function getAtSign() { return "@"; }
// which image is the current one for each project
var currentImages = new Array();
var projectBackgrounds = new Array();
// when the page loads do this stuff
$(document).ready(function(){
	// insert the email 
	$("#email").html("contact"+getAtSign()+"arthurschmitt.com");
	// there's no flash, but if flash can't play, 
	// then show a simpler version. (mainly for iPhone)
	if(MM_FlashCanPlay) {
		// set the first image button of each project to on
		var lis = $('#accordion').children();
		currentImages[0] = -1; // were on a 1-based index, ignore [0]
		projectBackgrounds[0] = -1; // ditto
		for(var i = 0; i < lis.length; i++) {
			$("#imageButton" + (i+1) + "-1").css("background-image", "url(img/imageOn.gif)");
			currentImages[i+1] = 1;
			projectBackgrounds[i+1] = $(lis[i]).children(".project").css("background-image");
		}
		
		// create the accordion
		$("#accordion").accordion({
			alwaysOpen: false,
			active: false,
			autoHeight:false
		});
	
		// hide all tabs
		$('#accordion').accordion('activate', false);
		
		$('div.content').css("display", "block");
	
		// disable the links, we're going to operate it manually.
		$('#accordion').accordion('disable');
		
		// add a mouse over/out behaviour
		$(".project").hover(
			// over
			function() {
				$(this).css("background-image","none");
				$(this).children("span.title").css("visibility","visible");
			},
			// out
			function() {
				itemIndex = getItemIndex(this);
				if(currentProject!=itemIndex) {
					// switch back the background
					$(this).css("background-image", projectBackgrounds[itemIndex+1]);
					$(this).children("span.title").css("visibility","hidden");
				}
			}
		);
		
		
		// show a project when it's clicked
		$(".project").click(function() {
			if(currentProject!=-1 || currentProject!=getItemIndex(this)) {
				$("#project"+currentProject).css("background-image", projectBackgrounds[currentProject+1]);
				$("#project"+currentProject).children("span.title").css("visibility","hidden");
			}
			var oldCurrentProject = currentProject;
			
			
			currentProject = getItemIndex(this);
			$("#accordion").accordion("enable");
			$("#accordion").accordion("activate", currentProject);
			$("#accordion").accordion("disable");
			
			// accordion("activate") on the same tab twice opens and closes
			// it so this just keeps an eye whether something's open or closed
			// and displays text accordingly.
			if(oldCurrentProject==currentProject) {
				toggleCount++;
				if(toggleCount%2==0) {
					// opening
					$("#project"+currentProject).css("background-image", "none");
					$("#project"+currentProject).children("span.title").css("visibility","visible");
				} else {
					// closing
					currentProject = -1;
				}
			} else {
				toggleCount = 0;
			}
			
			
			
			if(currentProject>oldCurrentProject && oldCurrentProject>-1) {
				setTimeout(function() {
					$.scrollTo($("#accordion").children()[currentProject], 500);
				}, 500);
			} else {
				$.scrollTo($("#accordion").children()[currentProject], 500);
			}
			
		});
	} else {
		$('div.content').css("display", "block");
		$(".project").css("background-image","none");
		$(".project span.title").css("visibility","visible");
		
	}
});

// gets the index of the list element in the list of projects
function getItemIndex(a) {
	var lis = $('#accordion').children();
	for(var i = 0; i < lis.length; i++) {
		if(a.id==$(lis[i]).children('a').get(0).id) {
			return i;
		}
	}
	return -1;
}

/**
 * when you press the little buttons below the main picture
 * this gets called to change the image.
 * parameters:
 * 		image 	- the URL of the image to switch to
 *		caption - the caption of the image to switch to
 * 		id 		- the id of the button pressed.
 * 				  This holds more information about it:
 * 				  the format of the id is imageButtonX-Y
 * 				  where X is the project index and Y is
 * 				  the image index.
 */
function switchImage(image, caption, id) {
	if(id.indexOf("imageButton")!=null) {
		numbers = id.substring(11);
		numbers = numbers.split("-");
		
		// change the image
		$("#image"+numbers[0]).attr('src', image);
		// change the clicked image button to loading
		$("#"+id).css("background-image", "url(img/imageLoading.gif)");

		$("#image"+numbers[0]).load(function() {
			// change the caption
			$("#caption"+numbers[0]).html(caption);
		
			// set the old imageButton to seen
			$("#imageButton" + numbers[0] + "-" + currentImages[parseInt(numbers[0])]).css("background-image", "url(img/imageSeen.gif)");
		
			// update the current image array
			currentImages[numbers[0]] = parseInt(numbers[1]);
		
			// update the current image button to on (same line as before but the currentImages array has been updated
			$("#imageButton" + numbers[0] + "-" + currentImages[parseInt(numbers[0])]).css("background-image", "url(img/imageOn.gif)");
		});
		
	}
}
