jQuery.fn.clearForm = function(){
    return this.each(function(){
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input',this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};

jQuery(function (){
    /* welcome slideshow */	
    $('#s1ideshow img:gt(0)').hide();
    $('#slideshow').cycle({timeout: 3500});
    /* portfolio thumb click handler */  
    $('.portfolio-thumb-row a').click(function(){
        // get desired gallery id
        var clicked = this.id
        var gallery = 'g' + clicked.match(/l(\d+)/)[1];
        // unselect thumb and hide all gallery
        $('.portfolio-thumb-row a img').removeClass('selected');
        $('.gallery').slideUp('slow');
        // select thumb and show desired gallery
        $('#' + gallery).slideDown('slow');
        $('#' + clicked + 'i' ).addClass('selected');
        // don't reload page
        return false;
    });
    /* contact form */
    $('form').clearForm();
    $('input[type="text"]:first').focus();
    $("#contact-form").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'Oops! There is a problem. (It has been highlighted)'
					: 'Oops! There are ' + errors + ' problems.  (They have been highlighted)';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
        rules: {
            name: "required",
            message: "required",
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            name: "(required)",
            message: "(required)",
            email: {
                required: "(required)",
                email: "(invalid)"
            }
        }
    });
});


