// JavaScript Document

var featureSecond = 0;
var slideshowSecond = 0;

$("document").ready(function() {

    $("dl#feature").addClass("loop");
	
	$.timer(1000, function (timer) {
		if ($("dl#feature").hasClass("loop")) {
			featureSecond++;
			if (featureSecond >= 5) {
				featureSecond = 0;
				moveNext();
			}
		}
	});   
    
    $("dl#feature dt").click(function() {
		pause();
		$("dl#feature dt.current").removeClass("current");
		$(this).addClass("current");
		$("dl#feature dd").stop(true).hide().removeClass("current");
		$(this).nextAll("dd").filter(":first").show().addClass("current");
	});
	
	
	$(".slideshow ul").addClass("loop");
	
	$('a[class="audio_player"]').click( function() {
        window.open( $(this).attr('href'),"audio_player","resizable=0,status=0,toolbar=0,location=0,menubar=0,directories=0,scrollbars=0,width=500,height=300" );
        return false;
    });
	
	$.timer(1000, function (timer) {
		if ($(".slideshow ul").hasClass("loop")) {
			slideshowSecond++;
			if (slideshowSecond >= 2) {
				slideshowSecond = 0;
				slideshowMoveNext();
			}
		}
	});
	
	
	//hack - technically not valid XHTML in DOM
	$("div img#microcosm-aquarium-explorer-starfish-living-stamp-inline").attr("frames", "13"); /* Projects : Microcosm Aquarium Explorer Living Stamps */
	
	$("img#microcosm-aquarium-explorer-starfish-living-stamp-inline").addClass("animated-sprite");

    $.timer(100, function (timer) {
        $("img.animated-sprite").each(function (i) {
            var numFrames = $(this).attr("frames");
            var top = String($(this).css("top"));
            var height = $(this).attr("height");
            var frameHeight = height / numFrames;
            top = top.split("p");
            top = top[0];
            top = top - frameHeight;
            if ((top * -1) / frameHeight >= (numFrames -1)) {
                top = 0;
            }
            top = String(top) + "px";
            $(this).css("top", top);
        });
    });
    
});

function moveNext() {
    if ($("dl#feature dt.current").nextAll("dt").filter(":first").size() == 0) {
        $("dl#feature dt.current").removeClass("current");
        $("dl#feature dt").filter(":first").addClass("current");
        $("dl#feature dd.current").fadeOut(500, function() {
            $(this).removeClass("current");
            $("dl#feature dd").filter(":first").fadeIn(500, function() {
                $(this).addClass("current");
            });
        });
    } else {
        $("dl#feature dt.current").removeClass("current").nextAll("dt").filter(":first").addClass("current");
        $("dl#feature dd.current").fadeOut(500, function() {
            $(this).removeClass("current").nextAll("dd").filter(":first").fadeIn(500, function() {
                $(this).addClass("current");
            });
        });
   }
};
function play() {
	featuredWorkSecond = 0;
	$("dl#feature").addClass("loop");
};
function pause() {
	$("dl#feature").removeClass("loop");
};

function slideshowMoveNext() {
	if ($(".slideshow ul li.current").next("li").size() == 0) {
			$(".slideshow ul li.current").fadeOut(500).removeClass("current");
			$(".slideshow ul li").fadeOut(500).filter(":first").addClass("current").fadeIn(500);
		} else {
			$(".slideshow ul li.current").fadeOut(500).removeClass("current").next("li").addClass("current").fadeIn(500);
		}
};
function slideshowMovePrevious() {
	if ($(".slideshow ul li.current").prev("li").size() == 0) {
			$(".slideshow ul li.current").fadeOut(500).removeClass("current");
			$(".slideshow ul li").fadeOut(500).filter(":last").addClass("current").fadeIn(500);
		} else {
			$(".slideshow ul li.current").fadeOut(500).removeClass("current").prev("li").addClass("current").fadeIn(500);
		}
};
function slideshowPlay() {
	slideshowSecond = 0;
	$(".slideshow ul").addClass("loop");
};
function slideshowPause() {
	$(".slideshow ul").removeClass("loop");
};