/**
 * 
 */

var currentPage;
var scrollBar;
var contentPane;
var scrollBarWidth = "600px";

$(document).ready(function() {
	adjustBody();
	if(scrollBar == null){
		scrollBar = $('.scrollBar').jScrollPane({
			hijackInternalLinks: true
		});
	}
	
	if(currentPage == null || currentPage == ''){
		currentPage = 'options';
	}
	
	//check whether to load contentA or contentB layout
	if(currentPage==null || currentPage == ''|| currentPage == 'options'|| currentPage == 'recovery'){
		//load contentA
		$(".contentA").show();
		$(".contentB").hide();
		//$('.scrollBar').css("width", "820");
		scrollBarWidth = "820px";
		contentPane = "contentA";
		//set the width of the scrollbars
		
	}
	else{
		//load contentB
		$(".contentB").show();
		$(".contentA").hide();
		contentPane = "contentB";
		//$('.scrollBar').css("width", "600");
		scrollBarWidth = "600px";
	}
	
	loadPage();
	adjustScrollbars();
	//resizeContent();
	
	
	//if the admin class exists, then we are in admin mode and the admin link
	//should now say logout
	if($(".admin").size() > 0){
		$('#adminLink').hide();
		$('#logoutLink').show();
	}
	else{
		$('#adminLink').show();
		$("#logoutLink").hide();
	}
	
	$(window).resize(function(){
		if($("#adminBlock").size() > 0){
			resizeContent();
		}
		
		adjustBody();
		
	});
	

	
	$("#adminLink").click(function(){
		//capture the current page, and then show the login page
		if(currentPage == null || currentPage == ''){
			currentPage = 'options';
		}
		
		blockPage();
		$('#orcBody').append('<div id="login"></div>');
		$('#login').load('login.php?page='+currentPage);
		
		$('#login').css("top", ($(window).height() - 150)/2);
		$('#login').css("left", ($(window).width() - 300)/2);
		$("#login").show(function(){$('#name').focus();});
		
	});
	
	$("#logoutLink").click(function(){
		if(currentPage == null || currentPage == ''){
			currentPage = 'options';
		}
		$("#orcBody").load("logout.php?page="+currentPage);
	});
	
	$('#editPageButton').click(function(){
		blockPage();
		if($('#editPage').size() == 0){
			$('#orcBody').append('<div id="editPage"></div>');
		}
		
		
		var queryString = 'page='+$(":hidden[name=page]").val();
		queryString += '&section='+$(":hidden[name=section]").val();
		$('#editPage').load("admin/addPage.php?"+queryString);
		
		$('#editPage').css("top", 0);
		var editWidth = $(window).width()*.8;
		if(editWidth < 800){
			editWidth = 800;
		}
		$('#editPage').css("left", ($(window).width() - editWidth)/2);
		
		return false;
	});
	
	$(document).ready(function(){
		$('#updatePageForm').submit(function(){
			$.post('admin/savePage.php', $('#updatePageForm').serialize(), function(){
				loadPage();
			});
			$('#adminBlock').remove();
			$('#editPage').remove();
			
			
			return false;
		});
		
		$('#updatePageCancelButton').click(function(){
			$('#adminBlock').remove();
			$('#editPage').remove();
			loadPage();
		});
	});
	

	
});

function resizeContent(){
	$('#adminBlock').width($(window).width());
	$('#adminBlock').height($(window).height());
}

function adjustScrollbars(){
	$('.scrollBar').css("width", scrollBarWidth);
	$('.scrollBar').jScrollPane().data('jsp').reinitialise({hijackInternalLinks: true});

}

function adjustBody(){
	var paperHeight = $("#paper").height();
	var windowHeight = $(window).height();
	
	if(paperHeight < windowHeight){
		$('#paper').css("top", (windowHeight - paperHeight)/2);
	}
	else{
		$('#paper').css("top", 0);
	}
}

function loadPage(){
	
	var api;

	if(contentPane=="contentA"){
		api = $('.scrollBar.contentA').jScrollPane().data('jsp');
		//$('.topPhoto').load('loadPageImage.php', {page: currentPage}, function(){});
	}
	else{
		api = $('.scrollBar.contentB').jScrollPane().data('jsp');
		$(".pagePhotos").load('loadPageImage.php', {page: currentPage}, function(){adjustScrollbars();});
	}
	
	api.getContentPane().load('loadPageSection.php', {page: currentPage}, function(){
		adjustScrollbars();
	});

		
}

function updatePageValue(pageName){
	currentPage = pageName;
	$("#editPageForm :input[name=page]").attr("value", pageName);
}

function blockPage(){
	$('#orcBody').append('<div id="adminBlock"></div>');
	//$('#adminBlock').height($('#orcBody').height());
	$('#adminBlock').width($(window).width());
	$('#adminBlock').height($(window).height());
	$("#adminBlock").show();
}
