$(document).ready(function() {
	// hide URL bar on mobile, note: only works on 'Go' not on refresh
	window.scrollTo(0,1);
	
	// TITLE ANIMATIONS
	$('#nav ul').css('opacity','0');
	$('#nav ul').css('margin-left','200px');
	$('#nav ul').delay(1000).animate({ 
		opacity: 1,
		marginLeft: 0
	}, 1500, "easeOutExpo");

	$('.phone a').css('opacity','0');
	$('.phone a').delay(1000).animate({ 
		opacity: 1
	}, 1000, "easeOutExpo");
	
	$('#subtitle span').css('opacity','0');
	$('#subtitle span').css('margin-left','-200px');
	$('#subtitle span').delay(1000).animate({ 
		opacity: 1,
		marginLeft: 0
	}, 1500, "easeOutExpo");


	// CONTENT ANIMATIONS
	if($("body").hasClass("projectpage")) {
		$('.pDescription').css('opacity','0');
		$('.pDescription').delay(1500).animate({ 
			opacity: 1
		  }, 2000, "easeOutExpo");
	}

	// CONTENT ANIMATIONS
	if($("body").hasClass("home") || $("body").hasClass("about") || $("body").hasClass("work")) {
		$('#info').css('opacity','0');
		$('#info').delay(2500).animate({ 
			opacity: 1
		  }, 2000, "easeOutExpo");
	}


	// PORTFOLIO SECTION
	if($("body").hasClass("portfolio")) {
		$('.header_title').css('opacity','0');
		$('.header_title').css('left','50px');
		$('.header_title').delay(1000).animate({ 
			opacity: 1,
			left: 0
		  }, 1000, "easeOutExpo");
	}
	// THUMBNAIL HOVERS
	$('.thumbnail').hover(function(){
		$(this).find('a.project_detail').stop().fadeTo('fast', 1);},
		function(){
			$(this).find('a.project_detail').stop().fadeTo('fast', 0);
	});
	
	// DISALBE SKYPE
	window.setTimeout(function() {
		$('.skype_pnh_container').html('');
		$('.skype_pnh_print_container').removeClass('skype_pnh_print_container');
	}, 800);

	// ENABLE PAGE SWIPE
	var swipeOptions= {
		swipeLeft:swipe,
		swipeRight:swipe,
		threshold:200
	}

	$(function() {
		if($.browser.msie && parseFloat($.browser.version) < 8){
		} else {
			$("#container").swipe( swipeOptions );
		}
	});	
});
	
// SWIPE FUNCTIONALITY ARRAY

function goHome(){
	fadeContainer();
	window.location.href = '/';
}
function goWork(){
	fadeContainer();
	window.location.href = '/work.php';
}
function goFolio(){
	fadeContainer();
	window.location.href = '/portfolio.php';
}
function goAbout(){
	fadeContainer();
	window.location.href = '/about.php';
}
function goContact(){
	fadeContainer();
	window.location.href = '/contact.php';
}
function fadeContainer(){
	/*
	$('#container').animate({ 
		opacity: 0
	}, 1000, "easeOutExpo");
	*/
}

function swipe(event, direction) {
	if (direction == "left"){

		if($("body").hasClass("home")) {
			fadeContainer();
			setTimeout('goAbout()', 100);
		}
		if($("body").hasClass("about")) {
			fadeContainer();
			setTimeout('goWork()', 100);
		}
		if($("body").hasClass("work")) {
			
		}
		if($("body").hasClass("portfolio")) {
			fadeContainer();
			if($("body").hasClass("projectpage")) {
				setTimeout('goFolio()', 100);
			} else {
				setTimeout('goWork()', 100);
			}
		}
	} else if (direction == "right") {
		if($("body").hasClass("home")) {
			fadeContainer();
			setTimeout('goWork()', 100);
		}
		if($("body").hasClass("about")) {
			fadeContainer();
			setTimeout('goHome()', 100);
		}
		if($("body").hasClass("work")) {
			
		}
		if($("body").hasClass("portfolio")) {
			fadeContainer();
			if($("body").hasClass("projectpage")) {
				setTimeout('goFolio()', 100);
			} else {
				setTimeout('goWork()', 100);
			}
		}
	}
}

function toggleMap(){
	if ($('#map').css('opacity') == '1') {
		$('#map').animate({ 
			opacity: 0
		  }, 500, "linear");
	} else {
		$('#map').animate({ 
			opacity: 1
		  }, 500, "linear");
	}
}

// WORK SECTION FEATURED ITEMS
var viewportWidth = $(window).width();
if (viewportWidth < 500) {
	var IMG_WIDTH = 287;
	var currentImg=0;
	var maxImages=6;
	var speed=500;
	var imgs;
	var swipeOptionsFolio=
	{
		triggerOnTouchEnd : true,
		swipeStatus : swipeStatus,
		allowPageScroll:"vertical",
		threshold:50
	}
} else {
	var IMG_WIDTH = 861;
	var currentImg=0;
	var maxImages=2;
	var speed=500;
	var imgs;
	var swipeOptionsFolio=
	{
		triggerOnTouchEnd : true,
		swipeStatus : swipeStatus,
		allowPageScroll:"vertical",
		threshold:200
	}
}

$(function()
{
	if($("body").hasClass("work")) {
		imgs = $("#project_imgs");
		imgs.swipe( swipeOptionsFolio );
	}
});

/**
* Catch each phase of the swipe.
* move : we drag the div.
* cancel : we animate back to where we were
* end : we animate to the next image
*/			
function swipeStatus(event, phase, direction, distance)
{
	//If we are moving before swipe, and we are going Lor R in X mode, or U or D in Y mode then drag.
	if( phase=="move" && (direction=="left" || direction=="right") )
	{
		var duration=0;
		
		if (direction == "left")
			scrollImages((IMG_WIDTH * currentImg) + distance, duration);
		
		else if (direction == "right")
			scrollImages((IMG_WIDTH * currentImg) - distance, duration);
	}
	
	else if ( phase == "cancel")
	{
		scrollImages(IMG_WIDTH * currentImg, speed);
	}
	
	else if ( phase =="end" )
	{
		if (direction == "right")
			previousImage()
		else if (direction == "left")
			nextImage()
	}
}

function previousImage()
{
	currentImg = Math.max(currentImg-1, 0);
	scrollImages( IMG_WIDTH * currentImg, speed);
}

function nextImage()
{
	currentImg = Math.min(currentImg+1, maxImages-1);
	scrollImages( IMG_WIDTH * currentImg, speed);
}
	
/**
* Manually update the position of the imgs on drag
*/
function scrollImages(distance, duration)
{
	imgs.css("-webkit-transition-duration", (duration/1000).toFixed(1) + "s");
	
	//inverse the number we set in the css
	var value = (distance<0 ? "" : "-") + Math.abs(distance).toString();
	
	imgs.css("-webkit-transform", "translate3d("+value +"px,0px,0px)");
}

function popup(url, w, h)
{
	var wid = 480;
	var hi = 600;

	if (w != null) wid = w; 
	if (h != null) hi  = h; 
	if (document.all && (navigator.userAgent.indexOf("Mac") > -1)) wid = wid+17;

	var winl = (window.screenLeft ? window.screenLeft : window.screenX) + (window.innerWidth / 2) - (wid/2);
	var wint = (window.screenTop ? window.screenTop : window.screenY) + (window.innerHeight / 2) - (hi/2);

	var newwin=window.open(url,wid + "_" + hi,"width="+wid+",height="+hi+",scrollBars=yes,resizable=no,left="+winl+",top="+wint);
	newwin.focus();
	return newwin.name;
}

