var OPENSPACE = {};

// General-purpose namespacing function
OPENSPACE.namespace=function(c,f,b){var e=c.split(f||"."),g=b||window,d,a;for(d=0,a=e.length;d<a;d++){g=g[e[d]]=g[e[d]]||{}}return g};

// Include a few jQuery plugins for global use
jQuery.fn.fadeToggle = function(speed, easing, callback) { return this.animate({opacity: 'toggle'}, speed, easing, callback); };

(function($){
	
	// Shortcuts
	var app = OPENSPACE;
	
	// Namespace modules that we're referencing below
	app.namespace('OPENSPACE.widgets.Lightbox');
	
	$(function(){ // On DOM ready
		
		// Make accordion-like toggling boxes in the sidebar
		$(".box-title").click(function(){
			$(this).next(".box-body").slideToggle(300);
			$(this).next(".up").fadeIn(300);
			$(this).children(".up").fadeToggle(300);
			$(this).children(".down").fadeToggle(300);
		});

		// Handle all elements with a class of .lightbox
		$('.lightbox').each(function(){
			app.widgets.Lightbox.init($(this));
		}).bind('cbox_complete', function(){

			if($('#cboxCurrent').html() == '') {
				$('#cboxLoadedContent').addClass('no-lb-nav');
				
				$('#cboxContent,#cboxMiddleLeft,#cboxMiddleRight').height($('#cboxLoadedContent').outerHeight());
			};
			
		}).bind('cbox_closed',function(){
			$('#cboxCurrent').empty();
		});
		
		// Clean up validation on the questions form when closing its lightbox
		$('#questions-btn').bind('cbox_closed',function(){
			$('#questions-form .wpcf7-response-output').removeClass('wpcf7-mail-sent-ok wpcf7-validation-errors')
				.hide()
				.empty();
			$('#questions-form .wpcf7-not-valid-tip').remove();
		});
		
		// Set up the input hint on the search box
		$('input.input-hint').toggleVal({
			focusClass : 'focus',
			changedClass : 'changed'
		});
		
		// Prevent submitting a search if the input hint has not been replaced
		$('#searchform :submit').click(function(e){
			var $searchInput = $('#searchform :text');
			
			if(!$searchInput.is('.focus, .changed')) return false;
		});

	});
	
}(jQuery));


