var assoc_type = null;

$(document).ready(function(){
	
	$('#top-menu-item5').click(function(){ // organizations
		
		assoc_type = 'organizations';
		open_assoc_nav($(this));
		$(this).find('a:first').addClass('open');
		$(this).find('img:first').attr('src', '/images/ico_arrow_tan.png');
		$('#top-menu-item6').find('a:first').removeClass('open');
		$('#top-menu-item6').find('img:first').attr('src', '/images/ico_arrow.png');
		$('a[rel="classification"]').hide();
		$('a[rel="priority"]').hide();
		$('a[rel="service"]').show();
		return false;
	});

	$('#top-menu-item6').click(function(){ // neighborhoods
		
		assoc_type = 'neighborhoods';
		open_assoc_nav($(this));
		$(this).find('a:first').addClass('open');
		$(this).find('img:first').attr('src', '/images/ico_arrow_tan.png');
		$('#top-menu-item5').find('a:first').removeClass('open');
		$('#top-menu-item5').find('img:first').attr('src', '/images/ico_arrow.png');
		$('a[rel="classification"]').show();
		$('a[rel="priority"]').show();
		//$('a[rel="service"]').hide();
		return false;
	});

	$('#briefs li a').click(function(){

		$('#briefs li a.active').removeClass('active');
		$(this).addClass('active');
		$(this).blur();
		
		$('#briefs div.brief').hide();
		$('#briefs #brief-' + $(this).attr('rel')).show();

		return false;
	});

	$('select.filter').change(function(){
		
		if ($(this).val() != ''){

			go($(this).val());
		}
	});

	$('a#finder-canceler').click(function(){
		
		$('#top-light-bar').hide();
		$('body').removeClass('open');
		$('#top-menu-item6').find('a:first').removeClass('open');
		$('#top-menu-item5').find('a:first').removeClass('open');
		$('#top-menu-item5').find('img:first').attr('src', '/images/ico_arrow.png');
		$('#top-menu-item6').find('img:first').attr('src', '/images/ico_arrow.png');
	});

	$('div.flickr').each(function(){
		
		var so = new SWFObject("/flash/gallery.swf", "gallery", "582", "452", "8", "#FFFFFF");
		so.addVariable("xml_file", "http://npn.bigglassonion.com/flickrssp.php?set=" + this.id);
		so.write(this.id);
	});

	init_cal();

	$('input#eletter-subscribe, input#send-to-friend').click(function(){
		
		$(this).val('');
		$(this).css('color', '#000000');
	});

	$('#eletter-submit').click(function(){
		
		$.post('/emails/submit/', { email: $('input#eletter-subscribe').val() }, function(data){
		
			$('input#eletter-subscribe').val(data.message);
			$('input#eletter-subscribe').css('color', data.status == 1 ? '#00C300' : '#FF0000');

		}, "json");
	});

	$('#send-to-friend-submit').click(function(){
		
		$.post('/emails/send_to_friend/', { email: $('input#send-to-friend').val(), link: window.location.href }, function(data){
		
			$('input#send-to-friend').val(data.message);
			$('input#send-to-friend').css('color', data.status == 1 ? '#00C300' : '#FF0000');

		}, "json");
	});

});

function init_cal(){
	
	$('table#cal-table tr.heading a').click(function(){
		
		href = $(this).attr('href');
		load_new_month(href);
		return false;
	});

	$('table#cal-table a.content').parent().addClass('active-cell');
}

function load_new_month(href){
	
	$('#cal-wrap').load(href, null, function(){
		
		init_cal();
	});
	
}

function open_assoc_nav(p){

	if ($('body').hasClass('second')){

		$('body').addClass('open');
	}
	
	$('#top-light-bar').show();
	p.find('a:first').blur();

	$('a#find-by-alpha').attr('href', '/associations/' + assoc_type + '/');

	reset_find();

	$('#top-light-bar').find('a.finder').click(function(){
		
		reset_find();

		$(this).addClass('curr');
		$('#by-' + $(this).attr('rel')).show();
		$('#by-' + $(this).attr('rel')).find('input:first').focus();
		$('#by-' + $(this).attr('rel')).find('input:last').click(function(){
			
			filter_type		= $('#top-light-bar').find('a.curr:first').attr('rel');
			query			= $(this).prev('input').val();
			query			= !query ? $(this).prev('select').val() : query;
			target			= '/associations/' + assoc_type + '/find_by/' + filter_type + '/' + query;
			
			go(target);
			return false;
		});

		return false;
	});
}

function reset_find(){
	
	$('#top-light-bar').find('div').hide();
	$('#top-light-bar').find('a').removeClass('curr');
}

function go(target){
	
	window.location = target;
}
