$(document).ready(function() {
	
	if (screen.width <= 480) {
	
	var curPage = "home";
	var tarPage;
	
	var eventsLoaded = false;
	
	// Nav Menu
	$("#nav>li>a").click(function () {
		tarPage = $(this).attr('href').split("#")[1];
		if (tarPage) {
			if (tarPage == "events")
				loadEvents();
			
			$("#"+curPage).slideUp('slow', function() {
				curPage = tarPage;
				tarPage = "";
				$("#"+curPage).slideDown('slow');
			});
			$("#back").slideDown('slow', function() { $("#back").css('display','block') });
			
			return false;
		}
	});
	
	// Back to home
	$("h1>a").click(function () {
		if (curPage != "home") {
			goingHome();
		}
		return false;
	});
	
	$("#back").click(function () {
		goingHome();
		
		return false;
	});
	
	function goingHome(){
		tarPage = "home";
		$("#"+curPage).slideUp('slow', function() {
			curPage = tarPage;
			$("#"+curPage).slideDown('slow');
		});
		
		$("#back").slideUp('slow');
		$("h1>a>img").animate({
			left: '0'
		});
		$("h1").animate({
			left: '0'
		});
	}
	
	function loadEvents() {
		if(!eventsLoaded) {
			eventsLoaded = true;
			$.ajax({
     			type: "GET",
				url: "events.xml",
				dataType: "xml",
				success: parseXML
			});
			$('#eventsList').html("<li>Loading Events...</li>");
		}
	}

	function parseXML(xml){
		var events = "";
		$(xml).find('event').each(function() {
			events += "<li><strong>"+ $(this).attr("title") + "</strong> - " + $(this).attr("date") + "</li>";
			//$("#output").append($(this).attr("title") + "<br />");
		});
		if(events == "")
			$('#eventsList').html("<li>No upcoming events</li>");
		else
			$('#eventsList').html(events);
	}
	
	// Photo Gallery
	$("#photoGal>li").click(function () {
		var nextPhoto;
		if($(this).next().length > 0)
			nextPhoto = $(this).next();
		else
			nextPhoto = $(this).parent().find(":nth-child(1)") ;
						
		if($(nextPhoto).is('.loaded')) {			
			$(this).slideUp('slow', function(){ $(nextPhoto).slideDown('slow'); });
		}
		else {
			var pic = ($(this).next().children().attr('href'));
			$(nextPhoto).html("<img src=\""+pic+"\">");
			$(nextPhoto).addClass('loaded');
			
			$(this).slideUp('slow', function(){ $(nextPhoto).slideDown('slow'); });
		}
		return false;
	});
	
	}
	
});
