$(document).ready(function(){

   // Open links in new windows

   $('a.new-window').click(function(){
      window.open(this.href);
      return false;
   });

   // Add / remove default text on form input fields

   var defaultValues =  new Array();

   $(".default-text").focus(function() {

      if (!defaultValues[$(this).attr('id')]) {

         defaultValues[$(this).attr('id')] = $(this).val();
         $(this).val('');

      } else if (defaultValues[$(this).attr('id')] == $(this).val()) {

         $(this).val('');

      }

   });

   $(".default-text").blur(function() {

      if ($(this).val().length <= 0) {
        $(this).val(defaultValues[$(this).attr('id')]);
      }

   });

   $("#upload-images-form").validationEngine({
      unbindEngine : false,
      success :  function(formData) {

         var filesQueryString = '';

         $('input[name="files[]"]').each(function(){
            filesQueryString += '&files[]=' + $(this).val();
         });

         $.ajax({
            type: "POST",
            url: "/index.php",
            data: "name="+ $("#name").val() +"&email="+ $("#email").val() +"&image-location="+ $("#image-location").val() +"&spam-check="+ $("#spam-check").val() +"&story="+ $("#story").val() + filesQueryString +"&submit=true",
            beforeSend: function(){

               if ($("#spam-check").val() != '7' && $("#spam-check").val() != 'seven') {
                  alert('Spam check incorrect!');
                  $("#spam-check").focus();
                  return false;
               }

               $("#submit").attr('disabled', true);
            },
            success: function(){
               $('#upload-form').hide();
               $('#uploadify-queue div').remove();
               $('#thanks').fadeIn();

               setTimeout(
                  function(){
                     $('#thanks').hide();
                     $('#name').val($('#name').attr('title'));
                     $('#email').val($('#email').attr('title'));
                     $('#image-location').val($('#image-location').attr('title'));
                     $('#story').val($('#story').attr('title'));
                     $('#spam-check').val($('#spam-check').attr('title'));
                     $('#upload-form').fadeIn();
                  },
                  2000
               );
            },
            complete: function() {
               $("#submit").attr('disabled', false);
               $('div.form-row.submit').hide();
            }
         });

      }
   });

   $('#uploadify').uploadify({
      'uploader'  : '/uploadify/uploadify.swf',
      'script'    : '/uploadify/uploadify.php',
      'buttonImg' : '/images/assets/upload-your-image-background-states.png',
      'width'     : 149,
      'height'    : 17,
      'rollover'  : true,
      'cancelImg' : '/uploadify/cancel.png',
      'folder'    : '/uploads',
      'fileExt'   : '*.jpg;*.gif;*.png',
      'fileDesc'  : 'Image Files',
      'auto'      : true,
      'multi'     : true,
      'queueID'   : 'uploadify-queue',
      'queueSizeLimit' : 5,
      'simUploadLimit' : 3,
      'removeCompleted' : false,
      'onInit'  :
         function(event,data) {
            $('div.form-row.submit').hide();
         },
      'onComplete' :
         function(event, ID, fileObj, response, data) {
            $('#uploadify').uploadifySettings('buttonImg', '/images/assets/upload-another-background-states.png');
            $('#uploadify').uploadifySettings('width', 126);

            var fileName = fileObj.name;
            var fileName = fileName.replace(' ', '-');

            $('#upload-images-form').append('<input name="files[]" type="hidden" value="' + fileName + '" />');
         },
      'onAllComplete'  :
         function(event,data) {
            $('div.form-row.submit').show();
         }
   });

});

