function lightbox() {
  var lb = $('a[rel=lightbox], a.lightbox');
  if (lb) {
    $(function () {
      $(lb).lightBox({
        fixedNavigation: true,
        //imageBtnPrev: '/img/lightbox/lightbox-btn-prev.gif',
        //imageBtnNext: '/img/lightbox/lightbox-btn-next.gif',
        imageBtnClose: '/img/lightbox/lightbox-btn-close.gif',
        imageLoading: '/img/lightbox/lightbox-ico-loading.gif',
        maxHeight: 500,
        maxWidth: 700,

      });
    });
  }
}

(function($) {
    $.fn.makePretty = function(options) {
        options = $.extend({
            submitButton: ''
        }, options);

        var element = this;

        //$(element).validate();

        if($(element).hasClass('formwizard')) {
        	$(element).formToWizard({ submitButton: 'submit' });

        	$(element).validate({
				    invalidHandler: function(form, validator) {
				      var errors = validator.numberOfInvalids();
				      if (errors) {
				        var message = errors == 1
				          ? 'You missed 1 field. It has been highlighted'
				          : 'You missed ' + errors + ' fields. They have been highlighted';
				        $("div.error span").html(message);
				        $("#error_notice").html(message);
				        $("div.error").show();

				        //var badfield = $(element).find('.invalid').first();
				        //console.debug(badfield);
				        //var fieldset = $(badfield).parents('fieldset').eq(0);
								//var index = $(fieldset).index('fieldset');
								//$(window).trigger('selectstep', {i: index});

				      } else {
				        $("div.error").hide();
				      }
				    }
        	});

        }

				var d = new Date();
				var curr_date = d.getDate();
				var curr_month = d.getMonth();
				var curr_year = d.getFullYear();
				var year_upper_range = curr_year+3;

				$(element).find('input.year').each(function() {
					$(this).rules("add", {
					 	minlength: 4,
					 	maxlength: 4,
					 	digits: true,
					 	max: year_upper_range,
					 	messages: {
					 		minlength: "Enter the year as YYYY",
					 		maxlength: "Enter the year as YYYY",
					 		max: "Is your boat from the future?"
					 	}
					});
				});

				$(element).find('input.zipcode').each(function() {
					$(this).rules("add", {
					 	minlength: 5,
					 	maxlength: 5,
					 	digits: true,
					 	messages: {
					 		minlength: "Enter a valid zipcode",
					 		maxlength: "Enter a valid zipcode"
					 	}
					});
				});

				$(element).find('input.numeric').each(function() {
					$(this).rules("add", {
					 	number: true
					});
				});

        $(element).find('.phone').attr('size', 14).attr('maxlength', 14).attr('minlength', 14).mask("(999) 999-9999");
        $(element).find('.date').attr('size', 10).attr('maxlength', 10).attr('minlength', 10).mask("99/99/9999");
    }


})(jQuery);


/* Convert select list optgroups to option classes for dependent select */
function optgroup2ds(id){
	$('#'+id).find('optgroup').each(function(i, optgroup){
			$(optgroup).find('option').each(function(j, option){
			$(option).addClass('sub_'+$(optgroup).attr('label')).appendTo($('#'+id));
		});
		$(optgroup).remove();
	});
}

/* Turns select lists into parent->child selectors */
function makeSublist(parent,child,isSubselectOptional,childVal)
{
	$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	$('#'+parent+child).html($("#"+child+" option"));

		var parentValue = $('#'+parent).attr('value');
		$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

	childVal = (typeof childVal == "undefined")? "" : childVal ;
	$("#"+child+' option[value="'+ childVal +'"]').attr('selected','selected');

	$('#'+parent).change(
		function()
		{
			var parentValue = $('#'+parent).attr('value');
			$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
			if(isSubselectOptional) $('#'+child).prepend("<option value='none'> -- Select -- </option>");
			$('#'+child).trigger("change");
                        $('#'+child).focus();
		}
	);
}

