/* 
/* (CC) 2011 Wevolve
/* 
/* Feel free to be inspired by this JS, 
/* see the Creative Commons License (http://creativecommons.org/) 
/* 
/* If you re-use parts or whole of this JS, 
/* you must include this copyright notice at the start of the file. 
/* 
/* Please contact us for commercial use and to find out 
/* how we can help you with various web-based solutions: 
/* 
/* web:     http://www.wevolve.nl 
/* e-mail:  info@wevolve.nl 
/* tel.:    +31-(0)74 255 2140 
/*
/* -------------------------------------------------------- */


// ---------- Code if jQuery is available

if (window.jQuery) {

// ---------- Plugin Triggers and Settings

  $(document.documentElement).addClass("jsenabled");

    $(document).ready(function() {


// ---------- Submit Form

      $("#contactform form").live("submit", function(e) {
        e.preventDefault();
        $.post("contactform.php", $("#contactform form").serialize(),
        function(data) {
          if (data.haserror) {
            if ($("#formerror").length==0)
            {
              $("#input_message").after('<p id="formerror" class="error">Niet alle velden zijn correct ingevuld. Het bericht is niet verzonden.</p>');
            }
          }
          else {
            $("#formerror").remove();
              if (data.emailsent) {
                $("table").attr("id", "success_page");
                $("table").after($("#contactform").remove().css("display", "none"));
                $(".textblock").html($("#formsuccess").remove().css("display", "block"));
              }
              else {
                alert("Sorry please contact us by phone.");
              }
            }
          });
        return false;
      });


// ---------- Fullscreen background / Fade for content 

      if ($("body").length > 0) {
        $.backstretch("resources/img/bg_home.jpg", {
          speed: 500
        });
      }

      $("#nav li a").click(function(e) {
        $("#nav li a").removeClass("onpath");
        $(this).addClass("onpath");
        thisid = $(this).attr("id");

        thispage = thisid + "_page";
        
        $("tr:lt(3), #backstretch img").fadeOut(500,
        function() {
          
          $("table").attr("id", thispage);
          if (thispage=="contact_page")
          {
            $("table").after($("#persons").remove().css("display", "none"));
            $("table").after($("#formsuccess").remove().css("display", "none"));
            $(".textblock").html($("#contactform").remove().css("display", "block"));
            setOverlabels();
          }
          else {
            if (thispage=="people_page") {
              $("table").after($("#contactform").remove().css("display", "none"));
              $("table").after($("#formsuccess").remove().css("display", "none"));
              $(".textblock").html($("#persons").remove().css("display", "block"));
            }
            else {
              $("table").after($("#contactform").remove());
              $("table").after($("#persons").remove());
              $("#contactform, #formsuccess, #persons").css("display", "none");
            }
          }
          
          $("#backstretch img").attr("src", "resources/img/bg_" + thisid + ".jpg");
          
          $("tr:lt(3), #backstretch img").fadeIn(500,
          function() {
          });
        });
      });


// ---------- height fix for IE7

      if ($.browser.msie && $.browser.version.substr(0,1)<=7) {

        windowHeight = $(window).height();
        $("#home_page").css("height", windowHeight);

        divHeight = $(window).height();
        $(".top, .bottom").css("height", (divHeight - 100 - 363) / 2 + 'px');
      }


// ---------- Fix links

      $(document.links).filter('[href][rel=external]').attr("target", "_blank");
      


// ---------- Form input label flip

      setOverlabels();
      function setOverlabels()
      {
        $('label.overlabel')
        .each(function()
         {
            var label = $(this);
            var id = this.htmlFor || this.getAttribute('for');
            var field = null;
            if (!id || !(field = document.getElementById(id))) return;

            $(field).addClass("overlabel-js")
            .focus(function() {
                label.hide();
            })
            .blur(function() {
                this.value === '' && label.show();
            });
            label.mousedown(function() {
                setTimeout(function() {
                    field.focus();
                },
                20);
            });
          }
        );
        setTimeout(function()
         {
            var fields = $("input.overlabel-js, textarea.overlabel-js");
            var labels = $("label.overlabel");
            fields.each(function() {
                this.value !== '' && labels.filter("[for=" + this.id + "]").hide();
            });
        },
        50);
      }


    }); // document.ready

} // window.jQuery


/*
 * jQuery Backstretch
 * Version 1.1.2
 * http://srobbin.com/jquery-plugins/jquery-backstretch/
 *
 * Add a dynamically-resized background image to the page
 *
 * Copyright (c) 2010 Scott Robbin (srobbin.com)
 * Dual licensed under the MIT and GPL licenses.
*/
(function(a){a.backstretch=function(k,i,l){function m(c){try{f={left:0,top:0};d=e.width();b=d/j;if(b>=e.height()){g=(b-e.height())/2;h.centeredY&&a.extend(f,{top:"-"+g+"px"})}else{b=e.height();d=b*j;g=(d-e.width())/2;h.centeredX&&a.extend(f,{left:"-"+g+"px"})}a("#backstretch img").width(d).height(b).css(f)}catch(n){}typeof c=="function"&&c()}var h={centeredX:true,centeredY:true,speed:0},e="onorientationchange"in window?a(document):a(window),j,d,b,g,f;i&&typeof i=="object"&&a.extend(h,i);a(document).ready(function(){if(k){var c= a("<div />").attr("id","backstretch").css({left:0,top:0,position:"fixed",overflow:"hidden",zIndex:-9999}),n=a("<img />").css({position:"relative",display:"none"}).bind("load",function(o){var p=a(this);j=a(o.target).width()/a(o.target).height();m(function(){p.fadeIn(h.speed,function(){typeof l=="function"&&l()})})}).appendTo(c);a("body").prepend(c);n.attr("src",k);a(window).resize(m)}});return this}})(jQuery);

