/*
 * Copyright by Oliver Musebrink (www.olivermusebrink.de)
 */


/*
 * jQuery
 */
$(document).ready(
  function()
  {
    /*
     * Image
     */
    if($("#image") && $("#image").length)
    {
      // Get image dimensions and adjust them
      imgw = $("#image").width ();
      imgh = $("#image").height();

      w = Math.max(900, imgw) + 80;
      h = Math.max(600, imgh);

      // Set image container dimension
      $("#image-container").width (w + "px");
      $("#image-container").height(h + "px");

      // Show image wrapper
      $("#image-wrapper").show();

      // Show image
      if($("#image").attr("complete"))
      {
        // Fade in image immediately
        $("#image").fadeIn(666);
      }
      else
      {
        // Load image completely, then fade in
        $("#image").load(
          function()
          {
            $("#image").fadeIn(666);
          }
        ); 
      }
    }




    /*
     * Contact form
     */
    if($("#contact-form") && $("#contact-form").length)
    {
      var contactFormValidator = null;


      contactFormValidator = $("#contact-form").validate(
      {
        submitHandler: function(form)
                       {
                         form.submit();
                       },

        errorClass:    "error",

        errorElement:  "div",

        rules:         {
                         name:
                         {
                           required:  true,
                           minlength: 1
                         },

                         email:
                         {
                           required: true,
                           email:    1
                         },

                         message:
                         {
                           required:  true,
                           minlength: 1
                         }
                       },

        messages:      {
                         name:
                         {
                           required: "Bitte geben Sie Ihren Namen ein!"
                         },

                         email:
                         {
                           required: "Bitte geben Sie Ihre E-mail-Adresse ein!",
                           email:    "Bitte geben Sie eine g&uuml;ltige E-mail-Adresse ein!"
                         },

                         message:
                         {
                           required: "Bitte geben Sie Ihre Nachricht ein!"
                         }
                       }
      });


      /*
       * Reset button
       */
      $("#contact-form .reset").click(
        function()
        {
          contactFormValidator.resetForm();
        }
      );
    }




    /*
     * Image button effects
     */
    if($("#image-wrapper") && $("#image-wrapper").length)
    {
      $("#image-button-previous").mouseover(
        function ()
        {
          // Stop everything
          $(this).stop();

          // Fade in
          $(this).stop  ();
          $(this).fadeTo(333, 1.0);
        }
      );

      $("#image-button-previous").mouseout(
        function ()
        {
          // Stop everything
          $(this).stop();

          // Fade out
          $(this).stop  ();
          $(this).fadeTo(333, 0.0);
        }
      );


      $("#image-button-next").mouseover(
        function ()
        {
          // Stop everything
          $(this).stop();

          // Fade in
          $(this).stop  ();
          $(this).fadeTo(333, 1.0);
        }
      );

      $("#image-button-next").mouseout(
        function ()
        {
          // Stop everything
          $(this).stop();

          // Fade out
          $(this).stop  ();
          $(this).fadeTo(333, 0.0);
        }
      );
    }




    /*
     * Browse thumbnail effects
     */
    if($("#browse-thumbnails") && $("#browse-thumbnails").length)
    {
      // Show thumbnail
      if($("#browse-thumbnails img").attr("complete"))
      {
        // Fade in thumbnail immediately
        $("#browse-thumbnails img").parent().parent(".thumbnail").fadeIn(666);
      }
      else
      {
        // Load thumbnail completely, then fade in
        $("#browse-thumbnails img").load(
          function()
          {
            $(this).parent().parent(".thumbnail").fadeIn(666);
          }
        ); 
      }


      $("#browse-thumbnails img").mouseover(
        function ()
        {
          // Stop everything
          $(this).stop();

          // Set opacity
          $(this).css("opacity", 0.33);

          // Fade in
          $(this).fadeTo(666, 1.0);
        }
      );

    }
  }
);

