//Non-Obtrusive Image Swap Script V1.1 by Hesido.com 
//(instead of default dreamweaver functions)
//http://www.hesido.com/web.php?page=imageswapping
function prepareImageSwap(elem,mouseOver,mouseOutRestore,mouseDown,mouseUpRestore,mouseOut,mouseUp){if(typeof(elem)=='string'){elem=document.getElementById(elem)}if(elem===null){return}var regg=/(.*)(_nm\.)([^\.]{3,4})$/;var prel=new Array(),img,imgList,imgsrc,mtchd;imgList=elem.getElementsByTagName('img');for(var i=0;img=imgList[i];i++){if(!img.rolloverSet&&img.src.match(regg)){mtchd=img.src.match(regg);img.hoverSRC=mtchd[1]+'_hv.'+mtchd[3];img.outSRC=img.src;if(typeof(mouseOver)!='undefined'){img.hoverSRC=(mouseOver)?mtchd[1]+'_hv.'+mtchd[3]:false;img.outSRC=(mouseOut)?mtchd[1]+'_ou.'+mtchd[3]:(mouseOver&&mouseOutRestore)?img.src:false;img.mdownSRC=(mouseDown)?mtchd[1]+'_md.'+mtchd[3]:false;img.mupSRC=(mouseUp)?mtchd[1]+'_mu.'+mtchd[3]:(mouseOver&&mouseDown&&mouseUpRestore)?img.hoverSRC:(mouseDown&&mouseUpRestore)?img.src:false}if(img.hoverSRC){preLoadImg(img.hoverSRC);img.onmouseover=imgHoverSwap}if(img.outSRC){preLoadImg(img.outSRC);img.onmouseout=imgOutSwap}if(img.mdownSRC){preLoadImg(img.mdownSRC);img.onmousedown=imgMouseDownSwap}if(img.mupSRC){preLoadImg(img.mupSRC);img.onmouseup=imgMouseUpSwap}img.rolloverSet=true}}function preLoadImg(imgSrc){prel[prel.length]=new Image();prel[prel.length-1].src=imgSrc}}function imgHoverSwap(){this.src=this.hoverSRC}function imgOutSwap(){this.src=this.outSRC}function imgMouseDownSwap(){this.src=this.mdownSRC}function imgMouseUpSwap(){this.src=this.mupSRC}

function shownewwin(windowURL, windowName, windowFeatures) {
	var newWindow = window.open(windowURL, windowName, windowFeatures);
	newWindow.focus(); 
	return;
 }

//custom jquery functions
(function($){

  $.fn.yellowFade = function(){
    return this.each(function(){
      var b = 155;
      var $this = $(this);
      function f() {
        $this.css("background-color", 'rgb(255,255,'+ (b+=4) +')' );
        if (b < 255) {
          setTimeout(f, 40);
        }
      };
      f();          
    });
  };
  
  /**
   * Toggle "How to save..." text on benefit pages.
   * 
   * First, we take the paragraph and rip out the strong tag. The text of the 
   * strong tag will become the toggle link. Next, create the toggle link and 
   * insert it before the paragraph. Next, add a class to the paragraph so that 
   * "only when javascript is active" styles can be applied to it. Finally, 
   * hide the paragraph and add a click event to the toggle link.
   */   
  function howto_toggler(){
    $("#howto").each(function(){
      var $this = $(this);
      var link = $this.find("strong").text();
      $this
        .find("strong").remove().end()
        .before( '<a href="#" id="howto_toggle">' + link + '</a>' )
        .attr("id", "howto_content")
        .hide();
        
      $("#howto_toggle").click(function(){
        $("#howto_content").slideToggle();
        return false;
      });    
    });  
  };
  
  /**
   * Toggle Email and Phone fields on Feedback page.
   */
  function contactmethod_toggler(){ 
    $("#contactmethod").each(function(){
      var $this = $(this);
      var $wrap = $("<div></div>");
      var $email = $("#_ctl0__ctl1_txtEmail").parent();
      var $confirmEmail = $("#_ctl0__ctl1_txtConfirmEmail").parent();
      var $phone = $("#_ctl0__ctl1_txtPhone").parent();
      
      $wrap
        .append($email)
        .append($confirmEmail)
        .append($phone)
        .insertAfter($this);
      
      $this
        .click(function(e, dofade){      
          var sel = $(e.target).val();
          if (sel == '') { return true; } //in case user does not actually click a radio button (return true because user may have clicked on label, so return true to allow click event to continue bubbling.
          
          if (sel == "radMethodEmail") {
            $phone.hide(); 
            $email.show();
            $confirmEmail.show();        
          }
          else {
            $email.hide();
            $confirmEmail.hide();
            $phone.show();           
          }
            
          if (typeof dofade == "undefined" || dofade) { 
            $wrap.yellowFade();
          }   
        })
        .find("input:checked").trigger("click", [0]);    
    });
  };
  
  /**
   * Open link in new window. Based on http://www.456bereastreet.com/archive/200610/opening_new_windows_with_javascript_version_12/ 
   */
  function newWin(e, url) {
    if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) {
      return true;
    }
    else {
      var oWin = window.open(url, '_blank');
      if (oWin) {
        if (oWin.focus) {
          oWin.focus();
        }
        return false;
      }
      oWin = null;
      return true;
    }
  }
  
  /**
   * Instead of attaching a click event to every external link (each event uses memory), attach
   * the click event to the body tag and delegate the click event from there.
   */
  function externalLinks() {
    $("body").click(function(e){
      var $t = $(e.target);      
      if ($t.is("a") && $t.attr("rel") == "external") {      
        return newWin(e, $t.attr("href"));  
      }   
    }); 
  }  
  
  jQuery(function($){
    $("#nav img").removeAttr("onmouseover","").removeAttr("onmouseout",""); //TODO should ask backend developer to remove inline events like what was done for mygreatdeals, 
    prepareImageSwap("nav");
    prepareImageSwap("nav2");
    externalLinks();
    howto_toggler(); 
    contactmethod_toggler(); 
  });

})(jQuery);
