/********************************************************
   Dynamic Menu
 ********************************************************/

// create the later that slides on page scroll
function initStatLyr() {
  // args: id, left, top, w, h, duration of glide to location onscroll, acceleration factor
  // acceleration factor should be -1 to 1. -1 is full deceleration
  var statLyr = new Glider("menu",536, 0,null,null,750,-1);
  statLyr.show();
}


function writeCSS(){
  document.write('<style type="text/css">');
  document.write('.pictures img { filter: alpha(opacity=25); }');
  document.write('.pictures a:hover img { filter: alpha(opacity=80); }');  
  document.write('ul.portfolio li a img { filter: alpha(opacity=45);}');
  document.write('ul.portfolio li a:hover img { filter: alpha(opacity=99);}');
  document.write('textarea { scrollbar-track-color: #252525; scrollbar-face-color: #252525; scrollbar-arrow-color: #FDFAEB; scrollbar-3dlight-color: #252525; scrollbar-highlight-color: #252525; scrollbar-shadow-color: #252525; scrollbar-darkshadow-color: #252525; }');
  document.write('</style>');
}


/********************************************************
   Form Validation
 ********************************************************/

function checkClearValue(field, label){   
  if(field.value == label)
    field.value = "";
}

function checkFillValue(field, label){
  if(field.value.length == 0)
    field.value = label;
}

function checkEmail(email){ 

  
  var at  = email.indexOf('@');
  if(at  == -1 || 
     at  ==  0 ||
     at  == (email.length - 1)){
     return false;
   }
  
  
  var dot = email.substr(at, email.length).indexOf('.');  
  if(dot == -1 || 
     dot ==  0 ||   
     dot == (email.length)) {
    return false;  
  }  
  
  return true;
}


function validateContactForm(form){
  
  var realName = form.realname.value;
  var subject = form.messageSubject.value;
  var email = form.email.value;
  var message = form.message.value;

  var errors = "";
  var count = 0;

  if(realName == "" || realName == "NAME")
    errors += "\n    " + (++count) + ". Missing name";
  
  if(subject == "" || subject == "SUBJECT")
    errors += "\n    " + (++count) + ". Missing subject";
  
  if(email == "" || email == "E-MAIL")
    errors += "\n    " + (++count) + ". Missing e-mail";
  else if(!checkEmail(email))
    errors += "\n    " + (++count) + ". Invalid e-mail";
    
  if(message == "" || message == "MESSAGE")
    errors += "\n    " + (++count) + ". Missing message";
  
  
  if(errors != ""){
    alert("Please correct the following and try again:\n" + errors);
    return false;
  }  

  return true;
}


function writeGmailLink(){
  var start = "pat.heard";
  var middle = "&#64;";
  var end = "gmail.com"; 
  
  document.write('<a href="mailto:' + start + middle + end + '" title="' + start + middle + end + '">Gmail Account</a>');
}

function preloadImages(){
  var imgs = new Array();
  imgs[0] = new Image();
  imgs[0].src = 'images/li_bullet.gif';
  imgs[1] = new Image();
  imgs[1].src = 'images/link.gif';
  imgs[2] = new Image();
  imgs[2].src = 'images/link_top.gif';
  imgs[3] = new Image();
  imgs[3].src = 'images/link_bottom.gif';  
  imgs[4] = new Image();
  imgs[4].src = 'images/menu_bg.gif'; 
  imgs[5] = new Image();
  imgs[5].src = 'images/menu_bg.png';     
}

/********************************************************
   Centered, popup window
 ********************************************************/

function popupCenter(url, width, height, opt){
  var top = (screen.height/2)-(height/2);
  var left = (screen.width/2)-(width/2);
  var features = 'height='+height+',width='+width+',top='+top+',left='+left+',scrollbars=' + opt;
  theWin = window.open(url,'',features);
}


/********************************************************
   Dynamically fills remaining page height
 ********************************************************/

function setDynProp(id, footerId){
  var px = window.opera ? 0: "px";
  var el = document.getElementById(id);
  var footer = document.getElementById(footerId);
  
  if(el && footer){    
    var winHt = getWindowHeight();
    var prop = winHt - (footer.offsetTop + footer.offsetHeight);
    el.style.height = prop + px;    
  }
}

/********************************************************
   Returns the browser's window height
 ********************************************************/
 
function getWindowHeight() {
  var winHt = 0;

  if (window.innerHeight)
    winHt = window.innerHeight - 10;
  else if (document.documentElement && document.documentElement.clientHeight)
    winHt = document.documentElement.clientHeight;
  else if (document.body && document.body.clientHeight)
    winHt = document.body.clientHeight;

  return winHt;
}
