﻿var activeShowElement = null;
$(function () {
	$(".menuroller").hide();

	$("#menu li").click(function () {
		$("#menu li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".menuroller").hide(); //Hide all tab content

		var a = $(this).find("a"); //Find the href attribute value to identify the active tab + content
		var activeTab = a.attr("href");
		var activeShow = activeTab.substring(1);
		var role = a.attr("data-role");
		$(".show").hide();
		$("#rolefoto, #roletext").hide();
		$("#role" + role).show();
		if (role == 'text') { $("#overlayinfo, .nav").hide(); $("#roletext div:first").show(); } else { $("#overlayinfo, .nav").show(); }
		activeShowElement = $("#show" + activeShow).show();
		$(activeTab).show(); //Fade in the active ID content
		$(activeTab).find("ul li:first a").click();
		return true;
	});

	addPhotos();
	$(".thumb").click(function () {
		if (activeShowElement) {
			$(".thumb").removeClass("active");
			$(this).addClass("active");
			var imgSrc = $(this).attr("data-img");
			var imgTitle = $(this).attr("data-desc");
			var img = $(activeShowElement).children("img")[0];
			$(img).attr("src", imgSrc);
			$(img).attr("title", imgTitle);
			$(img).attr("alt", imgTitle);
			$("#overlay").text(imgTitle);
			$("#forward").data("go", $(this).next(".thumb"));
			$("#back").data("go", $(this).prev(".thumb"));
			return false;
		}
	});

	$(".post").click(function () {
		if (activeShowElement) {
			$(".post").removeClass("active");
			$(".textcontent").hide();
			$(this).addClass("active");
			var id = $(this).children("a").attr("href");
			$(id).show();
			return false;
		}
	});

	$("#back").click(function () {
		var d = $(this).data("go");
		if ($(d).length > 0) {
			$(d).click();
		}
		return false;
	});
	$("#forward").click(function () {
		var d = $(this).data("go");
		if ($(d).length > 0) {
			$(d).click();
		}
		return false;
	});

	var hash = window.location.hash;
	if (hash.length > 0) {
		var menu = $("#menu li a[href='" + hash + "']");
		if (menu.length > 0) {
			menu.click();
		} else {
			$("#menu li:first").click();
		}
	} else {
		$("#menu li:first").click();
	}

	$("#overlayinfo").hover(
				function () { $("#overlay").slideDown("slow") },
				function () { $("#overlay").slideUp("slow") }
			);
	var top = $('#roller')[0].clientHeight * 0.5;
	var bottom = $('#roller')[0].clientHeight * 0.8;
	var height = $('#roller')[0].clientHeight;
	var goTo = 0;
	$('.carousel li a img').hover(function (e) {
		var c = $(this).parents('.carousel').children('li').length;
		var time = ((c / 6) * 4000);
		var r = $(this).parents('.carousel')[0].clientHeight;
		var cY = e.clientY;
		//$('#forward').text(cY + ' ' + top + ' ' + bottom);
		if (cY < top) { // scroll down
			goTo = 0;
			$(this).parents('.carousel').animate({ marginTop: "0px" }, time);
		} else if (cY > bottom) { // scroll up
			var mt = height - r;
			goTo = mt;
			$(this).parents('.carousel').animate({ marginTop: mt + "px" }, time);
		} else {
			$(this).parents('.carousel').stop(true);
		}
	}, function () {
		var mt = parseFloat($(this).parents('.carousel').css('marginTop'));
		if (mt == goTo) {
			$(this).parents('.carousel').stop(true);
		}
	});
});

function addPhoto(cid, thumbPath, mainPath, description, ahref) {
	var s = '<li class="thumb" data-img="' + mainPath + '" data-desc="' + description + '"><a href="#' + ahref + '"><img src="' + thumbPath + '" alt="' + description + '" width="120" height="80" /></a></li>';
	$(cid).append(s);
				
}
