// function positionBgPic(){
// 	//moves the background picture into the correct position, depending on the relative size of the picture and the window (through the wrapper element)
// 	var myWrapper = document.getElementById('wrapper');
// 	var myPic = document.getElementById('bg_pic');
// 	if (window.innerHeight < myPic.clientHeight) {
// 		myPic.style.marginTop = ((window.innerHeight - myPic.clientHeight) / 2) + 'px';
// 	}
// 	if (window.innerWidth < myPic.clientWidth) {
// 		myPic.style.marginLeft = ((window.innerWidth - myPic.clientWidth) / 2) + 'px';
// 	}
// }


function onResize(){
	var myContWrapper = document.getElementById('content_wrapper');
	var myBgWrapper = document.getElementById('bg_wrapper');
	var myPic = document.getElementById('bg_pic');
	if (window.innerHeight) {
		if (window.innerHeight < myPic.clientHeight) {
			myPic.style.marginTop = ((window.innerHeight - myPic.clientHeight) / 2) + 'px';
			myContWrapper.style.height = window.innerHeight + 'px';
		}
		else {
			myContWrapper.style.height = myPic.clientHeight + 'px';
		}
		if (window.innerWidth < myPic.clientWidth) {
			myPic.style.marginLeft = ((window.innerWidth - myPic.clientWidth) / 2) + 'px';
			myContWrapper.style.width = window.innerWidth + 'px';
		}
		else {
			myContWrapper.style.width = myPic.clientWidth + 'px';
		}
	}
	else if (myBgWrapper.clientHeight) {
		if (myBgWrapper.clientHeight < myPic.clientHeight) {
			myPic.style.marginTop = ((myBgWrapper.clientHeight - myPic.clientHeight) / 2) + 'px';
			myContWrapper.style.height = myBgWrapper.clientHeight + 'px';
		}
		else {
			myContWrapper.style.height = myPic.clientHeight + 'px';
		}
		if (myBgWrapper.clientWidth < myPic.clientWidth) {
			myPic.style.marginLeft = ((myBgWrapper.clientWidth - myPic.clientWidth) / 2) + 'px';
			myContWrapper.style.width = myBgWrapper.clientWidth + 'px';
		}
		else {
			myContWrapper.style.width = myPic.clientWidth + 'px';
		}
	}
}


function open_content() {
	
	$("#content").addClass("display_none");
	$("#content").empty();
	$("#submenu").addClass("display_none");
	$("#submenu").empty();

	if ($(this).attr("action") != "home") {			// retrieve the content
	
		$.ajax({
			url: 'includes/switchboard.php',
			type: 'POST',
			data: "action=open&content=" + $(this).attr('action'),
			complete: function(xhr, textStatus) {
			    //called when complete
		  },
			success: function(data, textStatus, xhr) {
				
				if (data.split("|")[1]) {										// add the submenu
					$("#submenu").attr("innerHTML", data.split("|")[1]);
					$("#submenu").removeClass("display_none");
				}


				$("#content").attr('innerHTML', data.split("|")[0]);			// add the content
				$("#viewer").removeClass("display_none");
				$("#content").removeClass("display_none");
		  },
			error: function(xhr, textStatus, errorThrown) {
			    //called when there is an error
		  }
		});
	}
	
	
}


function toggle_selected_submenu_item() {
	$(this).parent().children().removeClass('selected');
	$(this).addClass('selected');
}


function select_content() {
	
	if (!$(this).hasClass("selected")) {
		$("#content > div").addClass('display_none');
		$("#" + $(this).attr('id') + "_content").removeClass('display_none');
	}
}


function showNewsText() {
	var myId = this.id;
	$(this).parent().children('.news_text').toggleClass('display_none');
}


var scrolling = false;
function scrollContent(obj) {
	myScrollable = obj.parent().parent().children('.scrollable');
	if (!scrolling) {
		obj.stop();
	}
	else {
		if (obj.hasClass('up')) {
			myScrollable.animate({"top": "+=20px"}, 'fast', function() {
				if (scrolling) {
					scrollContent(obj);
				}
			});
		}
		if (obj.hasClass('down')) {
			myScrollable.animate({'top': '-=20px'}, 'fast', function() {
				if (scrolling) {
					scrollContent(obj);
				}
			});
		}
	}
}
function startScroll() {
	scrolling = true;
	scrollContent($(this));
}
function stopScroll() {
	scrolling = false;
}


function subscribeToMailingList() {
	$.ajax({
		type: 'GET', 
		url: 'db/subscribe_to_mailing_list.php', 
		data: 'email=' + document.getElementById('emailaddress').value, 
		dataType: 'text', 
		success: function(data) {
			console.log(data);
		}
	});
}





// ----------------------------------------------------------------------------
// Autorun
// ----------------------------------------------------------------------------

//$(document).ready(onResize);
$('#bg_pic').load(onResize);
$(window).resize(onResize);
$('#top_navigation > li').bind('click', open_content);
$("body").delegate("li.sm", "click", select_content);
$('.news_more').live('click', showNewsText);
$("body").delegate(".scroller > div", 'mousedown', startScroll);
$("body").delegate(".scroller > div", 'mouseup', stopScroll);
$("body").delegate(".sm", "click", toggle_selected_submenu_item);
$('#subscribeToMailingList').bind('click', subscribeToMailingList);

