// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// ==========================================================================
// Clears the search box content when it gets focus.
// ==========================================================================

function clearInputBox(box) {
  box.setAttribute("value", "")
}


// ==========================================================================
// Cookie functions
// ==========================================================================

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 var 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 eraseCookie(name) {
  createCookie(name,"",-1);
}


// ==========================================================================
// Print Rep Co-ordinates
// ==========================================================================

function doesRepExist() {
  var repInfo = readCookie('rep_info');
  return repInfo != undefined && !repInfo.blank();
}

function repData() {
  return unescape(readCookie('rep_info')).replace(/\+/g, ' ').evalJSON();
}

function printRepInfo() {
    if (doesRepExist()) {
      var output = new EJS({url: '/javascripts/views/representatives/header.ejs'}).render(repData());
      document.write(output);
    }
}

function printContactInfo() {  
  if (doesRepExist()) {
    var output = new EJS({url: '/javascripts/views/representatives/contact.ejs'}).render(repData());
    document.write(output);
  } else {
    var output = new EJS({url: '/javascripts/views/representatives/contact_default.ejs'}).render({});
    document.write(output);  
  }
}

function printContactLink() {  
  if (doesRepExist()) {
    var output = new EJS({url: '/javascripts/views/representatives/contact_link.ejs'}).render(repData());
    document.write(output);
  } else {
    var output = new EJS({url: '/javascripts/views/representatives/contact_link_default.ejs'}).render({});
    document.write(output);  
  }
}

function printContactPhoto() {
    if (doesRepExist()) {
      var output = new EJS({url: '/javascripts/views/representatives/photo.ejs'}).render(repData());
      document.write(output);
    }
}


// ==========================================================================
// Homepage : Print Gold Chef's Story
// Instructions : Just insert the new Gold Chef 'code' in the goldChefs array
// an the script will do the rest.
// ==========================================================================

function findGoldChef() {
  var goldChefs = new Array('kgjesdal','hklees','kkniskern','llockwood','kwalter')
  var chefsLength = goldChefs.length
  var rdmKey = Math.floor((chefsLength-0)*Math.random())

  var currentChef = goldChefs[rdmKey]
  return currentChef
}

function printGoldChefStory() {
  var currentChef = findGoldChef()
  var newContent = new EJS({url: '/javascripts/views/goldchefs/' + currentChef + '.ejs'}).render(currentChef)
  var newGoldChef = document.createElement('div')
  newGoldChef.className = "container"

  var container = $('testimonials')
  var oldGoldChef = $('gold-chef')
  container.replaceChild(newGoldChef,oldGoldChef)

  newGoldChef.innerHTML = newContent
}