// local variables
var selected_thumb_id = 0;
var focused_thumb_id = -1;
var timeout_id;
var selected_level_1_id;
var selected_level_2_id;

// Custom jQuery Code
$(document).ready(function() {
	// ----- [ navigation ] -----
	
	// wrap the navigation links with sqwiggle brackets on hover
	$("#navLeftMenu li a").hover(function() {
		// never apply highlighting on active navItem since it's already selected
		if (this.id != "level1Item_"+ selected_level_1_id && this.id != 'level2Item_'+ selected_level_2_id) {
			$(this).prepend($("<span>{&nbsp;</span>")).append($("<span>&nbsp;}</span>"));
		}
	}, function() {
		// never remove highlighting on active navItem since must remain selected
		if (this.id != "level1Item_"+ selected_level_1_id && this.id != 'level2Item_'+ selected_level_2_id) {
			$(this).find("span").remove();
		}
	});
	
	// expand / contract the nav groups
	// this should be dynamic, but for now i've hard-coded it 3 times
	$("#level1Item_0").click(function () {
		if ($("#level2Items_").is(":hidden")) {
			$("#navGroupItems_0").slideDown();
		} else {
			$("#navGroupItems_0").slideUp();
		}
    });
	
	$("#level1Item_1").click(function () {
		if ($("#level2ItemsForItem_1").is(":hidden")) {
			$("#level2ItemsForItem_1").slideDown();
		} else {
			$("#level2ItemsForItem_1").slideUp();
		}
    });
	
	$("#level1Item_2").click(function () {
		if ($("#level2ItemsForItem_2").is(":hidden")) {
			$("#level2ItemsForItem_2").slideDown();
		} else {
			$("#level2ItemsForItem_2").slideUp();
		}
    });
	
	$("#level1Item_3").click(function () {
		if ($("#level2ItemsForItem_3").is(":hidden")) {
			$("#level2ItemsForItem_3").slideDown();
		} else {
			$("#level2ItemsForItem_3").slideUp();
		}
    });
	
	$("#level1Item_4").click(function () {
		if ($("#level2ItemsForItem_4").is(":hidden")) {
			$("#level2ItemsForItem_4").slideDown();
		} else {
			$("#level2ItemsForItem_4").slideUp();
		}
    });

	$("#level1Item_5").click(function () {
		if ($("#level2ItemsForItem_5").is(":hidden")) {
			$("#level2ItemsForItem_5").slideDown();
		} else {
			$("#level2ItemsForItem_5").slideUp();
		}
    });
		
	// show the selectd group/item
	$("#level2ItemsForItem_"+ selected_level_1_id).show();
		
	
	/*
	$("#navGroupItems_"+ selected_nav_group_id).show();
	$("#navItem_"+ selected_nav_group_id +"_"+ selected_nav_item_id).prepend($("<span>{&nbsp;</span>")).append($("<span>&nbsp;}</span>"));
	$("#navItem_"+ selected_nav_group_id +"_"+ selected_nav_item_id).css('color', '#3998B1');
	*/
	
	
	
	// ----- [ thumbnails ] -----
	
	// thumbnails
	/* to prevent buffering issues, all blends are added to the timer
	 */
	$("#thumb0").hover(function() {
		clearTimeout(timeout_id);
		timeout_id = setTimeout ('focusThumb(0)', 100);
	}, function() {
		// highlight the active thumb
		clearTimeout(timeout_id);
		timeout_id = setTimeout ('focusThumb(selected_thumb_id)', 250);
	});
	
	$("#thumb1").hover(function() {
		clearTimeout(timeout_id);
		timeout_id = setTimeout ('focusThumb(1)', 100);
	}, function() {
		// highlight the active thumb
		clearTimeout(timeout_id);
		timeout_id = setTimeout ('focusThumb(selected_thumb_id)', 250);
	});
	
	$("#thumb2").hover(function() {
		clearTimeout(timeout_id);
		timeout_id = setTimeout ('focusThumb(2)', 100);
	}, function() {
		// highlight the active thumb
		clearTimeout(timeout_id);
		timeout_id = setTimeout ('focusThumb(selected_thumb_id)', 250);
	});
	
	// fade second and third thubmanisl by default
	focusThumb(0);
		
	/*
	// ----- [ big images ] -----
	// pre-load all of the images so that the site runs quicker, but only after the page has finnished executing
	document.getElementById('templateAContentDetails').innerHTML = "pre";
	
	//var big_image_1 = new Image();
	//big_image_1.src = galleries[1];
	//document.getElementById('templateAContentDetails').innerHTML = "post: "+ galleries[1];
	
	var big_image_2 = new Image();
	big_image_2.src = galleries[2];
	document.getElementById('templateAContentDetails').innerHTML = "post: "+ galleries[2];
	*/
	// hide the emailMe form
	//$("#emailMeForm").hide();
});

function popImage(element_id) {
	$("#"+element_id).fadeTo(0, 0);
	$("#"+element_id).fadeTo(750, 1);
}

/* returns the focus to the active thumb and fades all others
 */
function focusThumb(id) {
	// to conserve cpu cycles, only fade the thumbs it is not currently focused
	if (focused_thumb_id != id) {
		switch (id) {
		case 0:
			$("#thumb0").fadeTo(500,1.0);
			$("#thumb2").fadeTo(500,0.3);
			$("#thumb1").fadeTo(1000,0.3);
			break;
		case 1:
			$("#thumb1").fadeTo(500,1.0);
			$("#thumb0").fadeTo(500,0.3);
			$("#thumb2").fadeTo(500,0.3);
			break;
		case 2:
			$("#thumb2").fadeTo(500,1.0);
			$("#thumb0").fadeTo(500,0.3);
			$("#thumb1").fadeTo(1000,0.3);
			break;
		}
		
		focused_thumb_id = id;
	}
}

/* show a form that allows the user to e-mail the current url to a friend
 */
function showEmailMe() {
	//alert('show');
	//$("#emailMeForm").css('color', 'blue');
	if ($("#emailMeForm").is(":hidden")) {
		$("#emailMeForm").slideDown("slow");
	} else {
		$("#emailMeForm").slideUp();
	}
}

function hideEmailMe() {
	//alert('hide');
	//$("#emailMeForm").css('color', 'magenta');
	
      if ($("#emailMeForm").is(":hidden")) {
        $("#emailMeForm").show("slow");
      } else {
        $("#emailMeForm").slideUp();
      }
}