// front-end javascripts go here

jQuery(function($) {
	
	jQuery("#gallery > ul > li:first").css("display", "block");
	
	jQuery("li.photo a").click(function(event) {
		event.preventDefault();
		jQuery("#gallery > ul > li").each(function() {
//			jQuery(this).css("display", "none");
			jQuery(this).fadeOut(500);
			jQuery(this).children("img").removeClass("visible_image");
		});
//		jQuery("#gallery > ul > li").eq(jQuery("ul.gallery a").index(jQuery(this))).css("display", "block");
		jQuery("#gallery > ul > li").eq(jQuery("ul.gallery a").index(jQuery(this))).children("img").stop().addClass("visible_image");
		jQuery("#gallery > ul > li").eq(jQuery("ul.gallery a").index(jQuery(this))).fadeIn(500);
		set_visible_image_dimensions();
	});


	jQuery(".image_info_col_w_border > a.hide_button").click( function(event) {
		event.preventDefault();
		jQuery(this).parent().parent().parent().children("div[class^='image_info']").animate({height: 9	},250, "linear");
		var $this = jQuery(this);
		setTimeout(function() {$this.parent().children(".show_button").show(250);}, 250);
	});
	
	jQuery("a.show_button").click( function(event) {
		event.preventDefault();
		jQuery(this).parent().parent().parent().children("div[class^='image_info']").stop();
		jQuery(this).parent().parent().parent().children("div[class^='image_info']").animate({height: 36},250, "linear");
		jQuery(this).hide();
	});
	
	jQuery("#submenu > ul").mousewheel(function(event, delta) {
		min_y = get_gallery_min_y();
		max_y = 0
		current_top = parseInt(stripPx(jQuery("#submenu > ul").css("top")));
		scroll_amt = 10*delta;
		scroll_to = current_top + scroll_amt
		if (scroll_to > 0)
			scroll_to = 0;
		if (scroll_to < min_y)
			scroll_to = min_y;
		jQuery("#submenu > ul").css("top", scroll_to);
	});
	
	jQuery("#gallery_up, #gallery_down").hover(function() {
		jQuery(this).animate({opacity: .85}, 600);
	}, function() {
		jQuery(this).animate({opacity: .01}, 600);
	});

	jQuery("#gallery_up").hover(function() {
		jQuery("#submenu > ul").animate({top: 0}, get_gallery_scroll_time());
	}, function() {
		jQuery("#submenu > ul").stop();
	});
	
	jQuery("#gallery_down").hover(function() {
		min_y = get_gallery_min_y();
		jQuery("#submenu > ul").animate({top: min_y}, get_gallery_scroll_time());
	}, function() {
		jQuery("#submenu > ul").stop();
	});
	
	jQuery("#js_link").click( function(event) {
		event.preventDefault();
		alert(get_gallery_scroll_time());
	});
	
	jQuery("body").css({overflow: "hidden"});
	set_right_dimensions();
	set_up_images();
	jQuery("#submenu").load(function() {
		reset_gallery_menu();
	});
	
});

  function set_right_dimensions() {
    var window_height = jQuery(window).height();
    var window_width = jQuery(window).width();
    var right_width = window_width - jQuery("#left").width(); // #left width
		var header_padding = parseInt( stripPx( jQuery("#header").css("padding-top") ) );
		var submenu_height = window_height - jQuery("#header").height() - header_padding - 1;
    jQuery("#right").width(right_width);
    jQuery("#right").height(window_height); // safety
		jQuery("#submenu").height(submenu_height);
  }

	function set_visible_image_dimensions() {
		var $r = jQuery("#right");
		right_w_to_h = $r.width() / $r.height();
		var $vi = jQuery(".visible_image:first");
		$vi.css({width: "", height: ""});
		w_to_h = $vi.width() / $vi.height();
		if (w_to_h > right_w_to_h) {
			$vi.width($r.width());
			$vi.height($vi.width() / w_to_h);
		} else {
			$vi.height($r.height());
			$vi.width($vi.height() * w_to_h);
		}
	}

	function set_up_images() {
		if (jQuery("#gallery > ul.gallery > li:first").children("img:first-child")[0])	 {
			if (jQuery("#gallery > ul.gallery > li:first").children("img:first-child")[0].complete) {
				jQuery("#gallery > ul.gallery > li:first").children("img:first-child").addClass("visible_image");
				set_visible_image_dimensions();
				jQuery("#gallery > ul.gallery > li > img").each( function() {
					jQuery(this).parent().css("display", "none");
					jQuery(this).parent().css("visibility", "visible")
				});
				jQuery("#gallery > ul.gallery > li:first").fadeIn(1000);
			} else {
				setTimeout("set_up_images();", 100)
			}
		}
	}

	function set_image_dimensions() {
		// for each image in the gallery, see whether it should be set to width: 100% or height 100%
		// need to calculate the ratio of width to height for #right
		// then, if an image has a w to h larger, make height = 100%
		// if smaller, make width = 100%
		
		right_w_to_h = jQuery("#right").width() / jQuery("#right").height();
		jQuery("#gallery > ul.gallery > li > img").each( function() {
			w_to_h = jQuery(this).width() / jQuery(this).height();
			if (w_to_h > right_w_to_h) {
				jQuery(this).css("width", "100%");
				jQuery(this).css("height", "auto");
			} else {
				jQuery(this).css("width", "auto");
				jQuery(this).css("height", "100%");
			}
			jQuery(this).parent().css("display", "none");
			jQuery(this).parent().css("visibility", "visible")
		});
		
		jQuery("#gallery > ul.gallery > li:first").fadeIn(1000);
		jQuery("#gallery > ul.gallery > li:first").children("img").addClass("visible_image");
		
	}

	function calc_gallery_height() {
		calc_height = parseInt( stripPx ( jQuery("#submenu > ul.gallery").css("padding-top") ) ) * 2;
		jQuery("#submenu > ul > li").each( function() {
			calc_height = calc_height + jQuery(this).height();
		});
		return calc_height;
	}

	function get_gallery_min_y() {
		sidemenu_height = jQuery("#submenu").height();
		gallery_height = calc_gallery_height();
		min_y = 0;
		if (gallery_height > sidemenu_height) {
			min_y = sidemenu_height - gallery_height;
		}
		return min_y;
	}
	
	function get_gallery_scroll_time() {
		// I don't know what works... 200 px / sec?
		scroll_factor = -5;
		return get_gallery_min_y() * scroll_factor;
	}

	// IE chokes on parseInt() if there is a "px" on the end of the string
	// this strips that off so we can convert the value returned by a jQuery().css() call to
	// a number for calulations 
	function stripPx(px_str) {
		return px_str.substr(0, px_str.length - 2);
	}

	function reset_gallery_menu() {
		var min_y = get_gallery_min_y();
		var max_y = 0;
		var top = parseInt( stripPx( jQuery("#submenu > ul").css("top") ) );
		if (top > max_y) {
			top = max_y;
		}
		if (top < min_y) {
			top = min_y;
		}
		jQuery("#submenu > ul").css("top", top + "px");
		if (jQuery("#submenu").height() <= calc_gallery_height()) {
			jQuery("#gallery_up, #gallery_down").css({display: "block"});
		} else {
			jQuery("#gallery_up, #gallery_down").css({display: "none"});
		}
	}

  window.onresize = function() { 
		set_right_dimensions(); 
		set_visible_image_dimensions();
		reset_gallery_menu();
	};