$(document).ready(function() {

	pagename = gup("page");
	classname = gup("class");
	eventid = gup("event");
	
	// initialize things
	$.ajaxSetup({ cache: false }); 	
	
	$('#toppic img').hide();
	$('#logo').css('left', $('#content').offset().left-100);
	$('#address').css('left', $('#content').offset().left+550);
	$('#toppic').css('left', $('#content').offset().left+830);
	
	$('ul#nav a').click(function(e) {
		e.preventDefault();
		
		mypage = $(this);
		if ($(mypage).hasClass('currenttab')) return;		
		
		$('#content').slideUp('slow', function() {
			$('#'+$('a.currenttab').attr('name')).hide();
			$('#'+$(mypage).attr('name')).show();			
			document.location.href = '/page/'+$(mypage).attr('name')+'.html';
		});
		
	});		
	$('ul#nav a').addClass('othertabs');
	
	$('#toppic img').first().show();
	$('#toppic').everyTime('6s', function() {
		current = $('#toppic :visible');
		$(current).fadeOut('slow', function() {
			if ($(current).next().length) {
				$(current).next().fadeIn('slow');
			} else {
				$('#toppic img').first().fadeIn('slow');
			}
		});
	});
	
	// ABOUT
	$("#about h5").click(function(e) {
		$(this).next().slideToggle();
	});
	
	// CALENDAR
	$('#calendar').fullCalendar({
		editable: false,
		defaultView: 'basicWeek',
		events: "/calendar.php?events=1"
	});
	
	// EVENT
	if (eventid) {
		q = 'select event_imagefile, event_thumbfile from lmd_event where event_id='+eventid;
		$.getJSON('/querier.php?q='+q, function(data) {
			$.each(data, function(i, d) {
				setThumb($('#event_flyer'), d.event_imagefile, d.event_thumbfile);
				$('#event_flyer_box').show();
			});
		});
	}

	// CLASS
	if (classname) {
		q = 'select class_imagefile, class_thumbfile from lmd_class where class_urlname="'+classname+'"';
		$.getJSON('/querier.php?q='+q, function(data) {
			$.each(data, function(i, d) {
				setThumb($('#class_flyer'), d.class_imagefile, d.class_thumbfile);
				$('#class_flyer_box').show();
			});
		});
	}	
	$('#class_register_link').colorbox({width:'50%', inline:true, href:'#class_register'});
	
	
	// Display the correct page
	$('#content').hide();
	if (pagename) {
		$('ul#nav a[name='+pagename+']').addClass('currenttab');
		$('#'+pagename).show();
	} else if (classname) {
		$('#class').show();
	} else if (eventid) {
	
	} else {
		$('ul#nav a[name=home]').addClass('currenttab');
		$('#home').show();
	}
	$('#content').slideDown('slow', function() {
		// Create nifty corners
		Nifty('#content', 'tr bl br');		
	});
	
	// Create nifty corners
	Nifty('ul#nav a','big top'); 	
	Nifty('div.infoboxhead', 'top');
	Nifty('div.infoboxcontent', 'bottom');	

});


// This version does not work with mod_rewrite
/*function gup( name ) {  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null ) return "";  
	else return results[1];
}*/

function gup(name) {
	var str = window.location.href;
	if (str.indexOf('/'+name+'/') != -1)
		return str.substring(str.indexOf('/'+name+'/')+name.length+2, str.lastIndexOf('.'));
}

