var animated = false;
var thumbnailIndex = 1;
var imageSequenceIndex = 0; 
var projectSequenceIndex = 0;
var curSlideIndex = 0;
var curDisplay;
var curFooter;
var requests = new Object();
var cache = new Object();
var currProject = new Object();
var currProjectImage = 0;
var projectIdRequest = 0;
var newFooter;
var shiftWidth;
var animatingGallery = false;
var contactShow = false;
var slideTimer = window.setInterval(autoSlideshow, 7000);


function autoSlideshow() {
	if(curSlideIndex+1 < $('.landing-slideshow li').size()){
	slideshowSwitch(curSlideIndex+1);
	} else {
	slideshowSwitch(0);
    }
    }


function slideshowSwitch(newSlideIndex) {
	$slideshow = $('ul.landing-slideshow');
	$slideshow.children('li').eq(newSlideIndex).hide();
	$slideshow.children('li').eq(newSlideIndex).children('img').show();
	$slideshow.children('li').eq(curSlideIndex).fadeOut(500,function(){
    $slideshow.children('li').eq(curSlideIndex).addClass('nodisplay');
    $slideshow.children('li').eq(newSlideIndex).fadeIn(500);
    $slideshow.children('li').eq(newSlideIndex).removeClass('nodisplay');
    $slideshow.children('li').eq(newSlideIndex).show();
    curSlideIndex = newSlideIndex;
	});
	$('.pagination a').each(function(){
	$(this).removeClass('on');
	});
	$('.pagination a').eq(newSlideIndex).addClass('on');
    }


function slidingNavAnimateOff(attribute) {
	var distance = $slidingcontainer.innerHeight();
	$slidingcontainer.animate({
	bottom: '-='+ distance
	}, 500, function(){
	if(attribute != undefined) {
	slidingNavAnimateOn(attribute);
	} else {
	animated = false;
	offStateNav($('.footer .leftcol ul.nav'));
	}
	});
    }


function slidingNavAnimateOn(attribute) {
	var distance = $slidingchildren.height();
	if(attribute != undefined) {
	if(attribute == "resume") {
	togglePetrofStudio();
	$slidingchildren = $('.petrofstudio');
	} else if(attribute == "folio"){
	togglePetrofPortfolio();
	$slidingchildren = $('.petrofportfolio');
	} else {
	toggleCompact();
	$slidingchildren = $('.petrofportfolio-compact');
	}
	var newHeight = -1*$slidingcontainer.height()+$('.footer').outerHeight();
	$slidingcontainer.css("bottom",newHeight);
	distance = $slidingcontainer.height();
	}
	$slidingcontainer.animate({
	bottom: '+='+distance
	}, 500, function(){
	animated = false;
	});
    }


function isSlidingNavOn(){
	var position = parseInt($slidingcontainer.css('bottom'));
	if(position>=0){
	return true;
	} else {
	return false;
    }
    }


function slidingContainerToggle(attribute) {
	if(isSlidingNavOn() == false){
	slidingNavAnimateOn(attribute);
	if($('.contact').length > 0){
	if(contactShow == true) {
	$('.contactBtn').trigger('click');
	}
	}
	} else {
	slidingNavAnimateOff(attribute);
    }
    }


function togglePetrofStudio() {
	$slidingcontainer.children('.petrofstudio').removeClass('nodisplay');
	resizeTextContainer(true);
	$slidingcontainer.children('.petrofportfolio').addClass('nodisplay');
	$slidingcontainer.children('.petrofportfolio-compact').addClass('nodisplay');
    }


function togglePetrofPortfolio() {
	$slidingcontainer.children('.petrofportfolio').removeClass('nodisplay');
	$slidingcontainer.children('.petrofstudio').addClass('nodisplay');
	$slidingcontainer.children('.petrofportfolio-compact').addClass('nodisplay');
    }


function toggleCompact(){
	$slidingcontainer.children('.petrofportfolio-compact').removeClass('nodisplay');
	$slidingcontainer.children('.petrofstudio').addClass('nodisplay');
	$slidingcontainer.children('.petrofportfolio').addClass('nodisplay');
    }


function offStateNav(parentul,parentli){
	if(!$(parentul).hasClass('nostate')) {
	$(parentul).children('li').each(function(){
	if($(this).hasClass('on')){
	$(this).removeClass('on');
	}
	});
	if(parentli != undefined){
	$(parentli).addClass('on');
	}
	}
    }


function slideBodyText(forceResize) {
	var newPos = $('.petrofstudio ul.nav-studio li.on').index()*$('.viewpetrof').width();
	var curPos = $('.columns').position().left;
	if(forceResize == true) {
	if(newPos != curPos) {
	$('.columns').animate({
	left: newPos*-1
	},0);
	}
	} else {
	if(newPos != curPos) {
	$('.columns').animate({
	left: newPos*-1
	},500);
	}
	}	
    }


function slideGallery(pressedBtn) {
	var curPos = $('.thumbnails').position().left;
	
	var eachWidth = $('.thumbnails div').width();
	var totalWidth = $('.thumbnails').find('div.project-launch').length*eachWidth;
	var totalIndex = Math.ceil(totalWidth / $('.petrofportfolio').width());
	var addition = $('.petrofportfolio').width()%eachWidth;
	
	if(pressedBtn == 'next' && animatingGallery == false) {
	if(thumbnailIndex < totalIndex) {
	animatingGallery = true;
	if(((thumbnailIndex+1) * $('.petrofportfolio').width()) > totalWidth) {
	$('.thumbnails').animate({
	left: "-="+((totalWidth % $('.petrofportfolio').width())+(thumbnailIndex*addition))
	},500, function(){
	animatingGallery = false;
	});	
	} else {
	$('.thumbnails').animate({
	left: "-="+($('.petrofportfolio').width()-addition)
	},500, function(){
	animatingGallery = false;
	});

	}
	thumbnailIndex++;
	}
	} else {
	if(animatingGallery == false) {
	if(thumbnailIndex >= totalIndex) {
	animatingGallery = true;
	thumbnailIndex--;
	$('.thumbnails').animate({
	left: "+="+(((totalWidth - (thumbnailIndex * $('.petrofportfolio').width())))+(thumbnailIndex*addition))
	},500, function(){
	animatingGallery = false;
	});
	} else {
	if(curPos != 0) {
	animatingGallery = true;
	shiftWidth -= $('.petrofportfolio').width();
	$('.thumbnails').animate({
	left: "+="+($('.petrofportfolio').width()-addition)
	},500,function(){
	animatingGallery = false;
	});
	thumbnailIndex--;
	}
	}
	}
	}
    }


function resizeTextContainer(forceResize) {
	$('.txtcontainer').hide();
	$('.txtcontainer').width($('.viewpetrof').width()+2);
	if(forceResize == true){
	slideBodyText(forceResize);
	}
	$('.txtcontainer').show();
    }


function supersizeImage(image) {
	var imagewidth = 1400;
	var imageheight = 840;
	var browserwidth = $(window).width();
	var browserheight = $(window).height();

	var ratio = imageheight/imagewidth;

	if ((browserheight/browserwidth) > ratio){
	$(image).height(browserheight);
	$(image).width(browserheight / ratio);
	} else {
	$(image).width(browserwidth);
	$(image).height(browserwidth * ratio);
	}
	$(image).css('left', (browserwidth - $(image).width())/2);
	$(image).css('top', (browserheight - $(image).height())/2);
    }


function forceSupersize(){
	$('img.supersize').each(function(){
	if($(this).parent().is(':visible')) {
	supersizeImage($(this));
	} else {
	$(this).parent().removeClass('nodisplay');
	$(this).parent().show();
	supersizeImage($(this));
	$(this).parent().hide();
	}
	});
	resizeTextContainer(true);
	$('.thumbnails').animate({
	 left: 0 
	},0,function(){
	thumbnailIndex=1;
	});
    }


function killSlideshow() {
	if($('.landing-page').is(":visible") == true) {
	$('.slideshow-control').remove();
	window.clearInterval(slideTimer);
	$('.landing-page').fadeOut(300,function(){
	$('.landing-page').remove();
	});
	}
    }

function showSlideshow() {
	$loadingAnim.hide();
	$('.landing-page').css("visibility","visible");
    }


$(window).load(
	function(){
	$('#initialLoading').fadeOut(300,function(){
	$('#initialLoading').remove();
	});
	}
    );

$(document).ready(
	function() {

	$slidingcontainer = $('.slidingcontainer');
	$slidingchildren = $('.slidingcontainer div');
	

	$fullscreen = $('.content-fullscreen');

	
	$loadingAnim = $('#loadingAnim'); 
	

	$slidingcontainer.css('visibility','visible');
	$slidingcontainer.css("bottom","-331px");
	$fullscreen.css('visibility','visible');
	$fullscreen.hide();
	
	
	if($('.contact').length > 0){
	$('.contact').css("bottom",$('.contact').height()*-1);
	contactShow = false;
	}
	
	

	$('img.supersize').each(function(){
	supersizeImage($(this));
	});
	

	$(window).bind("resize", forceSupersize);
	
	$('.contactBtn').click(function(){
	if($(this).hasClass('ignorejs')) {
	return true;
	}
	
	var bottomVal;
	if(contactShow == false) {
	if($('.contact').height() > $(window).height()){
	$('.contact-content').css("height",$(window).height()-80);
	}
	bottomVal = 60;
	contactShow = true;
	} else {
	bottomVal = $('.contact').height()*-1;
	contactShow = false;
	}
	if(parseInt($('.slidingcontainer').css("bottom").replace("px","")) > 0) {
	slidingContainerToggle();
	}
	$('.contact').animate({
	bottom: bottomVal
	}, 700);
	});	


	$('ul.nav li a, ul.nav-studio li a').click(function() {
	if($(this).hasClass('ignorejs')) {
	return true;
	}
	else {
	$parentli = $(this).parent().parent();
	$parentul = $(this).parent().parent().parent();
	offStateNav($parentul,$parentli);
	}
	});
	
	
	$('.pagination a').click(function(){
	slideTimer = window.clearInterval(slideTimer);
	slideshowSwitch(parseInt($(this).html())-1);
	$('.slideshow-control a.pause').addClass("start");
	return false;
	});
	

	$('.slideshow-control a.pause').click(function(){
	if($(this).hasClass('start')) {
	$(this).removeClass("start");
	slideTimer = window.setInterval(autoSlideshow, 6000);
	} else {
	$(this).addClass("start");
	slideTimer = window.clearInterval(slideTimer);
	}
	return false;
	});
	
	$('.landing-slideshow img.supersize').each(function(){
	if($(this).parent().index()>0){
	$(this).parent().addClass('nodisplay');
	};
	});
	

	$('.staticfooter .leftcol ul.nav li a').click(function(){
	
	if($(this).hasClass('ignorejs')) {
	return true;
	}
	else {
	if(animated == false) {
	animated = true;
	newFooter = $(this).attr("class");
	if(curFooter == newFooter) {
	if($('.landing-page').is(":visible") == true) {
	slidingContainerToggle();
	offStateNav($('.footer .leftcol ul.nav'));
	} else {
	if($(this).attr("class").split(' ').slice(0,1) == 'resume'){
	slidingContainerToggle('compact');
	$jobdescription.fadeIn(300);
	offStateNav($('.footer .leftcol ul.nav'),$('.footer .leftcol ul.nav li.last'));
	curFooter = $('.footer .leftcol ul.nav li.last a').attr("class");
	} else {	
	slidingContainerToggle();
	curFooter = undefined;
	}
	}
	} else {
	if($(this).hasClass('resume')){
	document.title = $('div.content.petrofstudio ul.nav-studio li.on a').attr('title');
	}
	
	slidingContainerToggle($(this).attr("class").split(' ').slice(0,1));
	curFooter = newFooter;
	}
	}
	return false;
	}
	});
	

	$("#closeBtn").click(function() {
	if($('.landing-page').is(":visible") == true) {
	slidingContainerToggle();
	offStateNav($('.footer .leftcol ul.nav'));
	} else {
	slidingContainerToggle('compact');
	$jobdescription.fadeIn(300);
	offStateNav($('.footer .leftcol ul.nav'),$('.footer .leftcol ul.nav li.last'));
	curFooter = $('.footer .leftcol ul.nav li.last a').attr("class");
	}
	return false;
	});
	
	
	$("#hideThumbnail").click(function() {
	slidingContainerToggle('compact');
	return false;
	});
	

	$("#showThumbnail").click(function() {
	slidingContainerToggle('folio');
	return false;
	});
	
	
	resizeTextContainer(false);
	

	$('.petrofstudio ul.nav-studio li a').click(function(){
	slideBodyText();
	return false;
	});


	$('.petrofportfolio ul.nav-folio li a').click(function(){
	slideGallery($(this).attr('class'));
	return false;
	});

	$('.pageHref').click(function(){
	document.location.href=currUrl()+'#'+$(this).attr('href');
	return false;
	});
	}
    );

function currUrl(){
	url=document.location.href.split("#");
	return url[0];
    }

function isIE(){
	IE=false;
	jQuery.each(jQuery.browser, function(i, val) {
	if(i=="msie"){
	IE=true;
	}
	});	
	return IE;
    }
