﻿var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

$(window).load(function() {
  $('.slider').nivoSlider({
    pauseTime: 6000,
    controlNav: true,
    prevText: '',
    nextText: ''
  });
});

$(document).ready(function() {
  $('#map-canada ul').locations();

  $('a[rel*=facebox]').live('mousedown', function() {
    $(this).facebox({
      loadingImage: '/assets/images/loading.gif',
      closeImage: '/assets/images/closelabel.png'
    });
  });

  $("html").delegate("input", "focus", function() {
    var $this = $(this);
    if ($this.val() === '') {
      $this.val($this.attr('title'));
    }

    $this.focus(function() {
      if ($this.val() == $this.attr('title')) {
       $this.val('').addClass('focused');
      }
    });
    $this.blur(function() {
      if ($this.val() === '') {
        $this.val($this.attr('title')).removeClass('focused');
      }
    });
  });

  $("html").delegate("textarea", "focus", function() {
    var $this = $(this);
    if ($this.val() === '') {
      $this.val($this.attr('title'));
    }

    $this.focus(function() {
      if ($this.val() == $this.attr('title')) {
        $this.val('').addClass('focused');
      }
    });
    $this.blur(function() {
      if ($this.val() === '') {
        $this.val($this.attr('title')).removeClass('focused');
      }
    });
  });

  $("html").delegate("#contact-form-submit-button", "click", function() {

    var _bError = false;

    $("#contact-form-spinner").css('display', 'block');

    $('#contact-form-name-container').css('backgroundPosition', '0px 0px');
    $('#contact-form-email-container').css('backgroundPosition', '0px 0px');
    $('#contact-form-message-container').css('backgroundPosition', '0px 0px');

    if ($('#contact-form-name-id').val() == $('#contact-form-name-id').attr('title')) {
      _bError = true;
      $('#contact-form-name-container').css('backgroundPosition', '0px -128px');
    }

    if ($('#contact-form-email-id').val() == $('#contact-form-email-id').attr('title')) {
      _bError = true;
      $('#contact-form-email-container').css('backgroundPosition', '0px -128px');
    }

    if (!emailReg.test($('#contact-form-email-id').val())) {
      _bError = true;
      $('#contact-form-email-container').css('backgroundPosition', '0px -128px');
    }

    if ($('#contact-form-message-id').val() == $('#contact-form-message-id').attr('title')) {
      _bError = true;
      $('#contact-form-message-container').css('backgroundPosition', '0px -360px');
    }

    if (_bError == true) {
      $('#contact-message').css('background-color', '#b30606');
      $('#contact-message').html('Oops we had some issues')
      $("#contact-form-spinner").fadeOut("slow");

    } else {

      var contact_name = $("#contact-form-name-id").attr("value");
      var contact_email = $("#contact-form-email-id").attr("value");
      var contact_message = $("#contact-form-message-id").attr("value");
      var contact_city = $("#contact-form-city-id").val();

      $.ajax({
        url: "/contact/send",
        data: {
          "name": contact_name,
          "email": contact_email,
          "city": contact_city,
          "message": contact_message
        },

        type: "post",
        cache: false,
        dataType: "json",

        complete: function(result) {
          if (result.status == 200) {
            $('#contact-message').css('background-color', '#56af4a');
            $('#contact-message').html('Message Sent! Thank you.')
            $("#contact-form-spinner").fadeOut("slow");
          } else {
            $('#contact-message').css('background-color', '#cd8c00');
            $('#contact-message').html('Hmmmmm, we had an issue sending your message.')
            $("#contact-form-spinner").fadeOut("slow");
          }
        }
      });
    }
  });

  $("#twitter-feed").getTwitter({
    userName: "seeresults",
    numTweets: 1,
    loaderText: "Loading tweets...",
    slideIn: true,
    showHeading: false,
    headingText: "",
    showProfileLink: true
  });
});
