jQuery(document).ready(function() {
	
	if ($('#work_wrapper').hasClass('home')) {
		// update feed posts
		$.post(site_base_url + 'update_feeds');
		
		// activate masonry
		jQuery('#work_wrapper').masonry( masonry_options );
	}
	
	// clear input field of email sub form
	var subForm_default = $('#subForm input.email').val();
	jQuery('#subForm input.email').focus(function() {
		if ($(this).val() == subForm_default) {	
			jQuery(this).val('');
		}
	});
	
	// check styling of right column on Project detail
	jQuery('#content_column.project_detail .right_col :first, #content_column.music_detail .right_col :first').each(function() {
		if ($(this).is('h4')) {
			$(this).css('margin-top', 0);
		}
	});
	
	
	// Infinite Scrolling
	
	// 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';
	
	// 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;
	
	jQuery('#work_wrapper.home').infinitescroll(infiniteScroll_options, function(new_elements) {
		
		//fix_feed_content();
		
		masonry_options.appendedContent = $(new_elements);
		
		jQuery('#work_wrapper.home').masonry( masonry_options , function() {
			masonry_options.appendedContent = null;
		});
		
		setTimeout(function() {
			jQuery('#work_wrapper').masonry( masonry_options );
		}, 500);
		
		var total_pages = jQuery('span#total_page_count').text();
		var pages_loaded = jQuery('div.paged_content').length;
		
		if (pages_loaded == total_pages) {
			$.infinitescroll.isInvalidPage = true;
		}
		
		/*if (user_is_admin) {
			init_frontend_editing(); // function is in admin.onfrontend.js
		}*/
		
	});
	
	jQuery('#content_column.project_detail .slideshow_wrapper').each(function() {
		fe_init_image_nav(jQuery(this));
		fe_position_slide_images(jQuery(this));
	});
});

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

// GLOBAL VARS

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

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

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

// FUNCTIONS

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

function fe_position_slide_images(cur_gallery) {
	cur_gallery.find('.slideshow li.slide img').each(function() {
		
		var container_width = cur_gallery.find('.slideshow').width();
		var container_height = cur_gallery.find('.slideshow').height();
		
		if ($(this).attr('width') < container_width) {
			var new_left = (container_width - $(this).attr('width')) / 2;
			
			$(this).parent().css('left', new_left);
		}
		
		if ($(this).attr('height') < container_height) {
			var new_top = (container_height - $(this).attr('height')) / 2;
			
			$(this).parent().css('top', new_top);
		}
	});
}

function fe_init_image_nav(cur_gallery) {
	
	var nav_images = cur_gallery.find('.slide_nav .nav li img');
	var nav_list = nav_images.parents('ul');
	
	/*nav_images.each(function() {
		var thumb_width = jQuery(this).parent().width();
		var thumb_height = jQuery(this).parent().height();
		
		var nav_images = cur_gallery.find('.slide_nav .nav li img.nav_img');
		var nav_list = nav_images.parents('ul');
		
		if (nav_images.length < 4) {
			fe_position_nav(nav_list);
		}
		
		// if more than max number of images, reveal previous and next buttons
		if (nav_images.length > 4) {
			
			// activate next and previous navigation buttons
			var nav_prev = cur_gallery.find('.nav_prev');
			var nav_next = cur_gallery.find('.nav_next');

			nav_next.removeClass('hidden');
			nav_prev.removeClass('hidden');
			
		}
		
	});*/
	
	if (nav_images.length < 4) {
		// center image thumbs
		fe_position_nav(nav_list);
		
	} else if (nav_images.length > 4) {
		
		// activate next and previous navigation buttons
		var nav_next = cur_gallery.find('.nav_next');
		var nav_prev = cur_gallery.find('.nav_prev');
		
		nav_next.removeClass('hidden');
		nav_prev.removeClass('hidden');
		
		nav_next.click(function() {

			if (!jQuery(this).hasClass('disabled')) {
				
				// find left position that signifies end of nav list:
				// 1) multiply total number of thumbnails by 132 (width + margin)
				// 2) multiply maximum number of thumbnails to be shown by 132
				// 3) subtract first product from second product
				var leftmost_pos = 4*132 - nav_images.length*132;
				
				var cur_nav_img = nav_list.find('li.nav_slide.nav_cur');
				var next_nav_img = cur_nav_img.next('li');
				
				fe_scroll_nav(cur_gallery, next_nav_img);
				
			}

		});

		nav_prev.click(function() {

			if (!jQuery(this).hasClass('disabled')) {
				
				// find left position that signifies end of nav list:
				// 1) multiply total number of thumbnails by 70 (width + margin)
				// 2) multiply maximum number of thumbnails to be shown by 70
				// 3) subtract first product from second product
				var leftmost_pos = 4*132 - nav_images.length*132;
				
				var cur_nav_img = nav_list.find('li.nav_slide.nav_cur');
				var next_nav_img = cur_nav_img.prev('li');
				
				fe_scroll_nav(cur_gallery, next_nav_img);
				
			}
			
		});
	}
	
	nav_list.find('li.nav_slide').click(function() {
		if (!$(this).hasClass('cur')) {
			var slide_number = $(this).attr('id').split('nav_')[1];
			
			cur_gallery.find('.slideshow .slide.cur').removeClass('cur');
			cur_gallery.find('.slideshow li#slide_'+slide_number).addClass('cur');
			
			nav_list.find('li.nav_slide.cur').removeClass('cur');
			$(this).addClass('cur');
			
		}
	});
	
}

function fe_position_nav(nav_list) {
	
	var nav_width = nav_list.find('li').length*132;
	var container_width = nav_list.parents('.nav').width();
	
	// figure left position value
	var left_pos = (container_width - nav_width) / 2;
	
	nav_list.css('left', left_pos);
}

function fe_scroll_nav(cur_gallery, target_slide) {
	
	var nav_list = cur_gallery.find('.nav ul');
	var cur_slide = nav_list.find('li.nav_slide.nav_cur');
	var img_count = nav_list.find('li').length;
	var nav_width = img_count*132;
	var nav_leftmost = 4*132 - nav_width;
	var cur_nav_pos = nav_list.position().left;
	
	// assign "nav_cur" class to target nav slide
	cur_slide.removeClass('nav_cur');
	target_slide.addClass('nav_cur');
	
	nav_list.stop().animate({ left : -target_slide.position().left });
	
	if (-target_slide.position().left >= 0) {
		//nav_list.stop().animate({ left : 0 });
		cur_gallery.find('.nav_prev').addClass('disabled');
	} else if (-target_slide.position() <= nav_leftmost) {
		//nav_list.stop().animate({left: nav_leftmost});
		cur_gallery.find('.nav_next').addClass('disabled');
	} else {
		//nav_list.stop().animate({ left : position }, 200);
	}
	
	// if starting at the initial position...
	if (cur_nav_pos == 0) {
		cur_gallery.find('.nav_prev').removeClass('disabled');
	}
	// if scrolled to the initial position...
	if (-target_slide.position().left == 0 ) {
		cur_gallery.find('.nav_prev').addClass('disabled');
	}
	// if starting at the terminal position...
	if (cur_nav_pos == nav_leftmost) {
		cur_gallery.find('.nav_next').removeClass('disabled');
	}
	// if scrolled to the terminal position...
	if (-target_slide.position().left == nav_leftmost) {
		cur_gallery.find('.nav_next').addClass('disabled');
	}
}


function ath_nav_update( options ) {
	
	jQuery.address
	
	jQuery('#header .nav div.project_holder').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');
	
	if (options.category == undefined) {
		
		jQuery('#header div.work_holder').hide();
		
	} else {
		
		jQuery('#header ul.work a.' + options.category).addClass('active');
		
		if (last_section != 'work' && last_section != 'project' && last_section != '') {
			// slide out
			var orig_height = jQuery('#header div.work_holder').height();
			
			jQuery('#header div.work_holder').height(0).css('overflow','hidden').show();
			jQuery('#header div.work_holder').animate({
				'height': orig_height + 'px'
			} ,300 );
		}
		
		if (options.section == 'newsfeed') {
			jQuery('#header .nav .feed_menu').slideDown();
		}
	}
}

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 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 init_feed_menu() {
	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) {
							jQuery('#work_wrapper').masonry( masonry_options );
						}
					});
				}
			});
			
		} else if ($(this).hasClass('hiding')) {
			
			// 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('#work_wrapper').masonry( masonry_options , function() {
							fix_feed_content();
							jQuery('.box.'+source_id).animate({opacity: 1},300);
						});
					}
				}
			});
		}
	});
	
}

function 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();
			jQuery('#work_wrapper').masonry( masonry_options );
		}
	});
}





























