jQuery(document).ready(function($) {
	
	$.fn.extend({
		billboard: function(params) {
			
			var options = $.extend({}, params);
			
			return this.each(function() {
				
				$container = $(this);
				$slides = $container.find(".slide");
				
				$slides.stack();

				showNextSlide();
				function showNextSlide() {
					currentSlide = $container.find(".slide").last();
					currentSlide.fadeTo(options.transition_duration * 1000, 0, function() {
						currentSlide.remove()
									.prependTo($container)
									.css("opacity", 1);
						
						setTimeout(function() {showNextSlide() }, options.slide_duration * 1000);
					})
				}
				
			});			
		},
		stack: function() {
			return this.each(function() {
				$this = $(this);
				$this.addClass("stacked");
			})
		},
		autoClear: function() {
		  return this.each(function() {
		    $this = $(this).attr('title', $(this).val())
		    .focus(function() {
		      $this = $(this);
		      $this.removeClass("invalid");
		      if($this.val() == $this.attr('title')) { $this.val(''); }
		    })
		    .blur(function() {
		      $this = $(this);
		      if($this.val() == '') { $this.val($this.attr('title')) }
		    })
		  });
		}

	});
	
	$(".home #billboard").billboard({
		slide_duration: 4, //seconds
		transition_duration: 1 //seconds
	});
	
	$("a.print").click(function() {
	 window.print();
	 return false;
	});
	
	$(".auto-clear").autoClear();
	
	
	
	
  $("#quote-accept-button").click(function() {

    url = window.location.href;  
    
    if($('input[name="accept-name"]').val() == $('input[name="accept-name"]').attr('title')) {
      alert('Before you can accept, you must enter your name');
      $('input[name="accept-name"]').css('border', '2px solid #f00');
      return false;
    }
    
    data = {
      'action': 'accept-quote',
      'accept-date': $('input[name="accept-date"]').val(),
      'accept-name': $('input[name="accept-name"]').val(),
      'accept-comments' : $('textarea[name="accept-comments"]').val() != $('textarea[name="accept-comments"]').attr('title') ? $('textarea[name="accept-comments"]').val() : '',
      'quote-id': $('input[name="quote-id"]').val(),
      'pr-quote-nonce' : $('input[name="pr-quote-nonce"]').val()
    };
    
    $("#needs-accept").fadeTo('fast', 0)
  
    $.ajax({
      url: url,
      data: data,
      dataType: 'text',
      type: 'POST',
      success: function(data) {
      	
        if(data == 'success'){
          $("#needs-accept").html("<strong>Your quote has been accepted!</strong><br /><br />Someone from Print Resources will contact you shortly");
        } else {
          alert("Something went wrong.  We were unable to save your quote\n\nPlease try again");
        }

        $("#needs-accept").css('border', '2px solid #306CBF');
        $("#needs-accept").css('background-color', '#F0F7FF');
        $("#needs-accept").css('padding', '10px');
        $("#needs-accept").css('font-size', '14px');
        $("#needs-accept").fadeTo('fast', 100);
      },
      error: function() {
        alert("Something went wrong.  We were unable to save your quote." + "\n" + "Please try again");
      }
    });
      
    return false;
  });
  
  
  $("#proof-accept-button").click(function() {

    url = window.location.href;  
    
    if($('input[name="accept-name"]').val() == $('input[name="accept-name"]').attr('title')) {
      alert('Before you can accept, you must enter your name');
      $('input[name="accept-name"]').css('border', '2px solid #f00');
      return false;
    }
    
    data = {
      'action': 'accept-proof',
      'accept-date': $('input[name="accept-date"]').val(),
      'accept-status': $('input[name="astatus"]:checked').val(),
      'accept-name': $('input[name="accept-name"]').val(),
      'accept-comments' : $('textarea[name="accept-comments"]').val() != $('textarea[name="accept-comments"]').attr('title') ? $('textarea[name="accept-comments"]').val() : '',
      'proof-id': $('input[name="proof-id"]').val(),
      'pr-proof-nonce' : $('input[name="pr-proof-nonce"]').val()
    };
    
    $("#needs-accept").fadeTo('fast', 0);
  
    $.ajax({
      url: url,
      data: data,
      dataType: 'text',
      type: 'POST',
      success: function(data) {
        if(data == 'success'){
          $("#needs-accept").html("<strong>Your proof has been accepted!</strong><br />Someone from Print Resources will contact you shortly.");
        } else if(data == 'success2'){
          $("#needs-accept").html("<strong>Your request has been submitted.</strong><br /><br />Someone from Print Resources will contact you shortly.");
        } else {
        	//alert(data);
          	alert("Something went wrong.  We were unable to save your proof" + "\n" + "Please try again" + data);
        }        
        $("#needs-accept").css('border', '2px solid #306CBF');
        $("#needs-accept").css('background-color', '#F0F7FF');
        $("#needs-accept").css('padding', '10px');
        $("#needs-accept").css('font-size', '14px');
        $("#needs-accept").fadeTo('fast', 100);
      },
      error: function() {
        alert("Something went wrong.  We were unable to save your proof." + "\n" + "Please try again");
      }
    });
      
    return false;
  });
  	
});
