﻿$(document).ready(function(){
	/*=====================================
	General
	=====================================*/
	$("#copylink").attr("href",$("#ouhook").attr("href")); //get link from ouhook, put it on copylink
	$("#ouhook").parents("p").remove(); //get rid of ouhook
	$(".iframe").colorbox();
	
	/*===== Shortens banner on non-index pages =====*/
	/*path = window.location.pathname;
	pathfile = path.substring(path.lastIndexOf('/') + 1).toLowerCase();
	if (pathfile == "index.html" || pathfile == "") { // if we're at index page, replace short banner with full size one
		$("#banner img").attr("src", $("#banner img").attr("src").replace("_s",""));
		$("#banner").css("height","187px");
	}*/
	
	/*===== Adds activesection class to currently browsed page =====*/
	var url = window.location.href; //select active section (based on window url) and set class for corresponding anchor
	var noprotocol = url.split('//')[1];
	var slasharr = noprotocol.split('/');
	var htmlname = slasharr[slasharr.length-1];
	if(htmlname == '') { htmlname = 'index.html'; }
		$('#navlist li a').each(function(){
			var thisarr = $(this).attr('href').split('/');
			var thishref = thisarr[thisarr.length-1];
			if(htmlname == thishref) { $(this).addClass('activesection'); }
		});
	
	
	/*===== Adds previous/next arrows =====*/
	var nexthref = $('.activesection').parent().nextAll('li:has(a)').children('a').attr('href');
	var prevhref = $('.activesection').parent().prevAll('li:has(a)').children('a').attr('href');
	
	if($("#this_maintext").length) {$("#navtextphotos").append("<div id='arrows'></div>");}
	if($("#this_maintext_nophoto").length) {$("#this_maintext_nophoto").append("<div id='arrows'></div>");}	
	
	if (prevhref) $("#arrows").append("<a style='float:left;' href='"+prevhref+"'>&lt;&nbsp; Previous</a>");
	if (nexthref) $("#arrows").append("<a style='float:right;' href='"+nexthref+"'>Next &nbsp;&gt;</a>");

	/*=====================================
	Case/Text Page textarea
	=====================================*/
	/*$("#this_maintext p").each(function(){ //this is just a way to make first-letter/first-line CSS peudo selectors work in IE6
		$(this).hide()
		var t = $(this).text();
		t = "<div class='firstp'>" + t + "</div>";
		$(this).replaceWith(t).show();
	});*/
	
	/*=====================================
	Lightbox photos
	=====================================*/
	$("#this_picturearea td").each(function(){ //transform table to something we can use
		if($(this).children("img").length) { //if it's an image, get the src, chop off the _thumb part, wrap it in a link with lightbox rel, then wrap it in a div. Append the whole thing to the picturearea div
			href = $(this).children("img").attr("src").replace("_thumb","");
			$(this).children("img").wrap(document.createElement("a"));
			$(this).children("a").attr("href",href).appendTo("#this_picturearea").addClass("photos").wrap(document.createElement("div"));
		} else { //if it's not an image (i.e. it's a caption), just append it to picturearea div and wrap it in a caption class div.
			$(this).appendTo("#this_picturearea");
			var h = $(this).html();
			h = "<div class='caption'>" + h + "</div>";
			$(this).replaceWith(h);
		}// this has the added benefit that, were the user to wish more spacing between images, she could simply leave blank tds (which would create an empty caption div whose margin can be controlled via local.css
	});
	$("#this_picturearea td:last").parents("table").remove(); //destroy table once we're done
	$("#this_picturearea").wrapInner("<div id='pictureandcaptions'></div>"); //container div
	$.fn.colorbox.settings.transition = "fade";
	$.fn.colorbox.settings.bgOpacity = "0.7";
	$(".photos").colorbox();
	
	/*=====================================
	Gallery
	=====================================*/
	$('.textarea').hide();
	$('.ox_editable_region').parents('.textarea').show(); //show if we're in omniupdate **for editing purposes only**
	$('.ox_regioneditbutton').parents('.textarea').show(); //show if we're in omniupdate **for editing purposes only**
	if($("#gall").length) {
		$("#navtextphotos").css({"padding":"5px 0px 40px 0px"});
		$("#gall tr").each(function(){
			$(this).children("td:first").each(function() { 
				t = $(this).children("img").attr("alt"); //take alt from image
				$(this).children("img").attr("title",t); //make it title (appears on mouseover)
				href = $(this).children("img").attr("src").replace("_thumb",""); //get src (w/o thumb)
				h = $(this).siblings("td:first").html();
				$(this).children("img").attr("alt",h); // set captions to alt tag (replaces info that was used for title)
				$(this).children("img").wrap(document.createElement("a")); //setup gallery list
				$(this).children("a").attr("href",href);
				$(this).children("a").wrap(document.createElement("li"));
				$(this).children("li").addClass("galleryli").appendTo("#galleryd");
				//$(".galleryli:first").addClass("active");
			});
		});
		$(".galleryli").wrapAll(document.createElement("ul")).parents("ul").addClass("jgallery_unstyled");
		$("#gall").remove();
		$('.jgallery_unstyled').addClass('jgallery'); // adds new class name to maintain degradability
		
		$('.jgallery').prepend('<h1 id="galleryheader">Gallery</h1>'); // adds gallery header and menu items for information, about, and rights and reproductions
		$('.jgallery').prepend('<h1 class="galmenu" id="media">Media Files</h1>');
		$('.jgallery').prepend('<h1 class="galmenu" id="rights">Rights &amp; Reproductions</h1>');
		$('.jgallery').prepend('<h1 class="galmenu" id="about">About this Exhibit</h1>');
		$('.jgallery').prepend('<h1 class="galmenu" id="intro">Introduction</h1>');
		$('.jgallery > .galmenu').css("cursor","pointer");
		$('.jgallery > #galleryheader').css("cursor","pointer");
		
		$('ul.jgallery').galleria({
			history   : false, 
			clickNext : true, 
			insert    : undefined,
			onImage   : function(image,caption,thumb) {				
			image.css('display','none').fadeIn(1000);
			caption.css('display','none').fadeIn(500);
			var _li = thumb.parents('li');
			_li.siblings().children('img.selected').fadeTo(500,0.3);
			thumb.fadeTo('fast',1).addClass('selected');
			image.attr('title','Next image >');
		},
		onThumb : function(thumb) {
			var _li = thumb.parents('li');
			var _fadeTo = _li.is('.active') ? '1' : '0.2';
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.2); }
			)
		}
		});
		//$("#introtext p:first").css("margin-top","40px");
		$(".galleria_wrapper").html($("#introtext").html()).show();
		
		$('.galmenu').click(function(){
			//$(this).hide(250);
			var textid = "#" + $(this).attr('id') + "text";
			$(".galleria_wrapper").html($(textid).html()).show();
			$(".jgallerycaption").text('');
			$(".active img").animate({"opacity":0.2}, "slow");
			$(".active").removeClass("active");
			$(".iframe").colorbox();
			//firstClick = true;
		});
		
		$('#galleryheader').click(function(){
			firstsrc = $(".galleryli:first > img").attr("rel");
			$.galleria.activate(firstsrc);
		});
		$('a[href$="mp3"]').addClass("medialink");
		$('.medialink').flash(
			{ src: 'audio/mp3player.swf', height: 18, width: 300 },
			{ version: 7 },
			function(htmlOptions) {
				$this = $(this);
				htmlOptions.flashvars.file = $this.attr('href');
				$this.before($.fn.flash.transform(htmlOptions));						
			}
		);
	}	
});

