$(document).ready(function(){
  
  var tabs = [];
  var tabContainers = [];
  $('#services ul.tabs a').each(function () {
   if (this.pathname == window.location.pathname) {
     tabs.push(this);
     tabContainers.push($(this.hash).get(0));
     
     // hide all tabs initally, except for the main tab
     $(tabContainers).hide()
   }
  });
  
  $(tabs).click(function (event) {
    event.preventDefault();
    
    // hide main
    $('#services-main').hide();
      
    // hide all other tabs
    $(tabContainers).fadeOut().filter(this.hash).fadeIn();
    // console.log(this.hash);
      
    // set up the selected class
    $(tabs).removeClass('selected');
    $(this).addClass('selected');
      
    return false;
  });
  
  
  // Observe if a coupon code is entered
  $("#coupon_code").change(function(event){
    
    var code    = $(this).val();
    var plan_id = $("#user_plan_id").val();
    var url     = "/check_coupon?code=" + code + "&plan_id=" + plan_id;
    
    $.get(url, null, null, "script");
    return false
  });
  
  
	// Toggle text/password fields
	// TODO: Simplify toggle of link text!!!
  $(".change_to_password_field").click(function(event){
    event.preventDefault();

    var show_text_password = "Show my password text!";
    var hide_text_password = "Hide my password text!";    
    var show_text_ssn = "Show my social security number!";
    var hide_text_ssn = "Hide my social security number!";

    if ($(this).text() == show_text_password) { 
      $(this).text(hide_text_password);
    } else if ($(this).text() == hide_text_password) { 
      $(this).text(show_text_password);
    } else if ($(this).text() == show_text_ssn) { 
      $(this).text(hide_text_ssn);
    } else { 
      $(this).text(show_text_ssn);
    }

    $(".password_field").each(function(){

      if($(this).attr("type") == "text"){
        $(this).replaceWith('<input id="'+$(this).attr("id")+'" class="'+$(this).attr("class")+'" type="password" name="'+$(this).attr("name")+'" value="'+$(this).val()+'"/>');
      } else {
        $(this).replaceWith('<input id="'+$(this).attr("id")+'" class="'+$(this).attr("class")+'" type="text" name="'+$(this).attr("name")+'" value="'+$(this).val()+'"/>');
      }

    });

    return false;

  });
  
  // Open all external links in a new window
  $('a[rel="external"]').click( function() {
    window.open( $(this).attr('href') );
    return false;
  });

});

// sIFR config
var gotham = { src: '/swf/gotham-book.swf', wmode: 'transparent' };
sIFR.activate(gotham);
sIFR.replace(gotham, {
  selector: '.sifr',
  css: '.sIFR-root { font-size: 30px; font-weight: normal; leading: 5; letter-spacing: -1.5; color: #00519f; }'
});

function tab_event(el){}