window.addEvent('domready', function() {
  $$('a.tipz').each(function(element,index) {
    var content = element.get('title').split('::');
    element.store('tip:title', content[0]);
    element.store('tip:text', content[1]);
    element.set('onclick','return false;');
  });

  $$('a.tipz2').each(function(element,index) {
    element.set('onclick','return false;');
  });

  //create the tooltips
  var tipz = new Tips('.tipz',{
    className: 'tipz',
    fixed: true,
    offset: {'x':10, 'y':20},
    hideDelay: 50,
    showDelay: 50
  });
  
  tipz.addEvents({
    'show': function(tip) {
      tip.fade('in');
      //tip.setStyle("opacity",".7")
    },
    'hide': function(tip) {
      tip.fade('out');
    }
  });

  if ($("nav")) {
    // Flyout menu for "Get Started!"
    $('nav_gs_li').addEvents({
      'mouseenter': function(){
        // Always sets the duration of the tween to 1000 ms and a bouncing transition
        // And then tweens the height of the element
        var ht = ($('nav_gs_li').getElementsByTagName("A").length*22)+42 + "px";
        $('nav_gs_li').set('tween', {
          duration: 300,
          transition: Fx.Transitions.easeOut // This could have been also 'bounce:out'
        }).tween('height', ht);
      },
      'mouseleave': function(){
        // Resets the tween and changes the element back to its original size
        $('nav_gs_li').set('tween', {}).tween.delay(0,$('nav_gs_li'),['height', '40']);
      }
    });

    // Flyout menu for "Resources"
    $('nav_resc_li').addEvents({
      'mouseenter': function(){
        // Always sets the duration of the tween to 1000 ms and a bouncing transition
        // And then tweens the height of the element
        var ht = ($('nav_resc_li').getElementsByTagName("A").length*22)+42 + "px";
        $('nav_resc_li').set('tween', {
          duration: 300,
          transition: Fx.Transitions.easeOut // This could have been also 'bounce:out'
        }).tween('height', ht);
      },
      'mouseleave': function(){
        // Resets the tween and changes the element back to its original size
        $('nav_resc_li').set('tween', {}).tween.delay(0,$('nav_resc_li'),['height', '40']);
      }
    });
  }


  if ($("billboard_wrap")) {
    var myFunction = function(){ on_trust() };

    //Wait 50 milliseconds, then call myFunction and bind myElement to it.
    myFunction.delay(300);

    var numimg=2;
    var tstatus='hidden';
    var wstatus = 'hidden';
    var tbill = $('trust_billboard'); 
    var wbill = $('will_billboard');
    var tbucket = $('trust_bucket');
    var wbucket = $('will_bucket');
    var trans = new Fx.Transition(Fx.Transitions.Quart, [10, 40, 100]);

    tbill.apos = wbill.apos = 0;

    var trust_fx = new Fx.Tween(tbill,  {duration:518, transition: trans.easeInOut, onComplete:function() {
      tstatus = tstatus == "movingin" ? "in" : "out" ; 
      if (tstatus=="out") {
        tbill.apos ++ ;
        if (tbill.apos >= numimg) tbill.apos = 0;
        tbill.setStyle('backgroundPosition', '0px '+(tbill.apos*-232)+'px');
      }
    }}); 

    var will_fx = new Fx.Tween(wbill,  {duration:518, transition: trans.easeInOut, onComplete:function(){
      wstatus = wstatus == "movingin" ? "in" : "out" ;
      if (wstatus=="out") {
        wbill.apos ++ ;
        if (wbill.apos >= numimg ) wbill.apos = 0;
        wbill.setStyle('backgroundPosition', '0px '+(wbill.apos*-232)+'px') 
      } 
    }});

    var tbucket_fx = new Fx.Tween(tbucket,  {duration:200, transition: trans.easeInOut, onComplete:function(){
      if (tstatus=="movingout" || tstatus == "out") {
        tbucket.className = "bucket_small trust_bucket_sm";
        if (Browser.Engine.trident4) {
          $('tbh').setStyle("background-image", "url(/images/box_sm_trust.png)"); 
        }
      }
    }});

    var wbucket_fx = new Fx.Tween(wbucket,  {duration:200, transition: trans.easeInOut, onComplete:function(){
      if (wstatus=="movingout" || wstatus == "out") {
        wbucket.className = "bucket_small will_bucket_sm" ;
        if (Browser.Engine.trident4) {
          $('wbh').setStyle("background-image", "url(/images/box_sm_will.png)");
        }
      }
    }});

    tbucket.addEvents({'mouseover': function(){ on_trust() }});
    wbucket.addEvents({'mouseover': function(){ on_will() }});

    function on_trust() {
      if (tstatus=="movingin" || tstatus=="movingout" || tstatus=="in") return;
      tstatus = "movingin";
      wstatus = "movingout";
      trust_fx.start('margin-left','23');
      will_fx.start('margin-left','-900'); 
      tbucket_fx.start('margin-top','-28');
      wbucket_fx.start('margin-top','10');
      if (!Browser.Engine.trident) {
        tbucket.fade('1');
        wbucket.fade('.7');
      }
      tbucket.className = "bucket_large trust_bucket_lg" ; 
      if (Browser.Engine.trident4) {
        $('tbh').setStyle("background-image", "url(/images/box_lg_trust.png)"); 
      }  
    }

    function on_will() {
      if (wstatus=="movingin" || wstatus=="movingout" || wstatus=="in") return;
      wstatus ="movingin";
      tstatus ="movingout";
      trust_fx.start('margin-left','900');
      will_fx.start('margin-left','23');
      if (!Browser.Engine.trident) {
        tbucket.fade('.7');
        wbucket.fade('1');
      }
      tbucket_fx.start('margin-top','10')
      wbucket_fx.start('margin-top','-30'); 
      wbucket.className = "bucket_large will_bucket_lg";
      if (Browser.Engine.trident4) {
        $('wbh').setStyle("background-image", "url(/images/box_lg_will.png)"); 
      } 
    }
  }
});

function in_array (needle, haystack, argStrict) {
  var key = '', strict = !!argStrict;

  if (strict) {
    for (key in haystack) {
      if (haystack[key] === needle)
        return true;
    }
  } else {
    for (key in haystack) {
      if (haystack[key] == needle)
        return true;
    }
  }

  return false;
}    

function showAllAnswers(atag) {
  if (atag.innerHTML=="Show all answers") {
    $$(".answer").each(function(el){
      el.setStyle("display", "block")
    })
    $$(".faq_question").each(function(el){
      el.setStyle("backgroundColor", "#f0f0f0")
    })
	atag.innerHTML = "Hide all answers";
  } else {
    $$(".answer").each(function(el){
      el.setStyle("display", "none")
    })
    $$(".faq_question").each(function(el){
      el.setStyle("backgroundColor", "transparent")
    })
	atag.innerHTML = "Show all answers";
  }
}

function toggleAnswer(id, close) {
  var div = "a"+id ;
  var close = $(div).getStyle('display') == "block" ;
  var q = "q"+id ;

  $(div).setStyle('display', close ? 'none' : 'block')
  $(q).setStyle('backgroundColor', close ? 'transparent' : '#f0f0f0')
} 

function toggleDiv(id, atag) {
  var div = id;
  var close = $(div).getStyle('display') == "block";

  $(div).setStyle('display', close ? 'none' : 'block')
  $(atag).set('html', close ? '(what is this?)' : '(hide explanation)')
}

function SetCookie(cookieName,cookieValue,nDays) {
  var today = new Date();
  var expire = new Date();

  if (nDays==null || nDays==0) nDays=1;
  expire.setTime(today.getTime() + 3600000*24*nDays);
  document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function ReadCookie(cookieName) {
  var theCookie=""+document.cookie;
  var ind=theCookie.indexOf(cookieName);
  var ind1=theCookie.indexOf(';',ind);

  if (ind==-1 || cookieName=="") return ""; 
  if (ind1==-1) ind1=theCookie.length; 

  return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function populateField(r) {
  var myCheck = "";
  if ($("trust__sameAsResidence")) myCheck = "trust__sameAsResidence";
  else if ($("will__sameAsResidence")) myCheck = "will__sameAsResidence";

  if ((myCheck)&&($(myCheck).checked==true)) {
    document.getElementById(r+'_mail').value = document.getElementById(r).value;
  }
}
