 $(document).ready(function(){

	// textarea maxlength counter
	$('#fld_description').limit('500','#desc_charsLeft');
	$('#fld_whywin').limit('500','#whywin_charsLeft');

	jQuery.validator.messages.required = "";
	$("form").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'Please correct the error highlighted below'
					: 'Please correct the following ' + errors + ' fields.  They have been highlighted below';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		onkeyup: false,
		submitHandler: function() {
			$("div.error").hide();
			document.frmEnterMain.submit();
		},
		
		// --- form rules ---
		rules: {
			fld_description: {
				required: true,
				minlength: 2
			},
			fld_whywin: {
				required: true,
				minlength: 2
			},
			fld_url: {
				required: false,
				cust_url: true,
				minlength: 4
			},
			fld_title: {
				required: true,
				minlength: 2
			}

		},


		// --- inline error messages ---
		messages: {
			fld_description: {
				minlength: "<img align='absmiddle' src='graphics/unchecked.gif' alt='Error'><div>Please provide a valid description</div>",
				required: "<img align='absmiddle' src='graphics/unchecked.gif' alt='Error'><div>Please provide a description</div>"
			},
			fld_whywin: {
				minlength: "<img align='absmiddle' src='graphics/unchecked.gif' alt='Error'><div>Please provide a valid reason</div>",
				required: "<img align='absmiddle' src='graphics/unchecked.gif' alt='Error'><div>Why you should win is required</div>"
			},
			fld_url:	{
				cust_url: "<img align='absmiddle' src='graphics/unchecked.gif' alt='Error'><div>Website URL is invalid</div>",
				minlength: "<img align='absmiddle' src='graphics/unchecked.gif' alt='Error'><div>Website URL is invalid</div>"
			},			
			fld_title: {
				minlength: "<img align='absmiddle' src='graphics/unchecked.gif' alt='Error'><div>Please provide a valid title</div>",
				required: "<img align='absmiddle' src='graphics/unchecked.gif' alt='Error'><div>Title is required</div>"
			}
		},
		debug:true
	});
	
  $(".resize").vjustify();
  $("div.buttonSubmit").hoverClass("buttonSubmitHover");

  if ($.browser.safari) {
    $("body").addClass("safari");
  }

  // init upload
  var upload_button = $('#btn_upload'), interval;
  new Ajax_upload(upload_button,{
		action: 'includes/upload_handler.asp', 
		name: 'myfile',
		onSubmit : function(file, ext){
			upload_button.text('Uploading');
	
			// disable upload button so only 1 upload at a time
			this.disable();		
			upload_button.text('Uploading');
			
			$("#fld_status").empty();
			$("#fld_status").html("<img src='graphics/upload_spin.gif'>" + "Uploading " + file + "...");
			$("#fld_status").show();
		},
		onComplete: function(file, response){
			upload_button.text('Upload');
						
			window.clearInterval(interval);
						
			// enable upload button again after last upload completed
			this.enable();
			
			// set hidden field to uploaded file (server generated name)
			$('#fld_picture').val(response);
			
			// change the status to show the uploaded file
			$("#fld_status").empty();
			$("#fld_status").html("<img src='graphics/ico_success.png'>&nbsp;&nbsp;" + file + "&nbsp;&nbsp;(<a id='remove_picture' name='remove_picture' href='#'>remove</a>)");
	        $("#remove_picture").click(function(event){
				$('#fld_picture').val('');
				$("#fld_status").empty();
				$("#fld_status").hide();
		         event.preventDefault();
	        });
			
		}
  });

  
});

$.fn.vjustify = function() {
    var maxHeight=0;
    $(".resize").css("height","auto");
    this.each(function(){
        if (this.offsetHeight > maxHeight) {
          maxHeight = this.offsetHeight;
        }
    });
    this.each(function(){
        $(this).height(maxHeight);
        if (this.offsetHeight > maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight)));
        }
    });
};

$.fn.hoverClass = function(classname) {
	return this.hover(function() {
		$(this).addClass(classname);
	}, function() {
		$(this).removeClass(classname);
	});
};