$(document).ready(function(){
	// *** Portfolio Filter ***
	// ************************
	$("#filter").change(function(){
		// Get the selected option from dropdown
		var opt = $("#filter option:selected").val();
		
		// If the dropdown has already been used, hide and remove the cloned div's
		if($("#portfolioLayoutOn").length>0){
			$("#portfolioLayoutOn").fadeOut("fast",step2);
			//$("#portfolioLayoutOff").fadeOut("fast");
		} else {
			step2();
		}
		function step2(){
			if(opt==""){
				// If the user selected "Show All"
				$("#portfolioLayout").fadeIn("normal");
			} else if($("#portfolioLayout").attr("style")==undefined || $("#portfolioLayout").attr("style") == ""){
				// If this is the first time using the dropdown
				$("#portfolioLayout").fadeOut("fast",swapProjs);
			} else if($("#portfolioLayout").attr("style").indexOf("block")!=-1) {
				// If the main layout's display is set to block
				$("#portfolioLayout").fadeOut("fast",swapProjs);
			} else {
				swapProjs();
			}
		}
		function swapProjs(){
			// Create Project Thumbs for "ON" projects and highlight them
			$("#portfolioLayoutOn").remove();
			$("#portfolioLayout").clone().attr("id","portfolioLayoutOn").insertAfter("#portfolioLayout").fadeTo(25,0);
			$("#portfolioLayoutOn p.projThumb").each(function(){
				$(this).hide();
			})
			$("#portfolioLayoutOn p:has(a[href*='s="+opt+"'])").each(function(){
				$(this).show();
				//$(this).fadeTo(50,1);
				//$(this).addClass("highlight");
			})
			// Create Project Thumbs for "OFF" projects and then fade them back
			/*
			$("#portfolioLayoutOff").remove();
			$("#portfolioLayout").clone().attr("id","portfolioLayoutOff").insertAfter("#portfolioLayoutOn").fadeTo(25,0);
			$("#portfolioLayoutOff p:has(a[href*='s="+opt+"'])").each(function(){
				$(this).hide();
			})
			$("#portfolioLayoutOff p.projThumb").each(function(){
				$(this).fadeTo(50, 0.30);
			})
			*/
			fadeInOnDiv();
			//fadeInOffDiv();
		}
		function fadeInOnDiv(){
			$("#portfolioLayoutOn").css("display","block");
			$("#portfolioLayoutOn").fadeTo("normal",1);
		}
		/*function fadeInOffDiv(){
			$("#portfolioLayoutOff").css("display","block");
			$("#portfolioLayoutOff").fadeTo("normal",1);
		}*/					
	});
	
	
	
	// === CERTIFICATE OVERLAY ===
	// ===========================
	$("#wbencLogo a").click(function(event){
		event.preventDefault();
		if($("#certs").css("display") == "none"){
			positionOverlay();
			//$("#certsLinksBG").css("opacity",.3);
			//$("#certsLinksBG").fadeIn("fast");
			if(jQuery.support.style){
				// If NOT IE...
				$("#certs").fadeIn("normal");
			} else {
				// If IE...
				$("#certsHeader").css("background-image","none");
				$("#certsLinks").css("background-image","none");
				$("#certsFooter").css("background-image","none");
				$("#certs").fadeIn("normal");
			}
			certsTimer = setInterval("positionOverlay()",100)
		} else if ($("#certs").css("display") == "block"){
			event.preventDefault();
			if(jQuery.support.style){
				// If NOT IE...
				$("#certs").fadeOut("normal");
			} else {
				// If IE...
				$("#certs").fadeOut("normal");
			}
			window.clearInterval(certsTimer);
		}
	});
	$("#closeLink a").click(function(event){
		event.preventDefault();
		//$("#certsLinksBG").fadeOut("fast");
		if(jQuery.support.style){
			// If NOT IE...
			$("#certs").fadeOut("normal");
		} else {
			// If IE...
			$("#certs").fadeOut("normal");
		}
		window.clearInterval(certsTimer);
	});
	/*$("#certsLinksBG").click(function(event){
		$("#certsLinksBG").fadeOut("fast");
		$("#certs").fadeOut("normal");
		window.clearInterval(certsTimer);
	});*/
});

var certsTimer;
function positionOverlay(){
	// Get browser's width and height
	var bWidth = $(window).width();
	var bHeight = $(window).height();
	// Get Overlay Div's width and height
	var dWidth = $("#certs").width();
	var dHeight = $("#certs").height();
	// Position Overlay in the center of the browser
	$("#certs").css("left",(bWidth-dWidth)/2);
	$("#certs").css("top",(bHeight-dHeight)/2);
}


// === PROJECT SCREENS ===
// =======================
function projScreens(){
	if(!document.getElementById) return;
	if(!document.getElementById('ps1')) return;
	// If there's not more then 1, hide the numbers
	if(!document.getElementById('ps2')){
		document.getElementById('projectScreensNums').style.display = "none";
		return;
	}
	var p1 = document.getElementById('photo1');
	var p1num = document.getElementById('ps1');
	var p2 = document.getElementById('photo2');
	var p2num = document.getElementById('ps2');
	var p3 = document.getElementById('photo3');
	var p3num = document.getElementById('ps3');
	var p4 = document.getElementById('photo4');
	var p4num = document.getElementById('ps4');
	
	document.getElementById('ps1').onclick = function(){
		$("#projectScreens div:has(img)").each(function(i){
			var l = $("#projectScreens div:has(img)").length
			if((i+1)<l){$(this).css("display","none");}
			else {$(this).css("display","none")}
		});
		$("#photo1").fadeIn("slow");
		// Show Project Screen
		//$(p1).fadeIn("slow");
		//p1.style.display = "block";
		// Hide other Project Screens
		//if(p2)p2.style.display = "none";
		//if(p3)p3.style.display = "none";
		//if(p4)p4.style.display = "none";
		// Set Num to Active
		p1num.className = "active";
		// Set other Num's to inActive
		if(p2num)p2num.className = "";
		if(p3num)p3num.className = "";
		if(p4num)p4num.className = "";
	}
	if(document.getElementById('ps2')){
		document.getElementById('ps2').onclick = function(){
			$("#projectScreens div:has(img)").each(function(i){
				var l = $("#projectScreens div:has(img)").length
				if((i+1)<l){$(this).css("display","none");}
				else {$(this).css("display","none")}
			});
			$("#photo2").fadeIn("slow");
			
			// Hide other Project Screens
			/*if(p1)p1.style.display = "none";
			if(p3)p3.style.display = "none";
			if(p4)p4.style.display = "none";*/
			
			// Show Project Screen
			//$(p2).fadeIn("slow");
			//p2.style.display = "block";
			
			// Set Num to Active
			p2num.className = "active";
			// Set other Num's to inActive
			if(p1num)p1num.className = "";
			if(p3num)p3num.className = "";
			if(p4num)p4num.className = "";
		}
	}
	if(document.getElementById('ps3')){
		document.getElementById('ps3').onclick = function(){
			$("#projectScreens div:has(img)").each(function(i){
				var l = $("#projectScreens div:has(img)").length
				if((i+1)<l){$(this).css("display","none");}
				else {$(this).css("display","none")}
			});
			$("#photo3").fadeIn("slow");
			// Show Project Screen
			//$(p3).fadeIn("slow");
			//p3.style.display = "block";
			// Hide other Project Screens
			//if(p1)p1.style.display = "none";
			//if(p2)p2.style.display = "none";
			//if(p4)p4.style.display = "none";
			// Set Num to Active
			p3num.className = "active";
			// Set other Num's to inActive
			if(p1num)p1num.className = "";
			if(p2num)p2num.className = "";
			if(p4num)p4num.className = "";
		}
	}
	if(document.getElementById('ps4')){
		document.getElementById('ps4').onclick = function(){
			$("#projectScreens div:has(img)").each(function(i){
				var l = $("#projectScreens div:has(img)").length
				if((i+1)<l){$(this).css("display","none");}
				else {$(this).css("display","none")}
			});
			$("#photo4").fadeIn("slow");
			// Show Project Screen
			//$(p3).fadeIn("slow");
			//p3.style.display = "block";
			// Hide other Project Screens
			//if(p1)p1.style.display = "none";
			//if(p2)p2.style.display = "none";
			//if(p4)p4.style.display = "none";
			// Set Num to Active
			p4num.className = "active";
			// Set other Num's to inActive
			if(p1num)p1num.className = "";
			if(p2num)p2num.className = "";
			if(p3num)p3num.className = "";
		}
	}
}
addLoadEvent(projScreens)


// Adds functions to onload event 
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}