jQuery(document).ready(function() {
	
	// initialize var (checks if index has been requested yet)
	var index_requested = false;
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	// hijack 'address' links
	ath_hijack_links();
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	jQuery.address.change(function(event) {
        
		//return false; // for debugging

		// determine what the pathing is
		var path = event.value.replace( site_base_url , '' );
		var path_parts = path.split('/');
		
		//ath_log(path);
		//ath_log(last_section);
		
		if (path == '' || path == '/' || path == '/#') {
			// home
			
			// if our previous location was something besides home, adjust nav
			if (last_section != '') {
				ath_nav_update({
					section:'work'
					,category: 'selected'
				});
			}
			
			// only update the newsfeed here if not routing through jquery.address
			if (event.value == '/') {
				
			}
			
		} else if (path_parts[0] == 'work') {
			// work
			
			var category = 'selected'
			
			if (path_parts[1] != undefined && path_parts[1] != '') {
				category = path_parts[1];
			}
			
			ath_nav_update({
				section:'work'
				,'category' : category
			});
			
		} else if (path_parts[0] == 'project') {
			// project
			
			if (window.user_clicked_cat_project) {
				// are we navigating through projects in the same category?
				if (jQuery('#header div.project_holder').length > 0) {
					// yes, update project nav
					jQuery('#header ul.projects a').each(function() {
						jQuery(this).removeClass('active');
					
						if (jQuery(this).attr('rel') == event.value) {
							jQuery(this).addClass('active');
						}
					
					});
				
				} else {
					// no, gather project titles and links from category page
					var links_obj = [];

					jQuery('.box div.project_title').each(function(){

						// drop this project's title and link into an object
						var this_link = {
							"link": jQuery(this).find('a').parent().html()
						};

						links_obj.push(this_link);
					});

					ath_build_project_nav(links_obj, event.value);
				}
			} // end if user clicked cat_project
			
			
		} else if (path_parts[0] == 'about') {
			// about
			
			ath_nav_update({
				section:'about'
			});
		} else if (path_parts[0] == 'contact') {
			// contact
			
			ath_nav_update({
				section:'contact'
			});
			
		} else if (path_parts[0] == 'newsfeed') {
			// newsfeed
			
			ath_nav_update({
				section:'newsfeed'
			});
		}
		
		/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
		
		if (window.location.hash == '') {
			
			if (!index_requested) {
				index_requested = true;
				return;
			}
			
			event.value = 'index';
		}
		
		// update global var that keeps track of where we were...
		last_section = path_parts[0];
		
		// disable masonry (by killing off the window resize event)
		jQuery('#work_wrapper').masonry({resizeable: false});
		
		// display loader...
		jQuery('#content_loader').show();
		
		// hide existing content
		jQuery('#content_holder').hide();
		
		/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
		
		// Load into main #content_holder
		
        jQuery('#content_holder').load(
			event.value + '?ajax'
			,function() {
				// load complete
				
				// hide loader
				jQuery('#content_loader').hide();
				
				// show content
				jQuery('#content_holder').show();
				
				ath_hijack_links();
				
				ath_attach_tooltips();
				
				ath_init_contact();
				
				// Project-specific
				if (jQuery('#project_wrapper').length > 0) {
					
					ath_init_slideshows();
					
					if (!window.user_clicked_cat_project) {
						// the user didn't come from a category page. we need to figure
						// out the category information
						
						// get cat id
						var cat_id = parseInt($('div.project_meta span.category span.id').text());
						
						// update main nav
						ath_nav_update({
							section:'work'
							,'categoryID' : cat_id
							,'keep_project_holder' : true
						});
						
						// first, build out links based on links in the footer
						var links_obj = [];

						jQuery("div#categories_and_projects div.category[categoryid='"+cat_id+"'] span").each(function(){

							// drop this project's title and link into an object
							var this_link = {
								"link": jQuery(this).html()
							};

							links_obj.push(this_link);
						});

						$('div#header div.project_holder').remove();
						
						ath_build_project_nav(links_obj, jQuery.address.path() );
						
						window.user_clicked_cat_project = true;
					}
				}
				
				// Newsfeed
				if (jQuery('#work_wrapper.feeds').length > 0) {
					ath_init_feed_content();
					ath_clear_feed_floats();
					
				} else {
					// remove feed class from #content
					jQuery('#content').removeClass('feed');
					
					// Everything besides newsfeeds gets masonry, so re-instantiate
					jQuery('#work_wrapper').masonry( masonry_options );
				}
				
				/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
				
				// update page title
				var page_title = ''; // init variable
				
				if (path_parts[0] == '') {
					page_title += $("meta[name='default_page_title']").attr('content');
					
					// update newsfeeds
					if (!posts_updated) {
						$.post(site_base_url + 'update_feeds');
						posts_updated = true;
					}
					posts_updated = false;
					
				} else {
					page_title += jQuery('h1').text();
					page_title += ' — Brian McCutcheon';
				}
				
				jQuery.address.title(page_title);
				
				/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
				
				if (user_is_admin) {
					init_frontend_editing(); // function is in admin.onfrontend.js
				}
				
				/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
				
				// run window resizing
				ath_window_resize();
			}
		);
		
    });

	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	// activate masonry
	if (jQuery('#work_wrapper.feeds').length < 1) {
		jQuery('#work_wrapper').masonry( masonry_options );
	
		setTimeout(function() {
			masonry_options.animate = false;
			jQuery('#work_wrapper').masonry( masonry_options );
			masonry_options.animate = true;
		}, 500);
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	if (user_is_admin) {
		init_frontend_editing(); // function is in admin.onfrontend.js
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// initialize project slideshows
	
	if (jQuery('#project_wrapper').length > 0) {
		ath_init_slideshows();
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	// init feed content (if it exists)
	if (jQuery('#work_wrapper.feeds').length > 0) {
		ath_init_feed_content();
		ath_clear_feed_floats();
		
	} else {
		// remove feed class from #content
		jQuery('#content').removeClass('feed');
		
		// Unbind infinite scroll
		$(document).unbind('.infscr'); // prevents calling infinite scroll twice in some circumstances
	}
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	
	// init tooltips
	ath_attach_tooltips();
	
	// init contact
	ath_init_contact();
	
	// init all window resize logic
	jQuery(window).resize( function() {
		ath_window_resize();
	});
	
	ath_window_resize();
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// GLOBAL VARS

var last_section = '';

// this tracks whether the user clicked into a project
// detail state from the category level. if not, that means
// the user came directly to the site, and we need to figure
// out the project's category information
var user_clicked_cat_project = false;

var posts_updated = false;

// setup masonry's options (gets used a couple times...)
var masonry_options = {
	columnWidth: 220
	,itemSelector: '.box:visible'
	,animate: true
	,duration: 500
	,resizable: true
};

// setup infinitescroll's options (gets used a couple times...)
var infiniteScroll_options = {
	navSelector: 'div.navigation'
	,nextSelector: 'div.navigation a'
	,itemSelector: '#work_wrapper.feeds div.paged_content'
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// FUNCTIONS

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_init_feed_content() {
	
	// add feed class #content div
	jQuery('#content').addClass('feed');
	
	// show feed menu
	jQuery('#header .nav .feed_menu').slideDown();
	
	ath_init_feed_menu();
	
	// Infinite Scroll
	$(document).unbind('.infscr'); // prevents calling infinite scroll twice in some circumstances
	
	// have to specify loading image here, since "site_base_url" is not defined earlier
	infiniteScroll_options.loadingImg = site_base_url + 'img/loading_spinners/loading_spinner_white.gif';
	
	$.infinitescroll.currPage = 1;
	
	// disable infinitescroll if there's only one page of content
	var total_pages = $('span#total_page_count').text();
	var pages_loaded = jQuery('div.paged_content').length;
	
	$.infinitescroll.isInvalidPage = (pages_loaded >= total_pages) ? true : false;
	
	// Apply the Infinite Scroll Callback
	jQuery('#work_wrapper.feeds').infinitescroll(infiniteScroll_options, function(new_elements) {
		
		var total_pages = jQuery('span#total_page_count').text();
		var pages_loaded = jQuery('div.paged_content').length;
		
		// hide items that should be hidden
		ath_hide_deactivated_feeds();
		
		// adjust the clear divs between elements
		ath_clear_feed_floats();
		
		if (pages_loaded == total_pages) {
			$.infinitescroll.isInvalidPage = true;
		}
		
		if (user_is_admin) {
			init_frontend_editing(); // function is in admin.onfrontend.js
		}
		
	});
	
	// Add in clearing divs for the floated divs in the newsfeed
	ath_clear_feed_floats();
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_clear_feed_floats() {
	if (jQuery('#work_wrapper.feeds').length > 0) {
		
		// remove old clears
		jQuery('#work_wrapper.feeds div.feed_clear').remove();
		
		var windowWidth = parseInt( jQuery('#work_wrapper.feeds').width() );
		//ath_log('windowWidth: '+windowWidth);
		
		var rowWidth = 0;
		
		jQuery('#work_wrapper.feeds .paged_content').each(function() {
			
			rowWidth = 0;
			
			jQuery(this).find('.box:visible').each(function(){
				
				var this_width = jQuery(this).width() + parseInt(jQuery(this).css("margin-right"));

				if ((rowWidth + this_width) > windowWidth) {
					jQuery(this).before('<div class="clear feed_clear"></div>');
					rowWidth = 0;
				}

				rowWidth += this_width;
				//ath_log(rowWidth);
				
			});
			
		});
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_hijack_links() {
	jQuery('a.address').each(function() {
        var orig_href = jQuery(this).attr('rel');
		
		var new_href = site_base_url + '#' + orig_href;
		
		//ath_log(orig_href);
		//ath_log(new_href);
		
		jQuery(this).attr('href', new_href);
    });

	// attach logic to category_links
	$('a.address.category_links').unbind('click').bind('click',function(){
		window.user_clicked_cat_project = true;
	});
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_nav_update( options ) {
	
	if (options.keep_project_holder == undefined || options.keep_project_holder == false) {
		
		// roll up the project holder list
		
		jQuery('#header .nav div.project_holder').stop().animate({
			height: 0
		}, 300, function() {
			jQuery(this).remove();
		});
	}
	
	jQuery('#header .nav .feed_menu').hide();
	
	jQuery('#header ul a').removeClass('active');
	
	jQuery('#header ul.main a.' + options.section).addClass('active');
	
	// newsfeed
	if (options.section == 'newsfeed') {
		jQuery('#header .nav .feed_menu').slideDown();
	}
	
	// work
	if (options.category == undefined && options.categoryID == undefined) {
		
		jQuery('#header div.work_holder').hide();
		
	} else {
		
		jQuery('#header div.work_holder').slideDown();
		
		if (options.categoryID == undefined) {
			// look for category string
			$('#header ul.work a.' + options.category).addClass('active');
			
		} else {
			// look for category ID
			$("#header ul.work a[categoryid='"+options.categoryID+"']").addClass('active');
		}
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_build_project_nav(link_array, cur_url) {
	
	var nav_html = '';
	
	nav_html += '<div class="project_holder" style="display:none;"><ul class="projects">';
	
	for (var i=0; i < link_array.length; i++) {
		nav_html += '<li>' + link_array[i].link + '</li>';
	};
	
	nav_html += '</ul><div class="clear"></div></div>';
	
	jQuery('#header .nav').append(nav_html);
	
	// mark currently viewed project as active in nav
	jQuery('#header .project_holder a').each(function() {
		
		if (jQuery(this).attr('rel') == cur_url) {
			jQuery(this).addClass('active');
		}
	});
	
	jQuery('#header div.project_holder').slideDown();
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_init_slideshows() {
	
	jQuery('#project_wrapper div.slideshow_wrapper').each(function() {
		
		var cur_slideshow = jQuery(this);
		
		disableTextSelection(cur_slideshow);
		
		if (cur_slideshow.find('li.slide.video').length > 0) {
			
			// add video class to slideshow
			cur_slideshow.addClass('video')
				// change slideshow height
				.find('div.slideshow').height(cur_slideshow.find('div.slideshow .slide.cur').height());
				
			// position play icons
			cur_slideshow.find('li.slide.video').each(function() {
				var slide_width = jQuery(this).width();
				var icon_width = jQuery(this).find('div.play_icon').width();

				jQuery(this).find('div.play_icon').css('left', (slide_width - icon_width) / 2);
			})

		}
		// next button
		cur_slideshow.find('div.next_button').unbind('click').click(function() {
			
			if (!jQuery(this).hasClass('disabled')) {
				
				var cur_slide = cur_slideshow.find('.slideshow li.cur');
				var target_slide = cur_slide.next();
				
				if (cur_slideshow.find('li.slide div.video_container').length > 0) {
					ath_kill_video(cur_slideshow, function() {
						ath_scroll_slideshow(cur_slideshow, target_slide);
					});
					
				} else {
					ath_scroll_slideshow(cur_slideshow, target_slide);
				}
				
			}
		});
		
		// previous button
		cur_slideshow.find('div.prev_button').unbind('click').click(function() {
			
			if (!jQuery(this).hasClass('disabled')) {
				
				var cur_slide = cur_slideshow.find('.slideshow li.cur');
				var target_slide = cur_slide.prev();
				
				if (cur_slideshow.find('li.slide div.video_container').length > 0) {
					ath_kill_video(cur_slideshow, function() {
						ath_scroll_slideshow(cur_slideshow, target_slide);
					});
					
				} else {
					ath_scroll_slideshow(cur_slideshow, target_slide);
				}
				
			}
		});
		
		// slide nav
		cur_slideshow.find('div.slide_nav span').unbind('click').click(function() {
			
			if (!jQuery(this).hasClass('cur')) {
				
				var nav_number = jQuery(this).attr('id').split('_')[1];
				var target_slide = cur_slideshow.find('li#slide_'+ nav_number);
				
				if (cur_slideshow.find('li.slide div.video_container').length > 0) {
					ath_kill_video(cur_slideshow, function() {
						ath_scroll_slideshow(cur_slideshow, target_slide);
					});
					
				} else {
					ath_scroll_slideshow(cur_slideshow, target_slide);
				}
				
			}
			
		});
		
		// keyboard arrows
		jQuery(document).unbind('keydown').keydown(function(e){
			if (e.keyCode == 37) { // left arrow
				
				if (!cur_slideshow.hasClass('deactivated') && !cur_slideshow.find('div.prev_button').hasClass('disabled')) {
					
					var cur_slide = cur_slideshow.find('.slideshow li.cur');
					var target_slide = cur_slide.prev();
					
					if (cur_slideshow.find('li.slide div.video_container').length > 0) {
						ath_kill_video(cur_slideshow, function() {
							ath_scroll_slideshow(cur_slideshow, target_slide);
						});

					} else {
						ath_scroll_slideshow(cur_slideshow, target_slide);
					}
					
				}
				return false;
				
			} else if (e.keyCode == 39) { // right arrow
				
				if (!cur_slideshow.hasClass('deactivated') && !cur_slideshow.find('div.next_button').hasClass('disabled')) {
					
					var cur_slide = cur_slideshow.find('.slideshow li.cur');
					var target_slide = cur_slide.next();
					
					if (cur_slideshow.find('li.slide div.video_container').length > 0) {
						ath_kill_video(cur_slideshow, function() {
							ath_scroll_slideshow(cur_slideshow, target_slide);
						});

					} else {
						ath_scroll_slideshow(cur_slideshow, target_slide);
					}
					
				}
				return false;
			}
		});
		
		// click on image
		cur_slideshow.find('li.slide').click(function() {
			
			var target_slide = jQuery(this);
			
			// is the target slide fully visible?
			if (!ath_is_slide_visible(cur_slideshow, target_slide)) {
				// not visible, move to first position
				if (cur_slideshow.find('li.slide div.video_container').length > 0) {
					ath_kill_video(cur_slideshow, function() {
						ath_scroll_slideshow(cur_slideshow, target_slide);
					});

				} else {
					ath_scroll_slideshow(cur_slideshow, target_slide);
				}
				
			} else if (target_slide.hasClass('video')) {
				// is visible, and slide is a video
				
				// is another video already playing?
				if (target_slide.siblings('li.slide').find('div.video_container').length > 0) {
					// yes
					
					// add "clicked" class to slide
					target_slide.addClass('clicked');
					
					// kill off other video
					ath_kill_video(cur_slideshow, function() {
						ath_show_video(target_slide);
					});
					
				} else {
					// no
					
					// add "clicked" class to slide
					target_slide.addClass('clicked');
					
					// go ahead and play video
					ath_show_video(target_slide);
				}
				
			}
			
		});
		
		// hover over image
		cur_slideshow.find('li.slide').hover(
			function() {
				if (ath_is_slide_visible(cur_slideshow, jQuery(this))) {
					// slide is visible, so add "hovering" class
					jQuery(this).addClass('hovering');
				}
			}
			,function() {
				// remove "hovering" class
				jQuery(this).removeClass('hovering');
			}
		);
		
		// deactivate slideshow (hides controls) if all slides are visible
		var first_slide = cur_slideshow.find('li.slide:first');
		var last_slide = cur_slideshow.find('li.slide:last');
		
		if (ath_is_slide_visible(cur_slideshow, first_slide) && ath_is_slide_visible(cur_slideshow, last_slide)) {
			cur_slideshow.addClass('deactivated');
		}
		
	});
	
	// Connect hover code to the slide_nav
	$('div.slide_nav').unbind().hover(
		function() {
			$(this).addClass('hovering');
			
			setTimeout(function() {
				if ($('div.slide_nav').hasClass('hovering')) $('div.slide_nav div.instructions').stop().show().animate({'opacity':1},200);
			}, 750);
			
		}
		,function() {
			$(this).removeClass('hovering');
			
			$(this).find('div.instructions').stop().animate({'opacity':0},200, function() {
				$('div.slide_nav div.instructions').hide();
			});
		}
	);
	
	
	// Connect iOS gestures
	/*
	$('div#project_wrapper div.slideshow').swipe({
	    target: $('#project_wrapper div.slideshow_wrapper')
	});
	*/
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_scroll_slideshow(slideshow, target_slide) {
	
	slideshow.find('.slideshow ul').stop().animate({
		left : -target_slide.position().left
	}, function() {
		// run function defined for Window.resize() to determine if controls need to be hidden/shown
		jQuery(window).resize();
	});
	
	// remove "cur" class from previous slide...
	slideshow.find('.slideshow li.cur').removeClass('cur');
	// ...and add it to this slide
	target_slide.addClass('cur');
	
	// update scroll arrows
	ath_update_scroll_arrows(slideshow, target_slide);
	
	// update slideshow nav
	ath_update_slideshow_nav(target_slide);
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_update_scroll_arrows(slideshow, cur_slide) {
	
	var first_slide = slideshow.find('li:first');
	var last_slide = slideshow.find('li:last');
	
	// enable/disable prev and next arrows
	slideshow.find('div.prev_button, div.next_button').removeClass('disabled');
	
	if (cur_slide.attr('id') == first_slide.attr('id')) {
		// on first slide, disable previous arrow
		slideshow.find('div.prev_button').addClass('disabled');
		
	} else if (cur_slide.attr('id') == last_slide.attr('id')) {
		// on last slide, disabled
		slideshow.find('div.next_button').addClass('disabled');
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_update_slideshow_nav(cur_slide) {
	
	var nav_number = cur_slide.attr('id').split('_')[1];
	
	// remove "cur" class from any nav elements...
	cur_slide.parents('.slideshow_wrapper').find('div.slide_nav span').removeClass('cur')
		// ...and add it to appropriate element
		.siblings('span#nav_'+ nav_number).addClass('cur');
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_is_slide_visible(slideshow, slide) {

	var slide_rel_left = slideshow.find('ul').position().left + slide.position().left;
	
	if ((slideshow.width() - slide_rel_left) >= slide.width()) {
		return true;
	} else {
		return false;
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_kill_video(slideshow, callbackFunction) {
	
	var vid_container = slideshow.find('div.video_container');
	var vid_slide = vid_container.parent();
	
	vid_container.html('');
	
	vid_slide.find('div.vid_thumb').css('z-index', 10)
		.animate({
			opacity: 1
		}, 100, function() {
			vid_container.remove();
			
			if (callbackFunction) {
				callbackFunction();
			}
		});
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_show_video(vid_slide) {
	
	if (vid_slide.find('div.video_container').length < 1) {
		// video is not currently loaded, so continue
		
		// find width and height of slide
		var slide_width = vid_slide.find('div.vid_thumb img').width();
		var slide_height = '376px';
		
		// insert video container
		vid_slide.prepend('<div class="video_container" style="position:absolute;width:'+ slide_width +'px;height:'+ slide_height +'px"></div>');
		
		if (vid_slide.find('input.embed').length > 0) {
			// inserting embed video
			
			// gather embed code
			var embed_code = urldecode(vid_slide.find('input.vid_url').val());

			// reset width and height of embed, and set video html
			var width_regex = /width="(\d*)"/g;
			var height_regex = /height="(\d*)"/g;
			
			var video_html = embed_code
				.replace(width_regex, 'width="'+ slide_width +'"')
				.replace(height_regex, 'height="'+ slide_height +'"');
			
			// insert video html...
			vid_slide.find('div.video_container').html(video_html);
			
			// fade out image
			vid_slide.find('div.vid_thumb').animate({
				opacity: 0
			}, 300, function() {
				// send image behind video
				jQuery(this).css('z-index', -10);
				
				// remove "clicked" class from slide
				vid_slide.removeClass('clicked');

			});
			
			
		} else if (vid_slide.find('input.mov').length > 0) {
			// inserting quicktime
			
			var vid_url = urldecode(vid_slide.find('input.vid_url').val());
			
			var video_html = '<embed id="vidembed" scale="default" type="video/quicktime" autoplay="true" width="'+ slide_width +'" height="'+ slide_height +'" src="'+ vid_url +'" bgcolor="222222"></embed>';
			
			// fade out image
			vid_slide.find('div.vid_thumb').animate({
				opacity: 0
			}, 300, function() {
				// insert video html...
				vid_slide.find('div.video_container').html(video_html);

				// send image behind video
				jQuery(this).css('z-index', -10);
				
				// remove "clicked" class from slide
				vid_slide.removeClass('clicked');
				
			});
			
		} else if (vid_slide.find('input.swf').length > 0 || vid_slide.find('input.flv').length > 0) {
			// inserting other video types (flash)
			
			var vid_url = urldecode(vid_slide.find('input.vid_url').val());
			
			var video_html = '<div id="flash_viewer" style="width:'+ slide_width +'px;height:'+ slide_height +'px;"></div>';
			
			// fade out image
			vid_slide.find('div.vid_thumb').animate({
				opacity: 0
			}, 300, function() {
				// insert flash viewer (video html)...
				vid_slide.find('div.video_container').html(video_html);
				
				flowplayer("flash_viewer"
				 	,{
						src: site_base_url +"files/flash/flowplayer/flowplayer-3.1.5.swf"
						,w3c: true
					}
					,{
						clip: {
							url: vid_url
							,scaling: "fit"
						}
					}
				);
				
				// send image behind video
				jQuery(this).css('z-index', -10);
				
				// remove "clicked" class from slide
				vid_slide.removeClass('clicked');
				
			});
			
		}
		
	}
	
}

/*
function fix_feed_content() {
	
	jQuery('.box.facebook img').each(function() {
		
		// preload the image before checking its width
		preload_image($(this), function(image) {
			
			if (image.is(':visible') && image.width() <= 1) {
				// bad image
				
				// log the image's parent box before removing it
				var img_parent = image.parents('.box');
				
				image.remove();
				
				adjust_fb_detail_width(img_parent, true);
				
			} else {
				// good image
				
				// is image's width not the standard 90px?
				if (image.width() != 90) {
					adjust_fb_detail_width(image.parents('.box'));
				}
			}
		});
	});
	
}

function preload_image(image, callBackFunction) {
	var test_image = new Image();
	
	test_image.src = image.attr('src');
	
	test_image.onload = function() {
		if (callBackFunction) {
			callBackFunction(image);
		}
	}
	
}
*/

/*
function adjust_fb_detail_width(fb_box, set_full_width) {
	
	if (set_full_width) {
		
		var new_detail_width = '100%';
		
	} else {
		
		var img_width = fb_box.find('.picture img').width();
		var img_margin = fb_box.find('.picture').css('margin-right');
		
		var new_detail_width = fb_box.find('.post_wrapper').width() - (img_width + 10);
		
	}
	
	fb_box.find('.details_wrapper').css('width', new_detail_width);
	
}
*/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_init_feed_menu() {
	
	ath_hide_deactivated_feeds();
	
	jQuery('div.feed_menu li').unbind('click').click(function() {
		if ($(this).hasClass('showing')) {
			
			// set feed source to "hiding"
			$(this).removeClass('showing').addClass('hiding');
		
			// determine which feed to hide
			var source_id = $(this).attr('id');
			
			// hide all posts from this feed
			jQuery('div.box').each(function() {
				if ($(this).hasClass(source_id)) {
					$(this).fadeOut(function() {
						if (jQuery('.box.'+source_id+':visible').length < 1) {
							ath_clear_feed_floats();
						}
					});
				}
			});
			
			// is at least one source selected?
			if (jQuery('div.feed_menu li.showing').length < 1) {
				jQuery('#content.feed div.select_source').show("slow");
			}
		
		/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
			
		} else if ($(this).hasClass('hiding')) {
			
			// hide the 'select source' message
			jQuery('#content.feed div.select_source').hide();
			
			// set feed source to "showing"
			$(this).removeClass('hiding').addClass('showing');
			
			// determine which feed to show
			var source_id = $(this).attr('id');
			
			// show all posts from this feed
			jQuery('div.box').each(function() {
				if ($(this).hasClass(source_id)) {
					$(this).css('opacity',0).show();
					if (jQuery('.box.'+source_id+':hidden').length < 1) {

						jQuery('.box.'+source_id).animate({opacity: 1},300);
						ath_clear_feed_floats();
						
					}
				}
			});
		}
	});
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// This function hides the news items associated with currently-hidden news sources
function ath_hide_deactivated_feeds() {
	jQuery('div.feed_menu li').each(function() {
		if ($(this).hasClass('hiding')) {
			var source_id = $(this).attr('id');
			
			jQuery('.box.'+source_id).hide();
			ath_clear_feed_floats();
		}
	});
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ath_attach_tooltips() {
	
	// Attach Tooltips
	jQuery('#work_wrapper a.tooltip').tooltip({
		track : true
		,delay: 200
		,fade: 200
		/*,bodyHandler : function () {
			ath_log( jQuery(this) );
			return jQuery(this).attr('title');
		}*/
		,showURL : false
		,positionLeft : false
		,positionTop : true
		,left:18
		,top:18
	});
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var map;
var geocoder;
var infoWindow;

function ath_init_contact() {
	
	if ($('#contact_wrapper').length < 1) return false;
	
	// Init
	var myLatlng = new google.maps.LatLng(40.718789, -73.954962);
	
    var myOptions = {
      zoom: 12,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	// Init objects
	geocoder = new google.maps.Geocoder();
	infoWindow = new google.maps.InfoWindow();
	
	// Set the infoWindow content
	infoWindow.setContent( jQuery('#map_info_window').html() );
	
	// Get Address
	geocoder.geocode(
		{
	      'address': '6333 Washington Boulevard, Indianapolis, IN, 46220'
		}
		,function(results, status) {
			if (status == 'OK' && results.length > 0) {
				
				map.fitBounds(results[0].geometry.viewport);
		
				// build new marker
				
				var marker = new google.maps.Marker({
					position: map.getCenter()
					,map: map
				});
				
				// add click event
				google.maps.event.addListener(marker, 'click', function() {
					infoWindow.open(map, marker);
			    });
			
				// reveal infoWindow
				infoWindow.open(map, marker);
				
				// slide our map down slightly
				map.panBy(0,-70);
			}
		}
	);
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
var img_orig_width = 0;
var img_orig_height = 0;

function ath_window_resize() {
	
	// Clear feed floats
	ath_clear_feed_floats();
	
	// Manage About page scaling
	if ($('#about_wrapper').length > 0) {
		
		var img = $('#about_wrapper div.col2 img.portrait');
		
		if ( img_orig_width < 1 ) {
			img_orig_width = img.attr('width');
			img_orig_height = img.attr('height');
		}
		
		var img_w = img_orig_width;
		var img_h = img_orig_height;
		
		var col_width = $('#about_wrapper div.col2 div.wrapper').width();
		var new_w = col_width;
		if (new_w > 780) new_w = 780;
		
		var new_h = ((new_w * img_h) / img_w);
		
		//img.attr('width', new_w );
		//img.attr('height', new_h );
		
		// Adjust copy size based on scale
		if (col_width < 500) {
			$('#about_wrapper div.col1 div.wrapper')
				.removeClass('scale2 scale3')
				.addClass('scale1');
		} else if (col_width < 850) {
			$('#about_wrapper div.col1 div.wrapper')
				.removeClass('scale1 scale3')
				.addClass('scale2');
		} else if (col_width < 1000) {
			$('#about_wrapper div.col1 div.wrapper')
				.removeClass('scale1 scale2')
				.addClass('scale3');
		}
	}
	
	// Manage slideshow controls
	if ($('#project_wrapper div.slideshow_wrapper').length > 0) {
		jQuery('#project_wrapper div.slideshow_wrapper').each(function() {

			var cur_slideshow = jQuery(this);
			
			var first_slide = cur_slideshow.find('li.slide:first');
			var last_slide = cur_slideshow.find('li.slide:last');
			
			if (first_slide.hasClass('cur') && ath_is_slide_visible(cur_slideshow, last_slide)) {
				cur_slideshow.addClass('deactivated');
			} else {
				cur_slideshow.removeClass('deactivated');
			}
		});
	}
}






















