	var lastToggle = false;
	var activeGalleria = -1;
	var curlocation = "";
	var subnavLocation = [];
	var activePage;
	var loading = true;
	
	$(document).ready(function(){
		$('#util-nav li.disclaim div').hide();
		$('#util-nav li.disclaim').hoverIntent(function() {
			$(this).find('div').fadeIn();
		}, function() {
			$(this).find('div').fadeOut();
		});
		
		
		/* WHEN SUBNAV IS CLICKED ON INSIDE PAGE */
		$('.subnav a').click(function(){
			if (subnavLocation[activePage]) {
				subnavLocation[activePage].removeClass('selected');
			}
			subnavLocation[activePage] = $(this);
			$(activePage + ' .subContent').html($('#content-page-'+$(this).attr('rel')).html());
			scrollMe();
			
			
			$(this).addClass('selected');
			if ($(this).attr('img') == "") {
				if (!loading)
					$(activePage + ' > .subImage').fadeOut();
				else
					$(activePage + ' > .subImage').hide();
			}
			else {
				$(activePage + ' > .subImage').attr('src',$(this).attr('img'));
				$(activePage + ' > .subImage').fadeIn();
			}
			
			
			
			/* COUNT NO OF IMAGES TO TRIGGER SLIDESHOW */
			var imgCountStr = $('#content-pane .slideshow img').size();
			var imgCountNum = parseFloat(imgCountStr);

			if ( imgCountNum != "1" && imgCountNum!="0" ) {
				$('#content-pane .slideshow').cycle({ 
					fx: 'fade',
					next: ".slide-next",
					prev: ".slide-prev",
					timeout: 8000
				});
				$('#content-pane .slideshow-nav').show();
				
			} else {
				$('#content-pane .slideshow-nav').hide();
			}

		});

		$('a.page').click(function(){

			activePage = $(this).attr('href');
			
			var obj = $('<a href="' + $(this).attr('href') + '"></a>').fancybox({width: 870, height: 535, autoDimensions: false, padding: 0, centerOnScroll: false});
			obj.click();
			if (activeGalleria == -1) {
				openPage('recent');
			};
			$(activePage + " .subnav > li:first-child > a").click();
			
		});				
		//Galleria.loadTheme('/assets/themes/classic/galleria.classic.min.js');

		
		 
		$('#thumbnail_button').click(function(){
			if ($('.galleria-thumbnails-container').css("display") == "block") {
				$('#thubmnailIconArea').animate({height:23},450);
				$(this).text('view thumbnails');
			} else {
				$('#thubmnailIconArea').animate({height:100},430);
				$(this).text('hide thumbnails');
			}
			
			
			$('.galleria-thumbnails-container').toggleClass('galleria-carousel',Galleria.get(activeGalleria)._carousel.max > $('.galleria-thumbnails-container').width());
			$('.galleria-thumbnails').width($('.galleria-thumbnails-container').width());
			$('.galleria-thumbnails-container').slideToggle(400,function(){
				if ($('.galleria-thumbnails-container').css('display') == "block") {
					//$('.galleria-thumbnails').width($('.galleria-thumbnails-list').width());
					
					Galleria.get(activeGalleria).updateCarousel();
				//	$('.galleria-thumb-nav-left').html('<span>&nbsp;</span>');
				//	$('.galleria-thumb-nav-right').html('<span>&nbsp;</span>');
				}
			});
		});

		$(window).resize(function() {
			if (lastToggle) { lastToggle = false; return; }
			var display = $('.galleria-thumbnails-container').css('display');
			var currentIndex = Galleria.get(activeGalleria).getIndex();
			$('#photoArea').html($('#loadedPhotos').html());
			$('#photoArea').galleria({maxScaleRatio: 1, thumbFit: true, thumbCrop: false, transition: 'fade', transitionSpeed: 200, showCounter: false, show: currentIndex, showInfo: true});
			activeGalleria++;
			Galleria.get(activeGalleria).bind("image",function(){
				$('.galleria-images .galleria-image > img').each(function(){
					if ($(this).css('opacity') == 1) {
						$('.galleria-info').css('left',$(this).position().left+25);
						$('.galleria-info').css('top',$(this).position().top+25);
					}
				});
			});
			$('.galleria-thumbnails-container').css('display',display);
			if (display == "block")
				$('.galleria-thumbnails').width($('.galleria-thumbnails-list').width());
			lastToggle = true; // prevent duplicate triggering
		});
		
		// handle hash tag
		if (window.location.hash.substring(1) != "") {
			var hashString = window.location.hash.substring(1);
			if (hashString.indexOf('/') != -1) { //subpage
				//alert(pathway);
				var pathway = hashString.split('/');
				$('a[href="#'+pathway[0]+'"]').click();
				$('a[ref="'+pathway[1]+'"]').click();
			}
			else {	
				//alert(hashString);
				$('a[href="#'+hashString+'"]').click();
			}
		}
		else
			openPage('recent');
		
		loading = false;
	});

	function highlight(pageName) {
		$('a[href=#'+curlocation+']').removeClass('selected');
		curlocation = pageName;
		$('a[href=#'+curlocation+']').addClass('selected');
	}
	
	function openPage(href) {
		//console.log("function openPage called");
		if (activeGalleria == -1)
			firstLoad = true;
		else {
			var display = $('.galleria-thumbnails-container').css('display');
		}
		$('#thubmnailIconArea').animate({height:23},450);
		$('#thumbnail_button').text('view thumbnails');
		$('.galleria-thumbnails-container').hide();
		highlight(href);
		
		//console.log("load attempted on " + "/"+href+"/"+screen.height+"/"+screen.width);
		
		$('#photoArea').load("/"+href+"/"+screen.height+"/"+screen.width, null, function(response){
			
			if (href!="recent") {
				$('#promotionLink').load('/pages/promotion_link/' + href);
			}
			$('#loadedPhotos').html($('#photoArea').html());

			$('#photoArea').galleria({maxScaleRatio: 1, thumbFit: false, thumbCrop: false, transition: 'fade', transitionSpeed: 200, showCounter: false, showInfo: true});
			activeGalleria++;

			if (firstLoad)
				Galleria.get(activeGalleria).bind(Galleria.READY,function(){
					$('.galleria-thumbnails-container').hide();
				});
			else {
				$('.galleria-thumbnails-container').css('display',display);
			}
			
			Galleria.get(activeGalleria).bind("image",function(){
				$('.galleria-images .galleria-image > img').each(function(){
					if ($(this).css('opacity') == 1) {
						$('.galleria-info').css('left',$(this).position().left+25);
						$('.galleria-info').css('top',$(this).position().top+25);
					}
				});
			});
			$('.galleria-thumbnails').width($('.galleria-thumbnails-list').width());
		});
	}
	
	
	
	
	function scrollMe() {	
		$('#content-pane .txt').jScrollPane({
			showArrows:true,
			scrollbarWidth: 16
		});
	}
			
