/* Javascript for homepage */

$(document).ready(function() {

	adjustWindowSize();
	$("#div_starboard").hide();
	/* $('#advisory').delay(11000).fadeOut(1000); */
	
	var wmdimages = $('#imagerefs');
	var total_pictures = $('#imagerefs img' ).length;  
	var next_picture_index = randomFromTo(1, total_pictures)
		
	/* starts the slide rotation */		
	var ss_int = setInterval(function(){xfade();},9000);
		
	function xfade() {
	
		
		if( $('#div_port').is(":visible") ) {
		  // port is visible
		
		  // prepare starboard
			 //replace img in div_starboard
			var new_src = $('#imagerefs > img:nth-child('+next_picture_index+')').attr('src');
			var new_alt = $('#imagerefs > img:nth-child('+next_picture_index+')').attr('alt');
			var new_title = $('#imagerefs > img:nth-child('+next_picture_index+')').attr('title');
			$('#starboard_image').attr('src',new_src);
			$('#starboard_image').attr('alt',new_alt);
			$('#starboard_image').attr('title',new_title);
			$('#starboard_title').text(new_title);
			
			//cross-fade
	        $("#div_starboard").fadeIn(1000);
	        $("#div_port").fadeOut( 1000 );					
		}
		else {
		  // element is not visible
		  // starboard is visible
			 //replace img in div_port
			var new_src = $('#imagerefs > img:nth-child('+next_picture_index+')').attr('src');
			var new_alt = $('#imagerefs > img:nth-child('+next_picture_index+')').attr('alt');
			var new_title = $('#imagerefs > img:nth-child('+next_picture_index+')').attr('title');
			$('#port_image').attr('src',new_src);
			$('#port_image').attr('alt',new_alt);
			$('#port_image').attr('title',new_title);
			$('#port_title').text(new_title);
			
			//cross-fade
	        $("#div_port").fadeIn(1000);
	        $("#div_starboard").fadeOut(1000 );				
		}	
							
		if (next_picture_index == total_pictures)	{
		next_picture_index = 1;
		} else {
		next_picture_index++;
		}

				
	} //end function xfade

	//function to replace text in object
	jQuery.fn.toggleText = function (value1, value2) {
	    return this.each(function () {
	        var $this = $(this),
	            text = $this.text();
	 
	        if (text.indexOf(value1) > -1)
	            $this.text(text.replace(value1, value2));
	        else
	            $this.text(text.replace(value2, value1));
	    });
	};
	
	//operate the show/hide album list button				
	$(".btn-slide").click(function(){
			if ($("#profile").is(":visible")) {			
				$('#btn-profile-show').toggleText("Hide Profile","About Bill");			
				$('#profile').slideToggle('slow', function() {				
					$(".btn-slide").toggleText("Hide", "Show");
					$("#select-area").slideToggle("slow"); 				
					}
				);			
			}
			else
			{
			$(".btn-slide").toggleText("Hide", "Show");
			$("#select-area").slideToggle("slow"); 
			}
	});
	
	//close the IE advisory				
	$("#btn-close-advisory").click(function(){		
		$('#advisory').hide();	    
	});

	//close the profile window				
	$("#btn-close-profile").click(function(){		
		$('#btn-profile-show').toggleText("Hide Profile","About Bill");			
		$('#profile').slideToggle('slow');	        
	});
	
	//show the profile window
	$("#btn-profile-show").click(function(){
				
		if ($("#select-area").is(":visible")) {				
			$(".btn-slide").toggleText("Hide", "Show");
			$("#select-area").slideToggle("slow",function(){
				$('#btn-profile-show').toggleText("Hide Profile","About Bill");			
				$('#profile').slideToggle('slow');	    
				}
			); 
		} else {	
		$('#btn-profile-show').toggleText("Hide Profile","About Bill");			
		$('#profile').slideToggle('slow');
		}	    
	});
	
function adjustWindowSize() {
	var winW = 630, winH = 460;
	
	if (document.body && document.body.offsetWidth) {
		 winW = document.body.offsetWidth;
		 winH = document.body.offsetHeight;
	}
	
	if (document.compatMode=='CSS1Compat' &&
		    document.documentElement &&
		    document.documentElement.offsetWidth ) {
		 winW = document.documentElement.offsetWidth;
		 winH = document.documentElement.offsetHeight;
	}
	
	if (window.innerWidth && window.innerHeight) {
	 winW = window.innerWidth;
	 winH = window.innerHeight;
	}
	
	var newAvailWinW = winW
	var newAvailWinH = winW * (960/1450);
	if (newAvailWinH > winH) {
		newAvailWinW = winW * (winH/newAvailWinH);
		newAvailWinH = newAvailWinW * (960/1450);
	}
	newAvailWinH = Math.floor(newAvailWinH);
	newAvailWinW = Math.floor(newAvailWinW);
	

	upHeightBy = newAvailWinH - winH;
	upWidthBy = newAvailWinW - winW;
	
	window.resizeBy(upWidthBy,upHeightBy);
	}




});  //end of the Ready area
	

		

function changeScreenSize(w,h) {
	window.resizeTo(w,h);
	}
	
function randomFromTo(from, to){
       return Math.floor(Math.random() * (to - from + 1) + from);
    }


