﻿$.ajaxSetup({cache:false});

if( $.browser.msie && $.browser.version < 8.0)
{
	alert("This site requires Internet Explorer 8, and recommends Internet Explorer 9 or higher.");
	window.location = "http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx";
}

$(function()
{
	$("#noscriptmsg").remove();
	$("#content").css("visibility","visible");
	$("#toplogo").css("visibility","visible");
	$("#navstrip").css("visibility","visible");
	$("#home").append('<a href="mailto:JBorden@jamesborden.com">JBorden@jamesborden.com</a>.');
	
	var currentPosition = 0;
	var slideWidth = 480;
	var slides = $(".slide");
	var numberOfSlides = slides.length;
	
	var images = [ 'images/returnHover.png', 'images/leftHover.png', 'images/rightHover.png' ];
 
	jQuery.each(images, function(i) {
	  images[i] = new Image();
	  images[i].src = this;
	});
	
	$("#slidesContainer").css("overflow","hidden");
	slides.wrapAll('<div id="slideInner"></div>').css({
		"float" : "left",
		"width" : slideWidth});
		
	$("#slideInner").css("width", slideWidth * numberOfSlides);
	$("#slideshow")
		.prepend('<span class="control" id="leftControl">Move Left</span>')
		.append('<span class="control" id="rightControl">Move Right</span>');
		
	$("#leftControl").bind({mouseenter : function()
	{
		$("#leftControl").css("background","url(images/leftHover.png)");
	},
	mouseleave : function()
	{
		$("#leftControl").css("background","url(images/left.png)");
	}
	});
	
	$("#rightControl").bind({mouseenter : function()
	{
		$("#rightControl").css("background","url(images/rightHover.png)");
	},
	mouseleave : function()
	{
		$("#rightControl").css("background","url(images/right.png)");
	}
	});
		
	manageControls(currentPosition);
	
	$(".control").click(function()
	{
		currentPosition = ($(this).attr("id")=="rightControl")
	  ? currentPosition+1 : currentPosition-1;
	  
	  manageControls(currentPosition);
	  $("#slideInner").animate(
			{"marginLeft" : slideWidth*(-currentPosition)}
		);
	});
		
	function manageControls(position)
	{
		if(position==0) { $("#leftControl").hide() }
		else { $("#leftControl").show(); }
		
		if(position==numberOfSlides-1) { $("#rightControl").hide() }
		else { $("#rightControl").show() }
	}
	
	$("#homeHover").hide();
	$("#portHover").hide();
	$("#resHover").hide();
	$(".linkButton").mouseenter(function()
	{
		if($(this).attr("id")=="homeBtn") { $("#homeHover").fadeIn(); $("#homeBtn").fadeOut(); }
		else if($(this).attr("id")=="portBtn") { $("#portHover").fadeIn(); $("#portBtn").fadeOut();}
		else { $("#resHover").fadeIn(); $("#resBtn").fadeOut();}
	});
	$("#homeHover").mouseleave(function()
	{
		$("#homeHover").fadeOut();
		$("#homeBtn").fadeIn();
	});
	$("#portHover").mouseleave(function()
	{
		$("#portHover").fadeOut();
		$("#portBtn").fadeIn();
	});
	$("#resHover").mouseleave(function()
	{
		$("#resHover").fadeOut();
		$("#resBtn").fadeIn();
	});
	
	$("#portfolio").hide();
	$("#homeHover").click(function(event)
	{
		$("#portfolio").slideUp(600);
		$("#home").delay(600).slideDown(600);
		event.preventDefault();
	});
	$("#portHover").click(function(event)
	{
		$("#home").slideUp(600);
		$("#portfolio").delay(600).slideDown(600);
		event.preventDefault();
	});
	$("#resHover").click(function()
	{
		window.location = "data/Resume.pdf";
	});
	
	$(".thumbnail").click(function(event)
	{
		$("#portfolio").slideUp(600);
		$("#toplogo").hide(600);
		$("#navstrip").delay(600).hide(600);
		setTimeout('$("#content").animate({"top" : "0", "height" : "760"},300)', 1200);
		var href = $(this).attr("alt");
		setTimeout(function(){fillContent( href )}, 1500 );
		event.preventDefault();
	});
	
	$(".thumbnail").mouseenter(function()
	{
		$(this).animate({opacity : "1"},200);
	});
	$(".thumbnail").mouseleave(function()
	{
		$(this).animate({opacity : "0.8"},200);
	});
});

function fillContent(content)
{	
	$("#content").prepend('<div id="PulledContent" />');
	$("#PulledContent").load(content, function(response, status, xhr)
	{
		$("#PulledContent").prepend('<div class="return"></div>');
		$(".return").bind("click",function()
		{
			$(".return").remove();
			$("#PulledContent").remove();
			$("#content").animate({"top" : "275", "height" : "520"},300);
			$("#navstrip").delay(300).show(600);
			$("#toplogo").delay(900).show(600);
			$("#portfolio").delay(900).slideDown(600);
		});
		$(".return").bind({ mouseenter : function()
		{
			$(".return").css("background", "url(images/returnHover.png)");
		},
		mouseleave : function()
		{
			$(".return").css("background", "url(images/return.png)");
		}
		});
		if( content == "data/portDesc.html #MagicCard" && !DetectUnityWebPlayer())
		{
			$("#MagicCard").remove();
			$("#PulledContent").append('<div align="center" id="UnityPrompt"> \n');
			$("#PulledContent").append('<p>You need to install the Unity Web Player to view this item.  Please install it and restart your browser.  Note:  You must run a 32-bit browser!</p>');
			$("#PulledContent").append('<a href= ' + GetInstallerPath() + '><img src="http://webplayer.unity3d.com/installation/getunity.png" border="0"/></a> \n');
			$("#PulledContent").append('</div> \n');
		}
	});
}

function DetectUnityWebPlayer() {
	var tInstalled = false;
	if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1) {
		tInstalled = DetectUnityWebPlayerActiveX();
	}
	else {
		if (navigator.mimeTypes && navigator.mimeTypes["application/vnd.unity"]) {
			if (navigator.mimeTypes["application/vnd.unity"].enabledPlugin && navigator.plugins && navigator.plugins["Unity Player"]) {
				tInstalled = true;	
			}
		}	
	}
	return tInstalled;	
}

function GetInstallerPath() {
	var tDownloadURL = "";
	var hasXpi = navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1;
	
	// Use standalone installer
	if (1)
	{
		if (navigator.platform == "MacIntel")
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-i386.dmg";
		else if (navigator.platform == "MacPPC")
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/webplayer-ppc.dmg";
		else if (navigator.platform.toLowerCase().indexOf("win") != -1)
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.exe";
		return tDownloadURL;
	}
	// Use XPI installer
	else
	{
		if (navigator.platform == "MacIntel")
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerOSX.xpi";
		else if (navigator.platform == "MacPPC")
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerOSX.xpi";
		else if (navigator.platform.toLowerCase().indexOf("win") != -1)
			tDownloadURL = "http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayerWin32.xpi";
		return tDownloadURL;
	}    			
}
