var LastNumber;
var LastMenuChoice = '';
var MenuChoice;
var CateChoice;
var ImageChoice;
var AnimRunning = false;
var offset = 0;
var MenuAnim = false;

$(window).load(function(){
	$('body').fadeIn(700, function(){OpeningAnimation();});
	var h = $(window).height();
	$("#top-lines-nav").css('height', h);
	
	$('.nav-title-option').mouseover(function() { MenuChoice = $(this).attr('id'); ShowMenu();});
	$('.nav-dropdown-option').click(function() { CateChoice = $(this).attr('id'); CateContent();});
	$('#close-button').click(function() {CloseWindow();});
	$('#contact').click(function() { CateContact();});
	$(window).resize(function(){
		var h = $(window).height();
		$("#top-lines-nav").css('height', h);
    }).scroll(function(){
		var h = $(window).height();
		var t = $(window).scrollTop();
		h = h+t;
		$("#top-lines-nav").css('height', h);
	});
});

function OpeningAnimation() {
	$('#nav-main').slideDown(500);
	$('#content').fadeIn(500,  function(){RotateImages()});
	
};

function RotateImages(){
	NumberOfImagesToRotate = 5;
	FirstPart = '<img id="rotate-img" src="images/random';
	LastPart = '.jpg" width="450"/>';
	r = Math.ceil(Math.random() * NumberOfImagesToRotate);
	FullImage = FirstPart + r + LastPart;
	if (LastNumber == r){RotateImages();}
	else if ($('#rotator').length > 0){
		$('#rotator').html(FullImage).delay(1000).animate({'opacity': '1'}, 1000).delay(6000).animate({'opacity': '0'}, 1000, function(){LastNumber = r;RotateImages()});
	};
};

function ShowMenu(){
	if (MenuChoice == LastMenuChoice){
	}
	else if (AnimRunning == true){
	}
	else if (LastMenuChoice.length > 0){
		AnimRunning = true;
		$('#' + LastMenuChoice + '-menu').slideUp(300, function(){ 
			$('#' + MenuChoice + '-menu').slideDown(500, function(){
				LastMenuChoice = MenuChoice;
				AnimRunning = false;
			})
		});
		
	}
	else
	{
		AnimRunning = true;
		$('#' + MenuChoice + '-menu').slideDown(500, function(){
				LastMenuChoice = MenuChoice;
				AnimRunning = false;
			})
	}
};

function CateContent(){

	dataToSend = 'loadmenu=' + MenuChoice + '&loadcate=' + CateChoice + '&offset=' + offset;
	$('#content').animate({ opacity : 0}, 400, function(){
	$.ajax({
		type: "POST",
		url: 'cms/controller.php',
		data: dataToSend,
		success: function(data) {
			$('#content').html(data);
			$('#content').ready(function(){$('#content').animate({ opacity : 1}, 400)});
			if ( offset > 0){
				$('#prev-cate-link').show()
			};
			lastNumber = $('#last-number').html();
			lastNumber = parseInt(lastNumber);
			bigNumber = $('.15').attr('id');
			bigNumber = parseInt(bigNumber);
			if ( bigNumber < lastNumber ){
				$('#next-cate-link').show()
			};
			$('#next-cate-link').click(function() { offset = offset + 15; CateContent();})
			$('#prev-cate-link').click(function() { offset = offset - 15; CateContent();})
			$('.small-img-opt').click(function() { ImageChoice = $(this).attr('id');ImageChoice = ImageChoice - 1;ImageContent();});	
			$('#content-header').click(function() { window.open('http://www.type-h.net/resume.pdf');});
			$('#resume-background').click(function() { window.open('http://www.type-h.net/resume.pdf');});
			$('#last-number').hide()
		}
	});});
};

function CateContact(){

	dataToSend = 'loadmenu=contact';
	$('#content').animate({ opacity : 0}, 400, function(){
	$.ajax({
		type: "POST",
		url: 'cms/controller.php',
		data: dataToSend,
		success: function(data) {
			$('#content').html(data);
			$('#content').ready(function(){$('#content').animate({ opacity : 1}, 400)});
			$('#submit-email-link').click(function() { SendEmail();})
		}
	});
	});
};

function SendEmail(){

	emailAddy = $('#email-option').val();
	emailName = $('#name-option').val();
	emailText = $('#text-email').val();
	
	if (emailName.length > 0 && emailText.length > 0 && checkMail(emailAddy) == true){
		dataToSend = 'useremail=' + emailAddy + '&username=' + emailName + '&usermessage=' + emailText;

		$.ajax({
			type: "POST",
			url: 'cms/controller.php',
			data: dataToSend,
			success: function(data) {
				$('#content').html(data);	
			}
		});
	}
	else if (emailName.length == 0 || emailText.length == 0 || emailAddy.length == 0){
		alert('You haven\'t filled out all fields. Please make sure all fields are complete before sending email.');
	}
	else if (checkMail(emailAddy) == false){
		alert('The email address you entered is invalid. Make sure it is entered fully and correctly.');
	};
};

function checkMail(email){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	return false;
};

function SetWidthHeight(){
	ImageWidth = $('#float-main-image').width();
	ImageHeight = $('#float-main-image').height();
	if ( ImageWidth > 700 ){
		$('#float-main-image').width(700);
		$('#float-content').width(740).css('margin-left' , '-370px').hide().css('opacity' , 1).fadeIn(500, function(){$('#loading-circles').hide();;SetBottomPadding()});
		$('#close-button').css('margin-left' , -370).fadeIn(500)
	}
	else if ( ImageHeight > 750 ){
		$('#float-main-image').height(750);
		NewImageWidth = $('#float-main-image').width() + 40;
		$('#float-content').width(NewImageWidth).css('margin-left' , (0 - (NewImageWidth/2))).hide().css('opacity' , 1).fadeIn(500, function(){$('#loading-circles').hide();SetBottomPadding()});
		$('#close-button').css('margin-left' , (0 - (NewImageWidth/2))).fadeIn(500);
	}
	else {
		NewImageWidth = $('#float-main-image').width() + 40;
		$('#float-content').width(NewImageWidth).css('margin-left' , (0 - (NewImageWidth/2))).hide().css('opacity' , 1).fadeIn(500, function(){$('#loading-circles').hide();SetBottomPadding()});
		$('#close-button').css('margin-left' , (0 - (NewImageWidth/2))).fadeIn(500);
	};
	
}

function ImageContent(){

	dataToSend = '&loadcate=' + CateChoice + '&imageID=' + ImageChoice;
	$('#float-content-bg').fadeIn(500);
	$('#float-content-holder').fadeIn(500);
	$('#loading-circles').fadeIn(500);
	$.ajax({
		type: "POST",
		url: 'cms/controller.php',
		data: dataToSend,
		success: function(data) {
			$('#float-content').css('opacity' , 0).html(data).show().ready(function(){
				$('#float-main-image').load(function(){
					setTimeout("SetWidthHeight(); ShowNextPrev();", 300);
				});
			});
		}
	});
};

function CloseWindow(){
	$('#float-content-bg').fadeOut(500);
	$('#float-content-holder').fadeOut(500);
	$('#loading-circles').fadeOut(500);
	$('#float-content').fadeOut(500);
	$('#close-button').fadeOut(500);
	$('#loading-circles').show();
}

function ShowNextPrev(){
	lastNumber = $('#last-number-image').html();
	lastNumber = parseInt(lastNumber);
	$('#last-number-image').hide();
	$('#first-number-image').hide();
	ImageChoice = parseInt(ImageChoice);
	if ( (ImageChoice + 1) < lastNumber ){
		$('#next-image-link').show()
	};
	if ( ImageChoice > 0 ){
		$('#prev-image-link').show()
	};
	$('#next-image-link').click(function() { ImageChoice = ImageChoice + 1; $('#float-content').fadeOut(500, function(){ImageContent()}); $('#loading-circles').show();})
	$('#prev-image-link').click(function() { ImageChoice = ImageChoice - 1; $('#float-content').fadeOut(500, function(){ImageContent()}); $('#loading-circles').show();;})
}

function SetBottomPadding(){
	ContainerHeight = $('#float-content').height();
	BottomPadding = ContainerHeight +80;
	$('#float-content-bottom').css('top', BottomPadding);
}
