$(function() {
	//var thisPageUrl = document.URL.split('/');
	var thisPageUrl = document.location.pathname.split('/');
	var thisPageName = thisPageUrl.pop();
	thisPageName = thisPageUrl.pop() + '.' + thisPageName;
	thisPageName = escape(thisPageName);

	if ( $("#sectionmenu ul ul").length > 0 ) {
		$("#sectionmenu ul ul").css('display','none').each(function () {
			if (!$(this).parent('li').hasClass('select')) {
				var submenu = $(this);
				var t;
				submenu.parent('li').bind('mouseenter', function () {
					t = setTimeout(function(){submenu.slideDown();}, 750);
				}).bind('mouseleave', function () {
					clearTimeout(t);
				});
			}
		});
		$("#sectionmenu ul .select ul").css('display','block');
	}
	
	if ( $("#tabs").length > 0 ) {
		$("#tabs a").click(function() {
			$("#tabs li").removeClass('selected');
			var loc = $(this).attr('href');
			createCookie(thisPageName+'.tabLoc', loc, 0); // used to maintain state
			// check for inner link
			if (loc.charAt(0) == '#') { // internal link
				$('.pubcat').hide();
				$(loc).show();
			} else { // external link
				$('#tabloader_wrap').load(loc + ' ##text>*');
			}
			$(this).parent('li').addClass('selected');
			return false;
		});

		// load last used tab or first tab
		var tabLoc = '';
		var target = document.location.search;
		if (target) {
			var pathname = document.location.pathname;
			tabLoc = pathname.substring(0, pathname.length-4) + '-' + target.substring(1) + '.php';
		} else {
			tabLoc = readCookie(thisPageName+'.tabLoc');
		}
		if (tabLoc) {
			var loc = tabLoc;
			$("#tabs li").removeClass('selected');
			$("#tabs a[href='"+loc+"']").parent().addClass('selected');
		} else {
			var loc = $("#tabs a:first").attr('href');
		}
		if (loc.charAt(0) == '#') { // internal link
			$('.pubcat').hide();
			$('.pubcat h3').addClass('reset-margin');
			$(loc).show();
		} else { // external link
			$('#tabloader_wrap').load(loc + ' ##text>*');
		}
	}

	var frameCount = $(".framed").length;
	if ( frameCount > 0 ) {
		var currentFrame = 0;
		
		// hide all
		$(".framed").hide();
		// load last used tab or first tab
		var tabLoc = readCookie(thisPageName+'.tabLoc');
		if (tabLoc) {
			$(tabLoc).show();
		} else {
			$(".framed:first").show();
		}

		// load arrow	nav	
		$('<img id="framed_left" src="/img/arrow2_left.png" alt="&lt;" />').click(function() {
			currentFrame--;
			if (currentFrame < 0) currentFrame = frameCount-1;
			$(".framed").hide();
			$(".thumbs_menu a").removeClass('selected');
			$(".framed:eq("+currentFrame+")").show();
			$(".thumbs_menu a:eq("+currentFrame+")").addClass('selected');
			createCookie(thisPageName+'.tabLoc', '#'+$(".framed:eq("+currentFrame+")").attr('id'), 0); // used to maintain state
		}).appendTo('.framed_wrap');
		$('<img id="framed_right" src="/img/arrow2_right.png" alt="&gt;" />').click(function() {
			currentFrame++;
			if (currentFrame > frameCount-1) currentFrame = 0;
			$(".framed").hide();
			$(".thumbs_menu a").removeClass('selected');
			$(".framed:eq("+currentFrame+")").show();
			$(".thumbs_menu a:eq("+currentFrame+")").addClass('selected');
			createCookie(thisPageName+'.tabLoc', '#'+$(".framed:eq("+currentFrame+")").attr('id'), 0); // used to maintain state
		}).appendTo('.framed_wrap');

		// thumb nav		
		if ( $(".thumbs_menu").length > 0 ) {
			if (tabLoc) {
				$(".thumbs_menu a[href='"+tabLoc+"']").addClass('selected');
			} else {
				$(".thumbs_menu a:first").addClass('selected');
			}
			var thumbId = 0;
			$(".thumbs_menu a").each(function() {
				$(this).attr('rel',thumbId);
				thumbId++;
			}).click(function() {
				var loc = $(this).attr('href');
				$(".framed").hide();
				$(loc).show();
				$(".thumbs_menu a").removeClass('selected');
				$(this).addClass('selected');
				createCookie(thisPageName+'.tabLoc', loc, 0); // used to maintain state
				currentFrame = $(this).attr('rel');
				return false;
			});
		}
	}
	
	if ( $("#slideshow").length > 0 ) {

		// hide all
		$(".slide").hide();
		// load last used tab or first tab
		var tabLoc = readCookie(thisPageName+'.tabLoc');
		if (tabLoc) {
			$(tabLoc).show();
		} else {
			$(".slide:first").show();
		}

		// thumb nav		
		if ( $(".thumbs_menu").length > 0 ) {
			if (tabLoc) {
				$(".thumbs_menu a[href='"+tabLoc+"']").addClass('selected');
			} else {
				$(".thumbs_menu a:first").addClass('selected');
			}
			$(".thumbs_menu a").click(function() {
				var loc = $(this).attr('href');
				$(".slide").hide();
				$(loc).show();
				$(".thumbs_menu a").removeClass('selected');
				$(this).addClass('selected');
				createCookie(thisPageName+'.tabLoc', loc, 0); // used to maintain state
				return false;
			});
		}

	}

});

(function($) {

	$.fn.viewerImgList = function(imagePath, mainImageName, thumbImageName, imageCount) {
	
		return this.each(function(){

			var target = jQuery(this);
			
			var imageList = $('<ul>');
			
			for (i=1; i<=imageCount; i++) {
				var item = $('<li>');
				$('<img src="'+imagePath+thumbImageName+i+'.jpg" alt="'+mainImageName+i+'" />').click(function() {
					$(this).parents('ul').siblings('img').attr('src', imagePath+$(this).attr('alt')+'.jpg');
				}).appendTo(item);
				$(item).appendTo(imageList);
			}
			target.append(imageList);

		});

	}

}) (jQuery);
