/* 
 * Stylesheet switcher. Used to power the accessiblity text size links.
 *
 * Created by Paul Sowden/Peter-Paul Koch, 2001
 * http://www.alistapart.com/articles/alternate/
 */

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function addPlaceholderText( el, placeholder ) {
  if ( ( el = document.getElementById( el ) ) == null ) {
    return;
  }
  
  return handlePlaceholderText( el, placeholder );
}
  
function handlePlaceholderText( el, placeholder ) {
  el.value = placeholder;
  el.className = "inactive";
  
  el.onfocus = function( ) {
    if ( el.value === placeholder ) {
      el.value = "";
    }
    
    el.className = "";
  };
  
  el.onblur = function( ) {
    if ( el.value === "" || el.value === placeholder ) {
      el.value = placeholder;
      el.className = "inactive";
    }
  };
}

function attachPopups( el ) {
  if ( ( el = document.getElementById( el ) ) == null ) {
    return;
  }
  
  if ( el.tagName.toUpperCase() == "A" ) {
    el.target = "_blank";
    return;
  }
  
  links = el.getElementsByTagName( "a" );
  for ( var i = 0; i < links.length; i++ ) {
    links[i].target = "_blank";
  }
}

function enableTouchMenu( ) {
  if (!("ontouchstart" in document.documentElement)) {
    return;
  }
  
  var menu = document.getElementById( "menu" );
  
  if ( menu == null ) {
    return;
  }
  
  var menuItems = menu.childNodes;
  
  for ( var i = 0; i < menuItems.length; i++ ) {
    if ( !menuItems[i].getElementsByTagName ) {
      continue;
    }
    
    var menuLink = menuItems[i].getElementsByTagName("a")[0];
    
    if ( menuLink.tagName.toUpperCase() == "A" ) {
      if ( menuItems[i].getElementsByTagName( "ul" ).length <= 0 ) {
        continue;
      }
      
      menuLink.onclick = function () {
        document.getElementById("wrapper").style.webkitTransform = 'scale(1)';
        
        return false;
      };
    }
  }
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

window.onload = function( ) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  
  document.getElementById('a_small').onclick = function() { setActiveStyleSheet('Regular') };
  document.getElementById('a_med').onclick = function() { setActiveStyleSheet('Larger') };
  document.getElementById('a_large').onclick = function() { setActiveStyleSheet('Largest') };

  addPlaceholderText( "email", "Your e-mail address" );
  
  var inputs = document.getElementsByTagName("input");
  for ( var i = 0; i < inputs.length; i++ ) {
    if ( inputs[i].className == "placeholder" ) {
      handlePlaceholderText( inputs[i], inputs[i].value );
    }
  }
  
  //attachPopups( "latest" );
  attachPopups( "contact_gmaps" );
};

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}
