var loading = '<p id="loading"><img src="/images/loading.gif" alt="Loading..." class="loading" /> loading&hellip;</p>';
$(function() {
	// shuffle arrays or elements
	$.fn.shuffle = function() {
		return this.each(function() {
			var items = $(this).children();
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i;
			j = parseInt(Math.random() * i),
			x = arr[--i], arr[i] = arr[j], arr[j] = x
		);
		return arr;
	}
	
	// sync list & thumbnail rollovers
	$("#list a").hover(function() {
		var x = $(this).parent("li").attr("class");
		$("#thumbnails li." + x).addClass("active");
	}, function() {
		var y = $(this).parent("li").attr("class");
		$("#thumbnails li." + y).removeClass("active");
	});
	
	$("#thumbnails a").hover(function() {
		var x = $(this).parent("li").attr("class");
		$("#list li." + x).addClass("active");
	}, function() {
		var y = $(this).parent("li").attr("class");
		$("#list li." + y).removeClass("active");
	});
	
	$("#header h1 a").hover(function() {
		$(this).text("Dick");
	}, function() {
		$(this).text("Richard Henkels");
	});
	
	// photography section
	$("#photoDetail").hide();
	$("#photos a").click(function() {
		$("h2, #mainNavigation, #photos").hide();
		$("#footer").addClass("photography");
		var $photo = '<img src=\"' + this.href + '" alt="" />';
		$("#photoDetail").show().html($photo);
		return false;
	});
	
	$("#photoDetail").click(function() {
		$("h2, #mainNavigation, #photos").show();
		$("#footer").removeClass("photography");
		$("#photoDetail").empty().hide();
	});
	
	// open external links in new window
	$("a.external").click(function() {
		window.open(this.href);
		return false;
	});
	
	// cloak email address
	$("a.email").click(function() {
		var to = $(this).attr("id");
		var url = this.href.split("http://");
		var domain = url[url.length-1];
		domain = domain.replace('/', '');
		$(this).attr("href", "mailto:" + to + "@" + domain);
	});
	
	$("a.play").click(function() {
		$("#list li, #thumbnails li").removeClass("active");
		var me = $(this).parent().attr("class");
		$("li." + me).addClass("active");
		if ($.browser.msie) {
			if ($.browser.version < 7) {
				$("#video").append("<p>Internet Explorer 6 is not supported.<br />Please use <a href=\"http://getfirefox.com\" class=\"external\">Firefox</a> or <a href=\"http://www.apple.com/safari/download\" class=\"external\">Safari</a>.");
				return false;
			}
		}
		$("#video").empty();
		$path = $(this).attr("href").split("/");
		//alert($path[2]);
		$path = $path[2].split(".");
		$clip = $path[0];
		$.ajax({
			url: "/clips/" + $clip + ".html",
			cache: false,
			dataType: "html",
			success: function(html){
				$("#video").append(html);
				var $clipHeight = $("#video div:first").attr('class');
				$clipHeight = $clipHeight.split('x');
				$clipHeight = parseFloat($clipHeight[1]);
				//alert($clipHeight);
				$("#page").css('padding-top', $clipHeight + 30);
				$("#video").css('height', $clipHeight + 16);
			},
			complete: function() {
				loadClip($clip);
			}
		});
		window.scrollTo(0,0);
		return false;
	});
	
	// auto play from active clip
	$("p#loading").hide();
	$("#movPlayer").live('qt_ended', function() {
		if ($("#list li.active").is(":last-child")) {
			$("#video").empty();
		} else {
				$("#video").empty().append(loading).fadeIn("normal", function() {
				$("#video p#loading").delay(2000).fadeOut("normal", function() {
					$("#list li.active").next("li").children("a").trigger("click");
				});
			});
		}
	});
	
	$("#movPlayer").hide();
	$("#movPlayer").load(function() {
		$(this).show();
	});
});

function loadClip(movpath) {
	if ($.browser.safari) {
		var player = document.getElementById("movPlayer");
		if (player) {
			player.Stop();
		}
	}
	var theMov = QT_GenerateOBJECTText_XHTML('/videos/'+movpath+'.mov', '100%', '100%', '', 'EnableJavaScript','True', 'postdomevents','True', 'obj#ID','movPlayer', 'emb#NAME','movPlayer', 'autoplay','true', 'showlogo','true', 'controller','true', 'kioskmode','true', 'scale','1', 'allowembedtagoverrides','true', 'loop','false');
	$("#clip").empty().append(theMov);
}