//////////////////////////////////////////////////////////////////////
//                                                                  //
//  Site:        Stephen Gomori dot com                             //
//  Description: Javascript file                                    //
//  Last edited: July 26, 2008                                      //
//  Author:      Stephen Gomori                                     //
//  Contact:     webmaster at vividwind dot com                     //
//                                                                  //
//////////////////////////////////////////////////////////////////////

var timer;
var roll_timer;
var fade_timer;
var photo_changing = false;

function prepareForm(){
  
  if (!document.getElementById){
    return;
  }
  
  if (!document.getElementById('contact_form')){
    return;
  }
  
  document.getElementById('message').onkeyup = function(){
    getCharCount();
  }
  document.getElementById('reset').onclick = function(){
    document.getElementById('char_count').innerHTML='Character count: 0, Max: 1500';
  }
  document.getElementById('contact_form').onsubmit = function(){
    return validateForm();
  }
}

function preparePopUps(){

  if (!document.getElementsByTagName('a')){
    return;
  }
  
  var links = document.getElementsByTagName('a');
  
  for (var i=0; i<links.length; i++){
  
   if (links[i].getAttribute('class') == 'popup'){
     links[i].onclick = function(){
       var pop_up = open(this.getAttribute('href'));
	     pop_up.focus;
	     return false;
     }
   }
  }	
}

function prepareNav(){
  
  if (!document.getElementById){
    return;
  }

  if (!document.getElementById('navigation')){
    return;
  }
  
  var links = document.getElementById('navigation').getElementsByTagName('a');
  
  for (var i=0; i<links.length; i++){
    links[i].onmouseover = function(){
      var id = this.getAttribute('class');
      swapImage(id, true);
    }
    links[i].onmouseout = function(){
      var id = this.getAttribute('class');
      swapImage(id, false);
    }
  }
  
  document.getElementById('films').onmouseover = function(){
    flyOut('films_dd', true);
  }
  document.getElementById('films').onmouseout = function(){
    flyOut('films_dd', false);
  }
  document.getElementById('films_dd').onmouseover = function(){
    flyOut('films_dd', true);
  }
  document.getElementById('films_dd').onmouseout = function(){
    flyOut('films_dd', false);
  }
}

// Preload images and flyout menu rollovers.
function loadImages(num_photos){
	
  if (document.images){
		
		var page_array = new Array('home','news','resume','photos','films','contact','links');
		var roll_over = new Array(page_array.length);
		var here_indicator = new Array(page_array.length);
	
		for (var i=0; i<page_array.length; i++){
			roll_over[i] = new Image();
		  here_indicator[i] = new Image();
			
			roll_over[i].src = 'images/' + page_array[i] + '_rollover.jpg';
			here_indicator[i].src = 'images/' + page_array[i] + '_here.jpg';	
		}
		
		var item_bg_rollover = new Image();
		item_bg_rollover.src = 'images/item_bg_rollover.jpg';
  }
}

// Opacity values and height values need to be initialized before script can see them.
function initializeValues(num_photos){
	
	document.getElementById('films_dd').style.height = '0px';
	document.getElementById('films_dd_background').style.opacity = '0.75';
	document.getElementById('films_dd_background').style.filter = 'alpha(opacity=75)';
	document.getElementById('films_dd_background').style.MozOpacity = '0.75';
	document.getElementById('films_dd_background').style.KhtmlOpacity = '0.75';
	
	for (i=1; i<=num_photos; i++){
	  document.getElementById('slide_'+i).style.opacity = 1;
		document.getElementById('slide_'+i).style.filter = 'alpha(opacity=100)';
		document.getElementById('slide_'+i).style.MozOpacity = 1;
		document.getElementById('slide_'+i).style.KhtmlOpacity = 1;
	}
}

// Rollover image swap.
function swapImage(image, hovering){

  if (document.images){ 
    if ((document.getElementById(image).src).indexOf('rollover') == -1 && (hovering)){
	    document.getElementById(image).src = 'images/' + image + '_rollover.jpg';    
	  }else{
      document.getElementById(image).src = 'images/' + image + '.jpg';
	  }
  }
}

// Fade photo for slide show and photos page. When slide show, second photo doesn't fade in. Just first one fades out.
function fadePhoto(photo, next_photo, type, rate){
	
  var current = document.getElementById(type+photo);
	var next = document.getElementById(type+next_photo);
	var current_opacity = parseFloat(current.style.opacity);
	var next_opacity = parseFloat(next.style.opacity);
	var fade_rate = 0.05;
	
	if (current_opacity >= fade_rate){
	  current.style.opacity = current_opacity - fade_rate;
		current.style.filter = 'alpha(opacity='+((current_opacity - fade_rate)*100)+')';
		current.style.MozOpacity = current_opacity - fade_rate;
		current.style.KhtmlOpacity = current_opacity - fade_rate;
		next.style.opacity = next_opacity + fade_rate;
		next.style.filter = 'alpha(opacity='+((next_opacity + fade_rate)*100)+')';
		next.style.MozOpacity = next_opacity + fade_rate;
		next.style.KhtmlOpacity = next_opacity + fade_rate;
		setTimeout('fadePhoto('+photo+','+next_photo+',"'+type+'",'+rate+')', rate);
	}else{
		current.style.visibility = 'hidden';
		current.style.opacity = 1;
		current.style.filter = 'alpha(opacity=100)';
		current.style.MozOpacity = 1;
		current.style.KhtmlOpacity = 1;
		current.style.zIndex = 1;
		next.style.zIndex = 200;
		next.style.opacity = 1;
		next.style.filter = 'alpha(opacity=100)';
		next.style.MozOpacity = 1;
		next.style.KhtmlOpacity = 1;
		
		if (type == 'photo_'){
		  photo_changing = false;
		}
	}	
}

/* For photo page, accepts next photo and number of photos. Doesn't act if there's a fade already in progress. Loops through to
   find current photo.*/
function changePhoto(next_photo, num_photos){
	
	for (i=1; i<=num_photos; i++){	
		if (photo_changing){
			return;
		}
		if (document.getElementById('photo_'+i).style.zIndex == '200'){
			var current = i;
			if (current == next_photo){
		    return;
	    }
			break;
		}
	}

  document.getElementById('photo_'+next_photo).style.zIndex = 100;
	document.getElementById('photo_'+next_photo).style.opacity = 0;
	document.getElementById('photo_'+next_photo).style.MozOpacity = 0;
	document.getElementById('photo_'+next_photo).style.KhtmlOpacity = 0;
  document.getElementById('photo_'+next_photo).style.visibility = 'visible';
	
	photo_changing = true;
  fadePhoto(current, next_photo, 'photo_', 25);
}

// Cookie remembers where slide show is during user's visit. Checks for cookie, cycles from there. Refreshes cookie.
function slideShow(current, num_photos, check){
	
	if (check){
		if (document.cookie.indexOf('photo=') != -1){
			var start = document.cookie.indexOf('photo=') + 6;
			var end = document.cookie.length;
			
			current = parseInt(unescape(document.cookie.substring(start, end)));
		}
	}
	
	var next = current;
	var current_slide = document.getElementById('slide_'+current);
	
	if (++next > num_photos){
		next = 1;
	}
	
	document.getElementById('slide_'+next).style.zIndex = 100;
  document.getElementById('slide_'+next).style.visibility = 'visible';
	
	fadePhoto(current, next, 'slide_', 50);
  document.cookie = 'photo='+escape(current);
	setTimeout('slideShow('+next+','+num_photos+',false)', 4000);
												
}

// Fades div after is has rolled up.
function fadeDiv(menu){
	
	var theDiv = document.getElementById(menu);
	var opacity = parseFloat(theDiv.style.opacity);
	var fade_rate = 0.05;
	
	if (opacity >= fade_rate){
	  theDiv.style.opacity = opacity - fade_rate;
		theDiv.style.filter = 'alpha(opacity='+((opacity - fade_rate)*100)+')';
		theDiv.style.MozOpacity = opacity - fade_rate;;
		theDiv.style.KhtmlOpacity = opacity - fade_rate;;
		fade_timer = setTimeout("fadeDiv('"+menu+"')", 15);
	}else{
		theDiv.style.visibility = 'hidden';
	}	
}

// Rolls menu up or down. Rate slows when menu is expanded.
function rollMenu(menu, hovering){
	
	height = parseInt(document.getElementById(menu).style.height);
	
	var speed = parseInt(7 - parseInt(height / 15));
	
	  if (hovering){
			if (height + speed < 97){
				document.getElementById(menu).style.height = height + speed + 'px';
				document.getElementById(menu+'_background').style.height = height + speed + 'px';
				roll_timer = setTimeout("rollMenu('"+menu+"', true)", 5);
			}else{
				document.getElementById(menu).style.height = '97px';
				document.getElementById(menu+'_background').style.height = '97px';
			}
		}else{
			if (height - speed > 0){
				document.getElementById(menu).style.height = height - speed + 'px';
				document.getElementById(menu+'_background').style.height = height - speed + 'px';
				roll_timer = setTimeout("rollMenu('"+menu+"', false)", 5);
			}else{	
			  document.getElementById(menu).style.height = '0px';
				document.getElementById(menu+'_background').style.height = '0px';
				document.getElementById(menu+'_background').style.filter = '';
				fadeDiv(menu);
			}	
		}
}

// Activated from Menu item and flyout menu.
function flyOut(menu, hovering){
	
	if (hovering){
		clearTimeout(timer);
		clearTimeout(roll_timer);
		clearTimeout(fade_timer);
		document.getElementById(menu).style.opacity = 1;
		document.getElementById(menu).style.filter = '';
		document.getElementById(menu+'_background').style.filter = 'alpha(opacity=75)';
		document.getElementById(menu).style.MozOpacity = 1;
		document.getElementById(menu).style.KhtmlOpacity = 1;
		document.getElementById(menu).style.visibility = 'visible';
		rollMenu(menu, true);
	}else{
		timer = setTimeout("rollMenu('"+menu+"', false)", 1000);
	}
}

// Validates form against following regexs, and required fields.
function validateForm(){
	
	var contact_form = document.getElementById('contact_form');
	var email = /^\w[\w\_\&\-\.\~\%]*\@\w[\w\_\-\&\.\~]*\.[a-zA-Z]{2,7}$/;
	var badtext = /[^\w\!\@\#\$\%\^\&\*\ \(\)\-\+\=\{\[\}\]\"\'\:\;\<\,\>\.\?\/\\\~\`\n\r\t]/;
	
	if (badtext.test(contact_form.f_name.value)){
    alert('Sorry, I can\'t accept some of the characters you\'ve entered in the first name field.');
    contact_form.f_name.focus();
    return false;
  }
	
	if (badtext.test(contact_form.l_name.value)){
    alert('Sorry, I can\'t accept some of the characters you\'ve entered in the last name field.');
    contact_form.l_name.focus();
    return false;
  }
	
	if ((contact_form.l_name.value.length == 0) && (contact_form.f_name.value.length < 2)){
		alert('Please enter your name.');
		contact_form.f_name.focus();
		return false;
	}
	
	if (badtext.test(contact_form.city.value)){
    alert('Sorry, I can\'t accept some of the characters you\'ve entered in the city field.');
    contact_form.city.focus();
    return false;
  }
	
	if (contact_form.message.value.length == 0){
		alert('Please enter a message.');
		contact_form.message.focus();
		return false;
	}
	
	if (badtext.test(contact_form.message.value)){
    alert('Sorry, I can\'t accept some of the characters you\'ve entered in the message area.');
    contact_form.message.focus();
    return false;
  }
	
	if (!email.test(contact_form.email.value)){
    alert('You seem to have entered an incorrect email address.');
    contact_form.email.focus();
    return false;
  }
}

// Gets string length of textarea and prints back above form. If too big, cuts off last character.
function getCharCount(){

  if (!document.getElementById){
    return;
  }
	
	var field = document.getElementById('message');
	var max_length = 1500;
	
	if (field.value.length > max_length){
		field.value = field.value.substr(0, max_length); 
	}
	document.getElementById('char_count').innerHTML = 'Character count: '+field.value.length+', Max: '+max_length;
}