$(document).ready(function(){

	// Hide elements which we dont want to show initially
	$('.dontshow').hide();
	
	// Add "last" class to named elements
	$("#breadcrumb a:last-child").addClass("last");
	
	// Slideshow for topbar images
	/***
	    Simple jQuery Slideshow Script
	    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
	***/
	function slideSwitch() {
	    var $active = $('#topslide img.active');
	    if ( $active.length == 0 ) $active = $('#topslide img:last');
	    // use this to pull the images in the order they appear in the markup
	    var $next =  $active.next().length ? $active.next()
		: $('#topslide img:first');
	    $active.addClass('last-active');
	    $next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 3500, function() {
		    $active.removeClass('active last-active');
		});
	}
	var topimgs = $("topslide").children();
	var numtopimg = topimgs.length;
	if(numtopimg > 1) {
		setInterval( "slideSwitch()", 7800 );
	}
	
	
	// Load the superfish js for use on the menu
	$('ul.sf-menu').superfish();
	$('ul.sf-menu a.toplevel span').remove();

	// FAQs toggle
	$('#faq dt').css({'cursor':'pointer'});
	$('#faq .faq').find('dd').hide().end().find('dt').click(function() {
		var answer = $(this).next();
		answer.fadeToggle('slow', 'linear');;
	 });
	
	// Fancybox popups
	  $("#content a.fancybox").each(function(){
	     $(this).attr('rel','group');
	  }); 	
	$('.fancybox').fancybox();
	
	// Create ajax document folders
	$('.library_folder').each(function () {
		var thingy = $(this).attr('data-rel');
		$(this).load('ajax/library.asp?'+ thingy);
	});
	
	// Initially hide listed subfolders, then toggle in-out 
	$('.folder:has(> ul.folder_open_close)').each(function () {
		$(this).children('div.folder_opener').css('cursor','pointer');
		$(this).children('.folder ul').hide();
	});
	$('div.folder_opener').live('click', function() {
		$(this).toggleClass('open')
		$(this).next('.folder_open_close').slideToggle();
	});

	// Create ajax gallery
	$('.gallery_folder').each(function () {
		var thingy = $(this).attr('data-rel');
		$(this).load('ajax/gallery.asp?'+ thingy);
	});
	
	// Toggle calendar type
	$('.togglecalview').live('click', function() {
		$('#calendar').fadeToggle();
		$('#eventlist').fadeToggle();
		return false;
	});	
	
	if($().ckeditor) {
		// Load the editor
		var config = {
			customConfig : '/js/plexuseditor_config.js',
			extraPlugins : 'autogrow',
					autoGrow_maxHeight : 1200,
					// Remove the resize plugin, as it doesn't make sense to use it in conjunction with the AutoGrow plugin.
			removePlugins : 'resize'
		};
		var basicconfig = {
			customConfig : '/js/plexuseditor_config.js',
			toolbar : 'Basic'
		};	
		$('.rte').ckeditor(config);
		$('.basicrte').ckeditor(basicconfig);
	}
	
	// JqueryUI datepicker
	$(".datepicker").datepicker({ dateFormat: 'dd MM yy' });
	
	// JqueryUI Custom addon for timepicker
	$(".datetimepicker").datetimepicker({ dateFormat: 'dd MM yy', timeFormat: 'hh:mm' });
	$(".timepicker").timepicker({});
	
	if($().validate) {
	    // Validation plugin exists
	    $(".vform").validate();
	} else {
	    // Validation plugin DOES NOT exist
	}
});


$(window).load(function(){
});
