/* ---------------------------------------------------------------------------------------- */
/* CONTENT                                                                                  */
/* ---------------------------------------------------------------------------------------- */

// Function definitions -------------------------------------------------------------------
(function($) {
  $.fn.initFolders  = function() {
    $("#content .folder > a").click(function() {
      
      // Toggle content
      if ($(this).parent().find("ul").eq(0).css("display")!="none") {
        $(this).parent().css("background-image", "url(/resources/images/vuarnoz/folder-icon-closed.png)");
        $(this).parent().find("ul").eq(0).slideToggle("fast");
      } else {
        $(this).parent().css("background-image", "url(/resources/images/vuarnoz/folder-icon-open.png)");
        $(this).parent().find("ul").eq(0).slideToggle("fast");
      }
      return false;
    });
  
    $("#content .folderdone > a").click(function() {
      
      // Toggle content
      if ($(this).parent().find("ul").eq(0).css("display")!="none") {
        $(this).parent().css("background-image", "url(/resources/images/vuarnoz/folder-icon-closed-done.png)");
        $(this).parent().find("ul").eq(0).slideToggle("fast");
      } else {
        $(this).parent().css("background-image", "url(/resources/images/vuarnoz/folder-icon-open-done.png)");
        $(this).parent().find("ul").eq(0).slideToggle("fast");
      }
      return false;
    });
  };

})(jQuery);



(function($) {
  $.fn.initSimpleCaseForm  = function() {

	// Init "next" buttons
    $(this).find("#phasebutton_next1, #phasebutton_next2, #phasebutton_next3, #phasebutton_next4").click(function() {
    
       // Close this slot
       //$(this).parents(".slot").prev(".slotlinksmall").trigger("click"); 
       
       // Open next slot
       if ($(this).parent().parent().next().find(".slot").css("display") == "none") {
    	 $(this).parent().parent().next().find(".slotlinksmall").trigger("click"); 
       }
       
       // Scroll into view
       //$(this).parents(".slot").prev(".slotlinksmall").get(0).scrollIntoView();
       
       return false;
    });
    
    // Init checkoxes and radios
    $(this).find("input:checkbox").click(function () { $(this).parents("form").submit(); });
    $(this).find("input:radio").click(function () { $(this).parents("form").submit(); });
    
    // Init comments
    $(this).find(".comment").scale9Grid({ top:5, right:5, bottom:5, left:5 });
  };

})(jQuery);



(function($) {
  $.fn.initComplexCaseForm  = function() {

    // Init "next" buttons
    $(this).find("#phasebutton_next1, #phasebutton_next2, #phasebutton_next3, #phasebutton_next4, #phasebutton_next5, #phasebutton_next6, #phasebutton_next7").click(function() {
    
       // Close this slot
       //$(this).parents(".slot").prev(".slotlinksmall").trigger("click"); 
       
       // Open next slot
       if ($(this).parent().parent().next().find(".slot").css("display") == "none") {
       	 $(this).parent().parent().next().find(".slotlinksmall").trigger("click"); 
       }
       
       // Scroll into view
       //$(this).parents(".slot").prev(".slotlinksmall").get(0).scrollIntoView();
       
       return false;
    });
    
    // Init checkoxes and radios
    $(this).find("input:checkbox").click(function () { $(this).parents("form").submit(); });
    $(this).find("input:radio").click(function () { $(this).parents("form").submit(); });
    
    // Init comments
    $(this).find(".comment").scale9Grid({ top:5, right:5, bottom:5, left:5 });
    $(this).find(".confirmation").show();
    $(this).find(".confirmation").scale9Grid({ top:5, right:5, bottom:5, left:5 });
  };

})(jQuery);



(function($) {
  $.fn.initEfflorescenceForm  = function() {

    // Init checkoxes and radios
    $(this).find("input:checkbox").click(function () { $(this).parents("form").submit(); });
    $(this).find("input:radio").click(function () { $(this).parents("form").submit(); });
    
    // Init comments
    $(this).find(".comment").scale9Grid({ top:5, right:5, bottom:5, left:5 });
  };

})(jQuery);




// Initialization ---------------------------------------------------------------------------
$(document).ready(function() {



  // News form ------------------------------------------------------------------------------
  $("#content #newsform").ajaxForm({resetForm:false, success:function(responseText, statusText) {
  
    // Set new message
    $("#content #newsformconfirmation").remove9Grid({ top:5, right:5, bottom:5, left:5 });
    $("#content #newsformconfirmation").html(responseText);
    $("#content #newsformconfirmation").show();
    $("#content #newsformconfirmation").scale9Grid({ top:5, right:5, bottom:5, left:5 });
  }});
  
  $("#content #newsform input:reset").click(function () {
    $("#content #newsformconfirmation").remove9Grid({ top:5, right:5, bottom:5, left:5 });
    $("#content #newsformconfirmation").hide();
  });
  
  
  
  // Registration form ----------------------------------------------------------------------
  $("#content #registerform").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #registerform").html(responseText);  
    
    $("#content #registerform p.warning").show();
    $("#content #registerform p.warning").scale9Grid({ top:5, right:5, bottom:5, left:5 });
    
    $("#content #registerform p.confirmation").show();
    $("#content #registerform p.confirmation").scale9Grid({ top:5, right:5, bottom:5, left:5 });
  }});
  
  
  
  // Links form -----------------------------------------------------------------------------
  $("#content #linksform").ajaxForm({resetForm:true, success:function(responseText, statusText) {
  
    // Reset colors
    $("#content #linksform").find("input.inlinelabelled, textarea.inlinelabelled").css("color", "#999999");
    
    // Set new message
    $("#content #linksformconfirmation").remove9Grid({ top:5, right:5, bottom:5, left:5 });
    $("#content #linksformconfirmation").html(responseText);
    $("#content #linksformconfirmation").show();
    $("#content #linksformconfirmation").scale9Grid({ top:5, right:5, bottom:5, left:5 });
  }});
  
  $("#content #linksform input:reset").click(function () {
    $("#content #linksformconfirmation").remove9Grid({ top:5, right:5, bottom:5, left:5 });
    $("#content #linksformconfirmation").hide();
  });
  
  
  
  // Multiple choice form ---------------------------------------------------------------------
  $("#content #multiplechoiceform").ajaxForm({resetForm:false, success:function(responseText, statusText) {
  
    // Set new message
    $("#content #multiplechoiceform").html(responseText);
    $("#content .comment").scale9Grid({ top:5, right:5, bottom:5, left:5 });

    // Init close button (popup only)
    $(".windowclose").click(function () {
      window.opener.$("#simplecaseformtest, #complexcaseformtest").submit();
      window.close();
    });

  }});


  
  // Efflorescence question form --------------------------------------------------------------
  $("#content #efflorescenceform").initEfflorescenceForm();
  $("#content #efflorescenceform").ajaxForm({resetForm:false, success:function(responseText, statusText) {
  
    $("#content #efflorescenceform .answertable").replaceWith(responseText);
    $("#content #efflorescenceform .answertable").initEfflorescenceForm();

  }});


  
  // Simple case forms -----------------------------------------------------------------------
  $("#content .slot").initSimpleCaseForm();
  
  $("#content #simplecaseformanamnesis").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #simplecaseformanamnesis .answertable").replaceWith(responseText);
    $("#content #simplecaseformanamnesis .answertable").initSimpleCaseForm();
  }});

  $("#content #simplecaseformefflorescences").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #simplecaseformefflorescences .answertable").replaceWith(responseText);
    $("#content #simplecaseformefflorescences .answertable").initSimpleCaseForm();
  }});


  $("#content #simplecaseformdiagnosis").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #simplecaseformdiagnosis .answertable").replaceWith(responseText);
    $("#content #simplecaseformdiagnosis .answertable").initSimpleCaseForm();
  }});

  $("#content #simplecaseformtherapy").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #simplecaseformtherapy .answertable").replaceWith(responseText);
    $("#content #simplecaseformtherapy .answertable").initSimpleCaseForm();
  }});

  $("#content #simplecaseformtest").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #simplecasequestions").replaceWith(responseText);
    $("#content a[rel=popup]").click(function () {
      window.open($(this).attr("href"), "popup", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=650,width=1000");
      return false;
    });
    $(".exercisepicture").each(function () {
      $(this).prepend("<div class='exercisepicturemask'></div>");
    });
  }});

  
  
  // Complex case forms ----------------------------------------------------------------------
  $("#content .complexcaseform").initComplexCaseForm();
  
  $("#content #complexcaseformanamnesis").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #complexcaseformanamnesis .answertable").replaceWith(responseText);
    $("#content #complexcaseformanamnesis .answertable").initComplexCaseForm();
  }});

  $("#content #complexcaseformstatus").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #complexcaseformstatus .answertable").replaceWith(responseText);
    $("#content #complexcaseformstatus .answertable").initComplexCaseForm();
  }});

  $("#content #complexcaseformresume").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #complexcaseformresume .answersection").replaceWith(responseText);
    $("#content #complexcaseformresume .answersection").initComplexCaseForm();
  }});
  
  $("#content #complexcaseformlaboratory").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #complexcaseformlaboratory .answersection").replaceWith(responseText);
    $("#content #complexcaseformlaboratory .answersection").initComplexCaseForm();
  }});

  $("#content #complexcaseformdiagnosis").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #complexcaseformdiagnosis .answersection").replaceWith(responseText);
    $("#content #complexcaseformdiagnosis .answersection").initComplexCaseForm();
  }});

  $("#content #complexcaseformtherapy").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #complexcaseformtherapy .answersection").replaceWith(responseText);
    $("#content #complexcaseformtherapy .answersection").initComplexCaseForm();
  }});

  $("#content #complexcaseformtest").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #complexcasequestions").html(responseText);
    $("#content a[rel=popup]").click(function () {
      window.open($(this).attr("href"), "popup", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=650,width=1000");
      return false;
    });
    $(".exercisepicture").each(function () {
      $(this).prepend("<div class='exercisepicturemask'></div>");
    });
  }});



  // Case rating form ------------------------------------------------------------------------
  $("#content #caseratingselector").change(function() {
    $(this).parent("form").submit();
  });
  
  $("#content #caseratingform").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    
    // Set new message
    $("#content #caseratingformconfirmation").remove9Grid({ top:5, right:5, bottom:5, left:5 });
    $("#content #caseratingformconfirmation").html(responseText);
    $("#content #caseratingformconfirmation").show();
    $("#content #caseratingformconfirmation").scale9Grid({ top:5, right:5, bottom:5, left:5 });
  }});
  
  

  // Mark-as-read form ----------------------------------------------------------------------
  $("#content #markasreadform button").click(function () { $(this).parent("form").submit(); });
  $("#content #markasreadform").ajaxForm({resetForm:false, success:function(responseText, statusText) {

    // Set new content
    $("#content #markasreadform").html(responseText);
    $("#content #markasreadform button").click(function () { $(this).parent("form").submit(); });
  }});

  
  
  // Search extend content form -----------------------------------------------------------
  $("#content .searchextendcontentform").ajaxForm({resetForm:true, success:function(responseText, statusText) {
  
    // Create random id
    var id = "before" + (new Date()).getTime();
    
    // Set new content
    $("#content .searchextendcontentform").prev("ul").append("<div id='" + id + "'></div>");
    $("#" + id).html(responseText);
    $("#" + id).hide();
    $("#" + id).slideToggle("fast");
  }});



  // Exercise extend content form -----------------------------------------------------------
  $("#content .exerciseextendcontentform").ajaxForm({resetForm:true, success:function(responseText, statusText) {
  
    // Create random id
    var id = "before" + (new Date()).getTime();
    
    // Set new content
    $("#content .exerciseextendcontentform").before("<div id='" + id + "'></div>");
    $("#" + id).html(responseText);
        
    $(".exercisepicture").each(function () {
      if ($(this).parent().find(".exercisepicturemask").length==0) {
        $(this).prepend("<div class='exercisepicturemask'></div>");
      }
    });

    $("#" + id).hide();
    $("#" + id).slideToggle("fast");
  }});



  // Triangle list --------------------------------------------------------------------------
  $("#content .triangle > a, #content .triangleopen > a").click(function() {
  
    // Toggle content
    if ($(this).parent().find("ul").eq(0).css("display")!="none") {
      $(this).parent().css("background-image", "url(/resources/images/vuarnoz/triangle-icon-closed.png)");
      $(this).parent().find("ul").eq(0).slideToggle("fast");
    } else {
      $(this).parent().css("background-image", "url(/resources/images/vuarnoz/triangle-icon-open.png)");
      $(this).parent().find("ul").eq(0).slideToggle("fast");
    }
    return false;
  });
  
  
  
  // Folder list ----------------------------------------------------------------------------
  $("#content").initFolders();
  
  
  
  // Slot handling --------------------------------------------------------------------------
  $("#content .slotlink, #content .slotlinksmall").click(function() {
    // Toggle content
    if ($(this).hasClass(".slotlinksmallgrayopen, .slotlinksmallgrayclosed")) {
      if ($(this).next(".slot").css("display")!="none") {
        $(this).css("background-image", "url(/resources/images/vuarnoz/slot-link-gray-closed.png)");
        $(this).next(".slot").slideToggle("fast");
        $(this).parent().next("td").find(".slot").slideToggle("fast");
      } else {
        $(this).css("background-image", "url(/resources/images/vuarnoz/slot-link-gray-open.png)");
        $(this).next(".slot").slideToggle("fast");
        $(this).parent().next("td").find(".slot").slideToggle("fast");
      }
    } else {
      if ($(this).next(".slot").css("display")!="none") {
        $(this).css("background-image", "url(/resources/images/vuarnoz/slot-link-closed.png)");
        $(this).next(".slot").slideToggle("fast");
        $(this).parent().next("td").find(".slot").slideToggle("fast");
      } else {
        $(this).css("background-image", "url(/resources/images/vuarnoz/slot-link-open.png)");
        $(this).next(".slot").slideToggle("fast");
        $(this).parent().next("td").find(".slot").slideToggle("fast");
      }
    }
    return false;
  });



  // Inline labelling of input fields -------------------------------------------------------
  $("input:reset").click(function() {
    $(this).parent("form").find("input.inlinelabelled, textarea.inlinelabelled").css("color", "#999999");
  });
  
  $("input.inlinelabelled, textarea.inlinelabelled").focus(function() {
    if ($(this).val()==$(this).attr("title")) {
      $(this).val("");
    }
    $(this).css("color", "#000000");
  });
  
  $("input.inlinelabelled, textarea.inlinelabelled").blur(function() {
    if ($(this).val()==$(this).attr("title") || $(this).val()=="") {
      $(this).val($(this).attr("title"));
      $(this).css("color", "#999999");
    }
  });
  
  
  
  // Picture links --------------------------------------------------------------------------
  $(".picturesmall").each(function () {
    $(this).prepend("<div class='masksmall'></div>");
  });
  $(".picturelarge").each(function () {
    $(this).prepend("<div class='masklarge'></div>");
  });
  $(".gamespicture").each(function () {
    $(this).prepend("<div class='gamespicturemask'></div>");
  });
  $(".exercisepicture").each(function () {
    $(this).prepend("<div class='exercisepicturemask'></div>");
  });
  $("a[rel^='prettyPhoto']").prettyPhoto({ animationSpeed:"fast" });
  
  
  
  // Skin structures form -------------------------------------------------------------------
  $("#content #skinstructuresform").ajaxForm({resetForm:false, success:function(responseText, statusText) {
    $("#content #skinstructuresdescription").html(responseText);
    $("#content").initFolders();
    $("#content #skinstructuresdescription").hide();
    $("#content #skinstructuresdescription").fadeIn(500);
  }});
  
  $("#content #skinstructureselector").change(function() {
    $("#content #skinstructure .skinstructuremarker").removeClass("skinstructuremarkerselected");
    $("#content #skinstructure #skinstructuremarker" + $(this).val()).addClass("skinstructuremarkerselected");
    $(this).parent("form").submit();
  });
  
  $("#content #skinstructure .skinstructuremarker").click(function() { 
    $("#content #skinstructuresform #skinstructureselector").val($(this).attr("number"));
    $("#content #skinstructuresform").submit();
    $("#content #skinstructure .skinstructuremarker").removeClass("skinstructuremarkerselected");
    $(this).addClass("skinstructuremarkerselected");
    return false; 
  });
  
  $("#content #skinstructure .skinstructuremarker").tooltip( { 
    effect:        "fade", 
    fadeInSpeed:   200,
    fadeOutSpeed:  100,
    delay:         30, 
    opacity:       1, 
    offset:        [2, -42], 
    position:      ["top", "right"], 
    tip:           "#tooltip", 
    onBeforeShow:  function() {
      $("#tooltip").html("<h4>" + this.getTrigger().attr("title") + "</h4>" + this.getTrigger().attr("description"));
      $("#tooltip").scale9Grid({ top:10, right:10, bottom:45, left:10 });
      return true;
    },
    onBeforeHide:  function() {
      $("#tooltip").remove9Grid();
      $("#tooltip").html("");
      return false;
    }
  });
  
  
  
  // Tooltips -------------------------------------------------------------------------------
  $(".glossarylink").click(function() { return false; });
  $(".glossarylink").tooltip( { 
    effect:        "slideup", 
    slideInSpeed:  100,
    slideOutSpeed: 50,
    delay:         30, 
    opacity:       1, 
    offset:        [20, -25], 
    position:      ["top", "right"], 
    tip:           "#tooltip", 
    onBeforeShow:  function() {
      $("#tooltip").remove9Grid();
      if (this.getTrigger().attr("picture")) {
        $("#tooltip").html("<img src='" + this.getTrigger().attr("picture") + "' /><h4>" + this.getTrigger().attr("title") + "</h4>" + this.getTrigger().attr("description"));
      } else {
        $("#tooltip").html("<h4>" + this.getTrigger().attr("title") + "</h4>" + this.getTrigger().attr("description"));
      }
      $("#tooltip").scale9Grid({ top:10, right:10, bottom:45, left:10 });
      return true;
    },
    onBeforeHide:  function() {
      $("#tooltip").remove9Grid();
      $("#tooltip").html("");
      return false;
    }
  });
  
  $(".copyrightlink").click(function() { return false; });
  $(".copyrightlink").tooltip( { 
    effect:        "slideup", 
    slideInSpeed:  100,
    slideOutSpeed: 50,
    delay:         30, 
    opacity:       1, 
    offset:        [20, -25], 
    position:      ["top", "right"], 
    tip:           "#tooltip", 
    onBeforeShow:  function() {
      $("#tooltip").remove9Grid();
      $("#tooltip").html("<h4>" + this.getTrigger().attr("title") + "</h4>" + this.getTrigger().attr("description"));
      $("#tooltip").scale9Grid({ top:10, right:10, bottom:45, left:10 });
      return true;
    },
    onBeforeHide:  function() {
      $("#tooltip").remove9Grid();
      $("#tooltip").html("");
      return false;
    }
  });
  
  $(".tooltiplink").click(function() { return false; });
  $(".tooltiplink").tooltip( { 
    effect:        "fade", 
    fadeInSpeed:   200,
    fadeOutSpeed:  100,
    delay:         30, 
    opacity:       1, 
    offset:        [6, -37], 
    position:      ["top", "right"], 
    tip:           "#tooltip", 
    onBeforeShow:  function() {
      $("#tooltip").remove9Grid();
      $("#tooltip").html("<h4>" + this.getTrigger().attr("title") + "</h4>" + this.getTrigger().attr("description"));
      $("#tooltip").scale9Grid({ top:10, right:10, bottom:45, left:10 });
      return true;
    },
    onBeforeHide:  function() {
      $("#tooltip").remove9Grid();
      $("#tooltip").html("");
      return false;
    }
  });
  

  
  // Diseases filter ------------------------------------------------------------------------
  $("#content #diseasesfilter").change(function () {
    $("#content #diseasesfilterform").submit();
  });
  
  $("#content #diseasesfilter2").change(function () {
    $("#content #diseasesfilterform").submit();
  });
  

  // Popup links ----------------------------------------------------------------------------
  $("#content a[rel=popup]").click(function () {
    window.open($(this).attr("href"), "popup", "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=650,width=1000");
    return false;
  });
  

});




