// default

//
$.postJSON = function (url, data, callback) {
	$.post(url, data, callback, 'json');
};

//
$(function(){
	
	// event tracker for outgoing links/file downloads
	if(typeof _gaq != "undefined"){
		$('a').each(function(){
			var path;
			
			if(path = $(this).attr('href')){
				var outgoing 	= /^(http|https)/i;
				var ext 		= /[^/].(?:doc|eps|jpg|jpeg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3|mp4)$/i;
				
				if (path.match(outgoing) && !path.match(document.domain) ) {
					$(this).click(function(){
						_gaq.push(['_trackEvent', 'Link', 'Outgoing', $(this).attr('href')]);
					});
				} else if(path.match(ext)){
					$(this).click(function(){
						_gaq.push(['_trackEvent', 'File', 'Download', $(this).attr('href')]);
					});	
				}
			}
		});
	}
	
	// 
	$('input').focus(function(){
		if($(this).val() == $(this).attr('title')){ $(this).val(''); }
	}).blur(function(){
		if($(this).val() == ''){ $(this).val($(this).attr('title')); }
	});
	
	// forms
	$('#form').ajaxForm({
        beforeSubmit:  	function(formData, form, options){
							$('input.warning,textarea.warning,checkbox.warning,select.warning').removeClass('warning');
							$('.validate').slideUp();
							return true;
						}, 
        success:       	function(data, status, xhr, form){
							if(data.success == true){
								$('.validate>p').html(data.thank_you);
								$('.validate').slideDown();
								$('html, body').animate({ scrollTop: 200 }, 1000);
								form.slideUp();
							} else {
								$('.validate>p').html('');
								
								for (var i in data.error){
									$('.validate>p').append(data.error[i] + '<br />');
								}
								
								for (var i in data.error){
									$('[name=' + data.name[i] + ']').addClass('warning');
								}
								
								$('html, body').animate({ scrollTop: 200 }, 1000, function(){$('.validate').slideDown();});
							}
						},
		type:			'post',
		dataType:		'json'
	});
	
	// billboard
	$('#billboard').cycle({ 
		fx:				'fade', 
		speed:			1000,
		timeout:		5000,
		pause:			1,
		cleartype:		1,
		cleartypeNoBg:	1
	 });
	
	// more
	$('.moreLink,.moreBtn').click(function(){
		$(this).siblings('.moreTxt').show('slow', function(){ $(this).css({'display':'inline'})});
		$(this).siblings('.moreBtn').hide();
	});
	
	//windowshade
	$('.windowshade').each(function(){
		$(this).find('.string').bind('click', function(){
			$(this).parent('p').siblings('.shade').slideToggle();
			return false;
		});
	});
	
	// internal filterning nav
	$('#internal-nav>ul>li.plus>a').bind('click', function(){
		$(this).parent('li').children('ul:eq(0)').slideToggle(function(){ $(this).parent('li').toggleClass('minus'); });
		return false;
	});
	
	// gallery
	$(".gallery>ul>li>a,a.image").fancybox({
		'titlePosition'		: 'over',
		'overlayColor'		: '#000000',
		'overlayOpacity' 	: .5
	});
	
	// calendar
	$('#prev_month>a').bind('click', function(){
		var date 	 = $('table.calendar:first').attr('data-date');
		var type 	 = $('table.calendar:first').attr('data-type');
		var audience = $('table.calendar:first').attr('data-audience');
		
		if(type != ''){ type = 'type:' + type +  '/'; }
		if(audience != ''){ audience = 'audience:' + audience +  '/'; }
		
		$.getJSON('/ajax/command:mini-cal/direction:previous/date:' + date + '/' + type + audience, function(data){
			if(data.success == true){
				$('table.calendar:first').before(data.html);
				$('table.calendar:last').remove()
			}
		})
		
		return false;
	});
	
	$('#next_month>a').bind('click', function(){
		var date 	 = $('table.calendar:last').attr('data-date');
		var type 	 = $('table.calendar:last').attr('data-type');
		var audience = $('table.calendar:last').attr('data-audience');
		
		if(type != ''){ type = 'type:' + type +  '/'; }
		if(audience != ''){ audience = 'audience:' + audience +  '/'; }
		
		
		$.getJSON('/ajax/command:mini-cal/direction:next/date:' + date + '/' + type + audience, function(data){
			if(data.success == true){
				$('table.calendar:last').after(data.html);
				$('table.calendar:first').remove();
			}
		})
		
		return false;
	});
	
	//nav 
	
	$('#nav>ul>li>a').hover(function() {
		$(this).siblings('div').stop().animate({ 'opacity': '1' }, 400);
	}, function() {
		$(this).siblings('div').stop().animate({ 'opacity': '0' }, 400);
	});
	
	//
	$('.hide').hide();
	$('.show').show();
	
});
