/* DEFINE FUNCTIONS */
function hide_loading_layer(){
	$('#loading_block').animate({'opacity':0},1000,function(){
		$(this).hide();
	})
}
function homepage_resize(){
	var pad = 120;
	var logo = $('h1#logo');
	var nav = $('nav#main');
	var tag = $('h3#tag_line');
	var links = $('#social_media');
	var wrap = $('div#wrapper');
	var window_height = $(window).height();
	var content_height = $(logo).height() + $(nav).height() + $(tag).height() + $(links).height() + pad;
	if(content_height < window_height){
		diff = (window_height - content_height) / 2;
		$('div#wrapper').animate({'top':diff},750,function(){
			hide_loading_layer();
		});
	}else{
		hide_loading_layer();
	}
}
function selected_page_resize(page){
	var screen_height = $(window).height();
	$('div#wrapper').css({height:screen_height});
	$('h3#tag_line').animate({'top':screen_height-50},750);
	$('div#social_media').animate({'top':screen_height-70},750);
	if(page==='home'){
		reanimate_logo();
		reanimate_tag();
		reanimate_nav();
		reanimate_links();
	}else{
		$('#content').animate({'height':screen_height-210},750,function(){
			resize_contents();
		});
	}
}
function animate_logo(){
	/*
	logo_contents = $('h1#logo').html();
	$('#wrapper').append('<h1 id="logo" class="temp" style="width:auto;display:none">'+logo_contents+'</div>');
	logo_width = $('#logo.temp').width();
	$('h1#logo.temp').remove();
	*/
	$('h1#logo').animate({'width':330,'left':0},750);
}
function reanimate_logo(){
	wrapper_width = $('#wrapper').width();
	logo_width = $('#logo').width();
	left = (wrapper_width - logo_width) / 2;
	$('h1#logo').animate({'left':left},750);
}
function animate_tag(){
	tag_contents = $('h3#tag_line').html();
	$('#wrapper').append('<div id="temp_tag" style="width:auto;display:none">'+tag_contents+'</div>');
	tag_width = $('#temp_tag').width();
	$('#temp_tag').remove();
	$('h3#tag_line').animate({'width':tag_width},750,function(){
		var screen_height = $(window).height();
		$('h3#tag_line').animate({'top':screen_height-50},750,function(){
			$('h3#tag_line').css({'text-align':'left'});
		});
	});
	$('div#footer_tag').animate({'opacity':0},750,function(){
		$('div#footer_tag').hide();
	});
}
function reanimate_tag(){
	$('h3#tag_line').css({'text-align':'center'});
	$('h3#tag_line').animate({'top':100},750,function(){
		wrapper_width = $('#wrapper').width();
		$('h3#tag_line').animate({'width':wrapper_width},750,function(){
			/* DONE */
		});
	});
	$('div#footer_tag').show();
	$('div#footer_tag').css({'opacity':0});
	$('div#footer_tag').animate({'opacity':1},750,function(){
		/* DONE */
	});
}
function animate_nav(page){
	$('nav#main').css({'right':0});
	$('nav#main').animate({'width':300,'padding-left':0,'padding-right':0},750);
	$('div#main_nav').animate({'opacity':0},750,function(){
		animate_logo();
		$('div#main_nav').hide();
		$('nav#main').animate({'top':10},750);
		var screen_height = $(window).height();
		$('#content').animate({'height':screen_height-210},750,function(){
			$('#content').animate({'opacity':1},750,function(){
				animate_between_pages(page);
			});
		});
	});
}
function reanimate_nav(){
	$('nav#main').animate({'top':190},750,function(){
		wrapper_width = $('#wrapper').width();
		$('nav#main').animate({'width':(wrapper_width/100)*80,'padding-left':(wrapper_width/100)*10,'padding-right':(wrapper_width/100)*10},750);
		$('div#main_nav').show();
		$('div#main_nav').animate({'opacity':1},750,function(){
			/* DONE */
		});
	});
}
function animate_links(){
	var screen_height = $(window).height();
	$('div#social_media').animate({'top':screen_height-70},750,function(){
		$('div#social_media').animate({'right':0,'margin-right':0},750);
	});
}
function reanimate_links(){
	$('div#social_media').animate({'right':'50%','margin-right':-115},750,function(){
		$('div#social_media').animate({'top':350},750,function(){
			/* DONE */
			homepage_resize();
		});
	});
}
function start_animations(page){
	var screen_height = $(window).height();
	$('div#wrapper').css({height:screen_height});
	$('div#wrapper').animate({'top':0},750,function(){
		animate_tag();
		animate_links();
		animate_nav(page);
	});
}
function start_reanimations(){
	animate_between_pages('home');
}
function animate_from_home(page){
	$('body').removeClass('home');
	start_animations(page);
	$('body').attr('data-current-page',page);
}
function animate_to_home(){
	$('body').addClass('home');
	start_reanimations();
	$('body').attr('data-current-page','home');
	$('body').removeClass('tis-loading');
}
function resize_contents(){
	var content_height = $('#content-wrapper').height();
	var wrapper_height = $('#content').height();
	tag_top = $('h3#tag_line').css('top');
	links_top = $('#social_media').css('top');
	if(content_height <= wrapper_height){
		$('#content-wrapper').animate({'padding-top':(wrapper_height-content_height)/2},500);
		$('h3#tag_line').animate({'top':parseInt(tag_top)},500);
		$('#social_media').animate({'top':parseInt(links_top)},500);
	}else{
		extra = content_height - wrapper_height;
		content_height = $('#content').height();
		$('#content').height(content_height+extra);
		$('h3#tag_line').animate({'top':parseInt(tag_top)+extra},500);
		$('#social_media').animate({'top':parseInt(links_top)+extra},500);
		$('#content-wrapper').animate({'padding-top':0},500,function(){
			resize_contents();
		});
	}
}
function execute_content(page){
	resize_contents();
	$('#content').find('.entertainment').remove();
	$('#content').animate({'opacity':1},300,function(){
		/* ALL DONE */
		$('body').attr('data-current-page',page);
		$('body').removeClass('tis-loading');
		if (history.pushState) {
			window.history.pushState({},"", mp_root_url+page);
		}
	});
}
function animate_between_pages(page){
	/* COVER CONTENTS WITH LOADING SYMBOL */
	$('#content').show();
	$('#content').animate({'opacity':0},300,function(){
		$('#content').html('');
		$('#content').append('<div class="entertainment"></div>');
		$('#content').animate({'opacity':1},300,function(){
			if(page==='home'){
				/* REMOVE CONTENT */
				$('#content').animate({'opacity':0},750,function(){
					$('#content .entertainment').remove();
					$('#content').hide();
					selected_page_resize('home');
					if (history.pushState) {
						window.history.pushState({},"", mp_root_url);
					}
				});
			}else{
				/* GET CONTENT */
				var mongoID = $('a#'+page).attr('data-mongo-id');
				var nonce = $('a#'+page).attr('data-nonce');
				$.ajax({
					url:mp_root_url+'mp-includes/ajax/get-object.php',
					data:({ mongo_id: mongoID, nonce: nonce, use_mongo_meantime: 'true', shortcodes:true, filters:true }),
					type: "POST",
					dataType: 'json',
					success: function(result){
						if(result.success===true){
							$('#content').animate({'opacity':0},300,function(){
								$('#content').append(result.content);
								var images = $('#content img').length;
								if(images>0){
									$('#content img').load(function(){
										execute_content(page);
									});
								}else{
									execute_content(page);
								}
							});
						}
					}
				});
			}
		});
	});
}
function intercept_icon_clicks(){
	$('div.pages a').live('click',function(e){
		e.preventDefault();
		if(!$('body').hasClass('tis-loading')){
			$('body').addClass('tis-loading');
			var current_page = $('body').attr('data-current-page');
			var selected_page = $(this).attr('data-id');
			if(current_page!==selected_page){
				$('nav#main a').removeClass('current');
				$(this).addClass('current');
				if ($('body').hasClass('home')){
					animate_from_home(selected_page);
				}else{
					if(selected_page!=='home'){
						animate_between_pages(selected_page);
					}else{
						animate_to_home();
					}
				}
			}
		}
	});
}
function start_functions(){
	if ($('body').hasClass('home')){
		homepage_resize();
	}else{
		selected_page_resize();
	}
}
function setup_body_attributes(){
	if ($('body').hasClass('home')){
		$('body').attr('data-current-page','home');
	}
}

/* START FUNCTIONS */
$(document).ready(function(){
	$(window).bind("load", function() {
		var this_page = $('#wrapper').attr('data-this-page');
		setup_body_attributes();
		intercept_icon_clicks();
		if(this_page){
			start_functions();
			$('nav#main a#'+this_page).trigger('click');
		}else{
			start_functions();
		}
	});
});
$(window).resize(function(){
	resize_timeOut();
});
/* END OF STARTING FUNCTIONS */

/* TIMER LOOP */
var timer = 0;
function resize_timeOut(){
	if(!timer){
		timer = 1;
		timeOut_trigger();
	}
}
var timeOut_trigger = function(){
	setTimeout(timeOut,1000);
}
function timeOut(){
	timer = 0;
	start_functions();
}
/* END OF TIMER LOOP */
