function everest_gallery_init() {
/*
	$('#left-pane .gallery-list').each( function() {
		var width = 0;
		$(this).children('li').each( function() {
			width += $(this).outerWidth(true);
		});
		$(this).css('width', width + 'px');
	});
*/
	$('#left-pane .gallery-list a').click( function() {
		var image_src = $(this).attr('href');
		$('#gallery-main').animate({opacity: 0.01}, 250, 'linear', function() {
			$(this).attr('src', image_src);
			$('#gallery-main-link').attr('href', image_src);
			$('#gallery-main-link').fancybox();
			
			if (this.complete || this.readyState === 4) {
				$(this).animate({opacity: 1.0}, 250, 'linear');
			} else {
				$(this).bind('load', function() {
					$(this).animate({opacity: 1.0}, 250, 'linear');
				});
			}
		});
		var current_image = $(this).attr('class').match(/img(\d+)/)[1];
		var title_array = $(this).attr('title').match(/(.*)\s?\|\|\s?(.*)?/);
		var title = title_array != null ? title_array[1] : '';
		var caption = title_array != null ? title_array[2] : '';
		$('#left-pane .counter .current').html(current_image);
		$('#left-pane h2.image-title').html(title);
		$('#left-pane .caption').html(caption);
		return false;
	});
	if (navigator.userAgent.match(/Mobile/) && navigator.userAgent.match(/Safari/)) {
		$('#left-pane .gallery-list-wrapper').jScrollTouch();
		$('#left-pane .gallery-prev a').click( function() {
			var width_single = $('#left-pane .gallery-list li').first().outerWidth(true);
			var current_pos = parseInt($('#left-pane .gallery-list-wrapper').scrollLeft());
			if (current_pos > 0) {
				var new_pos = current_pos - width_single;
				if (new_pos < 0) {
					new_pos = 0;
				}
				$('#left-pane .gallery-list-wrapper').scrollLeft(new_pos);
			}
			return false;
		});
		$('#left-pane .gallery-next a').click( function() {
			var width = parseInt($('#left-pane .gallery-list').css('width'));
			var width_single = $('#left-pane .gallery-list li').first().outerWidth(true);
			var current_pos = parseInt($('#left-pane .gallery-list-wrapper').scrollLeft());
			if (current_pos < width - 4*width_single) {
				var new_pos = current_pos + width_single;
				if (new_pos > width - 4*width_single) {
					new_pos = width - 4*width_single;
				}
				$('#left-pane .gallery-list-wrapper').scrollLeft(new_pos);
			}
			return false;
		});
	} else {
		$('#left-pane .gallery-prev a').click( function() {
			var width_single = $('#left-pane .gallery-list li').first().outerWidth(true);
			var current_pos = parseInt($('#left-pane .gallery-list').css('margin-left'));
			if (current_pos < 0) {
				var new_pos = current_pos + width_single;
				if (new_pos > 0) {
					new_pos = 0;
				}
				$('#left-pane .gallery-list').animate({marginLeft: new_pos + 'px'}, 250);
			}
			return false;
		});
		$('#left-pane .gallery-next a').click( function() {
			var width = parseInt($('#left-pane .gallery-list').css('width'));
			var width_single = $('#left-pane .gallery-list li').first().outerWidth(true);
			var current_pos = parseInt($('#left-pane .gallery-list').css('margin-left'));
			if (current_pos > - width + 4*width_single) {
				var new_pos = current_pos - width_single;
				if (new_pos < - width + 4*width_single) {
					new_pos = - width + 4*width_single;
				}
				$('#left-pane .gallery-list').animate({marginLeft: new_pos + 'px'}, 250);
			}
			return false;
		});
	}
}

function everest_jump_init() {
	$('#project-select .jump-to-film').click( function() {
		$(this).parents('#project-select').addClass('active');
	}); 
	$('#project-select').mouseleave( function() {
		$(this).removeClass('active');
	});
}

//not used
function everest_title_replace() {
	$('#right-pane h1.title').each( function() {
		$(this).find('.text').addClass('hidden');
		$(this).find('.image').removeClass('hidden');
	});
}

function everest_subnav_init() {
	$('#sub-nav a').click( function() {
		$('#sub-nav li').removeClass('active');
		$(this).parent().addClass('active');
		var tgt_id = $(this).attr('class').match(/tab\-(\w+)/)[1];
		$('#right-pane .content').addClass('hidden');
		$('#right-pane #' + tgt_id).removeClass('hidden');
		if (tgt_id == 'media') {
			everest_mediaplayer_init();
		} else {
			everest_scrollbar_init();			
		}
		return false;
	});
}

function everest_contentnav_init() {
	$('.content-nav a').click( function() {
		var section = $(this).closest('div.content').attr('id');
		$('#' + section + ' .content-nav li').removeClass('active');
		$(this).parent().addClass('active');
		var tgt_id = $(this).attr('class').match(/tab\-(\w+)/)[1];
		$('#' + section + ' .subcontent').addClass('hidden');
		$('#' + section + ' #' + tgt_id).removeClass('hidden');
		everest_scrollbar_init();
		return false;
	});	
}

function everest_mediaplayer_init() {
	var media_element = $('#media .video').first().get()[0];
	if (media_element === undefined) {
		return false;
	}
	$('#media .video').click( function () {
		everest_clip_init(this);
		return false;
	});
	$(media_element).click();
}

function everest_clip_init(element) {
	var movie_href = $(element).attr('href');
	$('#player-box').css('display', 'block').css('width', '330px').css('height', '200px');
	$f('player-box', '/static/swf/flowplayer.swf', {
		clip:	{
			url:	movie_href.replace('+', '%2B'),
			autoplay:	false
		},
		plugins: {
			controls: {
				time:			false
			}
		}
	}).ipad();
}

function everest_home_init() {
	$('#home-projects li').click( function(event) {
		var nodename = event.target.nodeName.toLowerCase();
		if (nodename != 'a') {
			var href = $(this).find('a.view-project').attr('href');
			window.location = href;
		}
	});
	if (navigator.userAgent.match(/Mobile/) && navigator.userAgent.match(/Safari/)) {
		$('#home-projects').jScrollTouch();
	} else {
		$('#home-projects').jScrollPane({animateScroll:true}); //.addTouch();
	}
	$('#home-more-projects').removeClass('hidden');
	
	$('#home-more-projects a').bind(
	'click',
	function() {
		var api = $('#home-projects').data('jsp');
		api.scrollToY(500);
		return false;
	});
	$('#home-projects').bind('jsp-scroll-y', function() {
		$('#home-more-projects').addClass('hidden');
	});
}

function everest_project_switcher_init(type) {
	var regexp = new RegExp(type + "(\\d+)");
	$('.project-switcher').click( function() {
		var tgt_match = regexp.exec($(this).attr('class'));
		if (tgt_match != null) {
			var tgt_uri = '/film_ajax/' + tgt_match[1] + '/';
			$('.pane').animate({opacity: 0.01});
			$('#body').load(tgt_uri, function() {
				$('.pane').css('opacity', '0.01');
				$('.pane').animate({opacity: 1.0});
				everest_project_init(type);
			});
			return false;
		}
	})
}

function everest_scrollbar_init() {
	if (navigator.userAgent.match(/Mobile/) && navigator.userAgent.match(/Safari/)) {
		$('#right-pane .content:visible').jScrollTouch();
	} else {
		$('#right-pane .content:visible').jScrollPane({animateScroll:true}); //.addTouch();
	}
//	$('#right-pane .subcontent:visible').jScrollPane();	
}

function everest_project_init(type) {
	everest_jump_init();
	everest_subnav_init();
	everest_contentnav_init();
	$(document).ready( function() {
		everest_gallery_init();
	});
	everest_project_switcher_init(type);
	everest_scrollbar_init();
}

