$(document).ready(function() {
	
	// up states for rollOvers
	var color_array = new Array($('#box1_tag').css("background-color"), $('#box2_tag').css("background-color"), $('#box3_tag').css("background-color"), $('#journal_tag').css("background-color"));
	set_hovers();
	
	$('#faux').append('<div id="box_close"><p>X Close</p></div>');
	$('#box_close').hide();
	
	// preload images
	$.preloadImages("./img/_fpo_large_girl.jpg", "./img/_fpo_ben_large.jpg");
	
	$('#box1_tag').click(function(){
		if($('#box1').width() < 400) {
			unset_hovers();
			box_fade_out('box2');
			box_fade_out('box3', make_box1_grow);
			//$('#middle_section').css('overflow', 'visible');
		}
	});
	
	$('#box2_tag').click(function(){
		if($('#box2').width() < 400) {
			unset_hovers();
			box_fade_out('box1');
			box_fade_out('box3', make_box2_grow);
			//$('#middle_section').css('overflow', 'visible');
		}
	});
	
	$('#box3_tag').click(function(){
		if($('#box3').width() < 400) {
			unset_hovers();
			box_fade_out('box1');
			box_fade_out('box2', make_box3_grow);
			//$('#middle_section').css('overflow', 'visible');
		}
	});
	
	function set_hovers() {
		$('#box1_tag, #box2_tag, #box3_tag').hover(
			function(){
				$(this).css('background-color', '#E3DBCD');
			},
			function() {
				var num = $(this).attr('id').substring(3,4);
				$(this).css('background-color', color_array[--num]);
		});
	}
	function unset_hovers() {
		$('#box1_tag, #box2_tag, #box3_tag').hover(
			function(){
				var num = $(this).attr('id').substring(3,4);
				$(this).css('background-color', color_array[--num]);
			},
			function() {
				var num = $(this).attr('id').substring(3,4);
				$(this).css('background-color', color_array[--num]);
		});
	}
	
	function make_box3_grow() {
		$('#box3 h6.tag').css('cursor', 'default');
		$('#box3').css('background', 'url(./img/_fpo_ben_large.jpg) top left no-repeat');
		$('#award_intro_copy').hide();
		$('#box3 h6.tag').animate({
			top: 0,
			width:'940px',
			left: 0
		}, 500);
		$('#box3').animate({
			width: '940px',
			height: '500px'
		}, 500, show_close_btn);
		$('#box3_tag').after('<div id="full_awards"></div>');
		$('#full_awards').load('./inc/awards.html', function(){
			$('#awards_wrapper').fadeIn('slow');
			$.extend($.fn.jScrollPane.defaults, {showArrows:false});
			$('#awards_pane').jScrollPane();
		});
		$('#middle_section').css('overflow', 'visible');
	}
	
	function make_box2_grow() {
		$('#box2 h6.tag').css('cursor', 'default');
		$('#box2').css('background', 'url(./img/_fpo_large_girl.jpg) top left no-repeat');
		$('#box2 h6.tag').animate({
			top: 0,
			width:'940px'
		}, 500);
		$('#box2').animate({
			width: '940px',
			height: '500px'
		}, 500, show_close_btn);
		$('#box2_tag').after('<div id="full_services"></div>');
		$('#full_services').load('./inc/services.html', function(){$('#services_wrapper').fadeIn('slow');});
		$('#middle_section').css('overflow', 'visible');
	}
	
	function make_box1_grow() {
		$('#box1 h6.tag').css('cursor', 'default');
		// $('#box1').css('background', 'url(./img/_fpo_welcome_large.jpg) top left no-repeat');
		$('#box1 h6.tag').animate({
			left: 0,
			width:'915px'
		}, 500);
		$('p#welcome_msg').fadeOut('fast');
		$('p#welcome_msg').css({'float': 'right', 'color': '#c7f6dd'});
		$('p#welcome_msg').after('<div id=full_msg></div>');
		$('#full_msg').load('./inc/welcome_msg.html');
		$('#box1').animate({
			width: '940px'
		}, 500, show_close_btn);
		$('#middle_section').css('overflow', 'visible');
	}
	
	function show_close_btn() {
		$('#box_close').fadeIn('fast');
	}
	
	function box_fade_out(box, callback) {
		$('#'+box).fadeOut('slow', callback);
	}
	
	function box_fade_in(box, callback) {
		$('#'+box).fadeIn('slow', callback);
	}
	
	$('#box_close').click(function() {
		set_hovers();
		if($('#box1').width() > 350) {
			$('#box1 h6.tag').css('cursor', 'pointer');
			$(this).hide();
			$('#box1 h6.tag').animate({
				left: '108px',
				width:'180px'
			}, 500);
			$('p#welcome_msg').css('float', 'left');
			$('#full_msg').remove();
			$('#box1').animate({
				width: '313px'
			}, 500, function(){
				// on Complete
				$('#middle_section').css('overflow', 'hidden');
				box_fade_in('box2');
				setTimeout(function() {box_fade_in('box3')}, 100);
			});
			$('p#welcome_msg').css({'float': 'left', 'color': '#ffffff'});
			$('p#welcome_msg').fadeIn('slow');
			
			//$('#box1').css('background', 'url(./img/box1_bg.png) top left repeat');
			// recall png fix to fix this guy
			// pngfix();
		}
		
		 else if($('#box2').width() > 350) {
			$('#box2 h6.tag').css('cursor', 'pointer');
			$(this).hide();
			$('#full_services').remove();
			$('#box2 h6.tag').animate({
				left: '0px',
				top: '155px',
				width:'180px'
			}, 500);
			$('#box2').animate({
				width: '313px',
				height: '194px'
			}, 500, function(){
				// on Complete
				$('#middle_section').css('overflow', 'hidden');
				$('#box2').css('background', 'url(./img/_fpo_girl.jpg) top left no-repeat');
				box_fade_in('box1');
				setTimeout(function() {box_fade_in('box3')}, 100);
			});
		}
		
		else if($('#box3').width() > 350) {
			$('#box3 h6.tag').css('cursor', 'pointer');
			$(this).hide();
			$('#full_awards').remove();
			$('#box3 h6.tag').animate({
				left: '109px',
				top: '155px',
				width:'180px'
			}, 500);
			$('#box3').animate({
				width: '314px',
				height: '194px'
			}, 500, function(){
				// on Complete
				$('#award_intro_copy').fadeIn('slow');
				$('#middle_section').css('overflow', 'hidden');
				$('#box3').css('background', 'url(./img/_fpo_ben.jpg) top left no-repeat');
				box_fade_in('box1');
				setTimeout(function() {box_fade_in('box2')}, 100);
			});
		}
		
	});
	
	// ==================
	// = Journal events =
	// ==================
	$('#journal_tag').hover(
		function(){
			$(this).css('background-color', '#E3DBCD');
		},
		function() {
			var num = 4;
			$(this).css('background-color', color_array[--num]);
	});
	
	function unset_journal() {
		$('#journal_tag').css('cursor', 'default');
		$('#journal_tag').hover(
			function(){
				var num = 4;
				$(this).css('background-color', color_array[--num]);
			},
			function() {
				var num = 4;
				$(this).css('background-color', color_array[--num]);
		});
	}
	
	$('#journal_tag').click(function(){
		unset_journal();
		if(!$("#archives").length > 0) {
			$('#contact_gutter').prepend('<div style="display:none;" id="archives"></div>');
			$('#archives').load('./scripts/archives.php', function() {
				$('#archives').show('slow');
			});
		}
	});

});


// // delete entry button
// function delete_entry(i) {
// 	var bool = confirm('Are you sure you want to delete this item?');
// 	if(bool) {
// 		$('#server_msg').load('../scripts/admin_delete_entry.php', {id: i});
// 		$('#server_msg').show('fast');
// 		setTimeout('deleteElement($("#server_msg"))', 2000);
// 	} 
// }
// 
// // delete user button
// function delete_user(i) {
// 	var bool = confirm('Are you sure you want to delete this item?');
// 	if(bool) {
// 		$('#server_msg').load('../scripts/admin_delete_user.php', {id: i});
// 		$('#server_msg').show('fast');
// 		setTimeout('deleteElement($("#server_msg"))', 2000);
// 	} 
// }
// 
// function deleteElement(clip) {
// 	//clip.fadeOut('slow');
// 	location.reload(true);
// }

function show_year(y) {
	$('#entry').fadeOut('fast');
	$('#entry').load('./scripts/show_years.php', {year: y}, function() {
		$('#entry').hide();
		$('#entry').fadeIn('slow');
	});
}

function show_month(m) {
	$('#entry').fadeOut('fast');
	$('#entry').load('./scripts/show_months.php', {month: m}, function() {
		$('#entry').hide();
		$('#entry').fadeIn('slow');
	});
}

function show_entry(i) {
	$('#entry').fadeOut('fast');
	$('#entry').load('./scripts/show_entry.php', {id: i}, function() {
		$('#entry').hide();
		$('#entry').fadeIn('slow');
	});
}