$(document).ready(function() {

	$("li#subMenu").livequery(function() {
		$(this).hover(function() {
			if (!$(this).hasClass("active")) {
				$(this).children("ul").show('normal');
			}
		},
		function() {
			if (!$(this).hasClass("active")) {
				$(this).children("ul").hide('normal');
			}
		});
	});
	
	$(".Next").fadeTo("slow", 0.60);
	$(".Previous").fadeTo("slow", 0.60);
	$(".showSlide .outter img").fadeTo("slow", 0.75);
	
	$(".Next").livequery(function() {
		$(this).hover(function() {
			$(this).fadeTo("slow", 1.00);
		},
		function() {
			$(this).fadeTo("slow", 0.60);
		});
		$(this).click(function() {
			var imgWidth = 111;
			var goLeft;
			var toLoad = $(this).parent().find(".showSlide").find("img").size();
			var maxWidth = Math.round(toLoad * imgWidth);
			var slideWidth = $(this).parent().find(".showSlide").width();
			var maxBuffer = Math.round(maxWidth - slideWidth);
			var onLeft = parseInt($(this).parent().find(".showSlide").find(".outter").css('left'));
																						  
			if (onLeft > (maxBuffer * -1)) {
				if ((onLeft + (imgWidth * -3)) > (maxBuffer * -1)) {
					goLeft = (onLeft + (imgWidth * -3));
				}
				else {
					goLeft = (maxBuffer * -1);
				}
				$(this).parent().find(".showSlide").find(".outter").animate({ left: goLeft }, 300);
			}
			return false;
		});
	});
	$(".Previous").livequery(function() {
		$(this).hover(function() {
			$(this).fadeTo("slow", 1.00);
		},
		function() {
			$(this).fadeTo("slow", 0.60);
		});
		$(this).click(function() {
			var imgWidth = 111;
			var goLeft;
			var toLoad = $(this).parent().find(".showSlide").find("img").size();
			var maxWidth = Math.round(toLoad * imgWidth);
			var slideWidth = $(this).parent().find(".showSlide").width();
			var maxBuffer = Math.round(maxWidth - slideWidth);
			var onLeft = parseInt($(this).parent().find(".showSlide").find(".outter").css('left'));
																						  
			if (onLeft < 0) {
				if ((onLeft + (imgWidth * 3)) < 0) {
					goLeft = (onLeft + (imgWidth * 3));
				}
				else {
					goLeft = 0;
				}
				$(this).parent().find(".showSlide").find(".outter").animate({ left: goLeft }, 300);
			}
			return false;
		});
	});
	$(".showSlide .outter img").livequery(function() {
		$(this).hover(function() {
			$(this).fadeTo("slow", 1.00);
		},
		function() {
			$(this).fadeTo("slow", 0.75);
		});
		$(this).click(function() {
			var imgLink = $(this).attr("alt");
			$("body").append("<div id=\"vp-shader\"></div><div id=\"vp-alert\"></div>");
			$("#vp-shader").height($(document).height());
			$("#vp-shader").fadeTo(0, 0.45).addClass("vp-alert-on");
			$("#vp-alert").fadeTo(0, 1.00).addClass("vp-alert-on");
			$("#vp-alert").html('<img src="' + imgLink + '" class="sp" />');
			return false;
		});
	});
	$("#vp-shader").livequery(function() {
		$(this).click(function() {
			$("#vp-alert").remove();
			$("#vp-shader").remove();
			return false;
		});
	});
	$("#vp-alert img").livequery(function() {
		$(this).click(function() {
			$("#vp-alert").remove();
			$("#vp-shader").remove();
			return false;
		});
	});
	$("#ChangeIt #Image").livequery(function() {
		$(this).change(function() {
			var newImage = $(this).val();
			var Path = $("#DataPath").val();
			var img = new Image();
			$(img).load(function () {
					$(this).hide(); 
					$("#placeholder").html(this);
					$(this).width();
					$(this).height();
					$(this).fadeIn();
				}).error(function () {
					alert("Error Loading Image");
				}).attr('src', Path + newImage);
				return false;
			});
	});
	
});
