jQuery.preloadImages = function() {
	for(var i=0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}
$.preloadImages("/images/project_shadow.png", "/images/loading.gif", "/images/projects/sotm_bg.png", "/images/thickbox_top.png");

var currentSheet = "";
function switchStyleSheet(ssid) {
	if( currentSheet !== ssid ) {
		$("#css").after('<link type="text/css" rel="stylesheet" id="' + ssid + '" href="/css/' + ssid + '.css" />');
		currentSheet = ssid;
	} else {
		$("#"+ssid).remove();
		currentSheet = "";
	}
}

function showHideLogoBasedOnWindowWidth() {
	var width = $(window).width();
	if( width < 1100 ) {
		$("#logo").hide();
		$("#home").addClass("narrow");
		$("#header-logo").show();
	} else {
		$("#header-logo").hide();
		$("#logo").show();
		$("#home").removeClass("narrow");
	}
}

// convert to fancy pants version
$("#simple").remove();

$(function() {
	// convert email addresses
	$("span.mail").each(function() {
		var address = $(this).text();
		address = address.replace(/ at /, "@");
		address = address.replace(/ dot /g, ".");
		var link = $(this).attr("title") || address;
		$(this).html('<a href="mailto:'+address+'">'+link+'</a>');
	});

	// open all external links in new window
	$("a[href^=http]:not(.local)").live("click", function(e) {
		e.preventDefault();
		window.open(this.href);
	});

	$(".scrollies a").live("click", function(e) {
		e.preventDefault();
	});

	var home = $("#home").length,
	about = $("#about").length,
	archived = $("#archived").length,
	backroom = $("#backroom").length;
	if( about > 0 ) {
		sotm.setMode("about");
	} else if( archived > 0 ) {
		sotm.setMode("archive");
	} else if( backroom > 0 ) {
		sotm.setMode("backroom");
	} else {
		sotm.setMode("std");
	}

	// we're on the home page, archive page or backroom
	if( home > 0 || about > 0 || archived > 0 || backroom > 0 ) {
		// prepare first project
		sotm.init();

		// load any requested project
		sotm.loadProjectFromURL();

		// key commands
		$(document).keyup(function(e) {
			sotm.getProjectWrapper();
			var k = e.keyCode,
			totalSlides = sotm.getSlideCount();

			// general keycodes
			if( k === 20 ) {
				switchStyleSheet("LARGE");
			} else if( k === 35 ) {
				sotm.moveSlide(totalSlides);
			} else if( k === 36 ) {
				sotm.moveSlide(1);
			} else if( k === 8 || k === 37 || k === 109 ) {
				sotm.moveSlide("left");
			} else if( k === 9 || k === 39 || k === 61 || k === 107 ) {
				sotm.moveSlide("right");
			} else if( k === 48 ) {
				sotm.moveSlide(10);
			} else if( k >= 49 && k <= 57 ) {
				sotm.moveSlide(k-48);
			}

			// home page specific keycodes
			if( home > 0 ) {
				if( k === 33 ) {
					sotm.moveProject(1);
				} else if( k === 34 ) {
					sotm.moveProject(totalProjects);
				} else if( k === 38 ) {
					sotm.moveProject("up");
				} else if( k === 40 ) {
					sotm.moveProject("down");
				} else if( k === 96 ) {
					sotm.moveProject(10);
				} else if( k >= 97 && k <= 105 ) {
					sotm.moveProject(k-96);
				}
			}
			return false;
		});
	}

	// home page only
	if( home > 0) {

		// allow grid projects to be clicked once they are current
		$(".project-shadow a:not(.curr .project-shadow a)").live("click", function(e) {
			e.preventDefault();
			sotm.moveProject($(this).attr("class").replace("id-", ""));
		});

		swfobject.embedSWF("/sotm.swf", "logo", "223", "43", "8.0.0", "", "", { wmode: "transparent" });

		// run now, but also on resize
		showHideLogoBasedOnWindowWidth();
		$(window).resize(function() {
			showHideLogoBasedOnWindowWidth();
		});

		$("#project-list").click(function() {
			$("#project-list ul").slideToggle();
		}).bind("mouseleave", function() {
			$("#project-list ul").slideUp();
		});

		$("#up, #down").click(function(e) {
			e.preventDefault();
			sotm.moveProject($(this).attr("id"));
		});

		// turn on auto slide for the first project after 10 seconds
		//sotm.autoSlideTimeout = setTimeout(function() { sotm.autoSlide(1) }, 10000);

		var mouseWheelTimer = null,
		totalProjects = sotm.getProjectCount();

		$(document).mousewheel(function(e, delta) {
			var direction = "",
			move = false;
			// clear our timer 
			if( mouseWheelTimer ) clearTimeout(mouseWheelTimer);
			// move up
			if( delta > 0 && sotm.currentProject > 1 ) {
				direction = "up";
				move = true;
			// move down
			} else if( delta < 0 && sotm.currentProject < totalProjects ) {
				direction = "down";
				move = true;
			}
			if( move === true ) {
				mouseWheelTimer = setTimeout(function() { sotm.moveProject(direction) }, 100);
			}
		});

	}

	// backroom only
	if( backroom > 0) {
		$("#bricons .icon-row div").hover(function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		});
	}
});
