$(document).ready(function() {
	var animateWhere;
	var showPort = 1;
	
	$(".LoginPage form a").livequery(function() {
		$(this).click(function() {
			var goID = $(this).attr("href");
			if (goID=="#form_login") {
				$(".LoginPage form#form_forgot").fadeOut(400, function() {
					$(".LoginPage form#form_login").fadeIn(400);
				});
			}
			else {
				$(".LoginPage form#form_login").fadeOut(400, function() {
					$(".LoginPage form#form_forgot").fadeIn(400);
				});
			}
			return false;
		});
	});
	$(".LoginPage form#form_forgot").livequery(function() {
		$(this).submit(function() {
			var goAction = $(this).attr("action");
			var DomainVal = $(this).find("#UserName").val();
			
			$.post(goAction, { UserName: DomainVal },
				function(data) {
					var Report = $(data).find(".report").html();
					$(".LoginPage form#form_forgot").find("img").fadeOut();
					$(".LoginPage form#form_forgot").find("input").fadeOut();
					$(".LoginPage form#form_forgot").find(".sendReport").html('<p>' + Report + '</p>');
					
			}, "html");
			return false;
		});
	});
	$("ul#Program li").livequery(function() {
		$(this).click(function() {
			var GoTo = $(this).attr("id");
			if (GoTo) {
				if ($(this).hasClass("activeTop")) {
					$('.' + GoTo).parent("#showUp").animate({"height": 0}, "slow", function() {
						$("#Program li").removeClass("activeTop");
					});
				}
				else {
					var GoTo = $(this).attr("id");
					$(this).parent("ul").children("li").removeClass("activeTop");
					$(this).addClass("activeTop");
					$('.' + GoTo).parent("#showUp").animate({"height": 0}, "slow").animate({"height": 140}, "slow");
				}
				return false;
			}
		});
	});
	
	// Portfolio
	
	$(".thumbNails li").livequery(function() {
		$(this).click(function() {
			if ($(this).hasClass("activeThumb")==false) {
				$(".thumbNails li").removeClass("activeThumb");
				$(this).addClass("activeThumb");
				
				var findImage = $(this).children(".holder").children("img").attr("alt");
				$(function () {
					var img = new Image();
					$(".disConcept").fadeTo("fast", 0);
					$(".loading").fadeIn("fast");
					$(img).load(function () {
						$('.disConcept').html(this);
						$(".loading").fadeOut("normal");
						$(".disConcept").fadeTo("fast", 1.00);
						$(this).width();
						$(this).height();
					}).error(function () {
						// notify the user that the image could not be loaded
					}).attr('src', findImage);
				});
			}
		});
	});
	
	// Showcased Icons
	$(".Works").livequery(function() {
		$(this).mousemove(function(e) {
			var offset = $(this).offset();
			var x = e.pageX - (offset.left);
			var y = e.pageY - (offset.top);

			var divWidth = $(this).width();
			var divHeight = $(this).height();
			
			var maxWidth = $(this).children(".showBox").width();
			var maxHeight = $(this).children(".showBox").height();
			var imgHeight = $(this).children(".showBox").children("img").height();
			var imgWidth = $(this).children(".showBox").children("img").width();
			var maxLeft = Math.round(maxWidth - imgWidth);
			var maxTop = Math.round(maxHeight - imgHeight);
			
			if (x < Math.round(maxWidth / 2) && y < Math.round(maxHeight / 2) && animateWhere != "TL") {
				// Top Left
				animateWhere = "TL";
				$(this).children(".showBox").children("img").stop();
				$(this).children(".showBox").children("img").animate({ left: 0, top: 0 }, Math.round(Math.random() * 2500 + 1500));
			}
			else if (x < Math.round(maxWidth / 2) && y > Math.round(maxHeight / 2) && animateWhere != "BL") {
				// Bottom Left
				animateWhere = "BL";
				$(this).children(".showBox").children("img").stop();
				$(this).children(".showBox").children("img").animate({ left: 0, top: maxTop }, Math.round(Math.random() * 2500 + 1500));
			}
			else if (x > Math.round(maxWidth / 2) && y < Math.round(maxHeight / 2) && animateWhere != "TR") {
				// Top Right
				animateWhere = "TR";
				$(this).children(".showBox").children("img").stop();
				$(this).children(".showBox").children("img").animate({ left: maxLeft, top: 0 }, Math.round(Math.random() * 2500 + 1500));
			}
			else if (x > Math.round(maxWidth / 2) && y > Math.round(maxHeight / 2) && animateWhere != "BR") {
				animateWhere = "BR";
				$(this).children(".showBox").children("img").stop();
				$(this).children(".showBox").children("img").animate({ left: maxLeft, top: maxTop }, Math.round(Math.random() * 2500 + 1500));
			}
		});
		$(this).mouseout(function() {
			animateWhere = "";
			var maxWidth = $(this).children(".showBox").width();
			var maxHeight = $(this).children(".showBox").height();
			var imgHeight = $(this).children(".showBox").children("img").height();
			var imgWidth = $(this).children(".showBox").children("img").width();
			$(this).children(".showBox").children("img").stop();
			$(this).children(".showBox").children("img").animate({ left: Math.round(maxWidth / 2) - Math.round(imgWidth / 2), top: Math.round(maxHeight / 2) - Math.round(imgHeight / 2) }, Math.round(Math.random() * 2500 + 1500));
		});
	});
		// Showcase Slider
	$(".portSelect ul li a").livequery(function() {
		$(this).click(function() {
			
			var maxChildren = $(".portSelect ul").children("li").size() - 2;
			var viewWidth = $("#portListing").width();
			var goToPixel;
			
			if ($(this).hasClass("activePort")==false && $(this).parent().hasClass("prevSel")==false && $(this).parent().hasClass("nextSel")==false) {
				var goToPort = $(this).html();
				
				$(".portSelect ul li a").removeClass("activePort");
				$(this).addClass("activePort");
				
				goToPixel = Math.ceil(((goToPort - 1) * viewWidth) * -1);
				showPort = goToPort;
			}
			else if ($(this).parent().hasClass("prevSel")==true) {
				if (showPort==1) {
					showPort = maxChildren;
				}
				else {
					showPort = showPort - 1;
				}
				$(".portSelect ul li a").removeClass("activePort");
				$(this).parent().parent().find("li").eq(showPort - 1).children("a").addClass("activePort");
				goToPixel = Math.ceil(((showPort - 1) * viewWidth) * -1);
			}
			else if ($(this).parent().hasClass("nextSel")==true) {
				if (showPort==maxChildren) {
					showPort = 1;
				}
				else {
					showPort = showPort + 1;
				}
				$(".portSelect ul li a").removeClass("activePort");
				$(this).parent().parent().find("li").eq(showPort - 1).children("a").addClass("activePort");
				goToPixel = Math.ceil(((showPort - 1) * viewWidth) * -1);
			}
			$("#portListing div.slider").animate({ left: goToPixel - 1 }, Math.round(Math.random() * 500 + 700), "easeInOutSine");
			return false;
		});
	});
	$("#WebHolder .arrows a").livequery(function() {
		$(this).click(function() {
			var maxChildren = $("#WebHolder .holder").children("img").size() - 1;
			var viewWidth = $("#WebHolder").width();
			var goToPixel;
			if ($(this).hasClass("prev")==true) {
				if (showPort==1) {
					showPort = maxChildren;
				}
				else {
					showPort = showPort - 1;
				}
				goToPixel = Math.ceil(((showPort - 1) * viewWidth) * -1);
			}
			else if ($(this).hasClass("next")==true) {
				if (showPort==maxChildren) {
					showPort = 1;
				}
				else {
					showPort = showPort + 1;
				}
				goToPixel = Math.ceil(((showPort - 1) * viewWidth) * -1);
			}
			$("#WebHolder div.holder").animate({ left: goToPixel }, Math.round(Math.random() * 500 + 700), "easeInOutSine");
			return false;
		});
	});
});

function timedGallery() {
	$("#imgShow img").livequery(function() {

		var maxWidth = 656;
		var maxHeight = $("#imgShow").height();
		var imgHeight = $(this).height();
		var imgWidth = $(this).width();
		var maxLeft = maxWidth - imgWidth;
		var maxTop = maxHeight - imgHeight;
		
		$(this).animate({ left: Math.round(Math.random() * maxLeft), top: Math.round(Math.random() * maxTop) }, Math.round(Math.random() * 8000 + 3500), timedGallery);
		//$(this).animate({ left: maxWidth - imgWidth, top: maxHeight - imgHeight}, 8000).animate({ top: 0 }, 8000).animate({ left: 0, top: maxHeight - imgHeight}, 8000).animate({ left: 0, top: 0}, 8000);
	});
}