$(document).ready(function(){

	$('input#keyword-search').placeholder();
	
	// Create cookie holder for saving entries
	var cookieOptions = { duration: 30, path: '' };
	var cookieName = 'BIAC-directory';
	if( ! $.readCookie(cookieName) ) {
		$.setCookie(cookieName, '', cookieOptions);
    // Set cookied entries as saved
    } else {
		var savedCookies = $.readCookie(cookieName).split(':');
		$.each(savedCookies, function(i, e) {
			if( e != '' ) {
				$('#entry_'+e+' a.save').removeClass('save').addClass('saved').text('Remove saved');
			}
		});
		updateSavedItems();
    }
	
	// Entry Buttons
	$('ul.downloads a').live('click', function(){
		var $this = $(this);
		var $id = $this.parent().parent().parent().attr('id').replace('entry_','');
		var $title = $this.parent().parent().parent().find('h3').html();
		var cookie = $.readCookie(cookieName);
		var newCookie;
		
		// Save Entry to cookie
		if ( $this.hasClass('save') ) {
			//$.delCookie('BIAC-directory');
			$.setCookie(cookieName, cookie+':'+$id, cookieOptions );
			$this.removeClass('save').addClass('saved').text('Remove saved');
			_gaq.push(['_trackEvent', 'Directory','Save Entry', $title ]);
			updateSavedItems();

		// Remove Entry from cookie
		} else if( $this.hasClass('saved') ) {
			newCookie = cookie.replace(':'+$id, '');
			$.setCookie(cookieName, newCookie, cookieOptions );
			$this.removeClass('saved').addClass('save').text('Save for later');	
			_gaq.push(['_trackEvent', 'Directory','Unsave Entry', $title ]);
			updateSavedItems();
		
		// Print single entry
		} else if( $this.hasClass('print') ) {
			_gaq.push(['_trackEvent', 'Directory','Print Entry', $title ]);
			$('#entry_'+$id)
			.prepend('<img src="/images/biac-header-print.png" alt="" class="printonly" />')
			.printElement({
				pageTitle: $('#entry_'+$id).find('h3').text()
			});
		} else if( $this.hasClass('vcard') || $this.hasClass('email') ) {
			return true;
		}
		
		
		return false;
	});	
	
	// Print all records
	$('#printall a').live('click', function(){
		$('div.directory-results')
		.prepend('<img src="/images/biac-header-print.png" alt="" class="printonly" />')
		.printElement({
			pageTitle:$(this).parent().find('h3').text()
		});
		return false;
	});
	
	// First check for any location being set
	$('.select_location').each(function(i,e){
		$this = $('.select_location:eq('+i+')');
		if ( $this.val() != '' ) {
			$('.select_location').addClass('inactive').attr('disabled','disabled');
			$this.removeAttr('disabled').removeClass('inactive');
		}
	});
	// Location change disable others
	$('.select_location').live('change',function(){
		var $this = $(this);
		if( $this.val() == '' ) {
			$('.select_location').removeAttr('disabled').removeClass('inactive');
		} else {
			$('.select_location').addClass('inactive').attr('disabled','disabled');
			$this.removeAttr('disabled').removeClass('inactive');
		}
	});
	
	// Form Reset
	$('#reset_form').click(function(){
		$('select').val('').removeAttr('disabled').removeClass('inactive');
		return false;
	});
	
	// Directory Search formatting
	$('#directory_search form').submit(function(){
		var url = '';
	
		// Search Query
		var s = jQuery.trim($('#keyword-search').val());
		if( s == 'Keyword Search' || s == '' ) s = '_';
		
		// Category
		var catsearch = $('select[name=catsearch]').val() || '_';
		
		// Locations
		var location = $('select.select_location').not(':disabled').attr('name');
		var locationID = $('select.select_location').not(':disabled').val();
		
		if( locationID != '' ) {
			url = '/'+s+'/'+catsearch+'/'+location+'/'+locationID;
		} else {
			url = '/'+s+'/'+catsearch;			
		}
		
		window.location.href = 'http://www.biacolorado.org/directory'+url;
		
		return false;		
	});
	
	// Validate Email form
	$('#directory_email').validate({
		rules: {
			from_name: "required",
			from_email: {
				required: true,
				email: true
			},
			to_name: "required",
			to_email: {
				required: true,
				email: true
			}
		}
	});
	
});

function updateSavedItems(){
	var cookieName = 'BIAC-directory';
	var savedCookies = $.readCookie(cookieName).split(':');
	var countCookies = savedCookies.length - 1;
	$('#savedItems').html('<p>You have '+countCookies+' saved entries. <a href="/directory/index.php?saved='+$.readCookie(cookieName)+'" class="view_all">View Entries</a><a href="/directory/index.php?saved='+$.readCookie(cookieName)+'&email=1" class="email">Email Entries</a></p>');
}

































