﻿$().ready(function() {
	var _intervalID = ""
	$(".project-item").hover(function() {
		$(this).children(".project-image").children(".project-image-controller").css("z-index", "10")
		$(this).children(".project-image").children(".project-image-controller").stop().animate({
			marginTop: 0
		}, 500)
		$(this).addClass("animating");
		$(".project-item.animating img.preload").each(function() {
			if ($(this).attr("longdesc") != "") {
				$(this).attr({
					src: $(this).attr("longdesc"),
					longdesc: ""
				})
			}
		})
		_intervalID = setInterval(function() { $(".project-item.animating .project-image-controller a:first").next().click() }, 2000)
	}, function() {

		if (!$(".project-item.animating .project-image-controller a:first").hasClass('active')) {
			$(".project-item.animating .project-image-container img.active").removeClass("active").animate({ opacity: 0 }, 500);
			$(".project-item.animating .project-image-container img").eq(0).addClass("active").animate({ opacity: 1 }, 500);
			$(".project-item.animating .project-image-controller a").removeClass("active")
			$(".project-item.animating .project-image-controller a").eq(0).addClass("active")
		}
		$(this).removeClass("animating");
		$(this).children(".project-image").children(".project-image-controller").stop().animate({
			marginTop: -32
		}, 500)
		clearInterval(_intervalID)
	})

	$(".project-item .project-image-controller a").bind("click", function() {
		clearInterval(_intervalID)
		var _index = $(".project-item").index($(this).parents(".project-item"));
		var _clickIndex = $(".project-item.animating .project-image-controller a").index(this);
		$(".project-item.animating").find(".project-image-container img").css("opacity", "0")
		$(".project-item.animating .project-image-controller a").removeClass("active")
		$(this).addClass("active")
		$(".project-item.animating .project-image-container img.active").removeClass("active").animate({ opacity: 0 }, 500);
		$(".project-item.animating .project-image-container img").eq(_clickIndex).addClass("active").animate({ opacity: 1 }, 500);
		if ($(".project-item.animating .project-image-controller a.active").next().length == 0) {
			_intervalID = setInterval(function() { $(".project-item.animating .project-image-controller a").eq(0).click() }, 2500)
		} else {
			_intervalID = setInterval(function() { $(".project-item.animating .project-image-controller a.active").next().click() }, 2000)
		}
		return false;
	})




})
