
// Control fade effect on contact form
function showContact(name){
  $("#ContactForm tr").addClass("fade");
  //$("#ContactForm textarea").attr("disabled", true);
  $("#contact-" + name).removeClass("fade");
  $("#ContactForm textarea").attr("disabled", false);
}

// Check that inbox messages have been ticked
function check_ticks(){
  var ticks = false;
  $(".messageTick").each( function(){ if($(this).is(":checked"))ticks = true; });
  if(ticks){
    $("#DeleteLink").removeClass("fade");
    $("#SpamLink").removeClass("fade");
  }else{
    $("#DeleteLink").addClass("fade");
    $("#SpamLink").addClass("fade");
  }
}

// Favourites comments onFocus()
function editComments(subject_id){
  var comment_value = $("#comment_"+subject_id).val();
  if(comment_value == "Add notes"){
    $("#comment_"+subject_id).val("");
    $("#comment_"+subject_id).removeClass("center");
  }else if(comment_value.substring(0, 28) == "Notes (click here to edit): "){
    $("#comment_"+subject_id).val(comment_value.substring(28));
  }
}

// Update the rating form
function editRating(subject_id, current){
  $("#favourite-rating-value").val(current);
  $("#favourite-rating-subject").val(subject_id);
}

// Add a new qcrew position
function addPosition(){
  if($('select.crew-positions').length > 1){
    var select_value = $('select.crew-positions:last').val();
    var position = $("#CrewPositions h3:last").html().substring(15);
  }else{
    var position = 0;
  }
  
  if(select_value != 0 || $('select.crew-positions').length == 1){
    $('#CrewPositions').append($("#CrewPositions div.hidden").html());
    position = eval(position) + 1;
    $("#CrewPositions h3:last").html("&nbsp;Position " + position);
  }
}

// Add a new qualification row to the skills/qualification table
function addSkill(){
  var select_value = $('table.skills select:last').val();
  if(select_value != "")$('table.skills > tbody:last').append($("table.skills tbody.hidden").html());
}

// Add a new itinerary row to the itinerary table
function addItinerary(){
  var input_value = $('table.itinerary input:last').val();
  if(input_value != "")$('table.itinerary > tbody:last').append($("table.itinerary tbody.hidden").html());
}

$(document).ready(function(){    
  $("#NextQ").click( function(){ $("div.spotlight.hidden:first").removeClass("hidden"); window.scrollBy(0,92); }); // Spotlight questions  
  $("#new_blog_button").click( function(){ $(this).css("display", "none"); $("#blog_form").css("display", "block"); }); // New blog
      
  // Header nav
  $("ul.level-1 li").mouseenter( function(){ $(this).children(".level-2").addClass("over"); })
  .mouseleave( function(){ $(this).children(".level-2").removeClass("over"); });
   
  // Account nav
  $(".controlsMenu").mouseenter( function(){ $(this).children("ul.accountSub").addClass("over"); })
  .mouseleave( function(){ $(this).children("ul.accountSub").removeClass("over"); });
  
  // Flyouts
  $(".triggerFlyout").click( function(){ 
    $("#ScreenShadow").show(); 
    var trigger_id = $(this).attr("id");
    if(trigger_id == "")$(".flyout").show();
    else $("#Flyout"+trigger_id.substr(7)).show();
  }); 
  $("#ScreenShadow").click( function(){ $("#ScreenShadow").hide(); $(".flyout").hide(); });
  $(".flyout .close").click( function(){ $("#ScreenShadow").hide(); $(".flyout").hide(); });
  
  // Home page
  $("#HomeCarousel .block").mouseenter( function(){ 
    $(this).addClass("over"); 
    var img_src = $(this).find("img").attr("src");
    $("#HomeCarousel .image img").attr("src", img_src);
  }).mouseleave( function(){ $(this).removeClass("over"); });
  
  // Image rollovers
  $(".rollover").hover( function(){
    var src = $(this).attr("src");
    if(!src.match(/-over./)){
      src = src.replace(".jpg", "-over.jpg");
      src = src.replace(".gif", "-over.gif");
      src = src.replace(".png", "-over.png"); 
      $(this).attr("src", src);
      $(this).addClass("hovering"); 
    }
  }, function(){
    if($(this).hasClass("hovering")){
      var src = $(this).attr("src");
      src = src.replace('-over.', '.'); 
      $(this).attr("src", src);
      $(this).removeClass("hovering");
    }
  });
  
  // Profile page thumbs
  $(".lightbox a").fancybox({
  	cyclic	: true,
  	onStart	: function() {
  		$(".lightbox").trigger("pause");
  	},
  	onClosed: function() {
  		$(".lightbox").trigger("play");
  	}
  });
  
  // Profile images
  $("#profile_image_container").scrollview({
    grab:"images/openhand.cur",
    grabbing:"images/closedhand.cur"
  });
  
});
