$(document).ready(function(){

//------------------------------------------------------------------------------------------------//
//define var for currentSection
var currentURL = location.pathname.substring(1); //get URL of current page
var lastSlashPos = currentURL.lastIndexOf('/'); //determine location of last slash - filename start
var lastDotPos = currentURL.lastIndexOf('.') //determine location of last dot - filename extension start
var currentSection = currentURL.slice(lastSlashPos+1,lastDotPos); //extract just filename without ext == section name
//------------------------------------------------------------------------------------------------//

//------------------------------------------------------------------------------------------------//
function onm_edit_mode_css(){

	if($('#admin_logout').attr('id') != undefined){
	
		//alert('admin logout is not undefined');
	
		$('.project_main_div').css({
	
		'height':'700px'
		
		}); //make sure height is no longer restricted on editable pages
		
		
		$('.body_child_div').css({
	
		'height':'700px'
		
		}); //make sure height is no longer restricted on editable pages
		
	
	};//end if($('#admin_login') != false)
	
	
/*
$('#admin_login').click(function(){ 
	
	var currentProtocol = location.protocol;
	
	//alert(currentProtocol);
	
	if(currentProtocol != 'https:'){
	
		location.protocol = 'https:';
		
	}; //end if(currentProtocol == 'http:')
	
	$('#admin_login form').show();	
		
});//end $('#admin_login').click*/

};//end function onm_edit_mode_css()


onm_edit_mode_css();
//------------------------------------------------------------------------------------------------//

//------------------------------------------------------------------------------------------------//
//This function highlights the current page link in hardcoded style and makes sure its parent submenu stays open
var currentLinkID = '#' + currentSection + '_link'; //derive link ID from filename

//Target element
var currentLink = $(currentLinkID); //select current link

var currentLinkColor = '#000000';

currentLink.css({
	
	'color':currentLinkColor

}); //set current link style

$('.active_link').css({
	
	'color':currentLinkColor

}); //set current link style



//currentLink.css('fontWeight','bold'); //set current link style

//------------------------------------------------------------------------------------------------//


//------------------------------------------------------------------------------------------------//
//LINK HELPER CODE
$('#link_helper_form').submit(function() {

	
	var linkText = $('#link_text').attr('value');
	var linkHref = $('#link_href').attr('value');
	
	var linkHtml = '<a href="'+linkHref+'">'+linkText+'</a>';
	  
	$('#link_helper_answer').text(linkHtml).html();
	  
	return false;
});
//------------------------------------------------------------------------------------------------//

//------------------------------------------------------------------------------------------------//
//IMAGE HELPER CODE
$('#image_helper_form').submit(function() {

	
	var imageText = $('#img_alt').attr('value');
	var imageSrc = $('#img_src').attr('value');
	
	var imageHtml = '<img src="'+imageSrc+'" alt="'+imageText+'" />';
	  
	$('#image_helper_answer').text(imageHtml).html();
	  
	return false;
});
//------------------------------------------------------------------------------------------------//


//------------------------------------------------------------------------------------------------//
//PULL IN BACKGROUND IMAGE FROM DATABASE AND APPLY CSS
//background-image: url($simpleSrcString);

var imageSrcString = $('#index_bg span').attr('class');

$('#index_content').css({
	
	
	'background-position': '0px 100px',
	'background-repeat': 'no-repeat',
	'position': 'relative',
	'background-image': 'url('+imageSrcString+')'

}); //end $('#index_content').css

//------------------------------------------------------------------------------------------------//

//------------------------------------------------------------------------------------------------//
var imageIndex; //index of currently active viewer image
var imagePath; //current small image src attribute (called from mouseover on small images)
var fileName;

function onm_current_image_parameters(){ //imagePath is the path of the desired large image to inspect, sets above variables so they can be accessed from other functions
	
	if(imagePath != undefined){ //as in first page load!!
	
		//var lastSlash = imagePath.lastIndexOf('/'); //determine location of last slash - filename start
		//var lastDot = imagePath.lastIndexOf('.') //determine location of last dot - filename extension start
		//var fileName = imagePath.slice(lastSlash+1,lastDot); //extract just filename without ext
		fileName = imagePath;
		
		//alert(fileName); //debug step
		
		lastUnscor = fileName.lastIndexOf('_'); //determine location of last underscore - _s.jpg start
		
		fileName = fileName.slice(0,lastUnscor);
		
		//alert(fileName); //debug step
		
		lastUnscor = fileName.lastIndexOf('_'); //determine location of last underscore - index start
		//lastDot = fileName.lastIndexOf('.') //determine location of last dot - filename extension start
		imageIndex = fileName.slice(lastUnscor+1); //extract just index
		
		//alert(imageIndex); //debug step
		

	}; //end if(imagePath != undefined)
	
}; //end function onm_current_image_parameters()
//!!!NOTE FOR THE FUTURE - IE6 IN WINDOWS DOES NOT EXECUTE GET REQUEST LOCALLY AND CODE WILL FAIL - UPLOAD AND ALL IS WELL!!!
//------------------------------------------------------------------------------------------------//


//------------------------------------------------------------------------------------------------//
//VIEWER IMAGE SWAPPING CODE
$('#photo_viewer img').hide();// hide all viewer images at page load
$('#photo_viewer img:first').show(); // show first image in div
$('#photo_viewer form').hide();
$('#photo_viewer form:first').show();


$('#photo_thumbs img').mouseover(function(event){ //selector must define event handler on elements existing at document load, event object passes the image mouse event is triggered on

		var target = $(event.target);
		
		if(target.attr('id') != undefined){ //unless the target has no attribute "id"
			
			imagePath = target.attr('id'); //full path of image
			
			onm_current_image_parameters();
			
			$('#photo_thumbs img').css({ //make sure all image borders are default
				
				'border-color':'#ffffff'
			
			}); // end $('#photo_thumbs img').css
			
			target.css({ //active image thumb style
				
				'border-color':'#000000'
			
			}); // end $('#photo_thumbs img').css
			
			//var photoViewerHtml = '<img id="photo_viewer_image" src="images/image_' + imageIndex + '.jpg" alt="image_' + imageIndex + '" width="1000" height="590" />';
			
			//$('#photo_viewer').html(photoViewerHtml);
			
			$('#photo_viewer img').hide();
			$('#photo_viewer form').hide();

			$('#' + fileName).show();
			$('#image_upload_form_' + imageIndex).show();
			
			
		}; //end if(target.attr('src') != undefined && $('#thumbs_table').attr('class').indexOf('ui-draggable-dragging') == -1)
		
	});//end $('#image_viewer_thumbs img').mouseover(function(event)
//------------------------------------------------------------------------------------------------//

//------------------------------------------------------------------------------------------------//
//INDEX VIEWER IMAGE SWAPPING CODE
var fadeInterval = false;//initial outside desclaration so we can disable on admin login above

function onm_index_image_swapper(){ //selector must define event handler on elements existing at document load, event object passes the image mouse event is triggered on



	var lastImageID = $('#photo_viewer_index div:last').attr('id');
	//alert(lastImageID);
	
	lastUnscor = lastImageID.lastIndexOf('_'); //determine location of last underscore - index start
	var e = lastImageID.slice(lastUnscor+1); //extract just index - this is how many index images we have
	
	$('#photo_viewer_index div').hide();// hide all viewer images at page load
	$('#photo_viewer_index div:first').fadeIn(1500); // show first image in div
	
	var i = 0;
	var fadeSpeed = 2000;
		
		function onm_crossfade(){
			
			
			/*if(i==0){
				$('#index_image_'+e).hide();
			}
			else{*/
			
			$('#index_image_'+i).fadeOut(fadeSpeed);
			
			//};//end if(i==0)
			
			
			
			if(i==e){
			$('#index_image_'+0).fadeIn(fadeSpeed);
			}
			else
			{
			$('#index_image_'+(i+1)).fadeIn(fadeSpeed);
			
			};//end if(i==e)
			
			if(i == e){
				
				i = 0;
				
			}
			else{
			
				i = i+1;
				
			};//if(i == e)
			
			
			
			
			
		};//end function onm_crossfade(i)
		
		fadeInterval = setInterval(onm_crossfade, 5000);
	
};//end function onm_index_image_swapper()

if($('#photo_viewer_index div').attr('id') != undefined){

	onm_index_image_swapper();
	
};

//------------------------------------------------------------------------------------------------//

//------------------------------------------------------------------------------------------------//
//PUBLICATIONS ARROWS AND SCROLLING
if($('#navigation_links_publications').attr('id') != undefined){

	var publications_inner_height = $('#navigation_links_publications').height();
	var publications_outer_height = $('#navigation_links_publications_outer').height();
	
	
	//alert('middle element :' + publications_inner_height + ' outer element: ' + publications_outer_height + ' innermost element: ' + publications_innermost_height);//debug
	
	
	var list_item_height = $('#navigation_links_publications_ul li:last').height();
	var last_list_item = $('#navigation_links_publications_ul li:last').position().top + list_item_height;
	
	//alert(list_item_height);
	var offset_y = publications_inner_height - last_list_item;
	var current_inner_offset_text;
	var current_inner_offset;
	
	function onm_current_inner_offset_publications(){
		
		current_inner_offset_text = $('#navigation_links_publications_ul').css('top');
		current_inner_offset = parseInt(current_inner_offset_text);
		
	};//end function onm_current_inner_offset_publications()
	
	//alert(current_inner_offset);
	
	$('#down_arrow').click(function() {
		
		onm_current_inner_offset_publications();
		
		
		
		$('#navigation_links_publications_ul').css({
		
			'top': current_inner_offset - list_item_height
	
		});
		
		
		if(current_inner_offset < offset_y){
			$('#down_arrow').css({
			
				'visibility':'hidden'
			
			});
		};
	
		$('#up_arrow').css({
	
			'visibility':'visible'
		
		});//end if(current_inner_offset < offset_y)
		
		
	});
	
	$('#up_arrow').click(function() {
	
		onm_current_inner_offset_publications();
	
		$('#navigation_links_publications_ul').css({
		
			'top': current_inner_offset + list_item_height
	
		});
		
		if(current_inner_offset > 0){
			$('#up_arrow').css({
			
				'visibility':'hidden'
			
			});
		};//end if(current_inner_offset > 0)
		
		$('#down_arrow').css({
	
			'visibility':'visible'
		
		});
	
	});
	
	function onm_adjust_publication_links_offset(){
		
		current_link_height = currentLink.height();
		current_link_position = currentLink.position().top + current_link_height;
		
		//alert(' current_link_position: ' + current_link_position +' publications_inner_height: ' + publications_inner_height);
		
		if(current_link_position > publications_inner_height){//if link is hidden when page loads
		
			
			$('#navigation_links_publications_ul').css({
		
			'top': publications_inner_height - current_link_position
	
			});
			
			/*$('#down_arrow').css({
			
				'visibility':'hidden'
			
			});*/
			
			$('#up_arrow').css({
			
				'visibility':'visible'
			
			});
	
			
		};//end if(current_link_position >= publications_inner_height)
	
	};//end function onm_adjust_publication_links_offset()
	
	
		
		//alert('link offset function executing!');
		onm_adjust_publication_links_offset();

};//end if($('#navigation_links_publications').attr('id') != undefined)*
	
//------------------------------------------------------------------------------------------------//

}); //end $(document).ready
